From 894af3b3aaf0b3dce5fcbaff304d4c412bdc5a59 Mon Sep 17 00:00:00 2001 From: Martijn Laan <1092369+martijnlaan@users.noreply.github.com> Date: Sun, 16 Jun 2024 09:54:24 +0200 Subject: [PATCH] Cleanup. --- Components/ScintEdit.pas | 33 +++++++++++------------------ Components/ScintStylerInnoSetup.pas | 28 ++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 21 deletions(-) diff --git a/Components/ScintEdit.pas b/Components/ScintEdit.pas index 4cbfa67a6..429e267fe 100644 --- a/Components/ScintEdit.pas +++ b/Components/ScintEdit.pas @@ -443,6 +443,8 @@ TScintCustomStyler = class(TComponent) function ConsumeString(const Chars: TScintRawCharSet): TScintRawString; function CurCharIn(const Chars: TScintRawCharSet): Boolean; function CurCharIs(const C: AnsiChar): Boolean; + procedure GetFoldLevel(const LineState: TScintLineState; var Level: Integer; + var Header: Boolean); virtual; abstract; procedure GetStyleAttributes(const Style: Integer; var Attributes: TScintStyleAttributes); virtual; abstract; function LineTextSpans(const S: TScintRawString): Boolean; virtual; @@ -1815,32 +1817,21 @@ procedure TScintEdit.StyleNeeded(const EndPos: Integer); FStyler.FText := ''; end; - { Set line states and also add fold headers when not in a section. } + var FoldLevel: Integer; + var FoldHeader: Boolean; + FStyler.GetFoldLevel(FStyler.FLineState, FoldLevel, FoldHeader); + var FoldFlags := 0; + if FoldHeader then + FoldFlags := FoldFlags or SC_FOLDLEVELHEADERFLAG; + FoldLevel := (SC_FOLDLEVELBASE+FoldLevel) or FoldFlags; for var I := FirstLine to LastLine do begin var OldState := FLines.GetState(I); if FStyler.FLineState <> OldState then Call(SCI_SETLINESTATE, I, FStyler.FLineState); - - var Section := TInnoSetupStyler.GetSectionFromLineState(FStyler.LineState); - if Section <> scNone then - Call(SCI_SETFOLDLEVEL, I, SC_FOLDLEVELBASE+1) - else begin - { Everything outside a section should have the header flag, even if it's - just a blank line or a comment. Doing this doesn't cause many fold - markers: if two lines have the same level and header flag the first - line doesn't get a fold mark since there's nothing to expand. - Not doing this however is a problem: for example, if the first - line is empty and and the second line starts a section then those two - lines would logically considered to be in the same 'fold' and edits - on the first line would affect the section if collapsed. - Did notice an issue (Scintilla bug?): Add a section with some lines. - Collapse it. Break the section header for example by removing ']'. - Scintialla now auto expands the section and removes the fold mark. - Retype the ']'. Scintilla now displays the old fold mark to expand the - section but it's already expanded. } - Call(SCI_SETFOLDLEVEL, I, SC_FOLDLEVELBASE or SC_FOLDLEVELHEADERFLAG); - end; + var OldLevel := Call(SCI_GETFOLDLEVEL, I, 0); + if FoldLevel <> OldLevel then + Call(SCI_SETFOLDLEVEL, I, FoldLevel); end; Result := LastLine; diff --git a/Components/ScintStylerInnoSetup.pas b/Components/ScintStylerInnoSetup.pas index 594735d55..ce9d8d741 100644 --- a/Components/ScintStylerInnoSetup.pas +++ b/Components/ScintStylerInnoSetup.pas @@ -104,6 +104,8 @@ TInnoSetupStyler = class(TScintCustomStyler) procedure SetISPPInstalled(const Value: Boolean); protected procedure CommitStyle(const Style: TInnoSetupStylerStyle); + procedure GetFoldLevel(const LineState: TScintLineState; var Level: Integer; + var Header: Boolean); override; procedure GetStyleAttributes(const Style: Integer; var Attributes: TScintStyleAttributes); override; function LineTextSpans(const S: TScintRawString): Boolean; override; @@ -934,6 +936,32 @@ function TInnoSetupStyler.GetFlagsWordList(Section: TInnoSetupStylerSection): An Result := FFlagsWordList[Section]; end; +procedure TInnoSetupStyler.GetFoldLevel(const LineState: TScintLineState; + var Level: Integer; var Header: Boolean); +begin + var Section := TInnoSetupStyler.GetSectionFromLineState(LineState); + if Section <> scNone then begin + Level := 1; + Header := False; + end else begin + { Everything outside a section should have the header flag, even if it's just + a blank line or a comment. Doing this doesn't cause many fold markers: if + two lines have the same level and header flag the first line doesn't get a + fold mark since there's nothing to expand. Not doing this however is a + problem: for example, if the first line is empty and and the second line + starts a section then those two lines would logically considered to be in + the same 'fold' and edits on the first line would affect the section if + collapsed. + Did notice an issue (Scintilla bug?): Add a section with some lines. + Collapse it. Break the section header for example by removing ']'. + Scintialla now auto expands the section and removes the fold mark. + Retype the ']'. Scintilla now displays the old fold mark to expand the + section but it's already expanded. } + Level := 0; + Header := True; + end; +end; + function TInnoSetupStyler.GetKeywordsWordList(Section: TInnoSetupStylerSection): AnsiString; begin Result := FKeywordsWordList[Section];