forked from scalameta/scalafmt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConfiguration.scalatex
941 lines (760 loc) · 28.3 KB
/
Configuration.scalatex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
@import Main._
@import org.scalafmt.readme.Readme._
@import org.scalafmt.config.ScalafmtConfig
@import org.scalafmt.config.ScalafmtConfig.default
@import org.scalafmt.rewrite._
@import org.scalafmt.{Versions => V}
@sect{Configuration}
@p
Configuration for scalafmt is defined in a
plain text file @code{.scalafmt.conf} using
@lnk("HOCON", "https://github.com/typesafehub/config") syntax.
To reuse your configuration with IntelliJ,
@code(".scalafmt.conf") must be placed in the root directory of your project.
@p
Here is an example @code(".scalafmt.conf").
@cliFlags
align = true # For pretty alignment.
maxColumn = 100 # For my wide 30" display.
@p
A note of warning. I personally use the default style, which means that the
default style is by far the most tested and supported style.
Most of the configuration flags are quite innocent, while some of them work
very differently (esp. Scala.js).
It is very difficult to guarantee that all configurations options play nicely
together so I recommend you try not to go too crazy on this part.
@p
The following sections describe the most common configuration options.
@warning
If you are using scalafmt as a @sect.ref{Standalone library}, you can
pass in a @code{ScalafmtConfig} instance, which is set to @code{ScalafmtStyle.default}
by default.
@sect{Most Popular}
@sect{maxColumn}
Default: @b(default.maxColumn)
@ul
@li
Keep in mind that 80 characters fit perfectly on a split laptop screen
with regular resolution.
@li
Github mobile view only shows 80 characters and sometimes you might
review code on your phone.
@li
Consider refactoring your code before of choosing a value above 100.
@sect{docstrings}
Default: @b(default.docstrings.toString)
@hl.scala
// docstrings = ScalaDoc
/** Align by second asterisk.
*
*/
// docstrings = JavaDoc
/** Align by first asterisk.
*
*/
@sect{assumeStandardLibraryStripMargin}
Default: @b(default.assumeStandardLibraryStripMargin)
@warning
May cause non-idempotent formatting in rare cases, see @issue(192).
@p
If @b{true}, the margin character @code("|") is aligned with the opening triple
quote @code("\"\"\"") in interpolated and raw string literals.
@example(org.scalafmt.util.FileOps.readFile("readme/stripMargin.example"),
stripMarginStyle)
@sect{Indentation}
@sect{continuationIndent.callSite}
Default: @b(default.continuationIndent.callSite)
@p
Example:
@hl.scala
function(
argument1 // indented by 2
)
@sect{continuationIndent.defnSite}
Default: @b(default.continuationIndent.defnSite)
@p
Same as @code{continuationIndent.callSite} except for definition site.
Example:
@hl.scala
def function(
argument1: Type1): ReturnType // Indented by 4
@sect{Alignment}
@sect{align}
Default: @b{some}
Align has several nested fields, which you can customize.
However, it comes with four possible defaults: none, some, more, & most.
@sect{align=some}
@format(alignSome)
x match { // true for case arrows
case 2 => 22
case 22 => 222
}
def foo(a: Int, // true for defn site open paren
b: String): Int
foo(a: Int, // true for call site open paren
b: String): Int
val x = 2 // false for assignment
val xx = 22
case object B extends A // false for `extends`
case object BB extends A
@sect{align=none}
@format(alignNone)
x match { // false for case arrows
case 2 => 22 // also comments!
case 22 => 222 // don't align me!
}
def foo(a: Int, // false for defn site
b: String): Int
foo(a: Int, // false for call site
b: String): Int
Pro tip: Enable this setting to minimize git diffs/conflicts from renamings and other refactorings.
@configurationBlock(alignNone, true)
@sect{align=more}
@format(alignMore)
val x = 2 // true for assignment
val xx = 22
case object B extends A // false for `extends`
case object BB extends A
q -> 22 // true for various infix operators
qq -> 3 // and also comments!
for {
x <- List(1) // true for alignment enumerator
yy <- List(2)
} yield x ** xx
x match { // true for multiple tokens across multiple lines
case 1 => 1 -> 2 // first
case 11 => 11 -> 22 // second
// A blank line separates alignment blocks.
case `ignoreMe` => 111 -> 222
}
// Align assignments of similar type.
def name = column[String]("name")
def status = column[Int]("status")
val x = 1
val xx = 22
// Align sbt module IDs.
libraryDependencies ++= Seq(
"org.scala-lang" % "scala-compiler" % scalaVersion.value,
"com.lihaoyi" %% "sourcecode" % "0.1.1"
)
@configurationBlock(alignMore, true)
@sect{align=most}
@format(alignMost)
for {
// align <- with =
x <- List()
yy = 2
// aligns body by arrow
zzz <- new Integer {
def value = 3
}
} yield x
// Note. Only for the truest vertical aligners. This is a new option,
// feel free to open PR enabling more crazy vertical alignment here.
// Expect changes.
@configurationBlock(alignMost, true)
@sect{align.tokens}
Default: @b{[caseArrow]}
@p
An align token is a pair of @code{code}, which is the string literal
of an operator of token, and @code{owner}, which is the kind of
the closest tree node that owns that token.
If no @code{owner} is provided, then all tree kinds will be matched.
@p
@note. It is valid to set @code{align.tokens = more} to re-use settings
from @sect.ref{align=more}. Same applies for @code{none/some/most}.
@format(alignCaseArrow)
// =======================================================
// scalafmt: {align.tokens = [{code = "=>", owner = "Case"}]}
// =======================================================
x match {
case 1 => 1 -> 2
case 11 => 11 -> 22
}
@format(alignModuleId)
// =======================================================
// scalafmt: {align.tokens = ["%", "%%"]}
// =======================================================
val x = List(
"org.scala-lang" % "scala-compiler" % scalaVersion.value,
"com.lihaoyi" %% "sourcecode" % "0.1.1"
)
To find the @code{owner} part for a custom tree, depend on Scalameta and
use @code{scala.meta.Tree.productPrefix} from the the (for example, Ammonite)
repl
@hl.scala
@@ import $ivy.`org.scalameta:scalameta_2.12:@(V.scalameta)`, scala.meta._
@@ val termMatch = q"x match { case 2 => foo(bar) }"
termMatch: Term.Match = x match {
case 2 =>
foo(bar)
}
@@ termMatch.structure
res0: String = """
Term.Match(Term.Name("x"), Seq(Case(Lit.Int(2), None, Term.Apply(Term.Name("foo"), Seq(Term.Name("bar"))))))
"""
@@ termMatch.productPrefix
res1: String = "Term.Match"
To learn more about Scalameta, see this @lnk("tutorial", "http://scalameta.org/tutorial/").
@sect{align.arrowEnumeratorGenerator}
Default: @b(default.align.arrowEnumeratorGenerator)
@format
// align.arrowEnumeratorGenerator = false
for {
x <- new Integer {
def value = 2
}
} yield x
@format(alignArrowEnum)
// align.arrowEnumeratorGenerator = true
for {
x <- new Integer {
def value = 2
}
} yield x
@sect{align.openParenCallSite}
Default: @b(default.align.openParenCallSite)
@hl.scala
// Column limit |
// align.openParenCallSite = true
foo(arg1, arg2)
function(arg1, // align by (
arg2,
arg3)
function(
argument1,
argument2)
// align.openParenCallSite = false
foo(arg1, arg2)
function(
arg1, // no align by (
arg2,
arg3)
function(
argument1,
argument2)
@sect{align.openParenDefnSite}
Default: @b(default.align.openParenDefnSite)
@hl.scala
// align.openParenDefnSite = true
// Column limit |
class IntString(int: Int, string: String)
class IntStringLong(int: Int,
string: String,
long: Long)
// align.openParenDefnSite = false
// Column limit |
class IntString(int: Int, string: String)
class IntStringLong(
int: Int,
string: String,
long: Long
)
@sect{Newlines}
@p The @code{newlines.*} options are used to configure when and where @code{scalafmt} should
insert newlines.
@sidenote
You might be interested in the @sect.ref{Vertical Multiline} section.
@sect{newlines.alwaysBeforeTopLevelStatements}
Default: @b(default.newlines.alwaysBeforeTopLevelStatements)
@hl.scala
// newlines.alwaysBeforeTopLevelStatements = false
import org.scalafmt
package P {
object O {
val x1 = 1
val x2 = 2
def A = "A"
def B = "B"
}
}
// newlines.alwaysBeforeTopLevelStatements = true
import org.scalafmt
package P {
object O {
val x1 = 1
val x2 = 2
def A = "A"
def B = "B"
}
}
@sect{newlines.sometimesBeforeColonInMethodReturnType}
Default: @b(default.newlines.sometimesBeforeColonInMethodReturnType)
@hl.scala
// Column limit |
// newlines.sometimesBeforeColonInMethodReturnType = true
implicit def validatedInstances[E](implicit E: Semigroup[E])
: Traverse[Validated[E, ?]] with ApplicativeError[Validated[E, ?], E] = 2
// newlines.sometimesBeforeColonInMethodReturnType = false
implicit def validatedInstances[E](implicit E: Semigroup[E]): Traverse[
Validated[E, ?]] with ApplicativeError[Validated[E, ?], E] = 2
@sect{newlines.penalizeSingleSelectMultiArgList}
Default: @b(default.newlines.penalizeSingleSelectMultiArgList)
@hl.scala
// newlines.penalizeSingleSelectMultiArgList = true
logger.elem(a,
b,
c)
// newlines.penalizeSingleSelectMultiArgList = false
logger
.elem(a, b, c)
@p
See
@lnk("this comment", "https://github.com/scalameta/scalafmt/pull/611#issue-196230948")
for further motivation.
@sect{newlines.alwaysBeforeElseAfterCurlyIf}
Default: @b(default.newlines.alwaysBeforeElseAfterCurlyIf)
@hl.scala
// newlines.alwaysBeforeElseAfterCurlyIf = true
if(someCond) {
foo()
}
else {
bar()
}
// newlines.alwaysBeforeElseAfterCurlyIf = false
if(someCond) {
foo()
} else {
bar()
}
@sect{Rewrite Rules}
Default: @b{disabled}
@p
To enable a rewrite rule, add it to the config like this @config{rewrite.rules = [SortImports]}.
@sect(Rewrite.rewrite2name(AvoidInfix))
@demoStyle(rewriteInfix)
a success b
a error (b, c)
a map { x =>
x + 2
}
"o" % "a" % "v" c(D)
future recover {
case e: Err => 0
} map (_.toString)
For more examples, see @lnk("this diff", "https://github.com/olafurpg/scala-repos/pull/18/files#diff-f4f9daffe8f0a4aee6505c4eea8493ccL120").
Configuration contains include/exclude operator filters (those values are build in, and can't be configured outside)
@ul
@li
Configure under @code{rewrite.neverInfix}.
@sect(Rewrite.rewrite2name(ExpandImportSelectors))
@demoStyle(rewriteImportSelectors)
import a.{
b,
c
}, h.{
k, l
}
import d.e.{f, g}
import a.{
foo => bar,
zzzz => _,
_
}
@sect(Rewrite.rewrite2name(RedundantBraces))
@b{Warning}. This rewrite can cause @sect.ref{Non-idempotent} formatting, see @issue(1055).
@demoStyle(rewriteBraces)
object RedundantBraces {
def foo = {
List(1, 2, 3).sum
}
// stringInterpolation = true
q"Hello ${name}"
}
Configuration options and default values:
@ul
@li
@code{rewrite.redundantBraces.maxLines} = @rewriteBraces.rewrite.redundantBraces.maxLines
@li
@code{rewrite.redundantBraces.includeUnitMethods} = @rewriteBraces.rewrite.redundantBraces.includeUnitMethods
@li
@code{rewrite.redundantBraces.stringInterpolation} = @rewriteBraces.rewrite.redundantBraces.stringInterpolation
@li
@code{rewrite.redundantBraces.generalExpressions} = @rewriteBraces.rewrite.redundantBraces.generalExpressions
(disabled by default due to @issue(1147))
@sect(Rewrite.rewrite2name(RedundantParens))
@p
Removes redundant parentheses in guard clauses.
@demoStyle(rewriteParens)
object RedundantParenthesis {
def c(b: List[Int]): List[Int] =
for {
a <- b
if (a.nonEmpty)
} yield a
}
@sect(Rewrite.rewrite2name(SortImports))
@p
The imports are sorted by the groups: symbols, lower-case, upper-case.
@demoStyle(rewriteImports)
import foo.{Zilch, bar, Random, sand}
@sect(Rewrite.rewrite2name(AsciiSortImports))
@p
The imports are sorted by their Ascii codes
@demoStyle(rewriteAsciiImports)
import foo.{~>, `symbol`, bar, Random}
@sect(Rewrite.rewrite2name(SortModifiers))
@p
Modifiers are sorted based on the given order. Affects modifiers of the following
definitions: trait, class, object, type, and val+var, both as fields and class parameters.
@hl.scala
//before
final lazy private implicit val x = 42
lazy final implicit private val y = 42
//after
private final implicit lazy val x = 42
private final implicit lazy val y = 42
@hl.scala
//before
class Test(
implicit
final private val i1: Int,
private final val i2: String
)
//after
class Test(
implicit
private final val i1: Int,
private final val i2: String
)
@hl.scala
//before
object X {
sealed protected[X] trait ADT
final private case object A1 extends ADT
private final case class A2(x: Int) extends ADT
}
//after
object X {
protected[X] sealed trait ADT
private final case object A1 extends ADT
private final case class A2(x: Int) extends ADT
}
If you choose the non-default sort order then you have to specify all eight modifiers in the order you wish to see
them. Hint: since some modifiers are mutually exclusive, you might want to order them next to each other.
@p
Example config:
@cliFlags
rewrite {
rules = [SortModifiers]
#optional, see default values below
sortModifiers {
order = ["implicit", "final", "sealed", "abstract",
"override", "private", "protected", "lazy"]
}
}
Default values:
@ul
@li
@code{rewrite.sortModifiers.order} = @rewriteSortModifiersDefaultString
@sect(Rewrite.rewrite2name(PreferCurlyFors))
@p
Replaces parentheses into curly braces in for comprehensions that
contain multiple enumerator generators.
@demoStyle(rewritePreferCurlyFors)
object a {
for(a <- as; b <- bs if b > 2)
yield (a, b)
}
@sect{Vertical Multiline}
@p
If enabled this formats methods such that parameters are on
their own line indented by
@sect.ref{continuationIndent.defnSite}. Separation between
parameter groups are indented by two spaces less than
@sect.ref{continuationIndent.defnSite}. The return type is on
its own line at then end.
@sidenote
This formatting is only triggered if the method definition
exceeds the @sect.ref{maxColumn} value in width or if the number
of arguments to the method exceeds the
@sect.ref{verticalMultiline.arityThreshold}.
@fullWidthDemo(verticalAlign)
object a {
def format(code: String, age: Int)(implicit ev: Parser, c: Context): String
}
@warning
This setting ignores binPack.unsafeDefnSite, and @sect.ref{align.openParenDefnSite}.
@p
The default configuration options are
@hl.scala(VerticalMultilineDefultConfigStr)
@sect{verticalMultiline.atDefnSite}
Default: @b{false}
@p
Since: v1.6.0 (was @code{verticalMultilineAtDefinitionSite=true} before)
@p
If true, enable vertical multiline formatting as described above.
@sect{verticalMultiline.arityThreshold}
Default: @b{100}
@p
If set, this will trigger a vertical multi-line formatting
even though the definition falls below the
@sect.ref{maxColumn} width.
@fullWidthDemo(arityThreshold)
case class Foo(x: String)
case class Bar(x: String, y: String)
object A {
def foo(x: String, y: String)
def hello(how: String)(are: String)(you: String) = how + are + you
}
@sect{verticalMultiline.newlineAfterOpenParen}
Default: @b(default.verticalMultiline.newlineAfterOpenParen)
@p
If true, add a newline after the opening paren of a parameter
list. This can be used to control the formatting of methods that
have multiple parameter lists.
@fullWidthDemo(multilineNewlineAfterParen)
object a {
def other(a: String, b: String)(c: String) = a + b + c
}
@sect{verticalMultiline.newlineBeforeImplicitKW}
Default: @b(default.verticalMultiline.newlineBeforeImplicitKW)
@p If true, add a newline before an implicit keyword in function and class definitions.
@fullWidthDemo(verticalAlignImplicitBefore)
object a {
def format(code: String, age: Int)(implicit ev: Parser, c: Context): String
}
@sect{verticalMultiline.newlineAfterImplicitKW}
Default: @b(default.verticalMultiline.newlineAfterImplicitKW)
@p If true, add a newline after an implicit keyword in function and class definitions.
@fullWidthDemo(verticalAlignImplicitAfter)
object a {
def format(code: String, age: Int)(implicit ev: Parser, c: Context): String
}
@sect{verticalMultiline.excludeDanglingParens}
Default: @b(default.verticalMultiline.excludeDanglingParens.mkString(","))
@p
This setting defines which cases should be allowed to have
dangling parens.
@fullWidthDemo(multilineDanglingParens)
case class Bar(x: String, y: String)
@sect{newlines.alwaysBeforeMultilineDef}
Default: @b(default.newlines.alwaysBeforeMultilineDef)
@p
If true, add a newline before the body of a multiline def
without curly braces.
@fullWidthDemo(verticalAlign)
object a {
def foo(bar: Bar): Foo = bar
.flatMap(f)
.map(g)
}
@sect{Disabling Formatting}
@sect{// format: off}
Disable formatting for specific regions of code by wrapping them in
@code("// format: OFF") blocks:
@example(org.scalafmt.util.FileOps.readFile("readme/Matrix.example"))
To disable formatting for a whole file, put the comment at the top of
the file.
@ul
@li
the comment string is case insensitive, you can also write
@code("// format: OFF").
@li
The comments @code("// @formatter:off") and @code("// @formatter:on")
will also work, for compatibility with the IntelliJ formatter.
@li
Scalafmt will do it's best to resume formatting at the correct
indentation level. It's best to enable formatting at the same level as
when it was disabled.
@sect{project}
@p
Configure which source files should be formatted in this project.
@cliFlags
# Only format files tracked by git.
project.git = true
# manually exclude files to format.
project.excludeFilters = [
regex1
regex2
]
# manually include files to format.
project.includeFilters = [
regex1
regex2
]
@sect{Miscellaneous}
@sect{lineEndings}
Default: @b(default.lineEndings.toString)
@ul
@li
@code{preserve} output will include endings included in original file (windows if there was at least one windows
line ending, unix if there was zero occurrences of windows line endings)
@li
@code{unix} output will include only unix line endings
@li
@code{windows} output will include only windows line endings
@sect{binPack.literalArgumentLists}
Default: @b(default.binPack.literalArgumentLists)
@hl.scala
// binPack.literalArgumentLists = true
val secret: List[Bit] = List(0, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1,
0, 0, 1, 1, 0, 1, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1,
0, 1, 1, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1)
// binPack.literalArgumentLists = false
val secret: List[Bit] = List(
0,
0,
1,
1,
// really long list...
1
)
@sect{binPack.parentConstructors}
Default: @b(default.binPack.parentConstructors)
@hl.scala
// column limit |
// binPack.parentConstructors = false
object DefaultStyle
extends Parent
with SecondParent
with ThirdParent {
// body ...
}
// column limit |
// binPack.parentConstructors = true
object DefaultStyle
extends Parent with SecondParent
with ThirdParent {
// body ...
}
@sect{spaces.afterKeywordBeforeParen}
Default: @b(default.spaces.afterKeywordBeforeParen)
@hl.scala
// spaces.afterKeywordBeforeParen = true
if (a) foo()
while (a) foo()
for (a <- as) foo()
// spaces.afterKeywordBeforeParen = false
if(a) foo()
while(a) foo()
for(a <- as) foo()
@sect{spaces.afterSymbolicDefs}
Default: @b(default.spaces.afterSymbolicDefs)
@hl.scala
// spaces.afterSymbolicDefs = true
trait Test[A] {
def <=> [B](that: Test[B]): Int
}
// spaces.afterSymbolicDefs = false
trait Test[A] {
def <=>[B](that: Test[B]): Int
}
@sect{includeCurlyBraceInSelectChains}
Default: @b(default.includeCurlyBraceInSelectChains)
@hl.scala
// includeCurlyBraceInSelectChains = true
List(1)
.map { x =>
x + 2
}
.filter(_ > 2)
// includeCurlyBraceInSelectChains = false
List(1).map { x =>
x + 2
}.filter(_ > 2)
For more details, see
@lnk("this comment", "https://github.com/scalameta/scalafmt/issues/444#issuecomment-255215698").
@sect{optIn.breakChainOnFirstMethodDot}
Default: @b(default.optIn.breakChainOnFirstMethodDot)
@p
If true, forces a select chain (pipeline) to break if there is a newline
at the start of the chain.
@hl.scala
// original
foo
.map(_ + 1)
.filter(_ > 2)
// optIn.breakChainOnFirstMethodDot = true
foo
.map(_ + 1)
.filter(_ > 2)
// optIn.breakChainOnFirstMethodDot = false
foo.map(_ + 1).filter(_ > 2)
// note. chain starts at .foo() in a.b.foo()
@p
See
@lnk("this comment", "https://github.com/scalameta/scalafmt/issues/444#issuecomment-255215698")
for further motivation.
@sect{runner.optimizer.forceConfigStyleOnOffset}
Default: @b(default.runner.optimizer.forceConfigStyleOnOffset)
@p
Set to @code{-1} to disable. Increase number to require bigger argument
bodies to trigger flag.
@hl.scala
// Before: Argument body is too big
Account(userName = "user",
fullName = "user@@localhost",
mailAddress = "",
password = "",
isAdmin = false,
url = None,
registeredDate = new Date(),
updatedDate = new Date(),
lastLoginDate = None,
image = image,
isGroupAccount = false,
isRemoved = false)
// After: "config style"
Account(
userName = "user",
fullName = "user@@localhost",
mailAddress = "",
password = "",
isAdmin = false,
url = None,
registeredDate = new Date(),
updatedDate = new Date(),
lastLoginDate = None,
image = image,
isGroupAccount = false,
isRemoved = false
)
@p
Set @cliFlags{runner.optimizer.forceConfigStyleMinArgCount = 1} to enable
this rule for function calls with only 1 argument
(default = @default.runner.optimizer.forceConfigStyleMinArgCount).
@p
By forcing config style, scalafmt is able to greatly optimize performance
eliminating a large number of "search state exploded" errors.
See @lnk("these flame graphs", "https://github.com/scalameta/scalafmt/pull/621#issue-196451803").
@sect{style}
Option 1: @b{default}
@fmt(ScalafmtConfig.default.copy(maxColumn = 40))
// Column 40 |
// non bin packed parent constructors
object DefaultStyle extends Parent with SecondParent with ThirdParent {
// non bin packed arguments
function(argument1, argument2(argument3, argument4))
// Vertical alignment only for case arrows
x match {
case 1 => 1
case 11 => 1
}
}
Option 2: @b{IntelliJ}
@fmt(ScalafmtConfig.intellij.copy(maxColumn = 40))
// Column 40 |
// non bin packed parent constructors
object IntelliJStyle extends Parent with SecondParent with ThirdParent {
function(argument1, argument2)
// continuationIndent = 2 # for call + defn site
// align.openParenCallSite = false
// danglingParentheses = true
function(argument1, argument2, argument3, argument4)
// openParenCallSite = true
def foobar(argument1: Type1, argument2: Type2): Int = argument1 + argument2
}
@warning
There is a Scala.js style that is super experimental and does not
work for complicated code.
@sect{Other}
@p
To find all available configuration options, it's best to browse the
source code of Scalafmt.
A good place to start is @code{ScalafmtConfig}.
Observe that this listing below is the top-level,
there are more configuration options if you visited nested fields
like @code{spaces} and @code{newlines}.
@org.scalafmt.readme.Readme.allOptions