mirror of
https://gitlab.com/freepascal.org/lazarus/lazarus.git
synced 2025-11-23 11:59:30 +01:00
codetools: local var completion: also try first occurrence of undefined variable
git-svn-id: trunk@50042 -
This commit is contained in:
parent
7895129320
commit
b1193c668c
@ -9156,42 +9156,34 @@ end;
|
|||||||
function TCodeCompletionCodeTool.CompleteCode(CursorPos: TCodeXYPosition;
|
function TCodeCompletionCodeTool.CompleteCode(CursorPos: TCodeXYPosition;
|
||||||
OldTopLine: integer; out NewPos: TCodeXYPosition; out NewTopLine: integer;
|
OldTopLine: integer; out NewPos: TCodeXYPosition; out NewTopLine: integer;
|
||||||
SourceChangeCache: TSourceChangeCache): boolean;
|
SourceChangeCache: TSourceChangeCache): boolean;
|
||||||
|
|
||||||
|
function TryCompleteLocalVar(CleanCursorPos: integer;
|
||||||
|
CursorNode: TCodeTreeNode): Boolean;
|
||||||
|
begin
|
||||||
|
// test if Local variable assignment (i:=3)
|
||||||
|
Result:=CompleteLocalVariableAssignment(CleanCursorPos,OldTopLine,
|
||||||
|
CursorNode,NewPos,NewTopLine,SourceChangeCache);
|
||||||
|
if Result then exit;
|
||||||
|
|
||||||
|
// test if Local variable iterator (for i in j)
|
||||||
|
Result:=CompleteLocalVariableForIn(CleanCursorPos,OldTopLine,
|
||||||
|
CursorNode,NewPos,NewTopLine,SourceChangeCache);
|
||||||
|
if Result then exit;
|
||||||
|
|
||||||
|
// test if undeclared local variable as parameter (GetPenPos(x,y))
|
||||||
|
Result:=CompleteLocalIdentifierByParameter(CleanCursorPos,OldTopLine,
|
||||||
|
CursorNode,NewPos,NewTopLine,SourceChangeCache);
|
||||||
|
if Result then exit;
|
||||||
|
end;
|
||||||
|
|
||||||
|
function TryComplete(CleanCursorPos, OrigCleanCursorPos: integer): Boolean;
|
||||||
var
|
var
|
||||||
CleanCursorPos: integer;
|
|
||||||
CursorNode: TCodeTreeNode;
|
CursorNode: TCodeTreeNode;
|
||||||
OldCleanCursorPos: LongInt;
|
|
||||||
var
|
|
||||||
ProcNode, AClassNode: TCodeTreeNode;
|
ProcNode, AClassNode: TCodeTreeNode;
|
||||||
IsEventAssignment: boolean;
|
IsEventAssignment: boolean;
|
||||||
begin
|
begin
|
||||||
//DebugLn(['TCodeCompletionCodeTool.CompleteCode CursorPos=',Dbgs(CursorPos),' OldTopLine=',OldTopLine]);
|
Result := False;
|
||||||
|
|
||||||
Result:=false;
|
|
||||||
if (SourceChangeCache=nil) then
|
|
||||||
RaiseException('need a SourceChangeCache');
|
|
||||||
BuildTreeAndGetCleanPos(trTillCursor,lsrEnd,CursorPos,CleanCursorPos,
|
|
||||||
[btSetIgnoreErrorPos]);
|
|
||||||
OldCleanCursorPos:=CleanCursorPos;
|
|
||||||
NewPos:=CleanCodeXYPosition;
|
|
||||||
NewTopLine:=0;
|
|
||||||
|
|
||||||
// find CodeTreeNode at cursor
|
|
||||||
// skip newline chars
|
|
||||||
while (CleanCursorPos>1) and (Src[CleanCursorPos] in [#10,#13]) do
|
|
||||||
dec(CleanCursorPos);
|
|
||||||
// skip space (first try left)
|
|
||||||
while (CleanCursorPos>1) and (Src[CleanCursorPos] in [' ',#9,';']) do
|
|
||||||
dec(CleanCursorPos);
|
|
||||||
if (CleanCursorPos>0) and (CleanCursorPos<SrcLen)
|
|
||||||
and (Src[CleanCursorPos] in [#10,#13]) then begin
|
|
||||||
// then try right
|
|
||||||
repeat
|
|
||||||
inc(CleanCursorPos);
|
|
||||||
until (CleanCursorPos>=SrcLen) or (not (Src[CleanCursorPos] in [' ',#9]));
|
|
||||||
end;
|
|
||||||
|
|
||||||
CursorNode:=FindDeepestNodeAtPos(CleanCursorPos,true);
|
CursorNode:=FindDeepestNodeAtPos(CleanCursorPos,true);
|
||||||
CodeCompleteSrcChgCache:=SourceChangeCache;
|
|
||||||
FCompletingCursorNode:=CursorNode;
|
FCompletingCursorNode:=CursorNode;
|
||||||
try
|
try
|
||||||
|
|
||||||
@ -9238,19 +9230,7 @@ begin
|
|||||||
IsEventAssignment,NewPos,NewTopLine,SourceChangeCache);
|
IsEventAssignment,NewPos,NewTopLine,SourceChangeCache);
|
||||||
if IsEventAssignment then exit;
|
if IsEventAssignment then exit;
|
||||||
|
|
||||||
// test if Local variable assignment (i:=3)
|
Result:=TryCompleteLocalVar(CleanCursorPos,CursorNode);
|
||||||
Result:=CompleteLocalVariableAssignment(CleanCursorPos,OldTopLine,
|
|
||||||
CursorNode,NewPos,NewTopLine,SourceChangeCache);
|
|
||||||
if Result then exit;
|
|
||||||
|
|
||||||
// test if Local variable iterator (for i in j)
|
|
||||||
Result:=CompleteLocalVariableForIn(CleanCursorPos,OldTopLine,
|
|
||||||
CursorNode,NewPos,NewTopLine,SourceChangeCache);
|
|
||||||
if Result then exit;
|
|
||||||
|
|
||||||
// test if undeclared local variable as parameter (GetPenPos(x,y))
|
|
||||||
Result:=CompleteLocalIdentifierByParameter(CleanCursorPos,OldTopLine,
|
|
||||||
CursorNode,NewPos,NewTopLine,SourceChangeCache);
|
|
||||||
if Result then exit;
|
if Result then exit;
|
||||||
|
|
||||||
// test if procedure call
|
// test if procedure call
|
||||||
@ -9259,12 +9239,134 @@ begin
|
|||||||
if Result then exit;
|
if Result then exit;
|
||||||
|
|
||||||
// test if method body
|
// test if method body
|
||||||
Result:=CompleteMethodByBody(OldCleanCursorPos,OldTopLine,CursorNode,
|
if OrigCleanCursorPos <> -1 then
|
||||||
|
Result:=CompleteMethodByBody(OrigCleanCursorPos,OldTopLine,CursorNode,
|
||||||
NewPos,NewTopLine,SourceChangeCache);
|
NewPos,NewTopLine,SourceChangeCache);
|
||||||
if Result then exit;
|
if Result then exit;
|
||||||
finally
|
finally
|
||||||
FCompletingCursorNode:=nil;
|
FCompletingCursorNode:=nil;
|
||||||
end;
|
end;
|
||||||
|
end;
|
||||||
|
|
||||||
|
function TryFirstLocalIdentOccurence(CursorNode: TCodeTreeNode;
|
||||||
|
OrigCleanCursorPos, CleanCursorPos: Integer): boolean;
|
||||||
|
var
|
||||||
|
AtomContextNode, StatementNode: TCodeTreeNode;
|
||||||
|
IdentAtom, LastCurPos: TAtomPosition;
|
||||||
|
UpIdentifier: string;
|
||||||
|
LastAtomIsDot: Boolean;
|
||||||
|
Params: TFindDeclarationParams;
|
||||||
|
OldCodePos: TCodePosition;
|
||||||
|
begin
|
||||||
|
Result := false;
|
||||||
|
|
||||||
|
// get enclosing Begin block
|
||||||
|
if not (CursorNode.Desc in AllPascalStatements) then exit;
|
||||||
|
StatementNode:=CursorNode;
|
||||||
|
while StatementNode<>nil do begin
|
||||||
|
if (StatementNode.Desc=ctnBeginBlock) then begin
|
||||||
|
if (StatementNode.Parent.Desc in [ctnProcedure,ctnProgram]) then break;
|
||||||
|
end else if StatementNode.Desc in [ctnInitialization,ctnFinalization] then
|
||||||
|
break;
|
||||||
|
StatementNode:=StatementNode.Parent;
|
||||||
|
end;
|
||||||
|
if StatementNode=nil then exit;
|
||||||
|
|
||||||
|
// read UpIdentifier at CleanCursorPos
|
||||||
|
GetIdentStartEndAtPosition(Src,CleanCursorPos,
|
||||||
|
IdentAtom.StartPos,IdentAtom.EndPos);
|
||||||
|
if IdentAtom.StartPos=IdentAtom.EndPos then
|
||||||
|
Exit;
|
||||||
|
|
||||||
|
MoveCursorToAtomPos(IdentAtom);
|
||||||
|
if not AtomIsIdentifier then
|
||||||
|
Exit; // a keyword
|
||||||
|
|
||||||
|
UpIdentifier := GetUpAtom;
|
||||||
|
|
||||||
|
//find first occurence of UpIdentifier from procedure begin until CleanCursorPos
|
||||||
|
//we are interested only in local variables/identifiers
|
||||||
|
// --> the UpIdentifier must not be preceded by a point ("MyObject.I" - if we want to complete I)
|
||||||
|
// and then do another check if it is not available with the "with" command, e.g.
|
||||||
|
MoveCursorToCleanPos(StatementNode.StartPos);
|
||||||
|
if StatementNode.Desc=ctnBeginBlock then
|
||||||
|
BuildSubTreeForBeginBlock(StatementNode);
|
||||||
|
LastAtomIsDot := False;
|
||||||
|
while CurPos.EndPos < CleanCursorPos do
|
||||||
|
begin
|
||||||
|
ReadNextAtom;
|
||||||
|
if not LastAtomIsDot and AtomIsIdentifier and UpAtomIs(UpIdentifier) then
|
||||||
|
begin
|
||||||
|
AtomContextNode:=FindDeepestNodeAtPos(StatementNode,CurPos.StartPos,true);
|
||||||
|
Params:=TFindDeclarationParams.Create(Self, AtomContextNode);
|
||||||
|
try
|
||||||
|
// check if UpIdentifier doesn't exists (e.g. because of a with statement)
|
||||||
|
LastCurPos := CurPos;
|
||||||
|
if not IdentifierIsDefined(CurPos,AtomContextNode,Params) then
|
||||||
|
begin
|
||||||
|
FCompletingCursorNode:=CursorNode;
|
||||||
|
try
|
||||||
|
if not CleanPosToCodePos(OrigCleanCursorPos,OldCodePos) then
|
||||||
|
RaiseException('TCodeCompletionCodeTool.TryFirstLocalIdentOccurence CleanPosToCodePos');
|
||||||
|
CompleteCode:=TryCompleteLocalVar(LastCurPos.StartPos,AtomContextNode);
|
||||||
|
AdjustCursor(OldCodePos,OldTopLine,NewPos,NewTopLine);
|
||||||
|
exit(true);
|
||||||
|
finally
|
||||||
|
FCompletingCursorNode:=nil;
|
||||||
|
end;
|
||||||
|
end;
|
||||||
|
CurPos := LastCurPos;//IdentifierIsDefined changes the CurPos
|
||||||
|
finally
|
||||||
|
Params.Free;
|
||||||
|
end;
|
||||||
|
end;
|
||||||
|
LastAtomIsDot := CurPos.Flag=cafPoint;
|
||||||
|
end;
|
||||||
|
end;
|
||||||
|
|
||||||
|
var
|
||||||
|
CleanCursorPos, OrigCleanCursorPos: integer;
|
||||||
|
CursorNode: TCodeTreeNode;
|
||||||
|
begin
|
||||||
|
//DebugLn(['TCodeCompletionCodeTool.CompleteCode CursorPos=',Dbgs(CursorPos),' OldTopLine=',OldTopLine]);
|
||||||
|
|
||||||
|
Result:=false;
|
||||||
|
if (SourceChangeCache=nil) then
|
||||||
|
RaiseException('need a SourceChangeCache');
|
||||||
|
BuildTreeAndGetCleanPos(trTillCursor,lsrEnd,CursorPos,CleanCursorPos,
|
||||||
|
[btSetIgnoreErrorPos]);
|
||||||
|
OrigCleanCursorPos:=CleanCursorPos;
|
||||||
|
NewPos:=CleanCodeXYPosition;
|
||||||
|
NewTopLine:=0;
|
||||||
|
|
||||||
|
// find CodeTreeNode at cursor
|
||||||
|
// skip newline chars
|
||||||
|
while (CleanCursorPos>1) and (Src[CleanCursorPos] in [#10,#13]) do
|
||||||
|
dec(CleanCursorPos);
|
||||||
|
// skip space (first try left)
|
||||||
|
while (CleanCursorPos>1) and (Src[CleanCursorPos] in [' ',#9,';']) do
|
||||||
|
dec(CleanCursorPos);
|
||||||
|
if (CleanCursorPos>0) and (CleanCursorPos<SrcLen)
|
||||||
|
and (Src[CleanCursorPos] in [#10,#13]) then begin
|
||||||
|
// then try right
|
||||||
|
repeat
|
||||||
|
inc(CleanCursorPos);
|
||||||
|
until (CleanCursorPos>=SrcLen) or (not (Src[CleanCursorPos] in [' ',#9]));
|
||||||
|
end;
|
||||||
|
|
||||||
|
CodeCompleteSrcChgCache:=SourceChangeCache;
|
||||||
|
CursorNode:=FindDeepestNodeAtPos(CleanCursorPos,true);
|
||||||
|
FCompletingCursorNode:=CursorNode;
|
||||||
|
try
|
||||||
|
if TryComplete(CleanCursorPos, OrigCleanCursorPos) then exit;
|
||||||
|
finally
|
||||||
|
FCompletingCursorNode:=nil;
|
||||||
|
end;
|
||||||
|
|
||||||
|
{ Find the first occurence of the (local) identifier at cursor in current
|
||||||
|
procedure body and try again. }
|
||||||
|
Result:=TryFirstLocalIdentOccurence(CursorNode,OrigCleanCursorPos,CleanCursorPos);
|
||||||
|
if Result then exit;
|
||||||
|
|
||||||
{$IFDEF CTDEBUG}
|
{$IFDEF CTDEBUG}
|
||||||
DebugLn('TCodeCompletionCodeTool.CompleteCode nothing to complete ... ');
|
DebugLn('TCodeCompletionCodeTool.CompleteCode nothing to complete ... ');
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user