Skip to content

Commit

Permalink
Merge pull request scala-ide#150 from jkinkead/trailing_whitespace
Browse files Browse the repository at this point in the history
Remove trailing whitespace from non-Scaladoc comments.
  • Loading branch information
kiritsuku committed Feb 19, 2016
2 parents 2e8039e + f7c43cc commit 401b4a9
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ trait CommentFormatter { self: HasFormattingPreferences with ScalaFormatter ⇒

private def pruneEmptyFinal(lines: List[String]) = pruneEmptyInitial(lines.reverse).reverse

def formatComment(comment: HiddenToken, indentLevel: Int): String =
def formatScaladocComment(comment: HiddenToken, indentLevel: Int): String =
if (comment.rawText contains '\n') {
val sb = new StringBuilder
val (start, rawLines) = getLines(comment.rawText)
Expand Down Expand Up @@ -64,4 +64,8 @@ trait CommentFormatter { self: HasFormattingPreferences with ScalaFormatter ⇒
} else
comment.rawText

/** Formats a non-Scaladoc comment by trimming trailing whitespace from each line. */
def formatNonScaladocComment(comment: HiddenToken, indentLevel: Int): String = {
comment.rawText.replaceAll("""\s+(\r?\n)""", "$1")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -199,16 +199,16 @@ abstract class ScalaFormatter extends HasFormattingPreferences with TypeFormatte
val commentIndentLevel = if (nextTokenUnindents) indentLevel + 1 else indentLevel
for ((previousOpt, hiddenToken, nextOpt) Utils.withPreviousAndNext(hiddenTokens)) {
hiddenToken match {
case ScalaDocComment(token)
case ScalaDocComment(_)
builder.ensureAtBeginningOfLine()
builder.indent(commentIndentLevel, baseIndentOption)
builder.append(formatComment(hiddenToken, commentIndentLevel))
builder.append(formatScaladocComment(hiddenToken, commentIndentLevel))
case SingleLineComment(_) | MultiLineComment(_)
if (builder.atBeginningOfLine)
builder.indent(commentIndentLevel, baseIndentOption)
else if (builder.atVisibleCharacter) // Separation from previous visible token
builder.append(" ")
builder.write(hiddenToken)
builder.append(formatNonScaladocComment(hiddenToken, commentIndentLevel))
case Whitespace(token)
val newlineCount = token.text.count(_ == '\n')
val newlinesToWrite = previousOpt match {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,44 @@ class CommentFormatterTest extends AbstractFormatterTest {
| */
|"""

"""/**
| * Trailing whitespace on this line and the line below should be stripped.\u0020\u0020
| *\u0020\u0020\u0020\u0020
| */
|""" ==>
"""/**
| * Trailing whitespace on this line and the line below should be stripped.
| *
| */
|"""

"""// Trailing whitespace in single-line comments should be stripped.\u0020\u0020
|//\u0020\u0020
|""" ==>
"""// Trailing whitespace in single-line comments should be stripped.
|//
|"""

"""/* Normal multi-line comment.
| * Trailing whitespace here should be stripped.\u0020\u0020
| */
|""" ==>
"""/* Normal multi-line comment.
| * Trailing whitespace here should be stripped.
| */
|"""

"""/*\u0020
| Comment with trailing whitespace above.
| Indent should be preserved, whitespace trimmed.
| Visible separation (space below) should be added.
| */""" ==>
"""/*
| Comment with trailing whitespace above.
| Indent should be preserved, whitespace trimmed.
| Visible separation (space below) should be added.
| */ """

{
implicit val formattingPreferences = FormattingPreferences.setPreference(MultilineScaladocCommentsStartOnFirstLine, true)

Expand Down

0 comments on commit 401b4a9

Please sign in to comment.