ide: find/rename identifier use for source name the new codetools methods

This commit is contained in:
mattias 2025-02-06 18:00:52 +01:00
parent b0d17dbe66
commit 985bbdbf7f
5 changed files with 521 additions and 342 deletions

View File

@ -571,15 +571,16 @@ type
const OldIdentifier, NewIdentifier: string;
DeclarationCode: TCodeBuffer; DeclarationCaretXY: PPoint): boolean;
// find all references of the source name
function FindSourceNameReferences(TargetFilename: string;
Files: TStringList; SkipComments: boolean;
out ListOfSrcNameRefs: TObjectList; WithDuplicates: boolean = false): boolean;
function RenameSourceNameReferences(OldFilename, NewFilename, NewSrcname: string;
ListOfSrcNameRefs: TObjectList): boolean;
// todo: deprecate FindUnitReferences
// find all references inside UnitCode to TargetCode
function FindUnitReferences(UnitCode, TargetCode: TCodeBuffer;
SkipComments: boolean; var ListOfPCodeXYPosition: TFPList): boolean;
// todo: deprecate FindUsedUnitReferences
function FindUsedUnitReferences(Code: TCodeBuffer; X, Y: integer;
SkipComments: boolean; out UsedUnitFilename: string;
var ListOfPCodeXYPosition: TFPList): boolean;
@ -3056,7 +3057,7 @@ var
Tools, DirCachesSearch, DirCachesSkip: TFPList;
DirCache: TCTDirectoryCache;
TreeOfPCodeXYPosition: TAVLTree;
Param: TSrcNameRefs;
Refs: TSrcNameRefs;
Node, NextNode: TAVLTreeNode;
CodeXYPos: PCodeXYPosition;
begin
@ -3140,15 +3141,15 @@ begin
continue;
end;
if (not WithDuplicates) and (ListOfSrcNameRefs<>nil) then begin
// elimnate duplicates
// eliminate duplicates
for j:=0 to ListOfSrcNameRefs.Count-1 do begin
Param:=TSrcNameRefs(ListOfSrcNameRefs[j]);
if Param.TreeOfPCodeXYPosition=nil then continue;
Refs:=TSrcNameRefs(ListOfSrcNameRefs[j]);
if Refs.TreeOfPCodeXYPosition=nil then continue;
Node:=TreeOfPCodeXYPosition.FindLowest;
while Node<>nil do begin
NextNode:=TreeOfPCodeXYPosition.FindSuccessor(Node);
CodeXYPos:=PCodeXYPosition(Node.Data);
if Param.TreeOfPCodeXYPosition.Find(CodeXYPos)<>nil then
if Refs.TreeOfPCodeXYPosition.Find(CodeXYPos)<>nil then
TreeOfPCodeXYPosition.Delete(Node);
Node:=NextNode;
end;
@ -3158,14 +3159,14 @@ begin
{$IFDEF VerboseFindSourceNameReferences}
debugln(['TCodeToolManager.FindSourceNameReferences SrcName="',LocalSrcName,'" Count=',TreeOfPCodeXYPosition.Count]);
{$ENDIF}
Param:=TSrcNameRefs.Create;
Param.Tool:=FCurCodeTool;
Param.LocalSrcName:=LocalSrcName;
Param.InFilenameCleanPos:=InFilenameCleanPos;
Param.TreeOfPCodeXYPosition:=TreeOfPCodeXYPosition;
Refs:=TSrcNameRefs.Create;
Refs.Tool:=FCurCodeTool;
Refs.LocalSrcName:=LocalSrcName;
Refs.InFilenameCleanPos:=InFilenameCleanPos;
Refs.TreeOfPCodeXYPosition:=TreeOfPCodeXYPosition;
if ListOfSrcNameRefs=nil then
ListOfSrcNameRefs:=TObjectList.Create(true);
ListOfSrcNameRefs.Add(Param);
ListOfSrcNameRefs.Add(Refs);
end;
finally
DirCachesSearch.Free;

View File

@ -7385,7 +7385,7 @@ var
end;
end;
function CheckIdentifier(var p: integer): boolean;
function CheckIdentifier(p: integer): boolean;
// check the identifier at start of an expression
var
StartP, Ident: PChar;
@ -7398,15 +7398,11 @@ var
StartPos:=p;
StartP:=@Src[StartPos];
Ident:=PChar(LocalSrcName);
if CompareIdentifiers(StartP,Ident)<>0 then begin
inc(p,GetIdentLen(StartP));
if CompareIdentifiers(StartP,Ident)<>0 then
exit;
end;
if (StartP^<>'&') and WordIsKeyWordFuncList.DoIdentifier(StartP) then begin
if (StartP^<>'&') and WordIsKeyWordFuncList.DoIdentifier(StartP) then
// do not check keyword identifier references without ampersand
inc(p,GetIdentLen(StartP));
exit;
end;
Expr:=ReadDottedIdentifier(Src,p,Scanner.NestedComments);
//debugln([' CheckIdentifier At ',CleanPosToStr(p),' Expr="',Expr,'"']);
@ -7505,7 +7501,7 @@ var
function CheckComment(var StartPos: integer; MaxPos: integer): boolean;
var
c: Char;
AtomStart, CommentLvl: Integer;
AtomStart, CommentLvl, l: Integer;
InStrConst, LastTokenWasPoint, IsDirective: Boolean;
begin
Result:=true;
@ -7576,8 +7572,9 @@ var
if (not IsDirective) and (not SkipComments) and (not InStrConst)
and (not LastTokenWasPoint) then
if not CheckIdentifier(StartPos) then exit(false);
LastTokenWasPoint:=false;
if Src[StartPos]='&' then inc(StartPos); // skip invalid &
inc(StartPos,GetIdentLen(@Src[StartPos]));
LastTokenWasPoint:=false;
end;
'''':
begin
@ -7667,10 +7664,11 @@ var
'a'..'z','A'..'Z','_','&':
begin
if LastTokenWasPoint then
inc(StartPos,GetIdentLen(@Src[StartPos]))
else if not CheckIdentifier(StartPos) then
exit;
if not LastTokenWasPoint then
if not CheckIdentifier(StartPos) then
exit;
if Src[StartPos]='&' then inc(StartPos); // skip invalid &
inc(StartPos,GetIdentLen(@Src[StartPos]));
LastTokenWasPoint:=false;
end;
@ -7928,6 +7926,7 @@ begin
AnUnitName:=ExtractUsedUnitNameAtCursor(@UnitInFilename);
//debugln(['TFindDeclarationTool.FindUsedUnitReferences Used Unit=',AnUnitName,' in "',UnitInFilename,'"']);
TargetCode:=FindUnitSource(AnUnitName,UnitInFilename,true,Node.StartPos);
if TargetCode=nil then exit;
UsedUnitFilename:=TargetCode.Filename;
//debugln(['TFindDeclarationTool.FindUsedUnitReferences TargetCode=',TargetCode.Filename]);
TargetTool:=FOnGetCodeToolForBuffer(Self,TargetCode,false);

View File

@ -291,6 +291,7 @@ type
function ExtractSourceName: string;
function GetSourceNamePos(out NamePos: TAtomPosition): boolean;
function GetSourceName(DoBuildTree: boolean = true): string;
function GetSourceNameNode: TCodeTreeNode;
function GetSourceType: TCodeTreeNodeDesc;
function PositionInSourceName(CleanPos: integer): boolean;
@ -2880,13 +2881,15 @@ begin
end;
function TPascalReaderTool.GetSourceNamePos(out NamePos: TAtomPosition): boolean;
var
Node: TCodeTreeNode;
begin
Result:=false;
NamePos.StartPos:=-1;
if Tree.Root=nil then exit;
MoveCursorToNodeStart(Tree.Root);
ReadNextAtom; // read source type 'program', 'unit' ...
if (Tree.Root.Desc=ctnProgram) and (not UpAtomIs('PROGRAM')) then exit;
NamePos.EndPos:=-1;
Node:=GetSourceNameNode;
if Node=nil then exit;
MoveCursorToNodeStart(Node);
ReadNextAtom; // read name
if not AtomIsIdentifier then exit;
NamePos:=CurPos;
@ -2909,6 +2912,17 @@ begin
Result:=CachedSourceName;
end;
function TPascalReaderTool.GetSourceNameNode: TCodeTreeNode;
begin
Result:=Tree.Root;
if Result=nil then exit;
if not (Result.Desc in AllSourceTypes) then
exit(nil);
Result:=Result.FirstChild;
if Result=nil then exit;
if Result.Desc<>ctnSrcName then exit(nil);
end;
function TPascalReaderTool.NodeIsInAMethod(Node: TCodeTreeNode): boolean;
begin
Result:=false;

File diff suppressed because it is too large Load Diff

View File

@ -4470,7 +4470,7 @@ begin
end;
end;
//we try next file only if read and write were successful, otherwise we retry current file or abort
until not ReadSaveFailFlag;
until (not ReadSaveFailFlag) or AbortFlag;
end;
end;