SynEdit: auto fold region on load, if flagged / issue #13514

git-svn-id: trunk@20523 -
This commit is contained in:
martin 2009-06-08 15:27:18 +00:00
parent f112be2363
commit 16e227e58a
5 changed files with 91 additions and 3 deletions

View File

@ -719,6 +719,7 @@ type
procedure AddKey(Command: TSynEditorCommand; Key1: word; SS1: TShiftState;
Key2: word; SS2: TShiftState);
procedure AfterLoadFromFile;
procedure BeginUndoBlock(aList: TSynEditUndoList = nil);
procedure BeginUpdate;
function CaretXPix: Integer;
@ -6616,6 +6617,12 @@ begin
Key.Shift2 := SS2;
end;
procedure TCustomSynEdit.AfterLoadFromFile;
begin
if assigned(FFoldedLinesView) then
FFoldedLinesView.CollapseDefaultFolds;
end;
{ Called by FMarkList if change }
procedure TCustomSynEdit.MarkListChange(Sender: TObject);
begin

View File

@ -288,6 +288,7 @@ type
function LogicalPosToNodeIndex(AStartIndex: Integer; LogX: Integer; (* Returns the index of the node, at the logical char pos *)
Previous: Boolean = False): Integer;
procedure CollapseDefaultFolds;
procedure UnfoldAll;
procedure FoldAll(StartLevel : Integer = 0; IgnoreNested : Boolean = False);
@ -1864,6 +1865,37 @@ begin
Result := i;
end;
procedure TSynEditFoldedView.CollapseDefaultFolds;
var
i, j, c: Integer;
hl: TSynCustomFoldHighlighter;
nd: TSynFoldNodeInfo;
begin
if not(assigned(FHighLighter) and (FHighLighter is TSynCustomFoldHighlighter))
then exit;
hl := TSynCustomFoldHighlighter(FHighLighter);
i := 0;
while i < fLines.Count do begin
// Todo: Highlighter should return a list of typpes that can return default folded
// Currently PascalHl Type 2 = Region
c := hl.FoldOpenCount(i, 2);
if c > 0 then begin
c := hl.FoldNodeInfoCount[i];
j := 0;
while j < c do begin
nd := hl.FoldNodeInfo[i, j];
if (sfaDefaultCollapsed in nd.FoldAction) and
(not IsFoldedAtTextIndex(i, j))
then
fFoldTree.InsertNewFold(i+2, j, LengthForFoldAtTextIndex(i, j));
inc(j);
end;
end;
inc(i);
end;
end;
procedure TSynEditFoldedView.FoldAtTextIndex(AStartIndex : Integer;
ColIndex : Integer = -1; ColCount : Integer = 1; Skip: Boolean = False);
var

View File

@ -102,7 +102,8 @@ type
sfaClose, // At this node a fold ends
sfaMarkup, // This node can be highlighted, by the matching Word-Pair Markup
sfaFold, // Part of a foldable block
sfaInvalid // Wrong Index
sfaInvalid, // Wrong Index
sfaDefaultCollapsed
);
TSynFoldActions = set of TSynFoldAction;

View File

@ -1959,6 +1959,46 @@ end;
{$ENDIF}
procedure TSynPasSyn.BraceOpenProc;
function ScanRegion: Boolean;
var
Txt: String;
Idx, NestBrace, i, l: Integer;
InString: Boolean;
begin
Result := False;
Txt := copy(fLine, Run, length(fLine));
Idx := LineIndex;
InString := False;
NestBrace := 0;
while true do begin
i := 1;
l := length(Txt);
while i <= l do begin
case Txt[i] of
'{' : inc(NestBrace);
'}' : if NestBrace = 0
then exit
else dec(NestBrace);
'''' : if (i+1 <= l) and (Txt[i+1] = '''')
then inc(i)
else InString := not InString;
'-', '/' : If (not InString) and (i+4 <= l) and
((i=1) or (Txt[i-1] in [' ', #9, #10, #13])) and
(AnsiStrComp(PChar(copy(Txt, i+1, 4)), PChar('fold')) = 0)
and ((i+4 = l) or (Txt[i+5] in [' ', #9, #10, #13, '}']))
then
exit(True);
end;
inc(i);
end;
inc(Idx);
if Idx < CurrentLines.Count then
Txt := CurrentLines[Idx]
else
break;
end;
end;
begin
if (Run < fLineLen-1) and (fLine[Run+1] = '$') then begin
// compiler directive
@ -1974,8 +2014,15 @@ begin
EndCustomCodeFoldBlock(cfbtIfDef);
StartCustomCodeFoldBlock(cfbtIfDef);
end
else if KeyComp('region') then
StartCustomCodeFoldBlock(cfbtRegion)
else if KeyComp('region') then begin
StartCustomCodeFoldBlock(cfbtRegion);
if FCatchNodeInfo then begin
// Scan ahead
if ScanRegion and (FNodeInfoCount > 0) then
FNodeInfoList[FNodeInfoCount-1].FoldAction :=
FNodeInfoList[FNodeInfoCount-1].FoldAction + [sfaDefaultCollapsed];
end;
end
else if KeyComp('endregion') then
EndCustomCodeFoldBlock(cfbtRegion);
DirectiveProc;

View File

@ -6958,6 +6958,7 @@ begin
DoRestoreBookMarks(AnUnitInfo,NewSrcEdit);
DebugBoss.DoRestoreDebuggerMarks(AnUnitInfo);
NewSrcEdit.SyntaxHighlighterType:=AnUnitInfo.SyntaxHighlighter;
NewSrcEdit.EditorComponent.AfterLoadFromFile;
NewSrcEdit.EditorComponent.CaretXY:=NewCaretXY;
NewSrcEdit.EditorComponent.TopLine:=NewTopLine;
NewSrcEdit.EditorComponent.LeftChar:=NewLeftChar;