Skip to content

Commit

Permalink
Add an option to add newlines to the end of files.
Browse files Browse the repository at this point in the history
  • Loading branch information
jkinkead committed Feb 18, 2016
1 parent 46234e5 commit fba7239
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 2 deletions.
7 changes: 7 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,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 @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ object AllPreferences {
PreserveSpaceBeforeArguments, AlignParameters, AlignArguments, DoubleIndentClassDeclaration, FormatXml, IndentPackageBlocks,
AlignSingleLineCaseStatements, AlignSingleLineCaseStatements.MaxArrowIndent, IndentLocalDefs, PreserveDanglingCloseParenthesis, DanglingCloseParenthesis,
SpaceInsideParentheses, SpaceInsideBrackets, SpacesWithinPatternBinders, MultilineScaladocCommentsStartOnFirstLine, IndentWithTabs,
CompactControlReadability, PlaceScaladocAsterisksBeneathSecondAsterisk, SpacesAroundMultiImports
CompactControlReadability, PlaceScaladocAsterisksBeneathSecondAsterisk, SpacesAroundMultiImports, NewlineAtEndOfFile
)

val preferencesByKey: Map[String, PreferenceDescriptor[_]] =
Expand Down Expand Up @@ -244,3 +244,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
@@ -0,0 +1,41 @@
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 Foo {
|}""" ==>
"""import foo.bar
|
|class Foo {
|}
|"""

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

0 comments on commit fba7239

Please sign in to comment.