SynEdit: FoldView, rename/refactor TextToViewIndex/ViewToTextIndex to be 0-based

git-svn-id: trunk@63175 -
This commit is contained in:
martin 2020-05-16 22:17:06 +00:00
parent 0f8b9dd9a8
commit cbb6115663
5 changed files with 33 additions and 33 deletions

View File

@ -1902,7 +1902,7 @@ begin
if not Assigned(FTextArea) then if not Assigned(FTextArea) then
Result := -1 Result := -1
else else
Result := FFoldedLinesView.ViewPosToTextIndex(FTextArea.TopLine) + 1; Result := FFoldedLinesView.ViewToTextIndex(ToIdx(FTextArea.TopLine)) + 1;
end; end;
procedure TCustomSynEdit.SetBlockTabIndent(AValue: integer); procedure TCustomSynEdit.SetBlockTabIndent(AValue: integer);
@ -4740,7 +4740,7 @@ begin
if not HandleAllocated then if not HandleAllocated then
Include(fStateFlags, sfExplicitTopLine); Include(fStateFlags, sfExplicitTopLine);
NewTopView := FFoldedLinesView.TextIndexToViewPos(Value-1); NewTopView := ToPos(FFoldedLinesView.TextToViewIndex(ToIdx(Value)));
if NewTopView <> TopView then begin if NewTopView <> TopView then begin
TopView := NewTopView; TopView := NewTopView;
end; end;
@ -6822,22 +6822,22 @@ begin
end; end;
ecEditorTop, ecSelEditorTop: ecEditorTop, ecSelEditorTop:
begin begin
FCaret.LineCharPos := Point(1, FFoldedLinesView.ViewPosToTextIndex(1)+1); FCaret.LineCharPos := Point(1, ToPos(FFoldedLinesView.ViewToTextIndex(0)));
end; end;
ecEditorBottom, ecSelEditorBottom: ecEditorBottom, ecSelEditorBottom:
begin begin
CaretNew := Point(1, FFoldedLinesView.ViewPosToTextIndex(FFoldedLinesView.Count)+1); CaretNew := Point(1, ToPos(FFoldedLinesView.ViewToTextIndex(ToIdx(FFoldedLinesView.Count))));
if (CaretNew.Y > 0) then if (CaretNew.Y > 0) then
CaretNew.X := Length(FTheLinesView[CaretNew.Y - 1]) + 1; CaretNew.X := Length(FTheLinesView[CaretNew.Y - 1]) + 1;
FCaret.LineCharPos := CaretNew; FCaret.LineCharPos := CaretNew;
end; end;
ecColSelEditorTop: ecColSelEditorTop:
begin begin
FCaret.LinePos := FFoldedLinesView.ViewPosToTextIndex(1)+1; FCaret.LinePos := ToPos(FFoldedLinesView.ViewToTextIndex(0));
end; end;
ecColSelEditorBottom: ecColSelEditorBottom:
begin begin
FCaret.LinePos := FFoldedLinesView.ViewPosToTextIndex(FFoldedLinesView.Count)+1; FCaret.LinePos := ToPos(FFoldedLinesView.ViewToTextIndex(ToIdx(FFoldedLinesView.Count)));
end; end;
// goto special line / column position // goto special line / column position
@ -8388,7 +8388,7 @@ begin
if not(eoScrollPastEol in fOptions) then begin if not(eoScrollPastEol in fOptions) then begin
// move to begin of next line // move to begin of next line
NewCaret.Y:= FFoldedLinesView.TextPosAddLines(FCaret.LinePos, +1); NewCaret.Y:= FFoldedLinesView.TextPosAddLines(FCaret.LinePos, +1);
if NewCaret.Y <= FFoldedLinesView.ViewPosToTextIndex(FFoldedLinesView.Count)+1 then begin if NewCaret.Y <= ToPos(FFoldedLinesView.ViewToTextIndex(ToIdx(FFoldedLinesView.Count))) then begin
NewCaret.X := 1; NewCaret.X := 1;
FCaret.LineBytePos := NewCaret; FCaret.LineBytePos := NewCaret;
end; end;

View File

@ -438,9 +438,9 @@ type
destructor Destroy; override; destructor Destroy; override;
// Converting between Folded and Unfolded Lines/Indexes // Converting between Folded and Unfolded Lines/Indexes
function TextIndexToViewPos(aTextIndex : Integer) : Integer; (* Convert TextIndex (0-based) to ViewPos (1-based) *) function TextToViewIndex(aTextIndex : TLineIdx) : TLineIdx; (* Convert TextIndex (0-based) to ViewPos (1-based) *)
function TextIndexToScreenLine(aTextIndex : Integer) : Integer; (* Convert TextIndex (0-based) to Screen (0-based) *) function TextIndexToScreenLine(aTextIndex : Integer) : Integer; (* Convert TextIndex (0-based) to Screen (0-based) *)
function ViewPosToTextIndex(aViewPos : Integer) : Integer; (* Convert ViewPos (1-based) to TextIndex (0-based) *) function ViewToTextIndex(aViewIndex : TLineIdx) : TLineIdx; (* Convert ViewPos (1-based) to TextIndex (0-based) *)
function ScreenLineToTextIndex(aLine : Integer) : Integer; (* Convert Screen (0-based) to TextIndex (0-based) *) function ScreenLineToTextIndex(aLine : Integer) : Integer; (* Convert Screen (0-based) to TextIndex (0-based) *)
function TextIndexAddLines(aTextIndex, LineOffset : Integer) : Integer; (* Add/Sub to/from TextIndex (0-based) skipping folded *) function TextIndexAddLines(aTextIndex, LineOffset : Integer) : Integer; (* Add/Sub to/from TextIndex (0-based) skipping folded *)
@ -738,7 +738,7 @@ begin
FFoldView.MarkupInfoHiddenCodeLine.SetFrameBoundsLog(1, MaxInt, 0); FFoldView.MarkupInfoHiddenCodeLine.SetFrameBoundsLog(1, MaxInt, 0);
end; end;
inherited SetHighlighterTokensLine(FFoldView.ViewPosToTextIndex(ALine + 1), ARealLine); inherited SetHighlighterTokensLine(FFoldView.ViewToTextIndex(ALine), ARealLine);
end; end;
function TLazSynDisplayFold.GetNextHighlighterToken(out ATokenInfo: TLazSynDisplayTokenInfo): Boolean; function TLazSynDisplayFold.GetNextHighlighterToken(out ATokenInfo: TLazSynDisplayTokenInfo): Boolean;
@ -839,18 +839,18 @@ function TLazSynDisplayFold.TextToViewIndex(AIndex: TLineIdx): TLineRange;
begin begin
Result := inherited TextToViewIndex(AIndex); Result := inherited TextToViewIndex(AIndex);
if Result.Top = Result.Bottom then begin if Result.Top = Result.Bottom then begin
Result.Top := FFoldView.TextIndexToViewPos(Result.Top) - 1; Result.Top := FFoldView.TextToViewIndex(Result.Top);
Result.Bottom := Result.Top; Result.Bottom := Result.Top;
end end
else begin; else begin;
Result.Top := FFoldView.TextIndexToViewPos(Result.Top) - 1; Result.Top := FFoldView.TextToViewIndex(Result.Top);
Result.Bottom := FFoldView.TextIndexToViewPos(Result.Bottom) - 1; Result.Bottom := FFoldView.TextToViewIndex(Result.Bottom);
end; end;
end; end;
function TLazSynDisplayFold.ViewToTextIndex(AIndex: TLineIdx): TLineIdx; function TLazSynDisplayFold.ViewToTextIndex(AIndex: TLineIdx): TLineIdx;
begin begin
Result := FFoldView.ViewPosToTextIndex(inherited ViewToTextIndex(AIndex)+1); Result := FFoldView.ViewToTextIndex(inherited ViewToTextIndex(AIndex));
end; end;
{ TSynEditFoldExportStream } { TSynEditFoldExportStream }
@ -3148,7 +3148,7 @@ end;
//procedure TSynEditFoldedView.LinesInsertedAtViewPos(AStartPos, ALineCount : Integer; SkipFixFolding : Boolean); //procedure TSynEditFoldedView.LinesInsertedAtViewPos(AStartPos, ALineCount : Integer; SkipFixFolding : Boolean);
//begin //begin
// LinesInsertedAtTextIndex(ViewPosToTextIndex(AStartPos), ALineCount, SkipFixFolding); // LinesInsertedAtTextIndex(ViewToTextIndex(ToIdx(AStartPos)), ALineCount, SkipFixFolding);
//end; //end;
procedure TSynEditFoldedView.LinesDeletedAtTextIndex(AStartIndex, ALineCount, ABytePos: Integer; SkipFixFolding : Boolean); procedure TSynEditFoldedView.LinesDeletedAtTextIndex(AStartIndex, ALineCount, ABytePos: Integer; SkipFixFolding : Boolean);
@ -3165,33 +3165,33 @@ end;
//procedure TSynEditFoldedView.LinesDeletedAtViewPos(AStartPos, ALineCount : Integer; SkipFixFolding : Boolean); //procedure TSynEditFoldedView.LinesDeletedAtViewPos(AStartPos, ALineCount : Integer; SkipFixFolding : Boolean);
//begin //begin
// LinesDeletedAtTextIndex(ViewPosToTextIndex(AStartPos), ALineCount, SkipFixFolding); // LinesDeletedAtTextIndex(ViewToTextIndex(ToIdx(AStartPos)), ALineCount, SkipFixFolding);
//end; //end;
function TSynEditFoldedView.TextIndexToViewPos(aTextIndex : Integer) : Integer; function TSynEditFoldedView.TextToViewIndex(aTextIndex: TLineIdx): TLineIdx;
var var
n: TSynTextFoldAVLNode; n: TSynTextFoldAVLNode;
begin begin
n := fFoldTree.FindFoldForLine(aTextIndex + 1); n := fFoldTree.FindFoldForLine(aTextIndex + 1);
if n.IsInFold then if n.IsInFold then
Result := n.StartLine - 1 - n.FoldedBefore Result := ToIdx(n.StartLine) - 1 - n.FoldedBefore
else else
Result := aTextIndex + 1 - n.FoldedBefore; Result := aTextIndex - n.FoldedBefore;
end; end;
function TSynEditFoldedView.TextIndexToScreenLine(aTextIndex : Integer) : Integer; function TSynEditFoldedView.TextIndexToScreenLine(aTextIndex : Integer) : Integer;
begin begin
Result := TextIndexToViewPos(aTextIndex) - TopLine; Result := TextToViewIndex(aTextIndex) - TopLine + 1;
end; end;
function TSynEditFoldedView.ViewPosToTextIndex(aViewPos : Integer) : Integer; function TSynEditFoldedView.ViewToTextIndex(aViewIndex: TLineIdx): TLineIdx;
begin begin
result := aViewPos - 1 + fFoldTree.FindFoldForFoldedLine(aViewPos).FoldedBefore; result := aViewIndex + fFoldTree.FindFoldForFoldedLine(ToPos(aViewIndex)).FoldedBefore;
end; end;
function TSynEditFoldedView.ScreenLineToTextIndex(aLine : Integer) : Integer; function TSynEditFoldedView.ScreenLineToTextIndex(aLine : Integer) : Integer;
begin begin
Result := ViewPosToTextIndex(aLine + TopLine); Result := ViewToTextIndex(aLine + TopLine - 1);
end; end;
function TSynEditFoldedView.TextIndexAddLines(aTextIndex, LineOffset : Integer) : Integer; function TSynEditFoldedView.TextIndexAddLines(aTextIndex, LineOffset : Integer) : Integer;
@ -3202,7 +3202,7 @@ begin
node := fFoldTree.FindFoldForLine(aTextIndex+1, True); node := fFoldTree.FindFoldForLine(aTextIndex+1, True);
result := aTextIndex; result := aTextIndex;
if LineOffset < 0 then begin if LineOffset < 0 then begin
boundary := Max(0, ViewPosToTextIndex(1)); boundary := Max(0, ViewToTextIndex(0));
if node.IsInFold if node.IsInFold
then node := node.Prev then node := node.Prev
else node := fFoldTree.FindLastFold; else node := fFoldTree.FindLastFold;

View File

@ -158,7 +158,7 @@ begin
tmp2 := FoldView.FoldType[AScreenLine-1]; tmp2 := FoldView.FoldType[AScreenLine-1];
FIsFoldHidePreviousLine := False; FIsFoldHidePreviousLine := False;
if (AScreenLine = 0) and (FoldView.TextIndexToViewPos(FoldView.TextIndex[0]) = 1) and if (AScreenLine = 0) and (FoldView.TextToViewIndex(FoldView.TextIndex[0]) = 0) and
(cfCollapsedHide in tmp2) (cfCollapsedHide in tmp2)
then begin then begin
Result := cfCollapsedHide; Result := cfCollapsedHide;

View File

@ -14,7 +14,7 @@ uses
Classes, SysUtils, math, testregistry, TestBase, TestHighlightPas, Forms, Classes, SysUtils, math, testregistry, TestBase, TestHighlightPas, Forms,
LCLProc, SynEdit, SynHighlighterPas, SynEditFoldedView, LCLProc, SynEdit, SynHighlighterPas, SynEditFoldedView,
SynEditHighlighterFoldBase, SynGutterCodeFolding, SynEditKeyCmds, SynEditHighlighterFoldBase, SynGutterCodeFolding, SynEditKeyCmds,
SynEditTypes; SynEditTypes, SynEditMiscProcs;
type type
@ -163,10 +163,10 @@ begin
i := 0; i := 0;
while i < high(AExpectedPairs)-1 do begin while i < high(AExpectedPairs)-1 do begin
AssertEquals(AName+' TxtIdx('+IntToStr( AExpectedPairs[i])+') to ViewPos[1-based]('+IntToStr( AExpectedPairs[i+1])+') ', AssertEquals(AName+' TxtIdx('+IntToStr( AExpectedPairs[i])+') to ViewPos[1-based]('+IntToStr( AExpectedPairs[i+1])+') ',
AExpectedPairs[i+1], FoldedView.TextIndexToViewPos(AExpectedPairs[i])); AExpectedPairs[i+1], ToPos(FoldedView.TextToViewIndex(AExpectedPairs[i])));
if ADoReverse then if ADoReverse then
AssertEquals(AName+' ViewPos[1-based]('+IntToStr( AExpectedPairs[i+1])+') to TxtIdx('+IntToStr( AExpectedPairs[i])+') [R]', AssertEquals(AName+' ViewPos[1-based]('+IntToStr( AExpectedPairs[i+1])+') to TxtIdx('+IntToStr( AExpectedPairs[i])+') [R]',
AExpectedPairs[i], FoldedView.ViewPosToTextIndex(AExpectedPairs[i+1])); AExpectedPairs[i], FoldedView.ViewToTextIndex(ToIdx(AExpectedPairs[i+1])));
inc(i, 2); inc(i, 2);
end; end;
end; end;
@ -178,10 +178,10 @@ begin
i := 0; i := 0;
while i < high(AExpectedPairs)-1 do begin while i < high(AExpectedPairs)-1 do begin
AssertEquals(AName+' ViewPos[1-based]('+IntToStr( AExpectedPairs[i])+') to TxtIdx('+IntToStr( AExpectedPairs[i+1])+')', AssertEquals(AName+' ViewPos[1-based]('+IntToStr( AExpectedPairs[i])+') to TxtIdx('+IntToStr( AExpectedPairs[i+1])+')',
AExpectedPairs[i+1], FoldedView.ViewPosToTextIndex(AExpectedPairs[i])); AExpectedPairs[i+1], FoldedView.ViewToTextIndex(ToIdx(AExpectedPairs[i])));
if ADoReverse then if ADoReverse then
AssertEquals(AName+' TxtIdx('+IntToStr( AExpectedPairs[i+1])+') to ViewPos[1-based]('+IntToStr( AExpectedPairs[i])+') [R]', AssertEquals(AName+' TxtIdx('+IntToStr( AExpectedPairs[i+1])+') to ViewPos[1-based]('+IntToStr( AExpectedPairs[i])+') [R]',
AExpectedPairs[i], FoldedView.TextIndexToViewPos(AExpectedPairs[i+1])); AExpectedPairs[i], ToPos(FoldedView.TextToViewIndex(AExpectedPairs[i+1])));
inc(i, 2); inc(i, 2);
end; end;
end; end;

View File

@ -1819,7 +1819,7 @@ end;
function TIDESynEditor.TextIndexToViewPos(aTextIndex: Integer): Integer; function TIDESynEditor.TextIndexToViewPos(aTextIndex: Integer): Integer;
begin begin
Result := TextView.TextIndexToViewPos(aTextIndex - 1); Result := ToPos(TextView.TextToViewIndex(ToIdx(aTextIndex)));
end; end;
{$IFDEF WinIME} {$IFDEF WinIME}
@ -2824,8 +2824,8 @@ begin
end; end;
HasFolds := FoldView.TextIndexToViewPos(y2) - FoldView.TextIndexToViewPos(y1) <> y2 - y1; HasFolds := FoldView.TextToViewIndex(y2) - FoldView.TextToViewIndex(y1) <> y2 - y1;
//debugln(['*** HasFolds=', HasFolds, ' y1=',y1, ' y2=',y2, ' VP1=',FoldView.TextIndexToViewPos(y1), ' VP2=',FoldView.TextIndexToViewPos(y2)]); //debugln(['*** HasFolds=', HasFolds, ' y1=',y1, ' y2=',y2, ' VP1=',FoldView.TextToViewIndex(y1), ' VP2=',FoldView.TextToViewIndex(y2)]);
FProv := FoldView.FoldProvider; FProv := FoldView.FoldProvider;
Tree := TIDESynEditor(SynEdit).FMarkupIfDef.IfDefTree; Tree := TIDESynEditor(SynEdit).FMarkupIfDef.IfDefTree;