diff --git a/components/codetools/codecompletiontool.pas b/components/codetools/codecompletiontool.pas index 1584e36c5b..56b5c20b5b 100644 --- a/components/codetools/codecompletiontool.pas +++ b/components/codetools/codecompletiontool.pas @@ -1037,7 +1037,7 @@ begin end; if InsertPos<1 then begin InsertNode:=FindFirstSectionChild; - while (InsertNode<>nil) and (InsertNode.Desc=ctnIdentifier) do + if (InsertNode<>nil) and (InsertNode.Desc=ctnSrcName) then InsertNode:=InsertNode.NextBrother; if InsertNode<>nil then begin Indent:=Beauty.GetLineIndent(Src,InsertNode.StartPos); @@ -6832,7 +6832,7 @@ begin ctnProgram,ctnLibrary,ctnPackage] then begin Node:=CursorNode.FirstChild; - while (Node<>nil) and (Node.Desc=ctnIdentifier) do + if (Node<>nil) and (Node.Desc=ctnSrcName) then Node:=Node.NextBrother; // make sure to insert behind uses section and proc header if (Node<>nil) and (Node.Desc in [ctnUsesSection,ctnProcedureHead]) then diff --git a/components/codetools/codetree.pas b/components/codetools/codetree.pas index e7cd152802..9cc13f30b1 100644 --- a/components/codetools/codetree.pas +++ b/components/codetools/codetree.pas @@ -58,7 +58,7 @@ const // CodeTreeNodeDescriptors ctnNone = 0; - ctnProgram = 1; // children are ctnInterface, each namespace and sourcename + ctnProgram = 1; // children are ctnSrcName, ctnUsesSection ctnPackage = 2; ctnLibrary = 3; ctnUnit = 4; @@ -84,9 +84,10 @@ const ctnConstDefinition = 22; ctnGlobalProperty = 23; ctnVarArgs = 24; - ctnUseUnit = 25; // StartPos=unit, EndPos=unitname+inFilename, children ctnUseUnitNamespace, ctnUseUnitClearName, parent ctnUsesSection - ctnUseUnitNamespace = 26; // .clearname.pas, parent ctnUseUnit - ctnUseUnitClearName = 27; // namespace..pas, parent ctnUseUnit + ctnSrcName = 25; // children are ctnIdentifier + ctnUseUnit = 26; // StartPos=unit, EndPos=unitname+inFilename, children ctnUseUnitNamespace, ctnUseUnitClearName, parent ctnUsesSection + ctnUseUnitNamespace = 27; // .clearname.pas, parent ctnUseUnit + ctnUseUnitClearName = 28; // namespace..pas, parent ctnUseUnit ctnClass = 30; ctnClassInterface = 31; @@ -407,6 +408,8 @@ begin ctnPackage: Result:='Package'; ctnLibrary: Result:='Library'; ctnUnit: Result:='Unit'; + ctnSrcName: Result:='SourceName'; + ctnUseUnit: Result:='use unit'; ctnUseUnitNamespace: Result:='Namespace'; ctnUseUnitClearName: Result:='Use unit name'; ctnInterface: Result:='Interface Section'; @@ -429,7 +432,6 @@ begin ctnVarDefinition: Result:='Var'; ctnConstDefinition: Result:='Const'; ctnGlobalProperty: Result:='Global Property'; - ctnUseUnit: Result:='use unit'; ctnVarArgs: Result:='VarArgs'; ctnProperty: Result:='Property'; // can start with 'class property' diff --git a/components/codetools/finddeclarationtool.pas b/components/codetools/finddeclarationtool.pas index 7581fc5b06..0a599655b1 100644 --- a/components/codetools/finddeclarationtool.pas +++ b/components/codetools/finddeclarationtool.pas @@ -752,7 +752,7 @@ type function FindIdentifierInAncestors(ClassNode: TCodeTreeNode; Params: TFindDeclarationParams): boolean; function FindIdentifierInUsesSection(UsesNode: TCodeTreeNode; - Params: TFindDeclarationParams; FindMissingFPCUnits: Boolean): boolean; // ToDo: dotted + Params: TFindDeclarationParams; FindMissingFPCUnits: Boolean): boolean; function FindIdentifierInHiddenUsedUnits( Params: TFindDeclarationParams): boolean; function FindIdentifierInUsedUnit(const AnUnitName: string; @@ -5446,6 +5446,13 @@ begin then begin // this is the ON variable node, the type comes right behind Result.Node:=Result.Node.NextBrother; + end else if Result.Node.Desc=ctnSrcName then begin + break; + end else if (Result.Node.Desc=ctnIdentifier) + and (Result.Node.Parent.Desc=ctnSrcName) then begin + if (Result.Node.NextBrother=nil) then + Result.Node:=Result.Node.Parent; + break; end else if (Result.Node.Desc in [ctnIdentifier,ctnOnIdentifier]) then begin @@ -8973,7 +8980,7 @@ var DottedIdentifier: String; begin Result:=StartUseUnitNode.FirstChild; - //debugln(['ResolveUsenit START ',NextAtomType,' ',StartUseUnitNode.DescAsString]); + //debugln(['ResolveUsenit START ',NextAtomType,' ',StartUseUnitNode.DescAsString,' "',GetIdentifier(@Src[CurAtom.StartPos]),'"']); // find all candidates Count:=0; HasNamespace:=false; @@ -8991,7 +8998,7 @@ var UseUnitNode:=GetPrevUseUnit(UseUnitNode); until UseUnitNode=nil; //debugln(['ResolveUsenit CandidateCount=',Count,' HasNamespace=',HasNamespace]); - if (Count<=1) or not HasNamespace then exit; + if not HasNamespace then exit; // multiple uses start with this identifier -> collect candidates //debugln(['ResolveUsenit collect candidates ...']); @@ -9052,33 +9059,34 @@ var // check source name if (Tree.Root.Desc in AllSourceTypes) and (Tree.Root.FirstChild<>nil) + and (Tree.Root.FirstChild.Desc=ctnSrcName) and CompareSrcIdentifiers(Tree.Root.FirstChild.StartPos,PChar(DottedIdentifier)) then begin // found candidate Level:=1; - Node:=Tree.Root.FirstChild; + Node:=Tree.Root.FirstChild.FirstChild; //debugln(['ResolveUseUnit Candidate SrcName']); p:=PChar(DottedIdentifier); repeat //debugln('ResolveUseUnit SrcName p=',p,' Node=',ExtractNode(Node,[])); - if (Node.NextBrother=nil) or (Node.NextBrother.Desc<>ctnIdentifier) then begin + if (Node.FirstChild=nil) or (Node.NextBrother.Desc<>ctnIdentifier) then begin // fits //debugln(['ResolveUseUnit FITS Level=',Level,' Best=',BestLevel]); if Level>BestLevel then begin // source name fits best - Result:=Tree.Root.FirstChild; + Result:=Tree.Root.FirstChild.FirstChild; // move cursor forward while (Result.NextBrother<>nil) and (NextAtom.EndPosctnIdentifier) then + if (Result.NextBrother=nil) then exit(Tree.Root); ReadNextExpressionAtom; // read point ReadNextExpressionAtom; // read namespace/unitname //debugln(['ResolveUseUnit Next ',GetAtom(CurAtom)]); Result:=Result.NextBrother; end; - exit; //debugln(['ResolveUseUnit SrcName fits better']); + exit; end; break; end else if p^=#0 then begin @@ -9428,8 +9436,13 @@ var {$IFDEF ShowExprEval} debugln([' FindExpressionTypeOfTerm ResolveChildren ExprType=',ExprTypeToString(ExprType)]); {$ENDIF} - if (ExprType.Context.Node=nil) then exit; - if (ExprType.Context.Node.Desc in AllUsableSourceTypes) then begin + NewNode:=ExprType.Context.Node; + if (NewNode=nil) then exit; + if (NewNode.Desc in AllUsableSourceTypes) + or (NewNode.Desc=ctnSrcName) + or ((NewNode.Desc=ctnIdentifier) and (NewNode.Parent.Desc=ctnSrcName) + and (NewNode.NextBrother=nil)) + then begin if ExprType.Context.Tool=Self then begin // unit name of this unit => implementation // Note: allowed for programs too diff --git a/components/codetools/pascalparsertool.pas b/components/codetools/pascalparsertool.pas index be84342112..c5e39f6135 100644 --- a/components/codetools/pascalparsertool.pas +++ b/components/codetools/pascalparsertool.pas @@ -643,8 +643,7 @@ begin then begin // parse source from the beginning if CurNode<>nil then - while CurNode.FirstChild<>nil do - Tree.DeleteNode(CurNode.FirstChild); + DoDeleteNodes(CurNode.FirstChild); if (CurPos.StartPos=1) and (Src<>'') then begin // skip shebang @@ -699,8 +698,14 @@ begin if (CurPos.Flag<>cafWord) or (CurSection in [ctnUnit,ctnPackage]) then AtomIsIdentifierSaveE; + if aNameSpace='' then begin + CreateChildNode; + CurNode.Desc:=ctnSrcName; + end; CreateChildNode; CurNode.Desc:=ctnIdentifier; + CurNode.EndPos:=CurPos.EndPos; + EndChildNode; aName:=GetAtom; ReadNextAtom; // read ';' (or 'platform;' or 'unimplemented;') if CurPos.Flag=cafPoint then begin @@ -709,7 +714,7 @@ begin end else break; until false; - while CurNode.Desc=ctnIdentifier do begin + if CurNode.Desc=ctnSrcName then begin CurNode.EndPos:=CurPos.StartPos; EndChildNode; end; @@ -804,7 +809,7 @@ begin MoveCursorToCleanPos(Node.StartPos); end else begin SubNode:=Node.FirstChild; - while (SubNode<>nil) and (SubNode.Desc=ctnIdentifier) do + if (SubNode<>nil) and (SubNode.Desc=ctnSrcName) then SubNode:=SubNode.NextBrother; if (SubNode<>nil) and (SubNode.Desc=ctnUsesSection) then begin // uses section is already parsed @@ -6015,7 +6020,7 @@ begin Result:=nil; if Section=nil then exit; Result:=Section.FirstChild; - while (Result<>nil) and (Result.Desc=ctnIdentifier) do + if (Result<>nil) and (Result.Desc=ctnSrcName) then Result:=Result.NextBrother; if Result=nil then exit; if Result.Desc<>ctnUsesSection then @@ -6087,7 +6092,7 @@ begin if Result=nil then exit; end; Result:=Result.FirstChild; - while (Result<>nil) and (Result.Desc=ctnIdentifier) do + if (Result<>nil) and (Result.Desc=ctnSrcName) then Result:=Result.NextBrother; if (Result=nil) then exit; if (Result.Desc<>ctnUsesSection) then Result:=nil; @@ -6131,7 +6136,7 @@ begin // lsrSourceName if Range=lsrSourceName then begin if (Result.Desc in AllSourceTypes) and (Result.FirstChild<>nil) - and (Result.FirstChild.Desc=ctnIdentifier) then + and (Result.FirstChild.Desc=ctnSrcName) then Result:=Result.FirstChild; exit; end; @@ -6149,7 +6154,7 @@ begin exit; end; Result:=Result.FirstChild; - while (Result.NextBrother<>nil) and (Result.Desc=ctnIdentifier) do + if (Result.NextBrother<>nil) and (Result.Desc=ctnSrcName) then Result:=Result.NextBrother; // lsrMainUsesSectionStart in unit if Range=lsrMainUsesSectionStart then exit; @@ -6176,7 +6181,7 @@ begin exit; end; Result:=Result.FirstChild; - while (Result.NextBrother<>nil) and (Result.Desc=ctnIdentifier) do + if (Result.NextBrother<>nil) and (Result.Desc=ctnSrcName) then Result:=Result.NextBrother; // lsrImplementationUsesSectionStart if Range=lsrImplementationUsesSectionStart then exit; @@ -6222,7 +6227,7 @@ begin exit; end; Result:=Result.FirstChild; - while (Result.NextBrother<>nil) and (Result.Desc=ctnIdentifier) do + if (Result.NextBrother<>nil) and (Result.Desc=ctnSrcName) then Result:=Result.NextBrother; if Result.Desc<>ctnUsesSection then exit; // lsrMainUsesSectionStart in program @@ -6239,7 +6244,7 @@ begin exit; end; Result:=Result.FirstChild; - while (Result.NextBrother<>nil) and (Result.Desc=ctnIdentifier) do + if (Result.NextBrother<>nil) and (Result.Desc=ctnSrcName) then Result:=Result.NextBrother; if Result.Desc=ctnUsesSection then Result:=Result.NextSkipChilds; @@ -6269,7 +6274,7 @@ begin Result:=FindSectionNodeAtPos(P); if Result=nil then exit; UsesNode:=Result.FirstChild; - while (UsesNode<>nil) and (UsesNode.Desc=ctnIdentifier) do + if (UsesNode<>nil) and (UsesNode.Desc=ctnSrcName) then UsesNode:=UsesNode.NextBrother; if (UsesNode<>nil) and (UsesNode.Desc=ctnUsesSection) then begin diff --git a/components/codetools/pascalreadertool.pas b/components/codetools/pascalreadertool.pas index ce8bf19e05..baa418bafd 100644 --- a/components/codetools/pascalreadertool.pas +++ b/components/codetools/pascalreadertool.pas @@ -1839,7 +1839,7 @@ begin ctnProperty: Result:=GetPropertyNameIdentifier(Node); ctnTypeDefinition,ctnVarDefinition,ctnConstDefinition, - ctnEnumIdentifier,ctnIdentifier: + ctnEnumIdentifier,ctnIdentifier,ctnSrcName: Result:=@Src[Node.StartPos]; end; end;