From 45539af7ebdcab12f7edc9084e122c44086605d6 Mon Sep 17 00:00:00 2001 From: Andreas Weizel Date: Sun, 6 Apr 2014 14:24:07 +0200 Subject: [PATCH] Auto-formatting: Fixed some issues. --- .../CSharpFormattingStrategy.cs | 57 ++++++++++--------- 1 file changed, 31 insertions(+), 26 deletions(-) diff --git a/src/AddIns/BackendBindings/CSharpBinding/Project/Src/FormattingStrategy/CSharpFormattingStrategy.cs b/src/AddIns/BackendBindings/CSharpBinding/Project/Src/FormattingStrategy/CSharpFormattingStrategy.cs index 30eff34971..9d8ac3ca11 100644 --- a/src/AddIns/BackendBindings/CSharpBinding/Project/Src/FormattingStrategy/CSharpFormattingStrategy.cs +++ b/src/AddIns/BackendBindings/CSharpBinding/Project/Src/FormattingStrategy/CSharpFormattingStrategy.cs @@ -292,16 +292,15 @@ namespace CSharpBinding.FormattingStrategy /// True, if code has been formatted, false if auto-formatting is currently forbidden. private bool FormatCode(ITextEditor textArea, int offset, int length, bool respectAutoFormattingSetting) { + if (respectAutoFormattingSetting && !CSharpFormattingOptionsPersistence.AutoFormatting) + return false; + using (textArea.Document.OpenUndoGroup()) { // In any other case: Simply format selection or whole document var formattingOptions = CSharpFormattingOptionsPersistence.GetProjectOptions(SD.ProjectService.CurrentProject); - if (!respectAutoFormattingSetting || CSharpFormattingOptionsPersistence.AutoFormatting) { - CSharpFormatterHelper.Format(textArea, offset, length, formattingOptions.OptionsContainer); - return true; - } + CSharpFormatterHelper.Format(textArea, offset, length, formattingOptions.OptionsContainer); + return true; } - - return false; } public override void FormatLine(ITextEditor textArea, char ch) // used for comment tag formater/inserter @@ -311,6 +310,24 @@ namespace CSharpBinding.FormattingStrategy } } + bool FormatStatement(ITextEditor textArea, int cursorOffset, int formattingStartOffset) + { + var line = textArea.Document.GetLineByOffset(formattingStartOffset); + int lineOffset = cursorOffset; + // Walk up the lines until we arrive at previous statement or block + while (line.PreviousLine != null) { + line = line.PreviousLine; + string lineText = textArea.Document.GetText(line.Offset, line.Length); + if (IsLineEndOfStatement(lineText)) { + // Previous line is another statement, don't format it + break; + } + lineOffset = line.Offset; + } + + return FormatCode(textArea, lineOffset, cursorOffset - lineOffset, true); + } + void FormatLineInternal(ITextEditor textArea, int lineNr, int cursorOffset, char ch) { IDocumentLine curLine = textArea.Document.GetLineByNumber(lineNr); @@ -407,21 +424,7 @@ namespace CSharpBinding.FormattingStrategy var bracketSearchResult = textArea.Language.BracketSearcher.SearchBracket(textArea.Document, cursorOffset); if (bracketSearchResult != null) { // Format the block - var bracketLine = textArea.Document.GetLineByOffset(bracketSearchResult.OpeningBracketOffset); - 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); - if (IsLineEndOfStatement(lineText)) { - // Previous line is another statement, don't format it - break; - } - bracketLineOffset = bracketLine.Offset; - } - - if (!FormatCode(textArea, bracketLineOffset, cursorOffset - bracketLineOffset, true)) { + if (!FormatStatement(textArea, cursorOffset, bracketSearchResult.OpeningBracketOffset)) { // No auto-formatting seems to be active, at least indent the line IndentLine(textArea, curLine); } @@ -429,8 +432,10 @@ namespace CSharpBinding.FormattingStrategy break; case ';': // Format this line - var lineBeginningOffset = textArea.Document.GetOffset(lineNr, 0); - FormatCode(textArea, lineBeginningOffset, cursorOffset - lineBeginningOffset, true); + if (!FormatStatement(textArea, cursorOffset, cursorOffset)) { + // No auto-formatting seems to be active, at least indent the line + IndentLine(textArea, curLine); + } break; case '\n': string lineAboveText = lineAbove == null ? "" : textArea.Document.GetText(lineAbove); @@ -511,12 +516,12 @@ namespace CSharpBinding.FormattingStrategy normalizedLine = lineText; } - normalizedLine = lineText.Trim(' ', '\t'); + normalizedLine = normalizedLine.Trim(' ', '\t'); if (normalizedLine.EndsWith("*/")) { - int indexOfMultiLineCommentStart = lineText.LastIndexOf("/*"); + int indexOfMultiLineCommentStart = normalizedLine.LastIndexOf("/*"); if (indexOfMultiLineCommentStart > -1) { - normalizedLine = lineText.Substring(0, indexOfMultiLineCommentStart); + normalizedLine = normalizedLine.Substring(0, indexOfMultiLineCommentStart); } }