Browse Source

Handling comments correctly on auto-formatting.

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

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

@ -413,8 +413,8 @@ namespace CSharpBinding.FormattingStrategy @@ -413,8 +413,8 @@ namespace CSharpBinding.FormattingStrategy
// 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("}")) {
string lineText = textArea.Document.GetText(bracketLine.Offset, bracketLine.Length);
if (IsLineEndOfStatement(lineText)) {
// Previous line is another statement, don't format it
break;
}
@ -499,6 +499,36 @@ namespace CSharpBinding.FormattingStrategy @@ -499,6 +499,36 @@ namespace CSharpBinding.FormattingStrategy
}
}
bool IsLineEndOfStatement(string lineText)
{
string normalizedLine = null;
// Look if there is a comment at the end of line
int indexOfSingleLineComment = lineText.LastIndexOf("//");
if (indexOfSingleLineComment > -1) {
normalizedLine = lineText.Substring(0, indexOfSingleLineComment);
} else {
normalizedLine = lineText;
}
normalizedLine = lineText.Trim(' ', '\t');
if (normalizedLine.EndsWith("*/")) {
int indexOfMultiLineCommentStart = lineText.LastIndexOf("/*");
if (indexOfMultiLineCommentStart > -1) {
normalizedLine = lineText.Substring(0, indexOfMultiLineCommentStart);
}
}
// Usual statement endings
if (normalizedLine.EndsWith(";")
|| normalizedLine.EndsWith("{")
|| normalizedLine.EndsWith("}"))
return true;
return false;
}
/// <summary>
/// Checks if the cursor is inside a non-verbatim string.
/// This method is used to check if a line break was inserted in a string.

Loading…
Cancel
Save