codetools: move Find*UsesSection to TPascalParserTool, issue #28285

git-svn-id: trunk@49342 -
This commit is contained in:
mattias 2015-06-16 09:22:42 +00:00
parent fc378e05ef
commit 2f643bd4d4
12 changed files with 89 additions and 76 deletions

View File

@ -1308,7 +1308,7 @@ begin
Indent:=Beauty.GetLineIndent(Src,InFrontOfNode.StartPos);
InsertPos:=FindLineEndOrCodeInFrontOfPosition(InFrontOfNode.StartPos);
end else begin
Node:=FindMainUsesSection(false);
Node:=FindMainUsesNode(false);
if Node<>nil then begin
// insert behind uses section
Indent:=Beauty.GetLineIndent(Src,Node.StartPos);
@ -7917,7 +7917,7 @@ begin
Result:=true;
if (fNewMainUsesSectionUnits=nil) then exit;
//DebugLn(['TCodeCompletionCodeTool.InsertAllNewUnitsToMainUsesSection ']);
UsesNode:=FindMainUsesSection;
UsesNode:=FindMainUsesNode;
// remove units, that are already in the uses section
CurSourceName:=GetSourceName(false);

View File

@ -1059,7 +1059,7 @@ begin
exit(true);
end;
// search in used units
UsesNode:=FindMainUsesSection;
UsesNode:=FindMainUsesNode;
if UsesNode=nil then begin
debugln(['TEventsCodeTool.FindClassOfInstance no main uses section found']);
if ExceptionOnNotFound then RaiseClassNotFound;

View File

@ -768,8 +768,8 @@ type
): TCodeTreeNode; // search for type, const, var, proc, prop
function FindInitializationSection: TCodeTreeNode; deprecated; // use FindInitializationNode
function FindMainUsesSection(UseContainsSection: boolean = false): TCodeTreeNode;
function FindImplementationUsesSection: TCodeTreeNode;
function FindMainUsesSection(UseContainsSection: boolean = false): TCodeTreeNode; deprecated; // use FindMainUsesNode
function FindImplementationUsesSection: TCodeTreeNode; deprecated; // use FindImplementationUsesNode
function FindNameInUsesSection(UsesNode: TCodeTreeNode;
const AUnitName: string): TCodeTreeNode;
function FindUnitInUsesSection(UsesNode: TCodeTreeNode;
@ -1719,7 +1719,7 @@ begin
Result:=false;
if Identifier='' then exit;
BuildTree(lsrMainUsesSectionEnd);
UsesNode:=FindMainUsesSection;
UsesNode:=FindMainUsesNode;
if UsesNode=nil then exit;
Params:=TFindDeclarationParams.Create;
@ -1957,34 +1957,12 @@ end;
function TFindDeclarationTool.FindMainUsesSection(UseContainsSection: boolean
): TCodeTreeNode;
begin
Result:=Tree.Root;
if Result=nil then exit;
if UseContainsSection then begin
if Result.Desc<>ctnPackage then exit(nil);
Result:=Result.FirstChild;
while (Result<>nil) and (Result.Desc<>ctnContainsSection) do
Result:=Result.NextBrother;
end else begin
if Result.Desc=ctnUnit then begin
Result:=Result.NextBrother;
if Result=nil then exit;
end;
Result:=Result.FirstChild;
if (Result=nil) then exit;
if (Result.Desc<>ctnUsesSection) then Result:=nil;
end;
Result := FindMainUsesNode(UseContainsSection);
end;
function TFindDeclarationTool.FindImplementationUsesSection: TCodeTreeNode;
begin
Result:=Tree.Root;
if Result=nil then exit;
while (Result<>nil) and (Result.Desc<>ctnImplementation) do
Result:=Result.NextBrother;
if Result=nil then exit;
Result:=Result.FirstChild;
if (Result=nil) then exit;
if (Result.Desc<>ctnUsesSection) then Result:=nil;
Result := FindImplementationUsesNode;
end;
function TFindDeclarationTool.FindNameInUsesSection(UsesNode: TCodeTreeNode;
@ -2084,13 +2062,13 @@ begin
end;
// check if already there
UsesNode:=FindMainUsesSection;
UsesNode:=FindMainUsesNode;
if (UsesNode<>nil) and (FindNameInUsesSection(UsesNode,Result)<>nil)
then begin
Result:='';
exit;
end;
UsesNode:=FindImplementationUsesSection;
UsesNode:=FindImplementationUsesNode;
if (UsesNode<>nil) and (FindNameInUsesSection(UsesNode,Result)<>nil)
then begin
Result:='';
@ -2230,12 +2208,12 @@ begin
//debugln(['TFindDeclarationTool.FindUnitFileInAllUsesSections Self=',ExtractFilename(MainFilename),' Search=',ExtractFilename(AFilename)]);
if AFilename='' then exit;
if CheckMain then begin
Result:=FindUnitFileInUsesSection(FindMainUsesSection,AFilename);
Result:=FindUnitFileInUsesSection(FindMainUsesNode,AFilename);
//debugln(['TFindDeclarationTool.FindUnitFileInAllUsesSections Self=',ExtractFilename(MainFilename),' Search=',ExtractFilename(AFilename),' used in main uses=',Result<>nil]);
if Result<>nil then exit;
end;
if CheckImplementation then
Result:=FindUnitFileInUsesSection(FindImplementationUsesSection,AFilename);
Result:=FindUnitFileInUsesSection(FindImplementationUsesNode,AFilename);
end;
function TFindDeclarationTool.FindUnitSource(const AnUnitName,
@ -5168,14 +5146,14 @@ begin
try
BuildTree(lsrEnd);
InterfaceUsesNode:=FindMainUsesSection;
InterfaceUsesNode:=FindMainUsesNode;
if not CheckUsesSection(InterfaceUsesNode,Found) then exit;
StartPos:=-1;
if Found then begin
StartPos:=InterfaceUsesNode.EndPos;
end else begin
ImplementationUsesNode:=FindImplementationUsesSection;
ImplementationUsesNode:=FindImplementationUsesNode;
if not CheckUsesSection(ImplementationUsesNode,Found) then exit;
if Found then
StartPos:=ImplementationUsesNode.EndPos;

View File

@ -1698,14 +1698,14 @@ begin
debugln(['TCodyIdentifiersDlg.AddToUsesSection "',NewUnitName,'" is hidden used unit']);
exit;
end;
UsesNode:=CurTool.FindMainUsesSection;
UsesNode:=CurTool.FindMainUsesNode;
if (UsesNode<>nil) and (CurTool.FindNameInUsesSection(UsesNode,NewUnitName)<>nil)
then begin
debugln(['TCodyIdentifiersDlg.AddToUsesSection "',NewUnitName,'" is already used in main uses section']);
exit;
end;
if CurInImplementation then begin
UsesNode:=CurTool.FindImplementationUsesSection;
UsesNode:=CurTool.FindImplementationUsesNode;
if (UsesNode<>nil) and (CurTool.FindNameInUsesSection(UsesNode,NewUnitName)<>nil)
then begin
debugln(['TCodyIdentifiersDlg.AddToUsesSection "',NewUnitName,'" is already used in implementation uses section']);

View File

@ -267,7 +267,9 @@ type
// sections / scan range
function FindRootNode(Desc: TCodeTreeNodeDesc): TCodeTreeNode;
function FindInterfaceNode: TCodeTreeNode;
function FindMainUsesNode(UseContainsSection: boolean = false): TCodeTreeNode;
function FindImplementationNode: TCodeTreeNode;
function FindImplementationUsesNode: TCodeTreeNode;
function FindInitializationNode: TCodeTreeNode;
function FindFinalizationNode: TCodeTreeNode;
function FindMainBeginEndNode: TCodeTreeNode;
@ -5866,6 +5868,18 @@ begin
Result:=FindRootNode(ctnImplementation);
end;
function TPascalParserTool.FindImplementationUsesNode: TCodeTreeNode;
begin
Result:=Tree.Root;
if Result=nil then exit;
while (Result<>nil) and (Result.Desc<>ctnImplementation) do
Result:=Result.NextBrother;
if Result=nil then exit;
Result:=Result.FirstChild;
if (Result=nil) then exit;
if (Result.Desc<>ctnUsesSection) then Result:=nil;
end;
function TPascalParserTool.FindInitializationNode: TCodeTreeNode;
begin
Result:=FindRootNode(ctnInitialization);
@ -5891,6 +5905,27 @@ begin
if Result.Desc<>ctnBeginBlock then Result:=nil;
end;
function TPascalParserTool.FindMainUsesNode(UseContainsSection: boolean
): TCodeTreeNode;
begin
Result:=Tree.Root;
if Result=nil then exit;
if UseContainsSection then begin
if Result.Desc<>ctnPackage then exit(nil);
Result:=Result.FirstChild;
while (Result<>nil) and (Result.Desc<>ctnContainsSection) do
Result:=Result.NextBrother;
end else begin
if Result.Desc=ctnUnit then begin
Result:=Result.NextBrother;
if Result=nil then exit;
end;
Result:=Result.FirstChild;
if (Result=nil) then exit;
if (Result.Desc<>ctnUsesSection) then Result:=nil;
end;
end;
function TPascalParserTool.FindFirstSectionChild: TCodeTreeNode;
begin
Result:=Tree.Root;

View File

@ -603,8 +603,8 @@ begin
// first collect all units
if not FindUsedUnitNames(ExistingUnits) then exit;
// then change uses sections
Replace(FindMainUsesSection);
Replace(FindImplementationUsesSection);
Replace(FindMainUsesNode);
Replace(FindImplementationUsesNode);
finally
ExistingUnits.Free;
end;
@ -950,7 +950,7 @@ begin
if UsesSection=usMain then begin
// quick check using only the main uses section
BuildTree(lsrMainUsesSectionEnd);
UsesNode:=FindMainUsesSection;
UsesNode:=FindMainUsesNode;
if (UsesNode<>nil)
and (FindUnitInUsesSection(UsesNode,NewUnitName,Junk,Junk)) then
exit(true); // unit already in main uses section
@ -965,8 +965,8 @@ begin
Beauty:=SourceChangeCache.BeautifyCodeOptions;
SourceChangeCache.BeginUpdate;
try
UsesNode:=FindMainUsesSection;
OtherUsesNode:=FindImplementationUsesSection;
UsesNode:=FindMainUsesNode;
OtherUsesNode:=FindImplementationUsesNode;
if UsesSection=usImplementation then begin
SectionNode:=UsesNode;
UsesNode:=OtherUsesNode;
@ -1062,8 +1062,8 @@ begin
else
BuildTree(lsrImplementationUsesSectionEnd);
case UsesSection Of
usMain: UsesNode:=FindMainUsesSection;
usImplementation: UsesNode:=FindImplementationUsesSection;
usMain: UsesNode:=FindMainUsesNode;
usImplementation: UsesNode:=FindImplementationUsesNode;
end;
Result:=UnitExistsInUsesSection(UsesNode,AnUnitName);
end;
@ -1284,8 +1284,8 @@ begin
ImplementationUsesSection:=nil;
// find the uses sections
BuildTree(lsrImplementationUsesSectionEnd);
MainUsesNode:=FindMainUsesSection;
ImplementatioUsesNode:=FindImplementationUsesSection;
MainUsesNode:=FindMainUsesNode;
ImplementatioUsesNode:=FindImplementationUsesNode;
// create lists
try
MainUsesSection:=UsesSectionToUnitNames(MainUsesNode);
@ -1333,8 +1333,8 @@ begin
// find the uses sections
List:=TStringToStringTree.Create(false);
BuildTree(lsrImplementationUsesSectionEnd);
Collect(FindMainUsesSection,'Main');
Collect(FindImplementationUsesSection,'Implementation');
Collect(FindMainUsesNode,'Main');
Collect(FindImplementationUsesNode,'Implementation');
Result:=true;
end;
@ -1346,7 +1346,7 @@ begin
MainUsesSection:=nil;
// find the uses sections
BuildTree(lsrMainUsesSectionEnd);
MainUsesNode:=FindMainUsesSection;
MainUsesNode:=FindMainUsesNode;
// create lists
try
MainUsesSection:=UsesSectionToFilenames(MainUsesNode);
@ -1366,8 +1366,8 @@ begin
ImplementationUsesSection:=nil;
// find the uses sections
BuildTree(lsrImplementationUsesSectionEnd);
MainUsesNode:=FindMainUsesSection;
ImplementatioUsesNode:=FindImplementationUsesSection;
MainUsesNode:=FindMainUsesNode;
ImplementatioUsesNode:=FindImplementationUsesNode;
// create lists
try
MainUsesSection:=UsesSectionToFilenames(MainUsesNode);
@ -1408,7 +1408,7 @@ begin
NormalUnits:=nil;
// find the uses sections
BuildTree(lsrMainUsesSectionEnd);
UsesNode:=FindMainUsesSection(UseContainsSection);
UsesNode:=FindMainUsesNode(UseContainsSection);
if UsesNode=nil then exit;
FoundInUnits:=TStringList.Create;
MissingInUnits:=TStringList.Create;
@ -1558,9 +1558,9 @@ begin
if FixCase then
SourceChangeCache.MainScanner:=Scanner;
try
if not CheckUsesSection(FindMainUsesSection(true)) then exit;
if not CheckUsesSection(FindMainUsesNode(true)) then exit;
if SearchImplementation
and not CheckUsesSection(FindImplementationUsesSection) then exit;
and not CheckUsesSection(FindImplementationUsesNode) then exit;
except
FreeAndNil(MissingUnits);
raise;
@ -1664,8 +1664,8 @@ begin
Result:=false;
BuildTree(lsrInitializationStart);
SourceChangeCache.MainScanner:=Scanner;
if not CommentUnitsInUsesSection(MissingUnits, SourceChangeCache, FindMainUsesSection) then exit;
if not CommentUnitsInUsesSection(MissingUnits, SourceChangeCache, FindImplementationUsesSection) then exit;
if not CommentUnitsInUsesSection(MissingUnits, SourceChangeCache, FindMainUsesNode) then exit;
if not CommentUnitsInUsesSection(MissingUnits, SourceChangeCache, FindImplementationUsesNode) then exit;
if not SourceChangeCache.Apply then exit;
Result:=true;
end;
@ -1886,8 +1886,8 @@ begin
BuildTree(lsrEnd);
Identifiers:=nil;
try
CheckUsesSection(FindMainUsesSection,false);
CheckUsesSection(FindImplementationUsesSection,true);
CheckUsesSection(FindMainUsesNode,false);
CheckUsesSection(FindImplementationUsesNode,true);
finally
Identifiers.Free;
end;
@ -2415,7 +2415,7 @@ var
fdfIgnoreOverloadedProcs];
Params.ContextNode:=FindInterfaceNode;
if Params.ContextNode=nil then
Params.ContextNode:=FindMainUsesSection;
Params.ContextNode:=FindMainUsesNode;
Params.SetIdentifier(StartTool,Identifier,nil);
try
Params.Save(OldInput);
@ -6361,7 +6361,7 @@ begin
Indent:=Beauty.GetLineIndent(Src,ANode.StartPos);
InsertPos:=ANode.StartPos;
end else begin
ANode:=FindMainUsesSection;
ANode:=FindMainUsesNode;
if ANode<>nil then begin
Indent:=Beauty.GetLineIndent(Src,ANode.StartPos);
InsertPos:=ANode.StartPos;

View File

@ -4325,7 +4325,7 @@ var
if (InsertPos<1) then begin
if DefaultTypeSectionPos<1 then begin
// start a type section at the beginning
Node:=Tool.FindMainUsesSection(false);
Node:=Tool.FindMainUsesNode(false);
if Node<>nil then begin
if Node.NextBrother<>nil then
Node:=Node.NextBrother;

View File

@ -361,7 +361,7 @@ var
with fCTLink do begin
ResetMainScanner;
ParseToUsesSectionEnd;
// Calls either FindMainUsesSection or FindImplementationUsesSection
// Calls either FindMainUsesNode or FindImplementationUsesNode
UsesNode:=UsesSectionNode;
Assert(Assigned(UsesNode),
'UsesNode should be assigned in AddDelphiAndLCLSections->MoveToDelphi');
@ -511,7 +511,7 @@ var
begin
s:=ExtractFileExt(fOwnerTool.fFilename);
IsPackage := (s='.dpk') or (s='.lpk');
Result:=fCTLink.CodeTool.FindMainUsesSection(IsPackage);
Result:=fCTLink.CodeTool.FindMainUsesNode(IsPackage);
end;
procedure TMainUsedUnits.ParseToUsesSectionEnd;
@ -539,7 +539,7 @@ end;
function TImplUsedUnits.UsesSectionNode: TCodeTreeNode;
begin
Result:=fCTLink.CodeTool.FindImplementationUsesSection;
Result:=fCTLink.CodeTool.FindImplementationUsesNode;
end;
procedure TImplUsedUnits.ParseToUsesSectionEnd;
@ -690,10 +690,10 @@ begin
CodeTool.BuildTree(lsrInitializationStart);
if fMainUsedUnits.fUnitsToComment.Count > 0 then
if not CodeTool.CommentUnitsInUsesSection(fMainUsedUnits.fUnitsToComment,
SrcCache, CodeTool.FindMainUsesSection) then exit;
SrcCache, CodeTool.FindMainUsesNode) then exit;
if fImplUsedUnits.fUnitsToComment.Count > 0 then
if not CodeTool.CommentUnitsInUsesSection(fImplUsedUnits.fUnitsToComment,
SrcCache, CodeTool.FindImplementationUsesSection) then exit;
SrcCache, CodeTool.FindImplementationUsesNode) then exit;
if not SrcCache.Apply then exit;
end;
// Add more units meant for only LCL.

View File

@ -9590,14 +9590,14 @@ begin
jmpInterface: Node := Tool.FindInterfaceNode;
jmpInterfaceUses:
begin
Node := Tool.FindMainUsesSection;
Node := Tool.FindMainUsesNode;
if Node = nil then//if the uses section is missing, jump to interface
Node := Tool.FindInterfaceNode;
end;
jmpImplementation: Node := Tool.FindImplementationNode;
jmpImplementationUses:
begin
Node := Tool.FindImplementationUsesSection;
Node := Tool.FindImplementationUsesNode;
if Node = nil then//if the uses section is missing, jump to implementation
Node := Tool.FindImplementationNode;
end;

View File

@ -401,7 +401,7 @@ begin
CodeToolBoss.Explore(ACode,Tool,false);
if Tool=nil then exit;
// collect implementation use unit nodes
ImplUsesNode := Tool.FindImplementationUsesSection;
ImplUsesNode := Tool.FindImplementationUsesNode;
if Assigned(ImplUsesNode) then
for i := 0 to FImplUsedUnits.Count - 1 do
FImplUsedUnits.Objects[i] := ImplUsesNode;

View File

@ -2415,9 +2415,9 @@ var
if LoadCodeBuffer(Code,OldFilename,[lbfUpdateFromDisk,lbfCheckIfText],false)<>mrOk
then exit;
CodeToolBoss.Explore(Code,Tool,false);
if not CheckUsesSection(Tool,Tool.FindMainUsesSection,NewFilename) then
if not CheckUsesSection(Tool,Tool.FindMainUsesNode,NewFilename) then
Result:=false;
if not CheckUsesSection(Tool,Tool.FindImplementationUsesSection,NewFilename) then
if not CheckUsesSection(Tool,Tool.FindImplementationUsesNode,NewFilename) then
Result:=false;
end;
if not Result then begin

View File

@ -227,15 +227,15 @@ begin
lsrInterfaceStart:
AssertEquals('interface start scanned',true,Tool.FindInterfaceNode<>nil);
lsrMainUsesSectionStart:
AssertEquals('main uses section start scanned',true,Tool.FindMainUsesSection<>nil);
AssertEquals('main uses section start scanned',true,Tool.FindMainUsesNode<>nil);
lsrMainUsesSectionEnd:
AssertEquals('main uses section end scanned',true,Tool.FindMainUsesSection.FirstChild<>nil);
AssertEquals('main uses section end scanned',true,Tool.FindMainUsesNode.FirstChild<>nil);
lsrImplementationStart:
AssertEquals('implementation start scanned',true,Tool.FindImplementationNode<>nil);
lsrImplementationUsesSectionStart:
AssertEquals('implementation uses section start scanned',true,Tool.FindImplementationUsesSection<>nil);
AssertEquals('implementation uses section start scanned',true,Tool.FindImplementationUsesNode<>nil);
lsrImplementationUsesSectionEnd:
AssertEquals('implementation uses section end scanned',true,Tool.FindImplementationUsesSection.FirstChild<>nil);
AssertEquals('implementation uses section end scanned',true,Tool.FindImplementationUsesNode.FirstChild<>nil);
lsrInitializationStart:
AssertEquals('initialization section start scanned',true,Tool.FindInitializationNode<>nil);
lsrFinalizationStart: