codetools: identifier completion used unit

git-svn-id: trunk@19810 -
This commit is contained in:
mattias 2009-05-05 09:14:08 +00:00
parent b67b6ddaa2
commit 620900273e
3 changed files with 103 additions and 63 deletions

View File

@ -735,6 +735,13 @@ type
function FindInitializationSection: TCodeTreeNode;
function FindMainUsesSection(UseContainsSection: boolean = false): TCodeTreeNode;
function FindImplementationUsesSection: TCodeTreeNode;
function FindNameInUsesSection(UsesNode: TCodeTreeNode;
const UnitName: string): TCodeTreeNode;
function FindUnitInUsesSection(UsesNode: TCodeTreeNode;
const UpperUnitName: string;
out NamePos, InPos: TAtomPosition): boolean;
function FindUnitInAllUsesSections(const UpperUnitName: string;
out NamePos, InPos: TAtomPosition): boolean;
function FindUnitSource(const AnUnitName,
AnUnitInFilename: string; ExceptionOnNotFound: boolean): TCodeBuffer;
@ -1704,6 +1711,72 @@ begin
if (Result.Desc<>ctnUsesSection) then Result:=nil;
end;
function TFindDeclarationTool.FindNameInUsesSection(UsesNode: TCodeTreeNode;
const UnitName: string): TCodeTreeNode;
begin
Result:=UsesNode.FirstChild;
while (Result<>nil)
and CompareSrcIdentifier(Result.StartPos,PChar(UnitName)) do
Result:=Result.NextBrother;
end;
function TFindDeclarationTool.FindUnitInUsesSection(UsesNode: TCodeTreeNode;
const UpperUnitName: string; out NamePos, InPos: TAtomPosition): boolean;
begin
Result:=false;
NamePos:=CleanAtomPosition;
InPos:=CleanAtomPosition;
if (UsesNode=nil) or (UpperUnitName='') or (length(UpperUnitName)>255)
or (UsesNode.Desc<>ctnUsesSection) then exit;
MoveCursorToNodeStart(UsesNode);
ReadNextAtom; // read 'uses'
repeat
ReadNextAtom; // read name
if AtomIsChar(';') then break;
if UpAtomIs(UpperUnitName) then begin
NamePos:=CurPos;
InPos.StartPos:=-1;
ReadNextAtom;
if UpAtomIs('IN') then begin
ReadNextAtom;
InPos:=CurPos;
end;
Result:=true;
exit;
end;
ReadNextAtom;
if UpAtomIs('IN') then begin
ReadNextAtom;
ReadNextAtom;
end;
if AtomIsChar(';') then break;
if not AtomIsChar(',') then break;
until (CurPos.StartPos>SrcLen);;
end;
function TFindDeclarationTool.FindUnitInAllUsesSections(
const UpperUnitName: string; out NamePos, InPos: TAtomPosition): boolean;
var SectionNode, UsesNode: TCodeTreeNode;
begin
Result:=false;
NamePos.StartPos:=-1;
InPos.StartPos:=-1;
if (UpperUnitName='') or (length(UpperUnitName)>255) then exit;
BuildTree(false);
SectionNode:=Tree.Root;
while (SectionNode<>nil) and (SectionNode.Desc in [ctnProgram, ctnUnit,
ctnPackage,ctnLibrary,ctnInterface,ctnImplementation])
do begin
UsesNode:=SectionNode.FirstChild;
if (UsesNode<>nil) and (UsesNode.Desc=ctnUsesSection)
and FindUnitInUsesSection(UsesNode,UpperUnitName,NamePos,InPos) then begin
Result:=true;
exit;
end;
SectionNode:=SectionNode.NextBrother;
end;
end;
function TFindDeclarationTool.FindInitializationSection: TCodeTreeNode;
begin
Result:=Tree.Root;

View File

@ -203,6 +203,7 @@ type
function StartUpAtomBehindIs(const s: string): boolean;
function CompletePrefix(const OldPrefix: string): string;
procedure ToolTreeChange(Tool: TCustomCodeTool; NodesDeleting: boolean);
function GetUnitForUsesSection(Item: TIdentifierListItem): string;
public
property Context: TFindContext read FContext write FContext;
property ContextFlags: TIdentifierListContextFlags
@ -787,6 +788,35 @@ begin
end;
end;
function TIdentifierList.GetUnitForUsesSection(Item: TIdentifierListItem): string;
var
Alternative: String;
UsesNode: TCodeTreeNode;
begin
Result:='';
if (Item.Tool=nil) or (Item.Tool=StartContext.Tool) then
exit;
Result:=ExtractFileNameOnly(Item.Tool.MainFilename);
UsesNode:=Item.Tool.FindMainUsesSection;
if (UsesNode<>nil) and (Item.Tool.FindNameInUsesSection(UsesNode,Result)<>nil)
then begin
Result:='';
exit;
end;
UsesNode:=Item.Tool.FindImplementationUsesSection;
if (UsesNode<>nil) and (Item.Tool.FindNameInUsesSection(UsesNode,Result)<>nil)
then begin
Result:='';
exit;
end;
if Result=lowercase(Result) then begin
Alternative:=Item.Tool.GetSourceName(false);
if Alternative<>'' then
Result:=Alternative;
end;
end;
{ TIdentCompletionTool }
function TIdentCompletionTool.CollectAllIdentifiers(

View File

@ -79,11 +79,6 @@ type
SourceChangeCache: TSourceChangeCache): boolean;
// uses sections
function FindUnitInUsesSection(UsesNode: TCodeTreeNode;
const UpperUnitName: string;
out NamePos, InPos: TAtomPosition): boolean;
function FindUnitInAllUsesSections(const UpperUnitName: string;
out NamePos, InPos: TAtomPosition): boolean;
function RenameUsedUnit(const OldUpperUnitName, NewUnitName,
NewUnitInFile: string;
SourceChangeCache: TSourceChangeCache): boolean;
@ -377,64 +372,6 @@ begin
Result:=true;
end;
function TStandardCodeTool.FindUnitInUsesSection(UsesNode: TCodeTreeNode;
const UpperUnitName: string;
out NamePos, InPos: TAtomPosition): boolean;
begin
Result:=false;
NamePos:=CleanAtomPosition;
InPos:=CleanAtomPosition;
if (UsesNode=nil) or (UpperUnitName='') or (length(UpperUnitName)>255)
or (UsesNode.Desc<>ctnUsesSection) then exit;
MoveCursorToNodeStart(UsesNode);
ReadNextAtom; // read 'uses'
repeat
ReadNextAtom; // read name
if AtomIsChar(';') then break;
if UpAtomIs(UpperUnitName) then begin
NamePos:=CurPos;
InPos.StartPos:=-1;
ReadNextAtom;
if UpAtomIs('IN') then begin
ReadNextAtom;
InPos:=CurPos;
end;
Result:=true;
exit;
end;
ReadNextAtom;
if UpAtomIs('IN') then begin
ReadNextAtom;
ReadNextAtom;
end;
if AtomIsChar(';') then break;
if not AtomIsChar(',') then break;
until (CurPos.StartPos>SrcLen);;
end;
function TStandardCodeTool.FindUnitInAllUsesSections(
const UpperUnitName: string; out NamePos, InPos: TAtomPosition): boolean;
var SectionNode, UsesNode: TCodeTreeNode;
begin
Result:=false;
NamePos.StartPos:=-1;
InPos.StartPos:=-1;
if (UpperUnitName='') or (length(UpperUnitName)>255) then exit;
BuildTree(false);
SectionNode:=Tree.Root;
while (SectionNode<>nil) and (SectionNode.Desc in [ctnProgram, ctnUnit,
ctnPackage,ctnLibrary,ctnInterface,ctnImplementation])
do begin
UsesNode:=SectionNode.FirstChild;
if (UsesNode<>nil) and (UsesNode.Desc=ctnUsesSection)
and FindUnitInUsesSection(UsesNode,UpperUnitName,NamePos,InPos) then begin
Result:=true;
exit;
end;
SectionNode:=SectionNode.NextBrother;
end;
end;
function TStandardCodeTool.RenameUsedUnit(const OldUpperUnitName,
NewUnitName, NewUnitInFile: string;
SourceChangeCache: TSourceChangeCache): boolean;