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

Commit 45539af

Browse files
committed
Auto-formatting: Fixed some issues.
1 parent a2f4e3b commit 45539af

1 file changed

Lines changed: 31 additions & 26 deletions

File tree

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

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -292,16 +292,15 @@ public override void FormatLines(ITextEditor textArea)
292292
/// <returns><c>True</c>, if code has been formatted, <c>false</c> if auto-formatting is currently forbidden.</returns>
293293
private bool FormatCode(ITextEditor textArea, int offset, int length, bool respectAutoFormattingSetting)
294294
{
295+
if (respectAutoFormattingSetting && !CSharpFormattingOptionsPersistence.AutoFormatting)
296+
return false;
297+
295298
using (textArea.Document.OpenUndoGroup()) {
296299
// In any other case: Simply format selection or whole document
297300
var formattingOptions = CSharpFormattingOptionsPersistence.GetProjectOptions(SD.ProjectService.CurrentProject);
298-
if (!respectAutoFormattingSetting || CSharpFormattingOptionsPersistence.AutoFormatting) {
299-
CSharpFormatterHelper.Format(textArea, offset, length, formattingOptions.OptionsContainer);
300-
return true;
301-
}
301+
CSharpFormatterHelper.Format(textArea, offset, length, formattingOptions.OptionsContainer);
302+
return true;
302303
}
303-
304-
return false;
305304
}
306305

307306
public override void FormatLine(ITextEditor textArea, char ch) // used for comment tag formater/inserter
@@ -311,6 +310,24 @@ private bool FormatCode(ITextEditor textArea, int offset, int length, bool respe
311310
}
312311
}
313312

313+
bool FormatStatement(ITextEditor textArea, int cursorOffset, int formattingStartOffset)
314+
{
315+
var line = textArea.Document.GetLineByOffset(formattingStartOffset);
316+
int lineOffset = cursorOffset;
317+
// Walk up the lines until we arrive at previous statement or block
318+
while (line.PreviousLine != null) {
319+
line = line.PreviousLine;
320+
string lineText = textArea.Document.GetText(line.Offset, line.Length);
321+
if (IsLineEndOfStatement(lineText)) {
322+
// Previous line is another statement, don't format it
323+
break;
324+
}
325+
lineOffset = line.Offset;
326+
}
327+
328+
return FormatCode(textArea, lineOffset, cursorOffset - lineOffset, true);
329+
}
330+
314331
void FormatLineInternal(ITextEditor textArea, int lineNr, int cursorOffset, char ch)
315332
{
316333
IDocumentLine curLine = textArea.Document.GetLineByNumber(lineNr);
@@ -407,30 +424,18 @@ void FormatLineInternal(ITextEditor textArea, int lineNr, int cursorOffset, char
407424
var bracketSearchResult = textArea.Language.BracketSearcher.SearchBracket(textArea.Document, cursorOffset);
408425
if (bracketSearchResult != null) {
409426
// Format the block
410-
var bracketLine = textArea.Document.GetLineByOffset(bracketSearchResult.OpeningBracketOffset);
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);
417-
if (IsLineEndOfStatement(lineText)) {
418-
// Previous line is another statement, don't format it
419-
break;
420-
}
421-
bracketLineOffset = bracketLine.Offset;
422-
}
423-
424-
if (!FormatCode(textArea, bracketLineOffset, cursorOffset - bracketLineOffset, true)) {
427+
if (!FormatStatement(textArea, cursorOffset, bracketSearchResult.OpeningBracketOffset)) {
425428
// No auto-formatting seems to be active, at least indent the line
426429
IndentLine(textArea, curLine);
427430
}
428431
}
429432
break;
430433
case ';':
431434
// Format this line
432-
var lineBeginningOffset = textArea.Document.GetOffset(lineNr, 0);
433-
FormatCode(textArea, lineBeginningOffset, cursorOffset - lineBeginningOffset, true);
435+
if (!FormatStatement(textArea, cursorOffset, cursorOffset)) {
436+
// No auto-formatting seems to be active, at least indent the line
437+
IndentLine(textArea, curLine);
438+
}
434439
break;
435440
case '\n':
436441
string lineAboveText = lineAbove == null ? "" : textArea.Document.GetText(lineAbove);
@@ -511,12 +516,12 @@ bool IsLineEndOfStatement(string lineText)
511516
normalizedLine = lineText;
512517
}
513518

514-
normalizedLine = lineText.Trim(' ', '\t');
519+
normalizedLine = normalizedLine.Trim(' ', '\t');
515520

516521
if (normalizedLine.EndsWith("*/")) {
517-
int indexOfMultiLineCommentStart = lineText.LastIndexOf("/*");
522+
int indexOfMultiLineCommentStart = normalizedLine.LastIndexOf("/*");
518523
if (indexOfMultiLineCommentStart > -1) {
519-
normalizedLine = lineText.Substring(0, indexOfMultiLineCommentStart);
524+
normalizedLine = normalizedLine.Substring(0, indexOfMultiLineCommentStart);
520525
}
521526
}
522527

0 commit comments

Comments
 (0)