From f55b0cdc92d95033b377b284159a8801dcd9c05a Mon Sep 17 00:00:00 2001 From: Nikita Gazarov Date: Fri, 16 Aug 2024 05:10:41 -0700 Subject: [PATCH] Misc: Initial scalafmt config (WIP) --- .scalafmt.conf | 168 ++++++++++++++++++ project/DomDefsGenerator.scala | 25 +-- project/plugins.sbt | 2 + src/main/scala/com/raquo/laminar/DomApi.scala | 89 ++++++---- src/main/scala/com/raquo/laminar/Seq.scala | 2 +- .../com/raquo/laminar/api/Implicits.scala | 42 ++--- .../scala/com/raquo/laminar/api/Laminar.scala | 41 +++-- .../raquo/laminar/api/LaminarAliases.scala | 18 +- .../com/raquo/laminar/api/MountHooks.scala | 7 +- .../scala/com/raquo/laminar/api/package.scala | 4 +- .../laminar/inputs/InputController.scala | 11 +- .../laminar/inserters/ChildInserter.scala | 2 +- .../laminar/inserters/ChildTextInserter.scala | 2 +- .../inserters/ChildrenCommandInserter.scala | 3 +- .../laminar/inserters/ChildrenInserter.scala | 5 +- .../raquo/laminar/inserters/Inserter.scala | 1 - .../inserters/StaticChildInserter.scala | 4 +- .../com/raquo/laminar/keys/AriaAttr.scala | 4 +- .../com/raquo/laminar/keys/CompositeKey.scala | 7 +- .../raquo/laminar/keys/DerivedStyleProp.scala | 4 +- .../raquo/laminar/keys/EventProcessor.scala | 16 +- .../com/raquo/laminar/keys/HtmlAttr.scala | 4 +- .../com/raquo/laminar/keys/HtmlProp.scala | 7 +- .../laminar/keys/LockedCompositeKey.scala | 11 +- .../raquo/laminar/keys/LockedEventKey.scala | 4 +- .../com/raquo/laminar/keys/StyleProp.scala | 6 +- .../com/raquo/laminar/keys/SvgAttr.scala | 4 +- .../laminar/modifiers/EventListener.scala | 41 +++-- .../raquo/laminar/modifiers/KeySetter.scala | 2 +- .../raquo/laminar/modifiers/KeyUpdater.scala | 2 +- .../com/raquo/laminar/nodes/CommentNode.scala | 1 - .../raquo/laminar/nodes/ReactiveElement.scala | 40 +++-- .../laminar/nodes/ReactiveSvgElement.scala | 2 +- .../receivers/ChildOptionReceiver.scala | 4 +- .../laminar/receivers/ChildReceiver.scala | 4 +- .../receivers/ChildrenCommandReceiver.scala | 4 +- .../laminar/receivers/ChildrenReceiver.scala | 8 +- .../com/raquo/laminar/tags/HtmlTag.scala | 2 +- .../scala/com/raquo/laminar/tags/SvgTag.scala | 2 +- 39 files changed, 409 insertions(+), 196 deletions(-) create mode 100644 .scalafmt.conf diff --git a/.scalafmt.conf b/.scalafmt.conf new file mode 100644 index 00000000..20e8079c --- /dev/null +++ b/.scalafmt.conf @@ -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 diff --git a/project/DomDefsGenerator.scala b/project/DomDefsGenerator.scala index b299820e..1ccc97f8 100644 --- a/project/DomDefsGenerator.scala +++ b/project/DomDefsGenerator.scala @@ -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" diff --git a/project/plugins.sbt b/project/plugins.sbt index 3b64a0a7..e7f65086 100644 --- a/project/plugins.sbt +++ b/project/plugins.sbt @@ -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") diff --git a/src/main/scala/com/raquo/laminar/DomApi.scala b/src/main/scala/com/raquo/laminar/DomApi.scala index d5de9229..9f07fb6b 100644 --- a/src/main/scala/com/raquo/laminar/DomApi.scala +++ b/src/main/scala/com/raquo/laminar/DomApi.scala @@ -11,7 +11,7 @@ import org.scalajs.dom import scala.annotation.tailrec import scala.collection.immutable import scala.scalajs.js -import scala.scalajs.js.{JavaScriptException, |} +import scala.scalajs.js.{|, JavaScriptException} /** Low level DOM APIs used by Laminar. * @@ -89,8 +89,9 @@ object DomApi { } } - - /** Tree query functions */ + // + // Tree query functions + // def indexOfChild( parent: dom.Node, @@ -118,15 +119,15 @@ object DomApi { } } - - - /** Events */ + // + // Events + // def addEventListener[Ev <: dom.Event]( element: dom.Element, listener: EventListener[Ev, _] ): Unit = { - //println(s"> Adding listener on ${DomApi.debugNodeDescription(element.ref)} for `${eventPropSetter.key.name}` with useCapture=${eventPropSetter.useCapture}") + // println(s"> Adding listener on ${DomApi.debugNodeDescription(element.ref)} for `${eventPropSetter.key.name}` with useCapture=${eventPropSetter.useCapture}") element.addEventListener( `type` = EventProcessor.eventProp(listener.eventProcessor).name, listener = listener.domCallback, @@ -145,15 +146,17 @@ object DomApi { ) } - - /** HTML Elements */ + // + // HTML Elements + // def createHtmlElement[Ref <: dom.html.Element](tag: HtmlTag[Ref]): Ref = { dom.document.createElement(tag.name).asInstanceOf[Ref] } - - /** HTML Attributes */ + // + // HTML Attributes + // def getHtmlAttribute[V]( element: ReactiveHtmlElement.Base, @@ -202,8 +205,9 @@ object DomApi { element.ref.removeAttribute(attr.name) } - - /** HTML Properties */ + // + // HTML Properties + // /** Returns `js.undefined` when the property is missing on the element. * If the element type supports this property, it should never be js.undefined. @@ -240,8 +244,9 @@ object DomApi { element.ref.asInstanceOf[js.Dynamic].updateDynamic(prop.name)(value.asInstanceOf[js.Any]) } - - /** CSS Style Properties */ + // + // CSS Style Properties + // /** Note: this only gets inline style values – those set via the `style` attribute, which includes * all style props set by Laminar. It does not account for CSS declarations in `