SynEdit: Improve beautifier for comment extensions. Split the double usage of MaxIndent / add new flags for AlignOnce

IDE: Improve caption

git-svn-id: trunk@57632 -
This commit is contained in:
martin 2018-04-09 10:39:48 +00:00
parent d5c2ad0fca
commit 8cd65222af
3 changed files with 61 additions and 50 deletions

View File

@ -203,11 +203,20 @@ type
TSynCommentIndentFlag = (
// * For Matching lines (FCommentMode)
// By default indent is the same as for none comment lines (none overrides sciAlignOpen)
//
// * [], sciNone, sciAlignOpen
// [] (neither sciNone nor sciAlignOpen)
// indent is the same as for none comment lines
// that is indent from previous line
sciNone, // Does not Indent comment lines (Prefix may contain a fixed indent)
// sciNone overrides sciAlignOpen
sciAlignOpen, // Indent to real opening pos on first line, if comment does not start at BOL "Foo(); (*"
// Will force every new line back to Align.
// To align only once, use IndentFirstLineExtra=MaxInt
// add-ons (combine with above)
sciAlignOpenOnce, // Force AlignOpen once for first line
// Can be combined sciNone or used without any of the above
sciAlignOpenSkipBOL, // Combine with any sciAlignOpen...,
// only align if the comment starts behind other text (is not at BOL)
// sciAdd...: if (only if) previous line had started with the opening token "(*" or "{".
// or if sciAlignOpen is set
@ -221,9 +230,11 @@ type
sciMatchOnlyTokenLen, // Apply the Above only if first line matches. (Only if sciAddTokenLen is specified)
sciMatchOnlyPastTokenIndent,
sciAlignOnlyTokenLen, // Apply the Above only if sciAlignOpen was used (include via max)
sciAlignOnlyTokenLen, // Apply the Above only if sciAlignOpen/sciAlignOpenOnce was used
sciAlignOnlyPastTokenIndent,
// TODO: sciAlignOpenOnce followed by [] (neither = default indent) should keep the extra align, if it still is on the correct column
// flag to ignore spaces, that are matched.
// flag to be smart, if not matched
@ -338,18 +349,13 @@ type
// *** coments with (* *)
(* AnsiIndentFirstLineMax:
* For comments that do NOT start at the BOL in their opening line " Foo; ( * comment":
if AnsiIndentFirstLineMax is is set:
- If the comment starts before or at the "Max-Column, then sciAlignOpen is always used.
- If the comment starts after max column, then the specified mode is used.
If the specified mode is sciAlignOpen it will be cut at Max
* For comments that start at BOL in their opening line " ( * comment":
This is ignored
* For comments that use any sciAlignOpen...
if AnsiIndentFirstLineMax is is set (none zero):
The indent will be limited to this value
AnsiIndentFirstLineExtra:
For comments that do NOT start at the BOL, an extra indent is added
on the 2nd line (except if sciAlignOpen is used (in Flags, or via A.I.FirstLineMax))
on the 2nd line (except if sciAlignOpen is used (in Flags))
*)
property AnsiIndentMode: TSynCommentIndentFlags read FIndentMode[sctAnsi]
write FIndentMode[sctAnsi];
@ -999,7 +1005,7 @@ var
FoldTyp: TSynCommentType;
AnyEnabled: Boolean;
ExtendSlash, BeSmart, Matching: Boolean;
PreviousIsFirst, IsAtBOL, DidAlignOpen: Boolean;
PreviousIsFirst, CommentStartsAtBOL, DidAlignOpen: Boolean;
IndentTypeBackup: TSynBeautifierIndentType;
@ -1089,9 +1095,9 @@ begin
Matching := CheckMatch(FoldTyp);
PreviousIsFirst := (GetFirstCommentLine = WorkLine -1);
DidAlignOpen := False;
IsAtBOL := True;
CommentStartsAtBOL := True;
if PreviousIsFirst then
IsAtBOL := GetCommentStartCol = 1 + GetIndentForLine(nil, FCurrentLines[ToIdx(GetFirstCommentLine)], False);
CommentStartsAtBOL := GetCommentStartCol = 1 + GetIndentForLine(nil, FCurrentLines[ToIdx(GetFirstCommentLine)], False);
// Aply indent before prefix
@ -1104,26 +1110,15 @@ begin
if IndentType = sbitConvertToTabOnly then
IndentType := sbitConvertToTabSpace;
if PreviousIsFirst and not IsAtBOL and
(FIndentFirstLineMax[FoldTyp] > 0) and
( (FIndentFirstLineMax[FoldTyp] + 1 >= GetCommentStartCol) or
(FIndentMode[FoldTyp] * [sciNone, sciAlignOpen] = [sciAlignOpen])
)
// Align with open
if ( (FIndentMode[FoldTyp] * [sciNone, sciAlignOpen] = [sciAlignOpen]) or
( (sciAlignOpenOnce in FIndentMode[FoldTyp]) and PreviousIsFirst )
) and
( (not (sciAlignOpenSkipBOL in FIndentMode[FoldTyp])) or (not CommentStartsAtBOL) )
then begin
// Use sciAlignOpen
Indent := Min(
FCurrentLines.LogicalToPhysicalCol(FCurrentLines[ToIdx(GetFirstCommentLine)], ToIdx(GetFirstCommentLine), GetCommentStartCol-1),
FIndentFirstLineMax[FoldTyp]);
s := GetCharMix(WorkLine, Indent, dummy);
FLogicalIndentLen := length(s);
FCurrentLines.EditInsert(1, WorkLine, s);
DidAlignOpen := True;
end
else
if (FIndentMode[FoldTyp] * [sciNone, sciAlignOpen] = [sciAlignOpen]) and
(GetCommentStartCol > 0)
then begin
Indent := FCurrentLines.LogicalToPhysicalCol(FCurrentLines[ToIdx(GetFirstCommentLine)], ToIdx(GetFirstCommentLine), GetCommentStartCol-1);
Indent := FCurrentLines.LogicalToPhysicalCol(FCurrentLines[ToIdx(GetFirstCommentLine)],
ToIdx(GetFirstCommentLine),
GetCommentStartCol-1);
if FIndentFirstLineMax[FoldTyp] > 0
then Indent := Min(Indent, FIndentFirstLineMax[FoldTyp]);
s := GetCharMix(WorkLine, Indent, dummy);
@ -1132,6 +1127,16 @@ begin
DidAlignOpen := True;
end
else
// Do not align with open, but check MaxIndent
if PreviousIsFirst and (FIndentFirstLineMax[FoldTyp] > 0) and
(FIndentMode[FoldTyp] * [sciNone] = []) then begin
Indent := GetIndentForLine(nil, FCurrentLines[ToIdx(WorkLine-1)], False);
Indent := Min(Indent, FIndentFirstLineMax[FoldTyp]);
s := GetCharMix(WorkLine, Indent, dummy);
FLogicalIndentLen := length(s);
FCurrentLines.EditInsert(1, WorkLine, s);
end
else
if (sciNone in FIndentMode[FoldTyp]) then begin
// No indent
end
@ -1144,7 +1149,7 @@ begin
end;
// AnsiIndentFirstLineExtra
if PreviousIsFirst and (not IsAtBOL) and (not DidAlignOpen) then begin
if PreviousIsFirst and (not CommentStartsAtBOL) and (not DidAlignOpen) then begin
FCurrentLines.EditInsert(1 + FLogicalIndentLen, WorkLine, FIndentFirstLineExtra[FoldTyp]);
FLogicalIndentLen := FLogicalIndentLen + length(FIndentFirstLineExtra[FoldTyp]);
end;
@ -1176,7 +1181,7 @@ begin
end;
end;
// sciAddPastTokenIndent -- Spaces from after (* or { (to go befare prefix e.g " { * foo")
// sciAddPastTokenIndent -- Spaces from after (* or { (to go before prefix e.g " { * foo")
if BeSmart and
( (sciAddPastTokenIndent in FIndentMode[FoldTyp]) and
( (not(sciMatchOnlyPastTokenIndent in FIndentMode[FoldTyp])) or CheckMatch(FoldTyp, False, True) ) and

View File

@ -1115,6 +1115,8 @@ begin
// AnsiIndentFirstLineMax, indent is >= 2, so it is not affeceted by 1 or 2
for LoopIndentMax := 0 to 2 do
begin
if LoopIndentMax = 1 then Continue; // TODO: add tests // MaxIndent was extended to act on default indent to (first line only)
PushBaseName('Max='+IntToStr(LoopIndentMax));
ConfigBeautifier(sctBor, [] + ExtraIndentFlags, LoopIndentMax, '',
sccPrefixMatch, scmMatchAfterOpening, MatchLine,
@ -1140,7 +1142,9 @@ begin
end;
// Indent tabs / matching
if ParentIndentType in [sbitCopySpaceTab] then begin
if (ParentIndentType in [sbitCopySpaceTab])
and (LoopIndentMax = 0)
then begin
DoSetText('tabs matching', [2, #9' {*'#9' abc']);
DoNewLine('after 1st', 8, 2, 6, 3, [2, #9' {*'#9' a', #9' *'#9' bc']); // 2:"_ {*_ a|bc"
DoNewLine('any line', 7, 3, 6, 4, [3, #9' *'#9' b', #9' *'#9' c']); // 3:" * b|c"
@ -1176,7 +1180,9 @@ begin
end;
// Indent tabs / matching
if ParentIndentType in [sbitCopySpaceTab] then begin
if (ParentIndentType in [sbitCopySpaceTab])
and (LoopIndentMax = 0)
then begin
DoSetText('tabs matching', [2, #9' {*'#9' abc']);
DoNewLine('after 1st', 8, 2, 7, 3, [2, #9' {*'#9' a', #9' *'#9' bc']); // 2:"_ {*_ a|bc"
DoNewLine('any line', 8, 3, 7, 4, [3, #9' *'#9' b', #9' *'#9' c']); // 3:" * b|c"
@ -1204,7 +1210,7 @@ begin
end; // LoopIndentMax;
PushBaseName('Max='+IntToStr(10));
ConfigBeautifier(sctBor, [] + ExtraIndentFlags, 10, '',
ConfigBeautifier(sctBor, [sciAlignOpenOnce, sciAlignOpenSkipBOL] + ExtraIndentFlags, 0, '',
sccPrefixMatch, scmMatchAfterOpening, MatchLine,
sbitSpace,
'^\s*\*', '*');
@ -1214,13 +1220,13 @@ begin
DoNewLine('after 1st', 7, 2, 5, 3, [2, ' {* a', ' * bc']); // 2:" {* a|bc"
DoNewLine('any line', 6, 3, 5, 4, [3, ' * b', ' * c']); // 3:" * b|c"
// Indent, not BOL / matching // AnsiIndentFirstLineMax applied
// Indent, not BOL / matching // sciAlignOpenOnce applied
DoSetText('not BOL matching', [2, ' ;;;{* abc']);
DoNewLine('after 1st', 10, 2, 8, 3, [2, ' ;;;{* a', ' * bc']); // 2:" ;{* a|bc"
DoNewLine('any line', 9, 3, 8, 4, [3, ' * b', ' * c']); // 3:" * b|c"
// Check_that_Indent_is_NOT_restored // SEE Check_that_Indent_is_restored
// Indent, not BOL / matching // AnsiIndentFirstLineMax applied
// Indent, not BOL / matching // sciAlignOpenOnce applied
DoSetText('NOT restore Align', [2, ' ;;;{* abc', ' *']);
if ParentIndentType = sbitPositionCaret
then DoNewLine('after 2nd', 4, 3, 5, 4, [3, ' *', ' *']) // NOT added post indent of 1
@ -1232,13 +1238,13 @@ begin
DoNewLine('after 1st', 7, 2, 6, 3, [2, ' {* a', ' * bc']); // 2:" {* a|bc"
DoNewLine('any line', 7, 3, 6, 4, [3, ' * b', ' * c']); // 3:" * b|c"
// Indent, not BOL / matching // AnsiIndentFirstLineMax applied
// Indent, not BOL / matching // sciAlignOpenOnce applied
DoSetText('not BOL matching', [2, ' ;;;{* abc']);
DoNewLine('after 1st', 10, 2, 9, 3, [2, ' ;;;{* a', ' * bc']); // 2:" ;{* a|bc"
DoNewLine('any line', 10, 3, 9, 4, [3, ' * b', ' * c']); // 3:" * b|c"
// Check_that_Indent_is_NOT_restored // SEE Check_that_Indent_is_restored
// Indent, not BOL / matching // AnsiIndentFirstLineMax applied
// Indent, not BOL / matching // sciAlignOpenOnce applied
DoSetText('NOT restore Align', [2, ' ;;;{* abc', ' *']);
if ParentIndentType = sbitPositionCaret
then DoNewLine('after 2nd', 4, 3, 5, 4, [3, ' *', ' *'])
@ -1248,7 +1254,7 @@ begin
PushBaseName('Etra=" " + Max=10');
ConfigBeautifier(sctBor, [] + ExtraIndentFlags, 10, ' ',
ConfigBeautifier(sctBor, [sciAlignOpenOnce, sciAlignOpenSkipBOL] + ExtraIndentFlags, 0, ' ',
sccPrefixMatch, scmMatchAfterOpening, MatchLine,
sbitSpace,
'^\s*\*', '*');
@ -1258,7 +1264,7 @@ begin
DoNewLine('after 1st', 7, 2, 5, 3, [2, ' {* a', ' * bc']); // 2:" {* a|bc"
DoNewLine('any line', 6, 3, 5, 4, [3, ' * b', ' * c']); // 3:" * b|c"
// Indent, not BOL / matching // AnsiIndentFirstLineMax applied
// Indent, not BOL / matching // sciAlignOpenOnce applied
DoSetText('not BOL matching', [2, ' ;;;{* abc']);
DoNewLine('after 1st', 10, 2, 8, 3, [2, ' ;;;{* a', ' * bc']); // 2:" ;{* a|bc"
DoNewLine('any line', 9, 3, 8, 4, [3, ' * b', ' * c']); // 3:" * b|c"
@ -1269,7 +1275,7 @@ begin
DoNewLine('after 1st', 7, 2, 6, 3, [2, ' {* a', ' * bc']); // 2:" {* a|bc"
DoNewLine('any line', 7, 3, 6, 4, [3, ' * b', ' * c']); // 3:" * b|c"
// Indent, not BOL / matching // AnsiIndentFirstLineMax applied
// Indent, not BOL / matching // sciAlignOpenOnce applied
DoSetText('not BOL matching', [2, ' ;;;{* abc']);
DoNewLine('after 1st', 10, 2, 9, 3, [2, ' ;;;{* a', ' * bc']); // 2:" ;{* a|bc"
DoNewLine('any line', 10, 3, 9, 4, [3, ' * b', ' * c']); // 3:" * b|c"
@ -1410,7 +1416,7 @@ begin
end; // LoopIndentMax;
PushBaseName('Max='+IntToStr(10));
ConfigBeautifier(sctBor, [sciNone] + ExtraIndentFlags, 10, '',
ConfigBeautifier(sctBor, [sciNone, sciAlignOpenOnce, sciAlignOpenSkipBOL] + ExtraIndentFlags, 0, '',
sccPrefixMatch, scmMatchAfterOpening, MatchLine,
sbitSpace,
'^\s*\*', '*');
@ -1421,7 +1427,7 @@ begin
DoNewLine('after 1st', 7, 2, 3, 3, [2, ' {* a', '* bc']); // 2:" {* a|bc"
DoNewLine('any line', 4, 3, 3, 4, [3, '* b', '* c']); // 3:"* b|c"
// Indent, not BOL / matching // AnsiIndentFirstLineMax applied
// Indent, not BOL / matching // sciAlignOpenOnce applied
DoSetText('not BOL matching', [2, ' ;;;{* abc']);
DoNewLine('after 1st', 10, 2, 8, 3, [2, ' ;;;{* a', ' * bc']); // 2:" ;{* a|bc"
DoNewLine('any line', 9, 3, 3, 4, [3, ' * b', '* c']); // 3:" * b|c"
@ -1432,7 +1438,7 @@ begin
DoNewLine('after 1st', 7, 2, 4, 3, [2, ' {* a', ' * bc']); // 2:" {* a|bc"
DoNewLine('any line', 5, 3, 3, 4, [3, ' * b', '* c']); // 3:" * b|c"
// Indent, not BOL / matching // AnsiIndentFirstLineMax applied
// Indent, not BOL / matching // sciAlignOpenOnce applied
DoSetText('not BOL matching', [2, ' ;;;{* abc']);
DoNewLine('after 1st', 10, 2, 9, 3, [2, ' ;;;{* a', ' * bc']); // 2:" ;{* a|bc"
DoNewLine('any line', 10, 3, 3, 4, [3, ' * b', '* c']); // 3:" * b|c"

View File

@ -1662,7 +1662,7 @@ resourcestring
dlgCommentContinue = 'Prefix comments on linebreak';
dlgCommentContinueMatch = 'Match current line';
dlgCommentContinuePrefix = 'Prefix new line';
dlgCommentAlignMaxDefault = 'Make default indent for new line if comment opens at column:';
dlgCommentAlignMaxDefault = 'Max indent for new line if prefix is based on start of comment on first comment line:';
dlgCommentAlignMaxToken = 'Limit indent to';
dlgCommentContinueMatchText = 'Match text after token "%s"';
dlgCommentContinueMatchToken = 'Match text including token "%s"';