|
| 1 | +@import Main._ |
| 2 | +@import org.scalafmt.readme.Readme._ |
| 3 | +@import org.scalafmt.ScalafmtStyle |
| 4 | +@import org.scalafmt.ScalafmtStyle.default |
| 5 | + |
| 6 | +@sect{Configuration} |
| 7 | + |
| 8 | + @p |
| 9 | + There are two ways to configure @code("scalafmt"). |
| 10 | + |
| 11 | + @ol |
| 12 | + @li |
| 13 | + As Scala code via the @code("ScalafmtConfig") case class. |
| 14 | + This is handy when using scalafmt as a standalone library. |
| 15 | + For details, refer to the Scaladoc. |
| 16 | + @li |
| 17 | + In a plain text file, options are defined as CLI flags. |
| 18 | + I recommend you create a file named @code(".scalafmt") and keep it in |
| 19 | + the root directory of your project. |
| 20 | + |
| 21 | + @p |
| 22 | + Here is an example @code(".scalafmt"). |
| 23 | + |
| 24 | + @cliFlags |
| 25 | + # Example .scalafmt, comments are supported. |
| 26 | + --style defaultWithAlign # For pretty alignment. |
| 27 | + --maxColumn 100 # For my wide 30" display. |
| 28 | + |
| 29 | + To experiment with different options, I recommend you use the CLI. |
| 30 | + For details, read about the @code("--config") flag in the |
| 31 | + @code("--help") page of the CLI. |
| 32 | + |
| 33 | + Here are examples of the most common configuration options. |
| 34 | + For a full list of available flags, consult the @code{--help} page of the |
| 35 | + CLI or the @lnk("ScalafmtStyle docstring", "https://github.com/olafurpg/scalafmt/blob/master/core/src/main/scala/org/scalafmt/ScalafmtStyle.scala"). |
| 36 | + |
| 37 | + @sect{--style} |
| 38 | + Option 1: @b{default} |
| 39 | + @fmt(ScalafmtStyle.default.copy(maxColumn = 40)) |
| 40 | + // Column 40 | |
| 41 | + // non bin packed parent constructors |
| 42 | + object ScalaJsStyle extends Parent with SecondParent with ThirdParent { |
| 43 | + // non bin packed arguments |
| 44 | + function(argument1, argument2(argument3, argument4)) |
| 45 | + // No vertical alignment |
| 46 | + x match { |
| 47 | + case 1 => 1 |
| 48 | + case 11 => 1 |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + Option 2: @b{defaultWithAlign} |
| 53 | + @fmt(ScalafmtStyle.defaultWithAlign.copy(maxColumn = 40)) |
| 54 | + // Column 40 | |
| 55 | + object ScalaJsStyle extends Parent with SecondParent with ThirdParent { |
| 56 | + // vertical alignment |
| 57 | + x match { |
| 58 | + case 1 => 1 // align me |
| 59 | + case 11 => 11 // align me too |
| 60 | + |
| 61 | + case x => // blank line breaks alignment |
| 62 | + } |
| 63 | + for { |
| 64 | + x <- List(1) |
| 65 | + xx <- List(1, 2) |
| 66 | + } yield x * xx |
| 67 | + // A whole bunch of other |
| 68 | + // alignment goodies. |
| 69 | + // ... |
| 70 | + } |
| 71 | + |
| 72 | + Option 3: @b{Scala.js} (experimental) |
| 73 | + @fmt(ScalafmtStyle.scalaJs.copy(maxColumn = 40)) |
| 74 | + // Column 40 | |
| 75 | + // bin packed parent constructors |
| 76 | + object ScalaJsStyle extends Parent with SecondParent with ThirdParent { |
| 77 | + // bin packed arguments |
| 78 | + function(argument1, argument2, argument3, argument4) |
| 79 | + // Vertical alignment for only => |
| 80 | + x match { |
| 81 | + case 1 => 1 |
| 82 | + case 11 => 1 |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + @sect{--maxColumn} |
| 87 | + Default: @b{80} |
| 88 | + |
| 89 | + @ul |
| 90 | + @li |
| 91 | + Keep in mind that 80 characters fit perfectly on a split laptop screen |
| 92 | + with regular resolution. |
| 93 | + @li |
| 94 | + Github mobile view only shows 80 characters and sometimes you need |
| 95 | + to look at code on your phone. |
| 96 | + @li |
| 97 | + Consider refactoring your code instead of choosing a value over 100. |
| 98 | + |
| 99 | + @sect{--continuationIndentCallSite} |
| 100 | + Default: @b{default.continuationIndentCallSite} |
| 101 | + |
| 102 | + @p |
| 103 | + Example: |
| 104 | + @hl.scala |
| 105 | + function( |
| 106 | + argument1 // indented by 2 |
| 107 | + ) |
| 108 | + |
| 109 | + @sect{--continuationIndentDefnSite} |
| 110 | + Default: @b{default.continuationIndentDefnSite} |
| 111 | + |
| 112 | + @p |
| 113 | + Same as @code{--continuationIndentCallSite} except for definition site. |
| 114 | + Example: |
| 115 | + @hl.scala |
| 116 | + def function( |
| 117 | + argument1: Type1): ReturnType // Indented by 4 |
| 118 | + |
| 119 | + @sect{--alignTokens} |
| 120 | + Default: @b{off} |
| 121 | + |
| 122 | + @p |
| 123 | + The style @code("defaultWithAlign") has pre-configured a variety of |
| 124 | + tokens that align together nicely. |
| 125 | + |
| 126 | + @exampleAlign |
| 127 | + // 40 columns | |
| 128 | + object TokenAlignment { |
| 129 | + x match { |
| 130 | + // Align by => and -> and // |
| 131 | + case 1 => 1 -> 2 // first |
| 132 | + case 11 => 11 -> 22 // second |
| 133 | + |
| 134 | + // A blank line separates alignment blocks. |
| 135 | + case `ignoreMe` => 111 -> 222 |
| 136 | + } |
| 137 | + |
| 138 | + // Align assignments of similar type. |
| 139 | + def name = column[String]("name") |
| 140 | + def status = column[Int]("status") |
| 141 | + val x = 1 |
| 142 | + val xx = 22 |
| 143 | + |
| 144 | + // Align sbt module IDs. |
| 145 | + libraryDependencies ++= Seq( |
| 146 | + "org.scala-lang" % "scala-compiler" % scalaVersion.value, |
| 147 | + "com.lihaoyi" %% "sourcecode" % "0.1.1" |
| 148 | + ) |
| 149 | + } |
| 150 | + |
| 151 | + If you need more customization, refer to the @code{--help}. |
| 152 | + |
| 153 | + @sect{// format: off} |
| 154 | + Disable formatting for specific regions of code by wrapping them in |
| 155 | + @code("// format: OFF") blocks: |
| 156 | + |
| 157 | + @example(org.scalafmt.util.FileOps.readFile("readme/matrix.example")) |
| 158 | + |
| 159 | + To disable formatting for a whole file, put the comment at the top of |
| 160 | + the file. |
| 161 | + |
| 162 | + @ul |
| 163 | + @li |
| 164 | + the comment string is case insensitive, you can also write |
| 165 | + @code("// format: OFF"). |
| 166 | + @li |
| 167 | + The comments @code("// @formatter:off") and @code("// @formatter:off") |
| 168 | + will also work, for compatibility with the IntelliJ formatter. |
| 169 | + @li |
| 170 | + Scalafmt will do it's best to resume formatting at the correct |
| 171 | + indentation level. It's best to enable formatting at the same level as |
| 172 | + when it was disabled. |
| 173 | + |
| 174 | + @sect{--assumeStandardLibraryStripMargin} |
| 175 | + Default: @default.alignStripMarginStrings |
| 176 | + |
| 177 | + @p |
| 178 | + If @b{true}, the margin character @code("|") is aligned with the opening triple |
| 179 | + quote @code("\"\"\"") in interpolated and raw string literals. |
| 180 | + |
| 181 | + @example(org.scalafmt.util.FileOps.readFile("readme/stripMargin.example"), |
| 182 | + stripMarginStyle) |
| 183 | + |
| 184 | + @note. May cause non-idempotent formatting in rare cases, see @issue(192). |
| 185 | + |
| 186 | + @sect{--javaDocs vs. --scalaDocs} |
| 187 | + Default: --scalaDocs |
| 188 | + |
| 189 | + @hl.scala |
| 190 | + // --scalaDocs |
| 191 | + /** Align by second asterisk. |
| 192 | + * |
| 193 | + */ |
| 194 | + // --javaDocs |
| 195 | + /** Align by first asterisk. |
| 196 | + * |
| 197 | + */ |
| 198 | + |
| 199 | + @sect{--allowNewlineBeforeColonInMassiveReturnTypes} |
| 200 | + Default: @default.allowNewlineBeforeColonInMassiveReturnTypes |
| 201 | + |
| 202 | + @hl.scala |
| 203 | + // Column limit | |
| 204 | + // true |
| 205 | + implicit def validatedInstances[E](implicit E: Semigroup[E]) |
| 206 | + : Traverse[Validated[E, ?]] with ApplicativeError[Validated[E, ?], E] = 2 |
| 207 | + // false |
| 208 | + implicit def validatedInstances[E](implicit E: Semigroup[E]): Traverse[ |
| 209 | + Validated[E, ?]] with ApplicativeError[Validated[E, ?], E] = 2 |
| 210 | + |
| 211 | + @sect{--alignByArrowEnumeratorGenerator} |
| 212 | + Default: @default.alignByArrowEnumeratorGenerator |
| 213 | + |
| 214 | + @hl.scala |
| 215 | + // false |
| 216 | + for { |
| 217 | + x <- new Integer { |
| 218 | + def value = 2 |
| 219 | + } |
| 220 | + } yield x |
| 221 | + |
| 222 | + // true |
| 223 | + for { |
| 224 | + x <- new Integer { |
| 225 | + def value = 2 |
| 226 | + } |
| 227 | + } yield x |
| 228 | + |
| 229 | + @sect{--binPackParentConstructors} |
| 230 | + Default: @default.binPackParentConstructors |
| 231 | + |
| 232 | + @hl.scala |
| 233 | + // false |
| 234 | + for { |
| 235 | + x <- new Integer { |
| 236 | + def value = 2 |
| 237 | + } |
| 238 | + } yield x |
| 239 | + |
| 240 | + // true |
| 241 | + for { |
| 242 | + x <- new Integer { |
| 243 | + def value = 2 |
| 244 | + } |
| 245 | + } yield x |
| 246 | + @sect{--alignByOpenParenCallSite} |
| 247 | + Default: @default.alignByOpenParenCallSite |
| 248 | + |
| 249 | + @hl.scala |
| 250 | + // Column limit | |
| 251 | + // true |
| 252 | + foo(arg1, arg2) |
| 253 | + |
| 254 | + function(arg1, // align by ( |
| 255 | + arg2, |
| 256 | + arg3) |
| 257 | + function( |
| 258 | + argument1, |
| 259 | + argument2) |
| 260 | + |
| 261 | + // false |
| 262 | + foo(arg1, arg2) |
| 263 | + function( // no align by ( |
| 264 | + arg1, |
| 265 | + arg2, |
| 266 | + arg3) |
| 267 | + function( |
| 268 | + argument1, |
| 269 | + argument2) |
0 commit comments