Skip to content
This repository was archived by the owner on Oct 16, 2020. It is now read-only.

Commit 6b0b960

Browse files
committed
Another improvement to automatic formatting of blocks when closing "}". Currently doesn't handle comments correctly.
1 parent f22e332 commit 6b0b960

1 file changed

Lines changed: 14 additions & 12 deletions

File tree

src/AddIns/BackendBindings/CSharpBinding/Project/Src/FormattingStrategy/CSharpFormattingStrategy.cs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ bool NeedCurlyBracket(string text)
155155
if ((inString && !verbatim) || inChar)
156156
++i; // skip next character
157157
break;
158-
}
158+
}
159159
}
160160
return curlyCounter > 0;
161161
}
@@ -408,17 +408,19 @@ void FormatLineInternal(ITextEditor textArea, int lineNr, int cursorOffset, char
408408
if (bracketSearchResult != null) {
409409
// Format the block
410410
var bracketLine = textArea.Document.GetLineByOffset(bracketSearchResult.OpeningBracketOffset);
411-
int bracketLineOffset = bracketLine.Offset;
412-
int textLengthBeforeBracket = bracketSearchResult.OpeningBracketOffset - bracketLineOffset;
413-
if (textLengthBeforeBracket > 0) {
414-
string textBeforeBracket = textArea.Document.GetText(bracketLineOffset, textLengthBeforeBracket);
415-
if (textBeforeBracket.Trim(' ', '\t').Length == 0) {
416-
// Line seems to begin with the bracket, so format the line above, too
417-
if (bracketLine.PreviousLine != null) {
418-
bracketLineOffset = bracketLine.PreviousLine.Offset;
419-
}
411+
int bracketLineOffset = bracketSearchResult.OpeningBracketOffset;
412+
// Walk up the lines until we arrive at previous statement or block
413+
// TODO This doesn't handle comments appended to line end!
414+
while (bracketLine.PreviousLine != null) {
415+
bracketLine = bracketLine.PreviousLine;
416+
string lineText = textArea.Document.GetText(bracketLine.Offset, bracketLine.Length).TrimEnd(' ', '\t');
417+
if (lineText.EndsWith(";") || lineText.EndsWith("{") || lineText.EndsWith("}")) {
418+
// Previous line is another statement, don't format it
419+
break;
420420
}
421+
bracketLineOffset = bracketLine.Offset;
421422
}
423+
422424
if (!FormatCode(textArea, bracketLineOffset, cursorOffset - bracketLineOffset, true)) {
423425
// No auto-formatting seems to be active, at least indent the line
424426
IndentLine(textArea, curLine);
@@ -494,7 +496,7 @@ void FormatLineInternal(ITextEditor textArea, int lineNr, int cursorOffset, char
494496
}
495497
}
496498
return;
497-
}
499+
}
498500
}
499501

500502
/// <summary>
@@ -603,7 +605,7 @@ static int GetStartType(IDocument document, int linestart, int offset)
603605
if ((inString && !verbatim) || inChar)
604606
++i; // skip next character
605607
break;
606-
}
608+
}
607609
}
608610
return (inString || inChar) ? 2 : 0;
609611
}

0 commit comments

Comments
 (0)