Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master' into double_indent
Browse files Browse the repository at this point in the history
  • Loading branch information
jkinkead committed Feb 19, 2016
2 parents 7586675 + 401b4a9 commit 4ddd98f
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 6 deletions.
7 changes: 7 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,13 @@ If ``false``, start the comment body on a separate line below the opening delimi
* element of the given list.
*/
newlineAtEndOfFile
~~~~~~~~~~~~~~~~~~

Default: ``false``

If ``true``, newlines will be added at the end of all formatted files.

placeScaladocAsterisksBeneathSecondAsterisk
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Expand Down
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 @@ -40,7 +40,12 @@ abstract class ScalaFormatter extends HasFormattingPreferences with TypeFormatte
var result = format(topStats)
for (firstStat topStats.firstStatOpt)
result = result.before(firstStat.firstToken, EnsureNewlineAndIndent(0))
result

if (formattingPreferences(NewlineAtEndOfFile)) {
result.before(compilationUnit.eofToken, EnsureNewlineAndIndent(0))
} else {
result
}
}

/**
Expand Down Expand Up @@ -194,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 @@ -94,7 +94,8 @@ object AllPreferences {
PreserveSpaceBeforeArguments, AlignParameters, AlignArguments, DoubleIndentClassDeclaration, FormatXml, IndentPackageBlocks,
AlignSingleLineCaseStatements, AlignSingleLineCaseStatements.MaxArrowIndent, IndentLocalDefs, PreserveDanglingCloseParenthesis, DanglingCloseParenthesis,
SpaceInsideParentheses, SpaceInsideBrackets, SpacesWithinPatternBinders, MultilineScaladocCommentsStartOnFirstLine, IndentWithTabs,
CompactControlReadability, PlaceScaladocAsterisksBeneathSecondAsterisk, DoubleIndentMethodDeclaration, SpacesAroundMultiImports
CompactControlReadability, PlaceScaladocAsterisksBeneathSecondAsterisk, DoubleIndentMethodDeclaration, SpacesAroundMultiImports,
NewlineAtEndOfFile
)

val preferencesByKey: Map[String, PreferenceDescriptor[_]] =
Expand Down Expand Up @@ -250,3 +251,9 @@ case object SpacesAroundMultiImports extends BooleanPreferenceDescriptor {
val description = "Place spaces around multi imports (import a.{ b, c, d }"
val defaultValue = false
}

case object NewlineAtEndOfFile extends BooleanPreferenceDescriptor {
val key = "newlineAtEndOfFile"
val description = "Add a newline at the end of all files"
val defaultValue = false
}
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package scalariform.formatter

import scalariform.parser.{CompilationUnit, ScalaParser}
import scalariform.formatter.preferences.{FormattingPreferences, NewlineAtEndOfFile}

/** Tests that top-level parses add a newline when NewlineAtEndOfFile is set. */
class NewlineAtEndOfFileFormatterTest extends AbstractFormatterTest {
type Result = CompilationUnit

// Must parse as a full script to verify the newline formatting.
def parse(parser: ScalaParser) = parser.scriptBody()

def format(formatter: ScalaFormatter, result: Result) = formatter.format(result)(FormatterState())

override val debug = false

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

// No newline; should have one added.
"""import foo.bar
|
|class SettingOn {
|}""" ==>
"""import foo.bar
|
|class SettingOn {
|}
|"""

// Has newline; should stay the same.
"""import foo.bar
|class SettingOn {
|}
|""" ==>
"""import foo.bar
|class SettingOn {
|}
|"""
}

{
implicit val formattingPreferences = FormattingPreferences.setPreference(NewlineAtEndOfFile, false)

// No newline; should stay the same.
"""import foo.bar
|
|class SettingOff {
|}""" ==>
"""import foo.bar
|
|class SettingOff {
|}"""

// Has newline; should stay the same (preference off doesn't strip newlines that exist).
"""import foo.bar
|class SettingOff {
|}
|""" ==>
"""import foo.bar
|class SettingOff {
|}
|"""
}
}

0 comments on commit 4ddd98f

Please sign in to comment.