Skip to content

Commit

Permalink
Config: use SectionRename for renamed sections
Browse files Browse the repository at this point in the history
  • Loading branch information
kitbellew committed Dec 12, 2024
1 parent 35696f2 commit acbedbd
Show file tree
Hide file tree
Showing 12 changed files with 85 additions and 131 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,21 @@ import metaconfig._
* @param owners
* array of owner specs.
*/
case class AlignToken(
code: String,
@annotation.DeprecatedName("owner", "use owners instead", "3.0.0")
owner: String = null,
owners: Seq[AlignToken.Owner] = Seq.empty,
) {
def getMatcher: Seq[AlignToken.Matcher] = {
val specs =
if (owners.isEmpty) Option(owner) match {
case None => Seq.empty
case x => Seq(AlignToken.Owner(x))
}
else owners.distinct
specs.map(_.getMatcher)
}

case class AlignToken(code: String, owners: Seq[AlignToken.Owner] = Seq.empty) {
def getMatcher: Seq[AlignToken.Matcher] = owners.distinct.map(_.getMatcher)
}

object AlignToken {

def apply(code: String, owner: String): AlignToken = {
val owners = Option(owner) match {
case None => Seq.empty
case x => Seq(AlignToken.Owner(x))
}
AlignToken(code, owners)
}

private def pattern(value: String): jurPattern = value.r.pattern

/** @param regex
Expand All @@ -58,6 +54,12 @@ object AlignToken {
protected[scalafmt] val fallbackAlign = new AlignToken("<empty>")
implicit val decoder: ConfDecoderEx[AlignToken] = {
val base = generic.deriveDecoderEx[AlignToken](fallbackAlign).noTypos
.withSectionRenames(
// deprecated since v3.0.0
annotation.SectionRename { case x: Conf.Str =>
Conf.Lst(Conf.Obj("regex" -> x))
}("owner", "owners"),
)
ConfDecoderEx.from {
case (_, Conf.Str("caseArrow")) => Configured.Ok(caseArrow)
case (_, Conf.Str(regex)) => Configured
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ case class AvoidInfixSettings(
// strict match
private[config] val excludeFilters: Seq[AvoidInfixSettings.Filter],
private val excludeScalaTest: Option[Boolean] = None,
excludePlaceholderArg: Option[Boolean] = None,
excludePlaceholderArg: Boolean = true,
excludePostfix: Boolean = false,
excludeMatch: Boolean = true,
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,34 @@ import metaconfig._
* both below the two asterisks of the first line
* - AsteriskSpace: format intermediate lines with an asterisk and a space,
* both below the two asterisks of the first line
* @param forceBlankLineBefore
* If true, always insert a blank line before docstrings, If false, preserves
* blank line only if one exists before. Example:
* {{{
* // before
* object Foo {
* /** Docstring */
* def foo = 2
* }
* // after, if forceBlankLineBefore=false
* object Foo {
* /** Docstring */
* def foo = 2
* }
* // after, if forceBlankLineBefore=true
* object Foo {
*
* /** Docstring */
* def foo = 2
* }
* }}}
*/
case class Docstrings(
oneline: Docstrings.Oneline = Docstrings.Oneline.keep,
removeEmpty: Boolean = false,
wrap: Docstrings.Wrap = Docstrings.Wrap.unfold,
private[config] val wrapMaxColumn: Option[Int] = None,
forceBlankLineBefore: Option[Boolean] = None,
forceBlankLineBefore: Boolean = true,
blankFirstLine: Option[Docstrings.BlankFirstLine] = None,
style: Docstrings.Style = Docstrings.SpaceAsterisk,
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,7 @@ import metaconfig._
* [[https://github.com/scala-js/scala-js/blob/master/CODINGSTYLE.md#long-expressions-with-binary-operators]]
*/
case class IndentOperator(
private val exemptScope: Option[IndentOperator.Exempt] = None,
@annotation.DeprecatedName(
"topLevelOnly",
"Use indentOperator.exemptScope instead (true->topLevelOnly, false->all)",
"3.4.0",
)
private val topLevelOnly: Boolean = true,
exemptScope: IndentOperator.Exempt = IndentOperator.Exempt.oldTopLevel,
@annotation.ExtraName("include")
includeRegex: String = ".*",
@annotation.ExtraName("exclude")
Expand All @@ -68,11 +62,6 @@ case class IndentOperator(

def noindent(op: String): Boolean = excludeRegexp.matcher(op).find() ||
!includeRegexp.matcher(op).find()

lazy val getExemptScope: IndentOperator.Exempt = exemptScope.getOrElse(
if (topLevelOnly) IndentOperator.Exempt.oldTopLevel
else IndentOperator.Exempt.all,
)
}

object IndentOperator {
Expand All @@ -87,7 +76,12 @@ object IndentOperator {
.mapDecoder(generic.deriveDecoderEx(default).noTypos, "indentOperator") {
case Conf.Str("spray" | "akka" | "akka-http") => IndentOperator.akka
case Conf.Str("default") => IndentOperator.default
}
}.withSectionRenames(
// deprecated since v3.4.0
annotation.SectionRename { case Conf.Bool(value) =>
Conf.Str(if (value) "oldTopLevel" else "all")
}("topLevelOnly", "exemptScope"),
)

sealed abstract class Exempt
object Exempt {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,12 +194,6 @@ case class Newlines(
ForceBeforeMultilineAssign.never,
private val forceBeforeMultilineAssign: Option[ForceBeforeMultilineAssign] =
None,
@annotation.DeprecatedName(
"alwaysBeforeMultilineDef",
"Use newlines.forceBeforeMultilineAssign instead",
"3.0.0",
)
private val alwaysBeforeMultilineDef: Boolean = false,
private[config] val beforeMultiline: Option[SourceHints] = None,
@annotation.DeprecatedName(
"beforeMultilineDef",
Expand Down Expand Up @@ -293,8 +287,7 @@ case class Newlines(
lazy val getBeforeMultiline = beforeMultiline.getOrElse(source)
lazy val shouldForceBeforeMultilineAssign = forceBeforeMultilineAssign
.getOrElse {
val useDef = alwaysBeforeMultilineDef ||
beforeMultilineDef.contains(Newlines.unfold)
val useDef = beforeMultilineDef.contains(Newlines.unfold)
if (useDef) ForceBeforeMultilineAssign.`def`
else ForceBeforeMultilineAssign.never
}
Expand Down Expand Up @@ -344,7 +337,12 @@ case class Newlines(
object Newlines {
implicit lazy val surface: generic.Surface[Newlines] = generic.deriveSurface
implicit lazy val codec: ConfCodecEx[Newlines] = generic
.deriveCodecEx(Newlines()).noTypos
.deriveCodecEx(Newlines()).noTypos.withSectionRenames(
// deprecated since v3.0.0
annotation.SectionRename { case Conf.Bool(value) =>
Conf.Str(if (value) "def" else "never")
}("alwaysBeforeMultilineDef", "forceBeforeMultilineAssign"),
)

sealed abstract class IgnoreSourceSplit {
val ignoreSourceSplit: Boolean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,28 +75,6 @@ import metaconfig._
* See https://github.com/scalameta/scalafmt/issues/938 If true, will force a
* line break before a self annotation if there was a line break there
* before.
*
* @param forceBlankLineBeforeDocstring
* If true, always insert a blank line before docstrings, If false, preserves
* blank line only if one exists before. Example:
* {{{
* // before
* object Foo {
* /** Docstring */
* def foo = 2
* }
* // after, if forceBlankLineBeforeDocstring=false
* object Foo {
* /** Docstring */
* def foo = 2
* }
* // after, if forceBlankLineBeforeDocstring=true
* object Foo {
*
* /** Docstring */
* def foo = 2
* }
* }}}
*/
case class OptIn(
@annotation.DeprecatedName(
Expand All @@ -110,13 +88,6 @@ case class OptIn(
encloseClassicChains: Boolean = false,
selfAnnotationNewline: Boolean = true,
annotationNewlines: Boolean = true,
// Candidate to become default false at some point.
@annotation.DeprecatedName(
"forceBlankLineBeforeDocstring",
"Use docstrings.forceBlankLineBefore instead",
"3.4.0",
)
forceBlankLineBeforeDocstring: Boolean = true,
)

object OptIn {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ import org.scalafmt.rewrite._

import metaconfig._

@annotation.SectionRename("neverInfix", "avoidInfix") // renamed in v3.8.0
@annotation.SectionRename(
"allowInfixPlaceholderArg",
"avoidInfix.excludePlaceholderArg",
) // deprecated since v3.8.0
case class RewriteSettings(
rules: Seq[Rewrite] = Nil,
scala3: RewriteScala3Settings = RewriteScala3Settings.default,
Expand All @@ -14,18 +19,8 @@ case class RewriteSettings(
imports: Imports.Settings = Imports.Settings(),
preferCurlyFors: PreferCurlyFors.Settings = PreferCurlyFors.Settings(),
trailingCommas: TrailingCommas = TrailingCommas(),
@annotation.DeprecatedName(
"allowInfixPlaceholderArg",
"Use `avoidInfix.excludePlaceholderArg` instead",
"3.8.0",
)
private val allowInfixPlaceholderArg: Boolean = true,
@annotation.ExtraName("neverInfix")
avoidInfix: AvoidInfixSettings = AvoidInfixSettings.default,
) {
def isAllowInfixPlaceholderArg: Boolean = avoidInfix.excludePlaceholderArg
.getOrElse(allowInfixPlaceholderArg)

def withoutRewrites: RewriteSettings =
copy(rules = Nil, trailingCommas = trailingCommas.withoutRewrites)

Expand Down Expand Up @@ -67,6 +62,7 @@ object RewriteSettings {

implicit lazy val decoder: ConfDecoderEx[RewriteSettings] = generic
.deriveDecoderEx(default).noTypos.flatMap(Imports.validateImports)
.detectSectionRenames

case class InsertBraces(minLines: Int = 0, allBlocks: Boolean = false)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
package org.scalafmt.config

import org.scalafmt.Versions
import org.scalafmt.rewrite.FormatTokensRewrite
import org.scalafmt.rewrite.RedundantBraces
import org.scalafmt.rewrite._
import org.scalafmt.sysops.AbsoluteFile
import org.scalafmt.sysops.OsSpecific._
import org.scalafmt.util.LoggerOps
import org.scalafmt.util.ValidationOps
import org.scalafmt.util._

import scala.meta.Dialect
import scala.meta.Tree
import scala.meta.Type
import scala.meta._

import java.nio.file.FileSystems
import java.nio.file.Path
import java.nio.file._

import scala.collection.mutable
import scala.io.Codec
Expand Down Expand Up @@ -107,6 +102,10 @@ import metaconfig._
* .filter(_ > 2)
* }}}
*/
// scalafmt: { maxColumn = 100 }
@annotation.SectionRename("trailingCommas", "rewrite.trailingCommas.style") // v3.0.5
@annotation.SectionRename("optIn.forceBlankLineBeforeDocstring", "docstrings.forceBlankLineBefore") // v3.4.0
// scalafmt: { maxColumn = 80 }
case class ScalafmtConfig(
version: String = org.scalafmt.Versions.stable,
maxColumn: Int = 80,
Expand Down Expand Up @@ -139,12 +138,6 @@ case class ScalafmtConfig(
"2.5.0",
)
poorMansTrailingCommasInConfigStyle: Boolean = false,
@annotation.DeprecatedName(
"trailingCommas",
"use rewrite.trailingCommas.style instead",
"3.0.5",
)
private val trailingCommas: Option[TrailingCommas.Style] = None,
verticalMultiline: VerticalMultiline = VerticalMultiline(),
verticalAlignMultilineOperators: Boolean = false,
onTestFailure: String = "",
Expand Down Expand Up @@ -277,14 +270,10 @@ case class ScalafmtConfig(

// used in ScalafmtReflectConfig
def withoutRewrites: ScalafmtConfig = copy(
trailingCommas = None,
docstrings = docstrings.withoutRewrites,
rewrite = rewrite.withoutRewrites,
)

lazy val forceNewlineBeforeDocstring: Boolean = docstrings
.forceBlankLineBefore.getOrElse(optIn.forceBlankLineBeforeDocstring)

def breakAfterInfix(tree: => Tree): Newlines.AfterInfix = newlines.afterInfix
.getOrElse {
val useSome = newlines.source == Newlines.classic &&
Expand Down Expand Up @@ -458,6 +447,7 @@ object ScalafmtConfig {
}

private val baseDecoder = generic.deriveDecoderEx(default).noTypos
.detectSectionRenames

implicit final val decoder: ConfDecoderEx[ScalafmtConfig] =
(stateOpt, conf) => {
Expand Down Expand Up @@ -487,12 +477,8 @@ object ScalafmtConfig {
}
case _ => baseDecoder.read(stateOpt, conf)
}
parsed.map { cfg =>
cfg.trailingCommas.fold(cfg) { tc =>
val rt = cfg.rewrite.trailingCommas.copy(style = tc)
cfg.copy(rewrite = cfg.rewrite.copy(trailingCommas = rt))
}
}.andThen(validate)
val res = parsed.andThen(validate)
res
}

def fromHoconString(
Expand Down
Loading

0 comments on commit acbedbd

Please sign in to comment.