mirror of
https://gitlab.com/freepascal.org/lazarus/lazarus.git
synced 2025-08-18 07:39:22 +02:00
MG: started find used unit
git-svn-id: trunk@526 -
This commit is contained in:
parent
c703d8c2a5
commit
386178e8e4
@ -44,15 +44,129 @@ uses
|
|||||||
PascalParserTool;
|
PascalParserTool;
|
||||||
|
|
||||||
type
|
type
|
||||||
TFindDeclarationTool = class(TPascalParserTool)
|
TOnGetSearchPath = function(Sender: TObject): string;
|
||||||
public
|
|
||||||
//function FindDeclaration(CursorPos: TCodeXYPosition; var NewPos: TCodeXYPosition; var NewTopLine: integer): boolean;
|
|
||||||
|
|
||||||
|
TFindDeclarationTool = class(TPascalParserTool)
|
||||||
|
private
|
||||||
|
FOnGetUnitSourceSearchPath: TOnGetSearchPath;
|
||||||
|
function FindDeclarationInUsesSection(UsesNode: TCodeTreeNode;
|
||||||
|
CleanPos: integer;
|
||||||
|
var NewPos: TCodeXYPosition; var NewTopLine: integer): boolean;
|
||||||
|
public
|
||||||
|
function FindDeclaration(CursorPos: TCodeXYPosition;
|
||||||
|
var NewPos: TCodeXYPosition; var NewTopLine: integer): boolean;
|
||||||
|
function FindUnit(const AnUnitName, AnUnitInFilename: string): TCodeBuffer;
|
||||||
|
property OnGetUnitSourceSearchPath: TOnGetSearchPath
|
||||||
|
read FOnGetUnitSourceSearchPath write FOnGetUnitSourceSearchPath;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
implementation
|
implementation
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
{ TFindDeclarationTool }
|
||||||
|
|
||||||
|
function TFindDeclarationTool.FindDeclaration(CursorPos: TCodeXYPosition;
|
||||||
|
var NewPos: TCodeXYPosition; var NewTopLine: integer): boolean;
|
||||||
|
var r, CleanCursorPos: integer;
|
||||||
|
CursorNode: TCodeTreeNode;
|
||||||
|
begin
|
||||||
|
Result:=false;
|
||||||
|
// build code tree
|
||||||
|
{$IFDEF CTDEBUG}
|
||||||
|
writeln('TFindDeclarationTool.FindDeclaration A CursorPos=',CursorPos.X,',',CursorPos.Y);
|
||||||
|
{$ENDIF}
|
||||||
|
BuildTree(false);
|
||||||
|
if not EndOfSourceFound then
|
||||||
|
RaiseException('End Of Source not found');
|
||||||
|
{$IFDEF CTDEBUG}
|
||||||
|
writeln('TFindDeclarationTool.FindDeclaration B');
|
||||||
|
{$ENDIF}
|
||||||
|
// find the CursorPos in cleaned source
|
||||||
|
r:=CaretToCleanPos(CursorPos, CleanCursorPos);
|
||||||
|
if (r<>0) and (r<>-1) then
|
||||||
|
RaiseException('Cursor outside of code');
|
||||||
|
// find CodeTreeNode at cursor
|
||||||
|
CursorNode:=FindDeepestNodeAtPos(CleanCursorPos);
|
||||||
|
if CursorNode=nil then
|
||||||
|
RaiseException('no node found at cursor');
|
||||||
|
if CursorNode.Desc=ctnUsesSection then begin
|
||||||
|
Result:=FindDeclarationInUsesSection(CursorNode,CleanCursorPos,
|
||||||
|
NewPos,NewTopLine);
|
||||||
|
end else begin
|
||||||
|
|
||||||
|
end;
|
||||||
|
end;
|
||||||
|
|
||||||
|
function TFindDeclarationTool.FindDeclarationInUsesSection(
|
||||||
|
UsesNode: TCodeTreeNode; CleanPos: integer;
|
||||||
|
var NewPos: TCodeXYPosition; var NewTopLine: integer): boolean;
|
||||||
|
var UnitName, UnitInFilename: string;
|
||||||
|
UnitNamePos, UnitInFilePos: TAtomPosition;
|
||||||
|
begin
|
||||||
|
Result:=false;
|
||||||
|
{$IFDEF CTDEBUG}
|
||||||
|
writeln('TFindDeclarationTool.FindDeclarationInUsesSection A');
|
||||||
|
{$ENDIF}
|
||||||
|
// reparse uses section
|
||||||
|
MoveCursorToNodeStart(UsesNode);
|
||||||
|
repeat
|
||||||
|
ReadNextAtom; // read name
|
||||||
|
if CurPos.StartPos>CleanPos then break;
|
||||||
|
if AtomIsChar(';') then break;
|
||||||
|
AtomIsIdentifier(true);
|
||||||
|
UnitNamePos:=CurPos;
|
||||||
|
ReadNextAtom;
|
||||||
|
if UpAtomIs('IN') then begin
|
||||||
|
ReadNextAtom;
|
||||||
|
if not AtomIsStringConstant then
|
||||||
|
RaiseException(
|
||||||
|
'syntax error: string constant expected, but '+GetAtom+' found');
|
||||||
|
UnitInFilePos:=CurPos;
|
||||||
|
ReadNextAtom;
|
||||||
|
end else
|
||||||
|
UnitInFilePos.StartPos:=-1;
|
||||||
|
if CleanPos<UnitNamePos.EndPos then begin
|
||||||
|
// cursor is on a unitname -> try to locate it
|
||||||
|
UnitName:=copy(Src,UnitNamePos.StartPos,
|
||||||
|
UnitNamePos.EndPos-UnitNamePos.StartPos);
|
||||||
|
if UnitInFilePos.StartPos>=1 then
|
||||||
|
UnitInFilename:=copy(Src,UnitInFilePos.StartPos,
|
||||||
|
UnitInFilePos.EndPos-UnitInFilePos.StartPos)
|
||||||
|
else
|
||||||
|
UnitInFilename:='';
|
||||||
|
NewPos.Code:=FindUnit(UnitName,UnitInFilename);
|
||||||
|
if NewPos.Code=nil then
|
||||||
|
RaiseException('unit not found: '+UnitName);
|
||||||
|
NewPos.X:=1;
|
||||||
|
NewPos.Y:=1;
|
||||||
|
NewTopLine:=1;
|
||||||
|
Result:=true;
|
||||||
|
exit;
|
||||||
|
end;
|
||||||
|
if AtomIsChar(';') then break;
|
||||||
|
if not AtomIsChar(',') then
|
||||||
|
RaiseException(
|
||||||
|
'syntax error: ; expected, but '+GetAtom+' found')
|
||||||
|
until (CurPos.StartPos>SrcLen);
|
||||||
|
{$IFDEF CTDEBUG}
|
||||||
|
writeln('TFindDeclarationTool.FindDeclarationInUsesSection END cursor not on unitname');
|
||||||
|
{$ENDIF}
|
||||||
|
end;
|
||||||
|
|
||||||
|
function TFindDeclarationTool.FindUnit(const AnUnitName,
|
||||||
|
AnUnitInFilename: string): TCodeBuffer;
|
||||||
|
begin
|
||||||
|
{$IFDEF CTDEBUG}
|
||||||
|
writeln('TFindDeclarationTool.FindUnit A AnUnitName=',AnUnitName,' AnUnitInFilename=',AnUnitInFilename);
|
||||||
|
{$ENDIF}
|
||||||
|
Result:=nil;
|
||||||
|
|
||||||
|
|
||||||
|
end;
|
||||||
|
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -163,26 +163,26 @@ begin
|
|||||||
Result:=false;
|
Result:=false;
|
||||||
NewPos:=CursorPos;
|
NewPos:=CursorPos;
|
||||||
// build code tree
|
// build code tree
|
||||||
// scan for classes, objects and procedure definitions.
|
|
||||||
// there will be no nodes in a class/object
|
|
||||||
{$IFDEF CTDEBUG}
|
{$IFDEF CTDEBUG}
|
||||||
writeln('TMethodJumpingCodeTool.FindJumpPoint A CursorPos=',CursorPos.X,',',CursorPos.Y);
|
writeln('TMethodJumpingCodeTool.FindJumpPoint A CursorPos=',CursorPos.X,',',CursorPos.Y);
|
||||||
{$ENDIF}
|
{$ENDIF}
|
||||||
BuildTree(false);
|
BuildTree(false);
|
||||||
if not EndOfSourceFound then exit;
|
if not EndOfSourceFound then
|
||||||
|
RaiseException('End Of Source not found');
|
||||||
{$IFDEF CTDEBUG}
|
{$IFDEF CTDEBUG}
|
||||||
writeln('TMethodJumpingCodeTool.FindJumpPoint B');
|
writeln('TMethodJumpingCodeTool.FindJumpPoint B');
|
||||||
{$ENDIF}
|
{$ENDIF}
|
||||||
// find the CursorPos in cleaned source
|
// find the CursorPos in cleaned source
|
||||||
r:=CaretToCleanPos(CursorPos, CleanCursorPos);
|
r:=CaretToCleanPos(CursorPos, CleanCursorPos);
|
||||||
if (r<>0) and (r<>-1) then exit;
|
if (r<>0) and (r<>-1) then
|
||||||
|
RaiseException('Cursor outside of code');
|
||||||
GetLineInfo(CleanCursorPos,LineStart,LineEnd,FirstAtomStart,LastAtomEnd);
|
GetLineInfo(CleanCursorPos,LineStart,LineEnd,FirstAtomStart,LastAtomEnd);
|
||||||
if CleanCursorPos<FirstAtomStart then CleanCursorPos:=FirstAtomStart;
|
if CleanCursorPos<FirstAtomStart then CleanCursorPos:=FirstAtomStart;
|
||||||
if CleanCursorPos>=LastAtomEnd then CleanCursorPos:=LastAtomEnd-1;
|
if CleanCursorPos>=LastAtomEnd then CleanCursorPos:=LastAtomEnd-1;
|
||||||
// find CodeTreeNode at cursor
|
// find CodeTreeNode at cursor
|
||||||
CursorNode:=FindDeepestNodeAtPos(CleanCursorPos);
|
CursorNode:=FindDeepestNodeAtPos(CleanCursorPos);
|
||||||
if CursorNode=nil then
|
if CursorNode=nil then
|
||||||
exit;
|
RaiseException('no node found at cursor');
|
||||||
{$IFDEF CTDEBUG}
|
{$IFDEF CTDEBUG}
|
||||||
writeln('TMethodJumpingCodeTool.FindJumpPoint C ',NodeDescriptionAsString(CursorNode.Desc));
|
writeln('TMethodJumpingCodeTool.FindJumpPoint C ',NodeDescriptionAsString(CursorNode.Desc));
|
||||||
{$ENDIF}
|
{$ENDIF}
|
||||||
|
Loading…
Reference in New Issue
Block a user