Skip to content

Commit d1e824a

Browse files
author
Daniel Trinh
committed
switch type length calculation to a less brittle solution
1 parent d7b7932 commit d1e824a

File tree

4 files changed

+45
-46
lines changed

4 files changed

+45
-46
lines changed

scalariform/src/main/scala/scalariform/formatter/CaseClauseFormatter.scala

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,8 @@ trait CaseClauseFormatter { self: HasFormattingPreferences with ExprFormatter wi
7070
case (caseClause @ CaseClause(casePattern, statSeq)) :: otherClauses
7171
val otherClausesGrouped = groupClauses(otherClauses, first = false)
7272

73-
val formattedCasePattern = {
74-
val casePatternSource = getSource(casePattern)
75-
val casePatternFormatResult = formatCasePattern(casePattern)(FormatterState(indentLevel = 0))
76-
val offset = casePattern.firstToken.offset
77-
val edits = writeTokens(casePatternSource, casePattern.tokens, casePatternFormatResult, offset)
78-
TextEditProcessor.runEdits(casePatternSource, edits)
73+
val formattedCasePattern = formattedAstNode(casePattern) {
74+
formatCasePattern(casePattern)(FormatterState(indentLevel = 0))
7975
}
8076

8177
val newlineBeforeClause = hiddenPredecessors(caseClause.firstToken).containsNewline ||

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

Lines changed: 10 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -927,21 +927,15 @@ trait ExprFormatter { self: HasFormattingPreferences with AnnotationFormatter wi
927927
}
928928
}
929929

930+
// TODO: Parts of this function might be useful in implementing other alignment features
930931
private def calculateParamSectionLengths(param: Param, first: Boolean)(implicit formatterState: FormatterState): Option[ParamSectionLengths] = {
931932
val Param(annotations, modifiers, valOrVarOpt, id, paramTypeOpt, defaultValueOpt) = param
932933

933-
val formattedParam = {
934-
val source = getSource(param)
935-
val paramFormatResult = format(param)(formatterState)
936-
val offset = param.firstToken.offset
937-
val edits = writeTokens(source, param.tokens, paramFormatResult, offset)
938-
TextEditProcessor.runEdits(source, edits)
934+
val formattedParam = formattedAstNode(param) {
935+
format(param)(formatterState)
939936
}
940937

941-
// TODO: is there a less brittle way of calculating the extra '+ 1' character
942-
// spaces besides guessing?
943938
def calculateLengths: ParamSectionLengths = {
944-
945939
def calculatePrefixLength: Int = {
946940
// Calculate longest "prefix" length. Annotations, modifiers, and val/var contribute
947941
// to this number.
@@ -974,37 +968,16 @@ trait ExprFormatter { self: HasFormattingPreferences with AnnotationFormatter wi
974968
}
975969

976970
def calculateTypeLength: Int = {
977-
val spacesInsideBrackets = formattingPreferences(SpaceInsideBrackets)
978971
// Calculate longest "type" length.
979-
var typeLength = 0
980-
for ((_, typeAst) paramTypeOpt) {
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
990-
991-
// Add two to count, accounting for spaces between brackets, (ex: Option[ String ])
992-
if (spacesInsideBrackets && token.tokenType == RBRACKET)
993-
typeLength += 2
994-
995-
// Add two to count, accounting for spaces in function types, (ex: Int => String)
996-
if (token.tokenType == ARROW)
997-
typeLength += 2
998-
999-
// Add one to count, accounting for spaces after commas, (ex: Map[Int, String])
1000-
if (token.tokenType == COMMA)
1001-
typeLength += 1
1002-
1003-
typeLength += token.length
1004-
}
972+
val typeLengthOpt = paramTypeOpt map {
973+
case (_, typeAst)
974+
val formattedType = formattedAstNode(typeAst) {
975+
format(typeAst)(formatterState)
976+
}
977+
formattedType.length
1005978
}
1006979

1007-
typeLength
980+
typeLengthOpt.getOrElse(0)
1008981
}
1009982

1010983
val prefixLength = calculatePrefixLength

scalariform/src/main/scala/scalariform/formatter/ScalaFormatter.scala

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,23 @@ abstract class ScalaFormatter extends HasFormattingPreferences with TypeFormatte
4545
result
4646
}
4747

48+
/**
49+
* Converts an AstNode into what it should look like in text after Scalariform has run.
50+
* Useful for calculating the actual length of an [[scalariform.parser.AstNode]] after formatting.
51+
*
52+
* @param ast The AST to format and render as a string
53+
* @param astFormatResult Should run formatting actions for 'ast'
54+
* @return Formatted string representation of what the AstNode should look like after Scalariform
55+
* has run
56+
*/
57+
protected def formattedAstNode(ast: AstNode)(astFormatResult: FormatResult): String = {
58+
val source = getSource(ast)
59+
val formatResult = astFormatResult
60+
val offset = ast.firstToken.offset
61+
val edits = writeTokens(source, ast.tokens, formatResult, offset)
62+
TextEditProcessor.runEdits(source, edits)
63+
}
64+
4865
private def alterSuspendFormatting(text: String): Option[Boolean] =
4966
if (text contains "format: OFF")
5067
Some(true)

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

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -340,9 +340,22 @@ implicit val formattingPreferences = FormattingPreferences.setPreference(SpacesW
340340
{
341341
implicit val formattingPreferences = FormattingPreferences.
342342
setPreference(AlignParameters, true).
343-
setPreference(PreserveDanglingCloseParenthesis, true)
344-
345-
"""class a(
343+
setPreference(PreserveDanglingCloseParenthesis, true).
344+
setPreference(RewriteArrowSymbols, true)
345+
346+
// Formats rewritten arrows correctly
347+
"""def A(
348+
| a: A => B = null,
349+
| bee: => B = null,
350+
| c: B => C = null
351+
|): D""" ==>
352+
"""def A(
353+
| a: A ⇒ B = null,
354+
| bee: ⇒ B = null,
355+
| c: B ⇒ C = null
356+
|): D"""
357+
358+
"""class a(
346359
| b: Int
347360
|)""" ==>
348361
"""class a(

0 commit comments

Comments
 (0)