Skip to content

Commit 9f5abe0

Browse files
committed
Support align.tokens={none,some,more,most}
Instead of making rules more complicated to support ``` align = more align.openParenCallSite = true ``` We make it easier to override `align.tokens` by supporting ``` align.tokens = more align.openParenCallSite = true ```
1 parent 251d601 commit 9f5abe0

File tree

6 files changed

+53
-11
lines changed

6 files changed

+53
-11
lines changed

readme/Configuration.scalatex

+11
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,8 @@
140140

141141
Pro tip: Enable this setting to minimize git diffs/conflicts from renamings and other refactorings.
142142

143+
@configurationBlock(alignNone, true)
144+
143145
@sect{align=more}
144146
@format(alignMore)
145147
val x = 2 // true for assignment
@@ -176,6 +178,8 @@
176178
"com.lihaoyi" %% "sourcecode" % "0.1.1"
177179
)
178180

181+
@configurationBlock(alignMore, true)
182+
179183
@sect{align=most}
180184
@format(alignMost)
181185
for {
@@ -191,6 +195,9 @@
191195
// feel free to open PR enabling more crazy vertical alignment here.
192196
// Expect changes.
193197

198+
@configurationBlock(alignMost, true)
199+
200+
194201
@sect{align.tokens}
195202
Default: @b{[caseArrow]}
196203

@@ -200,6 +207,10 @@
200207
the closest tree node that owns that token.
201208
If no @code{owner} is provided, then all tree kinds will be matched.
202209

210+
@p
211+
@note. It is valid to set @code{align.tokens = more} to re-use settings
212+
from @sect.ref{align=more}. Same applies for @code{none/some/most}.
213+
203214

204215
@format(alignCaseArrow)
205216
// =======================================================

readme/src/main/scala/org/scalafmt/readme/Readme.scala

+12-5
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ object Readme {
100100

101101
def rows(frags: Frag*) = div(frags, `class` := "scalafmt-rows")
102102

103-
def sideBySide(left: String, right: String) =
103+
def sideBySide(left: String, right: String): TypedTag[String] =
104104
pairs(List(left, right).map(x => half(hl.scala(x))): _*)
105105

106106
def demo(code: String) = {
@@ -119,17 +119,24 @@ object Readme {
119119
def allOptions =
120120
hl.scala.apply(Conf.printHocon(ScalafmtConfig.default))
121121

122-
def configurationBlock(style: ScalafmtConfig) = {
122+
def configurationBlock(
123+
style: ScalafmtConfig,
124+
collapsed: Boolean = false): TypedTag[String] = {
123125
div(
124126
span(
125127
"Show/hide configuration used for this example",
126128
`class` := "scalafmt-configuration-toggle"),
127129
pre(changedConfig(style)),
128-
`class` := "scalafmt-configuration"
130+
`class` := {
131+
"scalafmt-configuration" + (
132+
if (collapsed) " collapsed"
133+
else ""
134+
)
135+
}
129136
)
130137
}
131138

132-
def fullWidthDemo(style: ScalafmtConfig)(code: String) = {
139+
def fullWidthDemo(style: ScalafmtConfig)(code: String): TypedTag[String] = {
133140
val formatted = Scalafmt.format(code, style).get
134141
div(
135142
rows(
@@ -140,7 +147,7 @@ object Readme {
140147
configurationBlock(style))
141148
}
142149

143-
def demoStyle(style: ScalafmtConfig)(code: String) = {
150+
def demoStyle(style: ScalafmtConfig)(code: String): TypedTag[String] = {
144151
val formatted =
145152
Scalafmt.format(code, style.copy(runner = ScalafmtRunner.sbt)).get
146153
div(

scalafmt-core/shared/src/main/scala/org/scalafmt/config/Align.scala

+9
Original file line numberDiff line numberDiff line change
@@ -115,4 +115,13 @@ object Align {
115115
)
116116
)
117117
val allValues = List(default, none, some, most)
118+
119+
object Builtin {
120+
def unapply(conf: Conf): Option[Align] = Option(conf).collect {
121+
case Conf.Str("none") | Conf.Bool(false) => Align.none
122+
case Conf.Str("some" | "default") => Align.some
123+
case Conf.Str("more") | Conf.Bool(true) => Align.more
124+
case Conf.Str("most") => Align.most
125+
}
126+
}
118127
}

scalafmt-core/shared/src/main/scala/org/scalafmt/config/ScalafmtConfig.scala

+3-5
Original file line numberDiff line numberDiff line change
@@ -335,18 +335,16 @@ object ScalafmtConfig {
335335
}
336336
def alignReader(base: ConfDecoder[Align]): ConfDecoder[Align] =
337337
ConfDecoder.instance[Align] {
338-
case Conf.Str("none") | Conf.Bool(false) => Ok(Align.none)
339-
case Conf.Str("some" | "default") => Ok(Align.some)
340-
case Conf.Str("more") | Conf.Bool(true) => Ok(Align.more)
341-
case Conf.Str("most") => Ok(Align.most)
338+
case Align.Builtin(a) => Ok(a)
342339
case els => base.read(els)
343340
}
344341
def alignTokenReader(
345342
initTokens: Set[AlignToken]): ConfDecoder[Set[AlignToken]] = {
346-
val baseReader = implicitly[ConfDecoder[Set[AlignToken]]]
343+
val baseReader = ConfDecoder[Set[AlignToken]]
347344
ConfDecoder.instance[Set[AlignToken]] {
348345
case Conf.Obj(("add", conf) :: Nil) =>
349346
baseReader.read(conf).map(initTokens ++ _)
347+
case Align.Builtin(a) => Ok(a.tokens)
350348
case els => baseReader.read(els)
351349
}
352350
}

scalafmt-tests/src/test/resources/align/DefaultWithAlign.stat

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
align = true
1+
align = more
22
<<< => with comment
33
x match {
44
case 1 => 2 // this is a comment
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
maxColumn = 20
2+
align.tokens = most
3+
<<< #1202
4+
{
5+
foo(aaaaaaaa, bbbbb)
6+
val x = 2
7+
val xx = 2
8+
}
9+
>>>
10+
{
11+
foo(
12+
aaaaaaaa,
13+
bbbbb
14+
)
15+
val x = 2
16+
val xx = 2
17+
}

0 commit comments

Comments
 (0)