Search code examples
delphidelphi-ide

How to get list of opened files in Delphi IDE


I’m working on an extension for Delphi, and I want to get a list of all opened files in the IDE, so as it listed in Buffer List if I push Ctrl + B.

I attempted two methods, but they list only the files contained in the extension project, even if the project group does not include this project.

class function IOTAUTils.GetOpenedEditorFiles : TArray<string>;
var
  module : IOTAModule;
  editor : IOTAEditor;
  service : IOTAModuleServices;
begin
  Result := [];
  service := (BorlandIDEServices as IOTAModuleServices);
  if Assigned(service) then begin
    for var i := 0 to service.ModuleCount - 1 do begin
      module := service.Modules[i];
      for var j := 0 to module.GetModuleFileCount - 1 do begin
        editor := module.GetModuleFileEditor(j);
        Result := Result + [editor.FileName];
      end;
    end;
  end;
end;

At least I can filter out invisible open files with this approach: EditViewCount > 0

class function IOTAUTils.GetOpenedEditBuffers: TArray<string>;
var
  service : IOTAEditorServices;
  it : IOTAEditBufferIterator;
  buffer : IOTAEditBuffer;
begin
  Result := [];
  service := (BorlandIDEServices as IOTAEditorServices);
  if Assigned(service) then begin
    if (service.GetEditBufferIterator(it)) then begin
      for var i := 0 to it.Count - 1 do begin
        buffer := it.EditBuffers[i];
        if buffer.EditViewCount > 0 then begin // if view count = 0, it is not shown between tabs
          Result := Result + [buffer.FileName];
        end;
      end;
    end;
  end;
end;

What is the proper way to get the desired list of open files?


Solution

  • After some sleep and rebooting both the IDE and the PC, the second method IOTAUtils.GetOpenedEditBuffers works as expected.