From b09fddb930e05507a40bd43d6a084f6a5b083653 Mon Sep 17 00:00:00 2001 From: mattias Date: Sun, 16 Mar 2025 13:45:12 +0100 Subject: [PATCH] ide: comment, codetools: FindSourceNameReferences: search virtual files --- components/codetools/codetoolmanager.pas | 39 +++++++++++++++++++----- ide/packages/ideproject/project.pp | 10 +++--- ide/sourcefilemanager.pas | 3 +- 3 files changed, 39 insertions(+), 13 deletions(-) diff --git a/components/codetools/codetoolmanager.pas b/components/codetools/codetoolmanager.pas index 340eb3be9e..ffdb772822 100644 --- a/components/codetools/codetoolmanager.pas +++ b/components/codetools/codetoolmanager.pas @@ -167,6 +167,8 @@ type procedure DoOnGlobalValuesChanged; function DoOnFindUsedUnit(SrcTool: TFindDeclarationTool; const TheUnitName, TheUnitInFilename: string): TCodeBuffer; + function DoOnFindUsedUnit(const SrcFilename, TheUnitName, + TheUnitInFilename: string): TCodeBuffer; function DoOnGetSrcPathForCompiledUnit(Sender: TObject; const AFilename: string): string; function DoOnInternalGetMethodName(const AMethod: TMethod; @@ -3054,7 +3056,7 @@ function TCodeToolManager.FindSourceNameReferences(TargetFilename: string; Files SkipComments: boolean; out ListOfSrcNameRefs: TObjectList; WithDuplicates: boolean): boolean; var i, j, InFilenameCleanPos: Integer; - Filename, Dir, TargetUnitName, InFilename, LocalSrcName: String; + Filename, Dir, TargetUnitName, InFilename, LocalSrcName, CurUnitName: String; Code: TCodeBuffer; Tools, DirCachesSearch, DirCachesSkip: TFPList; DirCache: TCTDirectoryCache; @@ -3062,11 +3064,20 @@ var Refs: TSrcNameRefs; Node, NextNode: TAVLTreeNode; CodeXYPos: PCodeXYPosition; + + function CanFindFile(const CurFilename: string): boolean; + begin + Result:=false; + if FilenameIsAbsolute(TargetFilename) then exit; + Result:=DoOnFindUsedUnit(CurFilename,TargetUnitName,'')<>nil; + end; + begin {$IFDEF VerboseFindSourceNameReferences} debugln(['TCodeToolManager.FindSourceNameReferences TargetFile="',TargetFilename,'" FileCount=',Files.Count,' SkipComments=',SkipComments]); {$ENDIF} Result:=false; + TargetUnitName:=ExtractFileNameOnly(TargetFilename); ListOfSrcNameRefs:=nil; Tools:=TFPList.Create; DirCachesSearch:=TFPList.Create; @@ -3089,17 +3100,21 @@ begin // check if directory has target in unitpath Dir:=ExtractFilePath(Filename); DirCache:=DirectoryCachePool.GetCache(Dir,true,false); - if DirCachesSkip.IndexOf(DirCache)>=0 then continue; - if DirCachesSearch.IndexOf(DirCache)<0 then begin - TargetUnitName:=ExtractFileNameOnly(TargetFilename); + if DirCachesSkip.IndexOf(DirCache)>=0 then begin + // this directory does not have target in unit path + // check custom search + if not CanFindFile(Filename) then + continue; + end else if DirCachesSearch.IndexOf(DirCache)<0 then begin + CurUnitName:=TargetUnitName; InFilename:=''; - if DirCache.FindUnitSourceInCompletePath(TargetUnitName,InFilename,true)<>'' then + if DirCache.FindUnitSourceInCompletePath(CurUnitName,InFilename,true)<>'' then begin {$IFDEF VerboseFindSourceNameReferences} debugln(['TCodeToolManager.FindSourceNameReferences File ',Filename,', target in unit path']); {$ENDIF} DirCachesSearch.Add(DirCache); - end else begin + end else if not CanFindFile(Filename) then begin {$IFDEF VerboseFindSourceNameReferences} debugln(['TCodeToolManager.FindSourceNameReferences File ',Filename,', target NOT in unit path, SKIP']); {$ENDIF} @@ -6399,8 +6414,16 @@ function TCodeToolManager.DoOnFindUsedUnit(SrcTool: TFindDeclarationTool; const TheUnitName, TheUnitInFilename: string): TCodeBuffer; begin if Assigned(OnSearchUsedUnit) then - Result:=OnSearchUsedUnit(SrcTool.MainFilename, - TheUnitName,TheUnitInFilename) + Result:=OnSearchUsedUnit(SrcTool.MainFilename,TheUnitName,TheUnitInFilename) + else + Result:=nil; +end; + +function TCodeToolManager.DoOnFindUsedUnit(const SrcFilename, TheUnitName, TheUnitInFilename: string + ): TCodeBuffer; +begin + if Assigned(OnSearchUsedUnit) then + Result:=OnSearchUsedUnit(SrcFilename,TheUnitName,TheUnitInFilename) else Result:=nil; end; diff --git a/ide/packages/ideproject/project.pp b/ide/packages/ideproject/project.pp index 9b469889d4..95e0f6b215 100644 --- a/ide/packages/ideproject/project.pp +++ b/ide/packages/ideproject/project.pp @@ -6648,7 +6648,7 @@ end; procedure TProjectDefineTemplates.UpdateMain; begin if (Owner as TProject).Destroying then exit; - // update the package block define template (the container for all other + // update the block define template (the container for all other // define templates of the project) if FMain=nil then begin // create the main project template @@ -6768,7 +6768,7 @@ var SrcDirDefTempl: TDefineTemplate; IDHasChanged: Boolean; SrcDirMarkDefTempl: TDefineTemplate; - CurUnitPath: String; + CurUnitPath, SrcDir: String; begin //DebugLn('TProjectDefineTemplates.UpdateDefinesForSourceDirectories ',Owner.IDAsString,' Active=',dbgs(Active),' TimeStamp=',dbgs(fLastSourceDirStamp),' Project.TimeStamp=',dbgs(Project.SourceDirectories.TimeStamp)); if (not Owner.NeedsDefineTemplates) or (not Active) then exit; @@ -6822,10 +6822,12 @@ begin UpdateSrcDirIfDef; for i:=0 to fLastSourceDirectories.Count-1 do begin // create directory template + SrcDir:=fLastSourceDirectories[i]; SrcDirDefTempl:=TDefineTemplate.Create('Source Directory '+IntToStr(i+1), - fLastSourceDirectories[i],'',fLastSourceDirectories[i],da_Directory); - DisableDefaultsInDirectories(SrcDirDefTempl,false); + SrcDir,'',fLastSourceDirectories[i],da_Directory); fLastSourceDirectories.Objects[i]:=SrcDirDefTempl; + // add disable defaults + DisableDefaultsInDirectories(SrcDirDefTempl,false); // add proj source directory marker SrcDirMarkDefTempl:=TDefineTemplate.Create('ProjectSrcDirMark', lisProjProjectSourceDirectoryMark, '#ProjectSrcMark'+Owner.IDAsWord, diff --git a/ide/sourcefilemanager.pas b/ide/sourcefilemanager.pas index 969da70ad6..6402d3d8d9 100644 --- a/ide/sourcefilemanager.pas +++ b/ide/sourcefilemanager.pas @@ -1758,6 +1758,7 @@ end; function TProjectUnitFileSelector.InitialSelection(aFilename: string): Boolean; begin Result:=False; + if aFilename='' then ; end; function TProjectUnitFileSelector.Select: TModalResult; @@ -5827,7 +5828,7 @@ begin // check new resource file NewLFMFilename:=''; if FilenameHasPascalExt(NewFilename) then - NewLFMFilename:=ChangeFileExt(NewFilename,'.lfm'); + NewLFMFilename:=ChangeFileExt(NewFilename,'.lfm'); if AnUnitInfo.ComponentName='' then begin // unit has no component // -> remove lfm file, so that it will not be auto loaded on next open