views:

77

answers:

2

I want to get the structure information of a module (pascal unit) by ToolsAPI. just like the structure view of the IDE does.

Classes, Records, Interfaces, Variables/Constants, etc Members, Parameters, etc.

Is there already an easy & efficient way to get these metadata?

+1  A: 

AFAIK there is no way to query special structured information for a given file.

What you could do is to access the information in the Structure pane. That way requires the file to be the active module (can be achieved by the OTA), the output depends on the Structure pane settings (Tools | Options... -> Environment Options | Explorer) and if a node is a field, a procedure or whatever needs to be determined over the image index, parent...

This code walks through the Structure pane.

procedure StructureViewToSL(ASL: TStringList);

  procedure TreeToSL(ANode: IOTAStructureNode; ASL: TStringList; const APrefix: string);
  var
    I: Integer;
  begin
    ASL.Add(APrefix + ANode.Caption);
    for I := 0 to ANode.ChildCount - 1 do
      TreeToSL(ANode.Child[I], ASL, APrefix + '  ');
  end;

var
  StructureView: IOTAStructureView;
  StructureContext: IOTAStructureContext;
  Node: IOTAStructureNode;
  I: Integer;
begin
  StructureView := BorlandIDEServices as IOTAStructureView;
  StructureContext := StructureView.GetStructureContext;
  for I := 0 to StructureContext.RootNodeCount - 1 do
  begin
    Node := StructureContext.GetRootStructureNode(I);
    TreeToSL(Node, ASL, '');
  end;
end;
Uwe Schuster
Thanks, Uwe Schuster. This way appeared in my mind. but as you said, it only works when the module is active. What I am looking for is a way to support all modules in a project group.
Paul
With the OTA it is possible to enumerate all projects and modules of a project group and it is possible to make each module the active module.
Uwe Schuster
yeah. Each module must be opened and active if I am not wrong. But it seems not the best way in my situation...
Paul
+2  A: 

Maybe using a parser is not so bad idea?

Torbins
Thank you. I am going to investigating on it.
Paul
Actually, this is the only way. It is exactly the reason that for instance http://www.modelmakertools.com/code-explorer/index.html (Model Maker Code Explorer) and http://edn.embarcadero.com/article/40552 (Delphi built-in-refactoring) have written their own parser (outside of the Delphi compiler).
Jeroen Pluimers