codetools: directorycache: invalidate directory when a codebuffer is marked deleted

This commit is contained in:
mattias 2025-02-04 16:45:03 +01:00
parent de11537070
commit 116b91b184
13 changed files with 459 additions and 348 deletions

View File

@ -53,6 +53,7 @@ type
TCodeBuffer = class(TSourceLog)
private
FFilename: string;
FOnDeleted: TNotifyEvent;
FReferenceCount: integer;
FScanner: TLinkScanner;
FOnSetScanner: TNotifyEvent;
@ -114,6 +115,7 @@ type
property FileChangeStep: integer read FFileChangeStep; // last loaded/saved changestep, only valid if LoadDateValid=true
property OnSetFilename: TNotifyEvent read FOnSetFilename write FOnSetFilename;
property OnSetScanner: TNotifyEvent read FOnSetScanner write FOnSetScanner;
property OnDeleted: TNotifyEvent read FOnDeleted write FOnDeleted;
property Scanner: TLinkScanner read FScanner write SetScanner;
property ReferenceCount: integer read FReferenceCount;
end;
@ -153,11 +155,13 @@ type
fLastIncludeLinkFileValid: boolean;
fLastIncludeLinkFileChangeStep: integer;
fChangeStep: integer;
FOnCodeDeleted: TNotifyEvent;
FOnDecodeLoaded: TOnCodeCacheDecodeLoaded;
FOnEncodeSaving: TOnCodeCacheEncodeSaving;
function FindIncludeLink(const IncludeFilename: string): string;
function FindIncludeLinkNode(const IncludeFilename: string): TIncludedByLink;
function FindIncludeLinkAVLNode(const IncludeFilename: string): TAVLTreeNode;
procedure OnCodeDeleted(Sender: TObject);
function OnScannerCheckFileOnDisk(Code: TSourceLog): boolean; // true if code changed
function OnScannerGetFileName(Sender: TObject; Code: TSourceLog): string;
function OnScannerLoadSource(Sender: TObject; const AFilename: string;
@ -216,6 +220,7 @@ type
write FOnDecodeLoaded;
property OnEncodeSaving: TOnCodeCacheEncodeSaving read FOnEncodeSaving
write FOnEncodeSaving;
property OnFileDeleted: TNotifyEvent read FOnCodeDeleted write FOnCodeDeleted;
property DefaultEncoding: string read FDefaultEncoding write FDefaultEncoding;
property ChangeStamp: int64 read FChangeStamp;
property DirectoryCachePool: TCTDirectoryCachePool read FDirectoryCachePool
@ -760,6 +765,7 @@ begin
FItems.Add(Result);
Result.FCodeCache:=Self;// must be called after FileName:=
Result.LastIncludedByFile:=FindIncludeLink(Result.Filename);
Result.OnDeleted:=@OnCodeDeleted;
end;
Result.DiskEncoding:=DefaultEncoding;
Result.MemEncoding:=Result.DiskEncoding;
@ -998,6 +1004,27 @@ begin
@CompareAnsiStringWithIncludedByLink);
end;
procedure TCodeCache.OnCodeDeleted(Sender: TObject);
var
Code: TCodeBuffer;
Dir: String;
Cache: TCTDirectoryCache;
begin
if DirectoryCachePool<>nil then begin
Code:=TCodeBuffer(Sender);
if Code.IsVirtual then
Cache:=DirectoryCachePool.GetCache('',false,false)
else begin
Dir:=ExtractFilePath(Code.Filename);
Cache:=DirectoryCachePool.GetCache(Dir,false,false);
end;
if Cache<>nil then
Cache.Invalidate;
end;
if Assigned(OnFileDeleted) then
OnFileDeleted(Sender);
end;
function TCodeCache.FindIncludeLink(const IncludeFilename: string): string;
var Link: TIncludedByLink;
begin
@ -1391,6 +1418,8 @@ begin
if FIsDeleted then begin
Clear;
FIsDeleted:=true;
if Assigned(OnDeleted) then
OnDeleted(Self);
//DebugLn(['TCodeBuffer.SetIsDeleted ',Filename,' ',FileNeedsUpdate]);
end;
end;

View File

@ -6946,7 +6946,7 @@ begin
if (Filename='') or (System.Pos(PathDelim,Filename)>0) then
exit;
Code:=FindFile(Filename);
if Code<>nil then
if (Code<>nil) and not Code.IsDeleted then
Result:=Code.Filename;
end;

View File

@ -43,6 +43,7 @@ procedure TTestChangeDeclaration.TestCTAddProcedureModifier;
+'implementation'+sLineBreak
+'end.';
Code:=CodeToolBoss.CreateFile(FDefFilename);
try
Code.Source:=Src;
if not CodeToolBoss.AddProcModifier(Code,3,3,aModifier) then
begin
@ -62,6 +63,9 @@ procedure TTestChangeDeclaration.TestCTAddProcedureModifier;
AssertEquals('ProcHead',Expected,ProcHead);
end;
end;
finally
Code.IsDeleted:=true;
end;
end;
begin

View File

@ -78,6 +78,7 @@ var
NewCode, Code: TCodeBuffer;
begin
Code:=CodeToolBoss.CreateFile('test1.pas');
try
s:='';
for i:=Low(Src) to High(Src) do
s+=Src[i]+LineEnding;
@ -102,6 +103,9 @@ begin
for i:=Low(Expected) to High(Expected) do
s+=Expected[i]+LineEnding;
CheckDiff(Title,s,Code.Source);
finally
Code.IsDeleted:=true;
end;
end;
procedure TTestCodeCompletion.TestIntfProcUpdateArgName;

View File

@ -111,6 +111,7 @@ begin
AssertEquals('Src is empty',Trim(Src)<>'',true);
AssertEquals('ExpectedSrc is empty',Trim(ExpectedSrc)<>'',true);
Code:=nil;
ExpectedCode:=TCodeBuffer.Create;
try
// replace cursor | marker in Src
@ -146,6 +147,7 @@ begin
AssertEquals('CompleteBlock did no or the wrong completion: ',TrimExpected,TrimResult);
finally
if Code<>nil then Code.IsDeleted:=true;
ExpectedCode.Free;
end;
end;
@ -173,6 +175,7 @@ begin
// replace cursor | marker in Src
Code:=CodeToolBoss.CreateFile('TestCompleteBlock.pas');
try
FullSrc:=CreateFullSrc(Src,p);
if p<1 then
AssertEquals('missing cursor | in test source: "'+dbgstr(Src)+'"',true,false);
@ -185,6 +188,9 @@ begin
debugln(['TTestCodetoolsCompleteBlock.CompleteBlockFail completion: ',dbgstr(Code.Source)]);
AssertEquals('CodeToolBoss.CompleteBlock returned true for incompletable src="'+dbgstr(Src)+'"',true,false);
end;
finally
Code.IsDeleted:=true;
end;
end;
procedure TTestCodetoolsCompleteBlock.TestCompleteBlockClassStart;

View File

@ -119,6 +119,8 @@ begin
AssertEquals('start comment header1.h in front of header2.h',true,Header1Start<Header2Start);
AssertEquals('start comment header2.h in front of end of header1.h',true,Header2Start<Header1End);
finally
Header1.IsDeleted:=true;
Header2.IsDeleted:=true;
Merger.Free;
Filenames.Free;
end;
@ -163,6 +165,7 @@ begin
Check('do not replace macros in #ifdef','#ifdef macro1','#ifdef macro1');
Check('do not replace macros in #ifndef','#ifndef macro1','#ifndef macro1');
finally
Header1.IsDeleted:=true;
Buffers.Free;
Merger.Free;
end;

View File

@ -123,7 +123,6 @@ begin
PascalCompilerNames[pcPas2js],
PascalCompilerNames[CodeToolBoss.GetPascalCompilerForDirectory('')]);
end;
FCode:=CodeToolBoss.CreateFile('test1.pas');
end;

View File

@ -164,6 +164,7 @@ var
TreeChangeStep: LongInt;
begin
Code:=CodeToolBoss.CreateFile('TestRangeScan.pas');
try
Tool:=CodeToolBoss.GetCodeToolForSource(Code,false,true) as TCodeTool;
// scan source
@ -197,6 +198,9 @@ begin
AssertEquals('Step3: RootNode=Tree.Root',true,RootNode=Tool.Tree.Root);
AssertEquals('Step3: TreeChangeStep<>Tool.TreeChangeStep',true,TreeChangeStep<>Tool.TreeChangeStep);
//Tool.WriteDebugTreeReport;
finally
Code.IsDeleted:=true;
end;
end;
procedure TTestCodetoolsRangeScan.TestCTScanRangeAscending;
@ -209,6 +213,7 @@ var
MaxRange: TLinkScannerRange;
begin
Code:=CodeToolBoss.CreateFile('TestRangeScan.pas');
try
Tool:=CodeToolBoss.GetCodeToolForSource(Code,false,true) as TCodeTool;
// empty tool
@ -260,6 +265,9 @@ begin
AssertEquals('end. found',true,Tool.Tree.FindRootNode(ctnEndPoint)<>nil);
end;
end;
finally
Code.IsDeleted:=true;
end;
end;
procedure TTestCodetoolsRangeScan.TestCTScanRangeDescending;
@ -271,6 +279,7 @@ var
r: TLinkScannerRange;
begin
Code:=CodeToolBoss.CreateFile('TestRangeScan.pas');
try
Tool:=CodeToolBoss.GetCodeToolForSource(Code,false,true) as TCodeTool;
// scan source
@ -287,6 +296,9 @@ begin
AssertEquals('RootNode must stay for descending range '+dbgs(r),true,Tool.Tree.Root<>nil);
//Tool.WriteDebugTreeReport;
end;
finally
Code.IsDeleted:=true;
end;
end;
procedure TTestCodetoolsRangeScan.TestCTScanRangeProcModified;
@ -295,6 +307,7 @@ var
Tool: TEventsCodeTool;
begin
Code:=CodeToolBoss.CreateFile('TestRangeScan.pas');
try
Tool:=CodeToolBoss.GetCodeToolForSource(Code,false,true) as TCodeTool;
// scan source
@ -309,6 +322,9 @@ begin
AssertEquals('step2: end. found',true,Tool.Tree.FindRootNode(ctnEndPoint)<>nil);
CheckTree(Tool);
finally
Code.IsDeleted:=true;
end;
end;
procedure TTestCodetoolsRangeScan.TestCTScanRangeImplementationToEnd;
@ -317,6 +333,7 @@ var
Tool: TEventsCodeTool;
begin
Code:=CodeToolBoss.CreateFile('TestRangeScan.pas');
try
Tool:=CodeToolBoss.GetCodeToolForSource(Code,false,true) as TCodeTool;
Code.Source:='';
@ -333,6 +350,9 @@ begin
AssertEquals('step2: end. found',true,Tool.Tree.FindRootNode(ctnEndPoint)<>nil);
CheckTree(Tool);
finally
Code.IsDeleted:=true;
end;
end;
procedure TTestCodetoolsRangeScan.TestCTScanRangeInitializationModified;
@ -341,6 +361,7 @@ var
Tool: TCodeTool;
begin
Code:=CodeToolBoss.CreateFile('TestRangeScan.pas');
try
Tool:=CodeToolBoss.GetCodeToolForSource(Code,false,true) as TCodeTool;
// scan source with initialization
@ -355,6 +376,9 @@ begin
CheckTree(Tool);
//Tool.WriteDebugTreeReport;
finally
Code.IsDeleted:=true;
end;
end;
procedure TTestCodetoolsRangeScan.TestCTScanRangeLibraryInitializationModified;
@ -363,6 +387,7 @@ var
Tool: TCodeTool;
begin
Code:=CodeToolBoss.CreateFile('TestRangeScan.pas');
try
Tool:=CodeToolBoss.GetCodeToolForSource(Code,false,true) as TCodeTool;
// scan source with initialization
@ -377,6 +402,9 @@ begin
CheckTree(Tool);
//Tool.WriteDebugTreeReport;
finally
Code.IsDeleted:=true;
end;
end;
procedure TTestCodetoolsRangeScan.TestCTScanRangeScannerAtEnd;
@ -387,6 +415,7 @@ var
CleanCursorPos: integer;
begin
Code:=CodeToolBoss.CreateFile('TestRangeScan.pas');
try
Tool:=CodeToolBoss.GetCodeToolForSource(Code,false,true) as TCodeTool;
// scan source til implementation uses end
@ -405,6 +434,9 @@ begin
[btSetIgnoreErrorPos]);
AssertEquals('scanner and tool at end',true,Tool.ScannedRange=lsrEnd);
AssertEquals('scanner and tool at end',true,Tool.Scanner.ScannedRange=lsrEnd);
finally
Code.IsDeleted:=true;
end;
end;
procedure TTestCodetoolsRangeScan.TestCTScanRangeProgramNoName;
@ -415,6 +447,7 @@ var
RootNode: TCodeTreeNode;
begin
Code:=CodeToolBoss.CreateFile('TestRangeScan.pas');
try
Tool:=CodeToolBoss.GetCodeToolForSource(Code,false,true) as TCodeTool;
Tool.BuildTree(lsrInit);
Code.Source:=GetSource([crsfProgram,crsfNoSrcName]);
@ -451,6 +484,9 @@ begin
AssertEquals('end. found',true,Tool.Tree.FindRootNode(ctnEndPoint)<>nil);
end;
end;
finally
Code.IsDeleted:=true;
end;
end;
procedure TTestCodetoolsRangeScan.TestCTScanRangeUnitModified;
@ -463,6 +499,7 @@ var
Src: String;
begin
Code:=CodeToolBoss.CreateFile('TestRangeScan.pas');
try
Tool:=CodeToolBoss.GetCodeToolForSource(Code,false,true) as TCodeTool;
Src:=
@ -497,6 +534,9 @@ begin
end;
end;
until false;
finally
Code.IsDeleted:=true;
end;
end;
initialization

View File

@ -79,6 +79,7 @@ begin
s+=Expected[i]+LineEnding;
CheckDiff(Title,s,Code.Source);
finally
Code.IsDeleted:=true;
CodeToolBoss.DefineTree.RemoveDefineTemplate(DefTemp);
end;
end;
@ -135,6 +136,7 @@ begin
Fail('VarNameToToType differ');
end;
finally
Code.IsDeleted:=true;
VarNameToToType.Free;
CodeToolBoss.DefineTree.RemoveDefineTemplate(DefTemp);
end;

View File

@ -1510,6 +1510,7 @@ var
UnitType: TCodeBuffer;
begin
UnitType:=CodeToolBoss.CreateFile('type.pas');
try
UnitType.Source:=
'unit &Type;'+sLineBreak
+'interface'+sLineBreak
@ -1524,6 +1525,9 @@ begin
' r{declaration:&type.r}:=3;',
'end.']);
FindDeclarations(Code);
finally
UnitType.IsDeleted:=true;
end;
end;
procedure TTestFindDeclaration.TestFindDeclaration_AmpersandArray;

View File

@ -146,6 +146,7 @@ begin
+'{$I TestMethodJumpTool2.inc}'+LineEnding
+'end.'+LineEnding;
IncCode:=CodeToolBoss.CreateFile('TestMethodJumpTool2.inc');
try
IncCode.Source:=''
+'{%MainUnit test1.pas}'+LineEnding
+'{$IFDEF UseInterface}'+LineEnding
@ -162,6 +163,9 @@ begin
IncCode,'ProcHeader',false,'ProcBody',true);
Test('Method jump from implementation to interface in one include file',
IncCode,'ProcBody',false,'ProcHeader',false);
finally
IncCode.IsDeleted:=true;
end;
end;
procedure TTestMethodJumpTool.TestMethodJump_IntfToImplSingleProc;

View File

@ -73,7 +73,6 @@ type
procedure TestRenameUsedUnit_Impl;
procedure TestRenameUsedUnit_FN_KeepShort;
procedure TestRenameUsedUnit_InFilename;
// todo: rename uses Bar in 'bar.pas'
end;
implementation
@ -1486,7 +1485,7 @@ var
begin
UsedUnit:=nil;
try
UsedUnit:=CodeToolBoss.CreateFile('type.pp');
UsedUnit:=CodeToolBoss.CreateFile('type.pas');
UsedUnit.Source:='unit &Type;'+LineEnding
+'interface'+LineEnding
+'type'+LineEnding

View File

@ -223,6 +223,7 @@ var
i: Integer;
begin
Code:=CodeToolBoss.CreateFile('test1.pas');
try
s:='';
for i:=Low(Src) to High(Src) do
s+=Src[i]+LineEnding;
@ -238,6 +239,9 @@ begin
for i:=Low(Expected) to High(Expected) do
s+=Expected[i]+LineEnding;
CheckDiff(Title,s,Code.Source);
finally
Code.IsDeleted:=true;
end;
end;
procedure TTestCTStdCodetools.DoTestAddUnitToMainUses(NewUnitName, NewUnitInFilename, UsesSrc, ExpectedUsesSrc: string;
@ -253,6 +257,7 @@ begin
+'begin'+LineEnding
+'end.'+LineEnding;
Code:=CodeToolBoss.CreateFile('TestStdCodeTools.pas');
try
Code.Source:=Header+UsesSrc+Footer;
if not CodeToolBoss.AddUnitToMainUsesSectionIfNeeded(Code,NewUnitName,NewUnitInFilename,Flags) then
begin
@ -267,6 +272,9 @@ begin
debugln(Code.Source);
AssertEquals('AddUnitToMainUsesSectionIfNeeded: ',ExpectedUsesSrc,Src);
end;
finally
Code.IsDeleted:=true;
end;
end;
procedure TTestCTStdCodetools.TestCTStdFindBlockStart;
@ -323,6 +331,7 @@ begin
Test('begin,try,finally,end|end','begin1','begin1end');
Test('begin,try,finally,|end,end','try1finally','try1end');
Test('begin,try,finally,|end,end','try1','try1finally');
Code.IsDeleted:=true;
end;
procedure TTestCTStdCodetools.TestCTUses_AddUses_Start;
@ -405,6 +414,7 @@ procedure TTestCTStdCodetools.TestCTUses_RemoveFromAllUsesSections;
+'begin'+LineEnding
+'end.'+LineEnding;
Code:=CodeToolBoss.CreateFile('TestStdCodeTools.pas');
try
Code.Source:=Header+UsesSrc+Footer;
if not CodeToolBoss.RemoveUnitFromAllUsesSections(Code,RemoveUnit) then
begin
@ -417,6 +427,9 @@ procedure TTestCTStdCodetools.TestCTUses_RemoveFromAllUsesSections;
System.Delete(Src,length(Src)-length(Footer)+1,length(Footer));
AssertEquals('RemoveUnitFromAllUsesSections: ',ExpectedUsesSrc,Src);
end;
finally
Code.IsDeleted:=true;
end;
end;
begin
@ -524,6 +537,7 @@ procedure TTestCTStdCodetools.TestCTSetApplicationTitleStatement;
OldSrc:=Header+OldBeginEnd;
ExpectedSrc:=Header+NewBeginEnd;
Code:=CodeToolBoss.CreateFile('TestStdCodeTools.pas');
try
Code.Source:=OldSrc;
if not CodeToolBoss.SetApplicationTitleStatement(Code,NewTitle) then
begin
@ -541,6 +555,9 @@ procedure TTestCTStdCodetools.TestCTSetApplicationTitleStatement;
writeln(ActualSrc);
Fail('SetApplicationTitleStatement with Title="'+NewTitle+'"');
end;
finally
Code.IsDeleted:=true;
end;
end;
procedure TestLines(NewTitle: string; OldBeginEnd, NewBeginEnd: array of string);