Skip to content

Commit d7b7932

Browse files
author
Daniel Trinh
committed
fixed another edge case with call by name params
1 parent f10a478 commit d7b7932

File tree

3 files changed

+23
-8
lines changed

3 files changed

+23
-8
lines changed

scalariform/src/main/scala/scalariform/formatter/ExprFormatter.scala

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -938,6 +938,8 @@ trait ExprFormatter { self: HasFormattingPreferences with AnnotationFormatter wi
938938
TextEditProcessor.runEdits(source, edits)
939939
}
940940

941+
// TODO: is there a less brittle way of calculating the extra '+ 1' character
942+
// spaces besides guessing?
941943
def calculateLengths: ParamSectionLengths = {
942944

943945
def calculatePrefixLength: Int = {
@@ -976,21 +978,27 @@ trait ExprFormatter { self: HasFormattingPreferences with AnnotationFormatter wi
976978
// Calculate longest "type" length.
977979
var typeLength = 0
978980
for ((_, typeAst) paramTypeOpt) {
979-
typeAst.tokens.foreach { token
981+
val firstTypeToken :: otherTypeTokens = typeAst.tokens
982+
983+
// Add one to count, accounting for spaces in call by name params, (ex: => String)
984+
if (firstTypeToken.tokenType == ARROW)
985+
typeLength += 1
986+
987+
typeLength += firstTypeToken.length
988+
989+
otherTypeTokens.foreach { token
980990

981991
// Add two to count, accounting for spaces between brackets, (ex: Option[ String ])
982992
if (spacesInsideBrackets && token.tokenType == RBRACKET)
983993
typeLength += 2
984994

985995
// Add two to count, accounting for spaces in function types, (ex: Int => String)
986-
if (token.tokenType == ARROW) {
996+
if (token.tokenType == ARROW)
987997
typeLength += 2
988-
}
989998

990999
// Add one to count, accounting for spaces after commas, (ex: Map[Int, String])
991-
if (token.tokenType == COMMA) {
1000+
if (token.tokenType == COMMA)
9921001
typeLength += 1
993-
}
9941002

9951003
typeLength += token.length
9961004
}

scalariform/src/main/scala/scalariform/lexer/Tokens.scala

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,5 +114,4 @@ object Tokens {
114114

115115
val LITERALS = Set(CHARACTER_LITERAL, INTEGER_LITERAL, FLOATING_POINT_LITERAL, STRING_LITERAL, STRING_PART, SYMBOL_LITERAL, TRUE, FALSE, NULL)
116116

117-
}
118-
117+
}

scalariform/src/test/scala/scalariform/formatter/TemplateFormatterTest.scala

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -414,14 +414,22 @@ implicit val formattingPreferences = FormattingPreferences.setPreference(SpacesW
414414
| f: HttpCrawlerClient => T,
415415
| port: Int = SpecHelper.port): T"""
416416

417+
// By name parameters have correct spacing
418+
"""def a(
419+
| p1: => (SomeLongByNameParam => SomeShorterParam) = Null,
420+
| param2: SomeShorterParam = Null): A""" ==>
421+
"""def a(
422+
| p1: => (SomeLongByNameParam => SomeShorterParam) = Null,
423+
| param2: SomeShorterParam = Null): A"""
424+
417425
// Formats parameterized types correctly
418426
"""def A(complicatedType: Option[B ,C, D[E, F,G]] = None,
419427
| simpleType: String = ""): B""" ==>
420428
"""def A(complicatedType: Option[B, C, D[E, F, G]] = None,
421429
| simpleType: String = ""): B"""
422430

423431

424-
// Param gets placed onto a new line due to current limitations of existing IntertokenFormatInstructions
432+
// Param gets placed onto a new line due to current limitations of existing IntertokenFormatInstructions
425433
"""case class Spacing(param: Int = 1,
426434
|paramTwo: Int = 2,
427435
|paramThree: String = "3")""" ==>

0 commit comments

Comments
 (0)