Browse Source

Another improvement to automatic formatting of blocks when closing "}". Currently doesn't handle comments correctly.

pull/478/head
Andreas Weizel 12 years ago
parent
commit
6b0b960742
  1. 20
      src/AddIns/BackendBindings/CSharpBinding/Project/Src/FormattingStrategy/CSharpFormattingStrategy.cs

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

@ -408,17 +408,19 @@ namespace CSharpBinding.FormattingStrategy @@ -408,17 +408,19 @@ namespace CSharpBinding.FormattingStrategy
if (bracketSearchResult != null) {
// Format the block
var bracketLine = textArea.Document.GetLineByOffset(bracketSearchResult.OpeningBracketOffset);
int bracketLineOffset = bracketLine.Offset;
int textLengthBeforeBracket = bracketSearchResult.OpeningBracketOffset - bracketLineOffset;
if (textLengthBeforeBracket > 0) {
string textBeforeBracket = textArea.Document.GetText(bracketLineOffset, textLengthBeforeBracket);
if (textBeforeBracket.Trim(' ', '\t').Length == 0) {
// Line seems to begin with the bracket, so format the line above, too
if (bracketLine.PreviousLine != null) {
bracketLineOffset = bracketLine.PreviousLine.Offset;
}
int bracketLineOffset = bracketSearchResult.OpeningBracketOffset;
// Walk up the lines until we arrive at previous statement or block
// TODO This doesn't handle comments appended to line end!
while (bracketLine.PreviousLine != null) {
bracketLine = bracketLine.PreviousLine;
string lineText = textArea.Document.GetText(bracketLine.Offset, bracketLine.Length).TrimEnd(' ', '\t');
if (lineText.EndsWith(";") || lineText.EndsWith("{") || lineText.EndsWith("}")) {
// Previous line is another statement, don't format it
break;
}
bracketLineOffset = bracketLine.Offset;
}
if (!FormatCode(textArea, bracketLineOffset, cursorOffset - bracketLineOffset, true)) {
// No auto-formatting seems to be active, at least indent the line
IndentLine(textArea, curLine);

Loading…
Cancel
Save