Skip to content

Commit

Permalink
Merge pull request scala-ide#191 from jkinkead/eol_at_eof
Browse files Browse the repository at this point in the history
Add an option to add newlines to the end of files.
  • Loading branch information
kiritsuku committed Feb 19, 2016
2 parents 46234e5 + dbbc4e8 commit 2e8039e
Show file tree
Hide file tree
Showing 4 changed files with 85 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,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 2e8039e

Please sign in to comment.