Skip to content

Commit

Permalink
Build: Initial scalafmt config (WIP)
Browse files Browse the repository at this point in the history
  • Loading branch information
raquo committed Nov 18, 2024
1 parent b15036b commit f91e220
Show file tree
Hide file tree
Showing 39 changed files with 409 additions and 196 deletions.
168 changes: 168 additions & 0 deletions .scalafmt.conf
Original file line number Diff line number Diff line change
@@ -0,0 1,168 @@
version = "3.8.3"
runner.dialect = "scala213"

# !!! TODO !!! Update styles in the generator to match scalafmt config
# - Maybe make a separate config for these files, that has two lines before method names, see https://scalameta.org/scalafmt/docs/configuration.html#fileoverride
# - Does this exclusion actually work? IntelliJ seems to ignore it
project.excludePaths = [
"glob:**/src/main/scala/com/raquo/laminar/defs/**",
"glob:**/project/VersionHelper.scala",
"glob:**/src/test/**", # !!! TODO - sort out binpacking, mostly
]

maxColumn = 1000

indent.main = 2
indent.callSite = 2
indent.binPackCallSite = 2
indent.ctrlSite = 2
indent.defnSite = 2
indent.binPackDefnSite = 2
indent.ctorSite = 2
indent.matchSite = 2
indent.caseSite = 2
indent.extendSite = 2
indent.withSiteRelativeToExtends = 0
indent.commaSiteRelativeToExtends = 2 # 0 is not allowed
indent.extraBeforeOpenParenDefnSite = 0
indent.relativeToLhsLastLine = [match, infix]
indent.fewerBraces = never
indent.afterInfixSite = 2 # not sure what this is https://scalameta.org/scalafmt/docs/configuration.html#indentafterinfixsite

# Indenting of nested calls:
# !!! #TODO !!!: https://scalameta.org/scalafmt/docs/configuration.html#indent-for-binpackcallsite
# !!! #TODO !!!: https://scalameta.org/scalafmt/docs/configuration.html#indentoperator

align.preset = none

# TODO: alignment can be more detailed but requires knowing Scala types
# https://scalameta.org/scalafmt/docs/configuration.html#aligntokens

align.arrowEnumeratorGenerator = false
align.closeParenSite = false
align.openParenCallSite = false
align.openBracketCallSite = false
align.openParenCtrlSite = false
align.openParenDefnSite = false
align.openBracketDefnSite = false
align.openParenCallSite = false
align.beforeOpenParenCallSite = false # ??? https://scalameta.org/scalafmt/docs/configuration.html#alignbeforeopenparenxxxsite
align.beforeOpenParenDefnSite = false # ??? https://scalameta.org/scalafmt/docs/configuration.html#alignbeforeopenparenxxxsite
align.stripMargin = true
align.multiline = false
align.allowOverflow = true # Not sure, it's unused since alignment is disabled https://scalameta.org/scalafmt/docs/configuration.html#alignallowoverflow
align.inInterpolation = false
align.delayUntilSpace = true

newlines.source = keep
# TODO: ??? https://scalameta.org/scalafmt/docs/configuration.html#newlinestoplevelstatementblanklines
newlines.topLevelStatementBlankLines = []
newlines.topLevelBodyIfMinStatements = []
newlines.beforeTemplateBodyIfBreakInParentCtors = false # TODO not sure https://scalameta.org/scalafmt/docs/configuration.html#newlinesbeforetemplatebodyifbreakinparentctors
newlines.beforeMultiline = keep # !!! TODO not sure, maybe `unfold` https://scalameta.org/scalafmt/docs/configuration.html#newlinesbeforemultiline
newlines.forceBeforeMultilineAssign = never
newlines.forceBeforeAssign = never
newlines.beforeTypeBounds = keep
newlines.alwaysBeforeElseAfterCurlyIf = false
newlines.beforeCurlyLambdaParams = never
newlines.afterCurlyLambdaParams = keep
newlines.implicitParamListModifierPrefer = after # TODO I want this to be before, but scalafmt does not work well with it
# newlines.implicitParamListModifierForce = [after] # !!! TODO not sure
newlines.afterInfix = keep
newlines.afterInfixBreakOnNested = false # ??? https://scalameta.org/scalafmt/docs/configuration.html#newlinesafterinfixbreakonnested
newlines.avoidForSimpleOverflow = [tooLong, punct, slc]
newlines.avoidInResultType = true
newlines.sometimesBeforeColonInMethodReturnType = false
# newlines.beforeOpenParenDefnSite = keep # TODO not sure https://scalameta.org/scalafmt/docs/configuration.html#newlinesbeforeopenparenxxxsite
# newlines.beforeOpenParenCallSite = keep # TODO only supported for Scala 3. Also, not sure... https://scalameta.org/scalafmt/docs/configuration.html#newlinesbeforeopenparenxxxsite
newlines.selectChains = keep
newlines.inInterpolation = avoid # TODO not sure https://scalameta.org/scalafmt/docs/configuration.html#newlinesininterpolation
newlines.ignoreInSyntax = true # TODO very not sure https://scalameta.org/scalafmt/docs/configuration.html#newlinesignoreinsyntax

optIn.annotationNewlines = true # TODO not sure https://scalameta.org/scalafmt/docs/configuration.html#optinannotationnewlines

danglingParentheses.defnSite = true
danglingParentheses.callSite = true
danglingParentheses.bracketDefnSite = true
danglingParentheses.bracketCallSite = true
danglingParentheses.ctrlSite = true
danglingParentheses.tupleSite = true
danglingParentheses.exclude = [] # TODO not sure https://scalameta.org/scalafmt/docs/configuration.html#danglingparenthesesexclude
# !!! TODO not sure how !!! newlines.configStyleXxxSite.prefer https://scalameta.org/scalafmt/docs/configuration.html#newlinesconfigstylexxxsiteprefer

# prefercurlyfors, redundantparens, redundantbraces, sortmodifiers, sortimports, expandimportselectors, imports, avoidinfix, asciisortimports

rewrite.rules = [SortModifiers, PreferCurlyFors, Imports]
# rewrite.sortModifiers.preset = styleGuide # TODO consider removing (changes position of `final` keyword) https://scalameta.org/scalafmt/docs/configuration.html#sortmodifiers
rewrite.sortModifiers.order = [ # TODO maybe a custom order...
"implicit", "final", "sealed", "abstract",
"override", "private", "protected", "lazy"
]
rewrite.preferCurlyFors.removeTrailingSemicolonsOnly = true # TODO not sure https://scalameta.org/scalafmt/docs/configuration.html#prefercurlyfors
rewrite.imports.expand = false
rewrite.imports.sort = scalastyle # TODO not sure - change this later https://scalameta.org/scalafmt/docs/configuration.html#imports-sort--ascii
rewrite.imports.groups = [ # TODO not sure
[".*"], # Other imports first
["java\\..*", "scala\\..*", "scala.scalajs\\..*"]
]
rewrite.imports.contiguousGroups = only # TODO not sure
rewrite.trailingCommas.style = keep # TODO consider changing to `never`
rewrite.trailingCommas.allowFolding = true # TODO not sure https://scalameta.org/scalafmt/docs/configuration.html#rewritetrailingcommasallowfolding

# TODO Scala 3 rewrites? https://scalameta.org/scalafmt/docs/configuration.html#scala3-rewrites

# !!!! TODO not sure about any of this !!! Maybe disable completely?
# https://scalameta.org/scalafmt/docs/configuration.html#vertical-multiline
verticalMultiline.atDefnSite = true
verticalMultiline.arityThreshold = 10
verticalMultiline.newlineAfterOpenParen = true

comments.wrap = no
comments.wrapStandaloneSlcAsSlc = true // # TODO not sure because unused
comments.wrapSingleLineMlcAsSlc = false

docstrings.style = keep # !!! TODO not sure, can also be Space, SpaceAsterisk, or AsteriskSpace https://scalameta.org/scalafmt/docs/configuration.html#docstringsstyle
docstrings.removeEmpty = true
docstrings.oneline = keep # TODO not sure. Note: Ignored if docstrings.style = keep https://scalameta.org/scalafmt/docs/configuration.html#docstringsoneline
docstrings.wrap = keep
docstrings.wrapMaxColumn = 100 # TODO change if want it to be different from maxColumn
docstrings.blankFirstLine = keep # !!! TODO not sure, try `fold`
docstrings.forceBlankLineBefore = false # TODO not sure https://scalameta.org/scalafmt/docs/configuration.html#docstringsforceblanklinebefore

spaces.beforeContextBoundColon = never # !!! TODO not sure, Always or IfMultipleBounds may be better style https://scalameta.org/scalafmt/docs/configuration.html#spacesbeforecontextboundcolon
spaces.inImportCurlyBraces = false
spaces.inInterpolatedStringCurlyBraces = false
spaces.inParentheses = false
spaces.neverAroundInfixTypes = []
spaces.aroundSymbolicInfixOperators.include = [".*"]
spaces.aroundSymbolicInfixOperators.exclude = []
spaces.afterKeywordBeforeParen = true
spaces.inByNameTypes = true
spaces.afterSymbolicDefs = false
spaces.beforeApplyArgInParens = AfterSymbolic # TODO not sure https://scalameta.org/scalafmt/docs/configuration.html#spacesbeforexxxarginparens
spaces.beforeInfixArgInParens = always
spaces.afterColonInMatchPattern = always

literals.long = Upper
literals.float = Lower
literals.double = Lower
literals.hexPrefix = Lower
literals.hexDigits = Lower # Not sure, unused https://scalameta.org/scalafmt/docs/configuration.html#literalshexdigits
literals.scientific = Lower

xmlLiterals.assumeFormatted = false

# !!! TODO Binpacking !!! https://scalameta.org/scalafmt/docs/configuration.html#binpacking

# !!! TODO Classic select chains !!! https://scalameta.org/scalafmt/docs/configuration.html#classic-select-chains
# - I don't think this applies to us, it's only for when newlines.source = classic, but we have `keep`

# lineEndings = unix # TODO: Is there a difference between omitting it and setting `unix`? https://scalameta.org/scalafmt/docs/configuration.html#lineendings

rewriteTokens = {
"⇒": "=>"
"→": "->"
"←": "<-"
}

importSelectors = binPack # TODO Not sure, is there a `keep` option? https://scalameta.org/scalafmt/docs/configuration.html#importselectors
25 changes: 13 additions & 12 deletions project/DomDefsGenerator.scala
Original file line number Diff line number Diff line change
@@ -1,21 1,22 @@
import com.raquo.domtypes.codegen.DefType.LazyVal
import com.raquo.domtypes.codegen.{CanonicalCache, CanonicalDefGroups, CanonicalGenerator, CodeFormatting, SourceRepr}
import com.raquo.domtypes.codegen.DefType.LazyVal
import com.raquo.domtypes.common.{HtmlTagType, SvgTagType}
import com.raquo.domtypes.defs.styles.StyleTraitDefs

object DomDefsGenerator {

private object generator extends CanonicalGenerator(
baseOutputDirectoryPath = "src/main/scala/com/raquo/laminar",
basePackagePath = "com.raquo.laminar",
standardTraitCommentLines = List(
"#NOTE: GENERATED CODE",
s" - This file is generated at compile time from the data in Scala DOM Types",
" - See `project/DomDefsGenerator.scala` for code generation params",
" - Contribute to https://github.com/raquo/scala-dom-types to add missing tags / attrs / props / etc.",
),
format = CodeFormatting()
) {
private object generator
extends CanonicalGenerator(
baseOutputDirectoryPath = "src/main/scala/com/raquo/laminar",
basePackagePath = "com.raquo.laminar",
standardTraitCommentLines = List(
"#NOTE: GENERATED CODE",
s" - This file is generated at compile time from the data in Scala DOM Types",
" - See `project/DomDefsGenerator.scala` for code generation params",
" - Contribute to https://github.com/raquo/scala-dom-types to add missing tags / attrs / props / etc.",
),
format = CodeFormatting()
) {

override def settersPackagePath: String = basePackagePath ".modifiers.KeySetter"

Expand Down
2 changes: 2 additions & 0 deletions project/plugins.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 8,7 @@ addSbtPlugin("com.github.sbt" % "sbt-git" % "2.0.1")

addSbtPlugin("org.scalameta" % "sbt-mdoc" % "2.5.4")

// addSbtPlugin("org.scalameta" % "sbt-scalafmt" % "2.5.2")

// #TODO Removed pending https://github.com/typelevel/sbt-tpolecat/issues/102
// addSbtPlugin("io.github.davidgregory084" % "sbt-tpolecat" % "0.4.11")
Loading

0 comments on commit f91e220

Please sign in to comment.