codetools: FindBlockStart: fixed when starting on last atom of block, bug #20797

git-svn-id: trunk@33915 -
This commit is contained in:
mattias 2011-12-02 20:07:45 +00:00
parent 55135cb93d
commit 3ec95f8d25
2 changed files with 34 additions and 26 deletions

View File

@ -4887,6 +4887,7 @@ function TStandardCodeTool.FindBlockStart(const CursorPos: TCodeXYPosition;
// jump to beginning of current block
// e.g. bracket open, 'begin', 'repeat', ...
var CleanCursorPos: integer;
CursorOnStart: Boolean;
begin
Result:=false;
// scan code
@ -4899,29 +4900,33 @@ begin
ReadNextAtom;
try
repeat
ReadPriorAtom;
//debugln(['TStandardCodeTool.FindBlockStart AAA1 ',CleanPosToStr(CurPos.StartPos),' ',GetAtom]);
if (CurPos.StartPos<0) then begin
// start of source found -> this is always a block start
CurPos.StartPos:=1;
Result:=true;
exit;
exit(true);
end
else if Src[CurPos.StartPos] in [')',']','}'] then begin
// jump backward to matching bracket
CursorOnStart:=(CleanCursorPos=CurPos.StartPos);
if not ReadBackwardTilAnyBracketClose then exit;
if CursorOnStart then exit(true);
end
else if WordIsBlockStatementStart.DoItCaseInsensitive(Src,
CurPos.StartPos,CurPos.EndPos-CurPos.StartPos) then
begin
// block start found
Result:=true;
exit;
exit(true);
end else if UpAtomIs('END') or UpAtomIs('FINALLY') or UpAtomIs('EXCEPT')
or UpAtomIs('UNTIL') then
begin
// read backward till BEGIN, CASE, ASM, RECORD, REPEAT
CursorOnStart:=(CleanCursorPos>=CurPos.StartPos)
and (CleanCursorPos<CurPos.EndPos);
ReadBackTilBlockEnd(true);
if CursorOnStart then exit(true);
end;
ReadPriorAtom;
until false;
finally
if Result then begin

View File

@ -36,9 +36,9 @@ var
'program TestStdCodeTools;'+LineEnding
+'begin'+LineEnding
+' if true then {begin1}begin'+LineEnding
+' {try}try'+LineEnding
+' {try1}try'+LineEnding
+' writeln;'+LineEnding
+' finally'+LineEnding
+' {try1finally}finally'+LineEnding
+' writeln;'+LineEnding
+' {try1end}end;'+LineEnding
+' writeln;'+LineEnding
@ -69,31 +69,34 @@ var
Result:=dbgs(XY)+': '+copy(Line,1,XY.X-1)+'|'+copy(Line,XY.X,length(Line));
end;
var
Tool: TCodeTool;
BlockStart: TPoint;
BlockEnd: TPoint;
NewCode: TCodeBuffer;
NewX: integer;
NewY: integer;
NewTopline: integer;
procedure Test(aTitle, StartMarker,EndMarker: string);
var
BlockStart: TPoint;
BlockEnd: TPoint;
NewCode: TCodeBuffer;
NewX: integer;
NewY: integer;
NewTopline: integer;
begin
BlockStart:=GetMarker(StartMarker);
BlockEnd:=GetMarker(EndMarker);
//debugln(['TTestCTStdCodetools.TestCTStdFindBlockStart BlockStart=',GetInfo(BlockStart),' BlockEnd=',GetInfo(BlockEnd)]);
if not CodeToolBoss.FindBlockStart(Code,BlockEnd.X,BlockEnd.Y,NewCode,NewX,NewY,NewTopline)
then
AssertEquals(aTitle+': '+CodeToolBoss.ErrorMessage,true,false)
else
AssertEquals(aTitle,GetInfo(BlockStart),GetInfo(Point(NewX,NewY)))
end;
begin
Code:=CodeToolBoss.CreateFile('TestStdCodeTools.pas');
Tool:=CodeToolBoss.GetCodeToolForSource(Code,false,true) as TCodeTool;
// scan source
Code.Source:=GetSource();
Tool.BuildTree(lsrEnd);
BlockStart:=GetMarker('{begin1}');
BlockEnd:=GetMarker('{begin1end}');
debugln(['TTestCTStdCodetools.TestCTStdFindBlockStart BlockStart=',GetInfo(BlockStart),' BlockEnd=',GetInfo(BlockEnd)]);
if not CodeToolBoss.FindBlockStart(Code,BlockEnd.X,BlockEnd.Y,NewCode,NewX,NewY,NewTopline)
then
AssertEquals('CodeToolBoss.FindBlockStart: begin,try,finally,end|end: '+CodeToolBoss.ErrorMessage,true,false)
else
AssertEquals('CodeToolBoss.FindBlockStart: begin,try,finally,end|end:',GetInfo(BlockStart),GetInfo(Point(NewX,NewY)))
Test('begin,try,finally,end|end','begin1','begin1end');
Test('begin,try,finally,|end,end','try1finally','try1end');
Test('begin,try,finally,|end,end','try1','try1finally');
end;
initialization