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

Commit a2f4e3b

Browse files
committed
Handling comments correctly on auto-formatting.
1 parent 6b0b960 commit a2f4e3b

1 file changed

Lines changed: 32 additions & 2 deletions

File tree

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

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -413,8 +413,8 @@ void FormatLineInternal(ITextEditor textArea, int lineNr, int cursorOffset, char
413413
// TODO This doesn't handle comments appended to line end!
414414
while (bracketLine.PreviousLine != null) {
415415
bracketLine = bracketLine.PreviousLine;
416-
string lineText = textArea.Document.GetText(bracketLine.Offset, bracketLine.Length).TrimEnd(' ', '\t');
417-
if (lineText.EndsWith(";") || lineText.EndsWith("{") || lineText.EndsWith("}")) {
416+
string lineText = textArea.Document.GetText(bracketLine.Offset, bracketLine.Length);
417+
if (IsLineEndOfStatement(lineText)) {
418418
// Previous line is another statement, don't format it
419419
break;
420420
}
@@ -499,6 +499,36 @@ void FormatLineInternal(ITextEditor textArea, int lineNr, int cursorOffset, char
499499
}
500500
}
501501

502+
bool IsLineEndOfStatement(string lineText)
503+
{
504+
string normalizedLine = null;
505+
506+
// Look if there is a comment at the end of line
507+
int indexOfSingleLineComment = lineText.LastIndexOf("//");
508+
if (indexOfSingleLineComment > -1) {
509+
normalizedLine = lineText.Substring(0, indexOfSingleLineComment);
510+
} else {
511+
normalizedLine = lineText;
512+
}
513+
514+
normalizedLine = lineText.Trim(' ', '\t');
515+
516+
if (normalizedLine.EndsWith("*/")) {
517+
int indexOfMultiLineCommentStart = lineText.LastIndexOf("/*");
518+
if (indexOfMultiLineCommentStart > -1) {
519+
normalizedLine = lineText.Substring(0, indexOfMultiLineCommentStart);
520+
}
521+
}
522+
523+
// Usual statement endings
524+
if (normalizedLine.EndsWith(";")
525+
|| normalizedLine.EndsWith("{")
526+
|| normalizedLine.EndsWith("}"))
527+
return true;
528+
529+
return false;
530+
}
531+
502532
/// <summary>
503533
/// Checks if the cursor is inside a non-verbatim string.
504534
/// This method is used to check if a line break was inserted in a string.

0 commit comments

Comments
 (0)