Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Config: move all methods into ScalafmtConfig #3680

Merged
merged 1 commit into from
Nov 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions scalafmt-cli/src/main/scala/org/scalafmt/cli/CliOptions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import java.io.{InputStream, OutputStream, PrintStream}
import java.nio.file.{Files, NoSuchFileException, Path}

import metaconfig.Configured
import org.scalafmt.config.{Config, ConfParsed, ScalafmtConfig}
import org.scalafmt.config.{ConfParsed, ScalafmtConfig}
import org.scalafmt.sysops.{AbsoluteFile, GitOps, OsSpecific}

import scala.io.Codec
Expand Down Expand Up @@ -145,7 +145,9 @@ case class CliOptions(
* will return the default configuration
*/
def scalafmtConfig: Configured[ScalafmtConfig] =
hoconOpt.fold(Configured.ok(baseConfig))(Config.fromConf(_, baseConfig))
hoconOpt.fold(Configured.ok(baseConfig))(
ScalafmtConfig.fromConf(_, baseConfig)
)

private[cli] lazy val hoconOpt: Option[ConfParsed] =
configStr.map(ConfParsed.fromString(_)).orElse {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import scala.util.Failure
import scala.util.Success
import scala.util.Try

import org.scalafmt.config.Config
import org.scalafmt.Error.PreciseIncomplete
import org.scalafmt.config.FormatEvent.CreateFormatOps
import org.scalafmt.config.LineEndings
Expand Down Expand Up @@ -191,11 +190,14 @@ object Scalafmt {

// used by ScalafmtReflect.parseConfig
def parseHoconConfigFile(configPath: Path): Configured[ScalafmtConfig] =
Config.fromHoconFile(configPath, ScalafmtConfig.uncheckedDefault)
ScalafmtConfig.fromHoconFile(configPath, ScalafmtConfig.uncheckedDefault)

// used by ScalafmtReflect.parseConfig
def parseHoconConfig(configString: String): Configured[ScalafmtConfig] =
Config.fromHoconString(configString, ScalafmtConfig.uncheckedDefault)
ScalafmtConfig.fromHoconString(
configString,
ScalafmtConfig.uncheckedDefault
)

/** Utility method to change dialect on ScalafmtConfig.
*
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.scalafmt.config

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

import scala.collection.mutable
import scala.io.Codec
Expand All @@ -18,6 +19,7 @@ import org.scalafmt.sysops.FileOps
import org.scalafmt.sysops.OsSpecific._
import org.scalafmt.util.LoggerOps
import org.scalafmt.util.ValidationOps
import org.scalafmt.Versions

/** Configuration options for scalafmt.
*
Expand Down Expand Up @@ -198,7 +200,7 @@ case class ScalafmtConfig(
eitherPat -> cfg
}
val langResult = patStyles.collect { case (Left(lang), cfg) => lang -> cfg }
val fs = file.FileSystems.getDefault
val fs = FileSystems.getDefault
val pmResult = patStyles.collect { case (Right(pat), cfg) =>
val pattern = if (pat(0) == '.') "glob:**" + pat else pat.asFilename
fs.getPathMatcher(pattern) -> cfg
Expand Down Expand Up @@ -492,4 +494,38 @@ object ScalafmtConfig {
.andThen(validate)
}

def fromHoconString(
string: String,
default: ScalafmtConfig = ScalafmtConfig.default,
path: Option[String] = None
): Configured[ScalafmtConfig] =
fromConf(ConfParsed.fromString(string, path), default = default)

/** Read ScalafmtConfig from String contents from an optional HOCON path. */
def fromHoconFile(
file: Path,
default: ScalafmtConfig = ScalafmtConfig.default,
path: Option[String] = None
): Configured[ScalafmtConfig] =
fromConf(ConfParsed.fromPath(file, path), default = default)

def fromConf(
parsed: ConfParsed,
default: ScalafmtConfig
): Configured[ScalafmtConfig] = {
ScalafmtConfig.decoder.read(Option(default), parsed.conf) match {
case Configured.Ok(x)
if default.version == null &&
x.version != Versions.stable && x.version != Versions.version =>
val version = Option(x.version).getOrElse("missing")
val expected = s"${Versions.stable} or ${Versions.version}"
Configured.error(s"version [expected $expected]: $version")
case Configured.Ok(x)
if default.eq(ScalafmtConfig.uncheckedDefault) &&
x.runner.isDefaultDialect =>
Configured.error(NamedDialect.getUnknownError)
case x => x
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import scala.meta.tokens.Token.LeftParen
import scala.meta.tokens.Token.RightParen

import org.scalafmt.config.BinPack
import org.scalafmt.config.Config
import org.scalafmt.config.FilterMatcher
import org.scalafmt.config.ScalafmtConfig
import org.scalafmt.internal.FormatToken
Expand Down Expand Up @@ -50,7 +49,8 @@ class StyleMap(
}
tok.left match {
case Comment(c) if prefix.findFirstIn(c).isDefined =>
val configured = Config.fromHoconString(c, init, Some("scalafmt"))
val configured =
ScalafmtConfig.fromHoconString(c, init, Some("scalafmt"))
// TODO(olafur) report error via callback
configured.foreach(logger.elem(_)) { style =>
init.rewrite.rulesChanged(style.rewrite).foreach { x =>
Expand Down
3 changes: 1 addition & 2 deletions scalafmt-docs/src/main/scala/docs/ScalafmtModifier.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package docs

import org.scalafmt.Scalafmt
import org.scalafmt.config.Config
import org.scalafmt.config.NamedDialect
import org.scalafmt.config.ScalafmtConfig
import scala.meta.inputs.Input
Expand Down Expand Up @@ -32,7 +31,7 @@ class ScalafmtModifier extends StringModifier {
} else {
val config = Input.Slice(code, 0, i)
val program = Input.Slice(code, i + separator.length, code.chars.length)
val configured = Config.fromHoconString(config.text, base)
val configured = ScalafmtConfig.fromHoconString(config.text, base)
configured.fold { e =>
reporter.error(pos, e.toString())
"fail"
Expand Down
8 changes: 5 additions & 3 deletions scalafmt-docs/src/main/scala/website/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import java.nio.file.StandardOpenOption
import org.scalafmt.Scalafmt
import org.scalafmt.config.ScalafmtRunner
import org.scalafmt.config.ScalafmtConfig
import org.scalafmt.config.Config

package object website {
def replaceMargin(s: String): String = {
Expand Down Expand Up @@ -70,7 +69,10 @@ package object website {
): Unit = {
val processedCode = preProcess(code)
val parsedConfig1 =
Config.fromHoconString(config.mkString("\n")).get.copy(runner = runner)
ScalafmtConfig
.fromHoconString(config.mkString("\n"))
.get
.copy(runner = runner)
val isCustomMaxColumn = config.exists(_.contains("maxColumn"))
val parsedConfig =
if (isCustomMaxColumn) parsedConfig1
Expand Down Expand Up @@ -103,7 +105,7 @@ package object website {
* the config to format the code (defaults to `default40`)
*/
def formatExample(code: String, config: String*): Unit = {
val parsedConfig = Config
val parsedConfig = ScalafmtConfig
.fromHoconString(config.mkString("\n"))
.get
.copy(maxColumn = 40, runner = ScalafmtRunner.sbt)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package org.scalafmt.cli

import java.nio.file.{Files, NoSuchFileException, Path, Paths}

import org.scalafmt.config.{Config, ScalafmtConfig}
import org.scalafmt.config.ScalafmtConfig
import FileTestOps._
import org.scalafmt.Versions

Expand All @@ -12,31 +12,30 @@ import munit.FunSuite
class CliOptionsTest extends FunSuite {

test("preset = ...") {
import org.scalafmt.config.Config
assertEquals(
Config.fromHoconString("preset = foobar"),
ScalafmtConfig.fromHoconString("preset = foobar"),
Configured.error(
"Unknown style \"foobar\". Expected one of: " +
"Scala.js, IntelliJ, default, defaultWithAlign"
)
)

assertEquals(
Config.fromHoconString("""|preset = defaultWithAlign
ScalafmtConfig.fromHoconString("""|preset = defaultWithAlign
|maxColumn = 100
|""".stripMargin),
Configured.ok(ScalafmtConfig.defaultWithAlign.copy(maxColumn = 100))
)
assertEquals(
Config.fromHoconString("preset = intellij"),
ScalafmtConfig.fromHoconString("preset = intellij"),
Configured.ok(ScalafmtConfig.intellij)
)
assertEquals(
Config.fromHoconString("preset = Scala.js"),
ScalafmtConfig.fromHoconString("preset = Scala.js"),
Configured.ok(ScalafmtConfig.scalaJs)
)
assertEquals(
Config.fromHoconString("preset = defaultWithAlign"),
ScalafmtConfig.fromHoconString("preset = defaultWithAlign"),
Configured.ok(ScalafmtConfig.defaultWithAlign)
)
}
Expand All @@ -52,7 +51,7 @@ class CliOptionsTest extends FunSuite {
)
)
.configPath
val config = Config.fromHoconFile(path).get
val config = ScalafmtConfig.fromHoconFile(path).get
assertEquals(config.onTestFailure, expected)
}

Expand Down
4 changes: 2 additions & 2 deletions scalafmt-tests/src/test/scala/org/scalafmt/cli/CliTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import munit.FunSuite
import org.scalafmt.Error.NoMatchingFiles
import org.scalafmt.Versions.{stable => stableVersion}
import org.scalafmt.cli.FileTestOps._
import org.scalafmt.config.{Config, ProjectFiles, ScalafmtConfig}
import org.scalafmt.config.{ProjectFiles, ScalafmtConfig}
import org.scalafmt.sysops.{AbsoluteFile, FileOps}
import org.scalafmt.sysops.OsSpecific._

Expand Down Expand Up @@ -80,7 +80,7 @@ abstract class AbstractCliTest extends FunSuite {
|""".stripMargin

def gimmeConfig(string: String): ScalafmtConfig =
Config.fromHoconString(string).get
ScalafmtConfig.fromHoconString(string).get

def noArgTest(
input: AbsoluteFile,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import munit.FunSuite
class ScalafmtConfigTest extends FunSuite {

test("project.matcher") {
val config = Config
val config = ScalafmtConfig
.fromHoconString(
"""
|project.excludeFilters = [
Expand All @@ -21,7 +21,7 @@ class ScalafmtConfigTest extends FunSuite {
}

test("file overrides") {
val config = Config
val config = ScalafmtConfig
.fromHoconString(
"""
|newlines.source = fold
Expand All @@ -47,7 +47,7 @@ class ScalafmtConfigTest extends FunSuite {
}

test("align preset no override") {
val config = Config
val config = ScalafmtConfig
.fromHoconString("""
|align = none
|align.stripMargin = true
Expand All @@ -58,7 +58,7 @@ class ScalafmtConfigTest extends FunSuite {
}

test("align preset with override") {
val config = Config
val config = ScalafmtConfig
.fromHoconString("""
|align.preset = none
|align.stripMargin = true
Expand All @@ -68,13 +68,13 @@ class ScalafmtConfigTest extends FunSuite {
}

test("dialect override") {
val config1 = Config
val config1 = ScalafmtConfig
.fromHoconString("""
|runner.dialect = scala213
|""".stripMargin)
.get
assert(!config1.runner.getDialect.allowToplevelTerms)
val config2 = Config
val config2 = ScalafmtConfig
.fromHoconString("""
|runner.dialectOverride.allowToplevelTerms = true
|runner.dialect = scala213
Expand All @@ -84,7 +84,7 @@ class ScalafmtConfigTest extends FunSuite {
}

test("hasRewriteRules-and-withoutRewriteRules trailingCommas") {
val config1 = Config
val config1 = ScalafmtConfig
.fromHoconString("""
|runner.dialect = scala213
|rewrite.trailingCommas = never
Expand All @@ -96,7 +96,7 @@ class ScalafmtConfigTest extends FunSuite {
}

test("hasRewriteRules-and-withoutRewriteRules docstrings") {
val config1 = Config
val config1 = ScalafmtConfig
.fromHoconString("""
|runner.dialect = scala213
|rewrite.trailingCommas = keep
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import munit.Assertions._
import org.scalafmt.{Debug, Scalafmt}
import org.scalafmt.config.FormatEvent._
import org.scalafmt.config.{
Config,
DanglingParentheses,
Indents,
NamedDialect,
Expand Down Expand Up @@ -93,7 +92,7 @@ trait HasTests extends FormatAssertions {
val moduleSkip = isSkip(head)

def loadStyle(cfg: String, base: ScalafmtConfig): ScalafmtConfig =
Config.fromHoconString(cfg, base).getOrRecover { c =>
ScalafmtConfig.fromHoconString(cfg, base).getOrRecover { c =>
throw new IllegalArgumentException(
s"""Failed to parse filename $filename:
|$cfg
Expand Down
Loading