Skip to content

Commit

Permalink
Update Scalafmt to the latest version to enable Scala 3 formatting (#230
Browse files Browse the repository at this point in the history
)

* Updates for scalafmt to support Scala 3

* Try and use snapshot

* Remove extra parens

* Latest scalafmt version

* Update param T to be more Scala like

* Use the correct snapshot version

* Add link scripts and update to scalafmt 3.3.2

* Update format

* Create deliberate wraps for better formatting

* Format rebase code
  • Loading branch information
ekrich authored Jan 24, 2022
1 parent b763478 commit 6629bbc
Show file tree
Hide file tree
Showing 140 changed files with 3,009 additions and 2,861 deletions.
11 changes: 11 additions & 0 deletions .github/workflows/check-link.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
name: Check Lint
on:
push:
branches: [ main ]
pull_request:
jobs:
check-lint:
runs-on: ubuntu-20.04
steps:
- uses: actions/checkout@v2
- run: ./scripts/check-lint.sh
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: CI
on:
push:
branches: [ main ]
branches: [ main ]
pull_request:
jobs:
build:
Expand Down
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,7 @@ target/

# vscode
/.vscode/

# scripts generated
/scripts/.coursier
/scripts/.scalafmt*
35 changes: 22 additions & 13 deletions .scalafmt.conf
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
version = 2.4.2
style = defaultWithAlign
docstrings = JavaDoc
# Test upgrades: $ scripts/scalafmt --test 2> diff.txt
version = 3.3.2
runner.dialect = scala213source3
preset = default

# JavaDoc style (ported from Java)
docstrings.style = Asterisk
assumeStandardLibraryStripMargin = true
project.git = true
# changed 1.6 or after
align.openParenCallSite = true
align.openParenDefnSite = true
# avoid wrapping parens on new line
danglingParentheses.defnSite = false
danglingParentheses.callSite = false
# manually exclude files to format.
project.excludeFilters = [
sharedScala3
]

# This creates less of a diff but is not default
# but is more aligned with Scala.js syntax.
newlines.beforeCurlyLambdaParams = multilineWithCaseOnly

# Keep control sites more streamlined
indent.ctrlSite = 4
danglingParentheses.ctrlSite = false

# allow dialect for Scala 3
fileOverride {
"glob:**/scala-3/**.scala" {
runner.dialect = scala3
}
}
18 changes: 9 additions & 9 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ def versionFmt(out: sbtdynver.GitDescribeOutput): String = {

val dotcOpts = List("-unchecked", "-deprecation", "-feature")
val scalacOpts = dotcOpts ++ List(
//"-Ywarn-unused:imports", // no 2.11 - maybe time for sbt-tpolecat
// "-Ywarn-unused:imports", // no 2.11 - maybe time for sbt-tpolecat
"-Xsource:3"
//"-Xlint:nonlocal-return" // no 2.11/2.12
// "-Xlint:nonlocal-return" // no 2.11/2.12
)

Compile / console / scalacOptions --= Seq(
Expand All @@ -46,9 +46,9 @@ val scala300 = "3.1.0"
val javaTime = "1.1.8"
val scCompat = "2.6.0"

val versionsBase = Seq(scala211, scala212, scala213)
val versionsJVM = versionsBase :+ scala300
val versionsJS = versionsJVM
val versionsBase = Seq(scala211, scala212, scala213)
val versionsJVM = versionsBase :+ scala300
val versionsJS = versionsJVM
val versionsNative = versionsJVM

ThisBuild / scalaVersion := scala212
Expand Down Expand Up @@ -156,12 +156,12 @@ lazy val sconfig = crossProject(JVMPlatform, NativePlatform, JSPlatform)
Test / run / fork := true,
// env vars for tests
Test / envVars ++= Map(
"testList.0" -> "0",
"testList.1" -> "1",
"testList.0" -> "0",
"testList.1" -> "1",
"testClassesPath" -> (Test / classDirectory).value.getPath
),
// uncomment for debugging
//Test / javaOptions += "-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005",
// Test / javaOptions += "-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005",
// mima settings
mimaPreviousArtifacts := Set("org.ekrich" %% "sconfig" % prevVersion),
mimaBinaryIssueFilters ++= ignoredABIProblems
Expand Down Expand Up @@ -236,7 +236,7 @@ lazy val sharedJvmNativeSource: Seq[Setting[_]] = Def.settings(
lazy val sconfigJVM = sconfig.jvm
.dependsOn(testLibJVM % "test->test")
lazy val sconfigNative = sconfig.native
lazy val sconfigJS = sconfig.js
lazy val sconfigJS = sconfig.js

lazy val ignoredABIProblems = {
import com.typesafe.tools.mima.core._
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ class SimpleLibSettings(config: Config) {

// note that these fields are NOT lazy, because if we're going to
// get any exceptions, we want to get them on startup.
val foo = config.getString("simple-lib.foo")
val hello = config.getString("simple-lib.hello")
val foo = config.getString("simple-lib.foo")
val hello = config.getString("simple-lib.hello")
val whatever = config.getString("simple-lib.whatever")
}

Expand Down
12 changes: 6 additions & 6 deletions project/plugins.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@
resolvers += Resolver.sonatypeRepo("snapshots")

// versions
val crossVer = "1.1.0"
val scalaJSVersion = "1.8.0"
val crossVer = "1.1.0"
val scalaJSVersion = "1.8.0"
val scalaNativeVersion = "0.4.3"

// includes sbt-dynver sbt-pgp sbt-sonatype sbt-git
addSbtPlugin("com.github.sbt" % "sbt-ci-release" % "1.5.10")
addSbtPlugin("com.typesafe" % "sbt-mima-plugin" % "1.0.1")
addSbtPlugin("com.github.sbt" % "sbt-ci-release" % "1.5.10")
addSbtPlugin("com.typesafe" % "sbt-mima-plugin" % "1.0.1")

// Scala Native support
addSbtPlugin("org.portable-scala" % "sbt-scala-native-crossproject" % crossVer)
addSbtPlugin("org.scala-native" % "sbt-scala-native" % scalaNativeVersion)
addSbtPlugin("org.scala-native" % "sbt-scala-native" % scalaNativeVersion)

// Scala.js support
addSbtPlugin("org.portable-scala" % "sbt-scalajs-crossproject" % crossVer)
addSbtPlugin("org.scala-js" % "sbt-scalajs" % scalaJSVersion)
addSbtPlugin("org.scala-js" % "sbt-scalajs" % scalaJSVersion)

addSbtPlugin("ch.epfl.scala" % "sbt-scalafix" % "0.9.34")
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ private object ConfigParseOptionsEmptyParenFunCall
"getClassLoader",
"getIncluder",
"getOriginDescription",
"getSyntax")
"getSyntax"
)

private object ConfigRenderOptionsEmptyParenFunCall
extends AbstractEmptyParenFunCall(
Expand All @@ -107,7 +108,8 @@ private object ConfigRenderOptionsEmptyParenFunCall
"getComments",
"getFormatted",
"getJson",
"getOriginComments")
"getOriginComments"
)

private object ConfigResolveOptionsEmptyParenFunCall
extends AbstractEmptyParenFunCall(
Expand All @@ -116,31 +118,38 @@ private object ConfigResolveOptionsEmptyParenFunCall
"getAllowUnresolved",
"getResolver",
"getUseSystemEnvironment",
"noSystem")
"noSystem"
)

private object ConfigDocumentEmptyParenFunCall
extends AbstractEmptyParenFunCall(
Symbol("com/typesafe/config/parser/ConfigDocument#"),
"render")
"render"
)

private object ConfigNodeEmptyParenFunCall
extends AbstractEmptyParenFunCall(
Symbol("com/typesafe/config/parser/ConfigNode#"),
"render")
"render"
)

private abstract class AbstractEmptyParenFunCall(
typesafeConfigSymbol: Symbol,
atLeastOneMethodName: String,
emptyParenMethodNames: String*) {
emptyParenMethodNames: String*
) {
def unapply(tree: Tree)(implicit doc: SemanticDocument): Option[Term] =
EmptyParenFunCallsOnSymbol(typesafeConfigSymbol,
atLeastOneMethodName +: emptyParenMethodNames,
tree)
EmptyParenFunCallsOnSymbol(
typesafeConfigSymbol,
atLeastOneMethodName +: emptyParenMethodNames,
tree
)
}

private object EmptyParenFunCallsOnSymbol {
def apply(symbol: Symbol, methodNames: Seq[String], tree: Tree)(
implicit doc: SemanticDocument): Option[Term] = {
def apply(symbol: Symbol, methodNames: Seq[String], tree: Tree)(implicit
doc: SemanticDocument
): Option[Term] = {

object SelectSymbolEmptyParenMethod {
def unapply(subtree: Tree)(implicit doc: SemanticDocument): Option[Term] =
Expand All @@ -156,12 +165,15 @@ private object EmptyParenFunCallsOnSymbol {
}

object SymbolEmptyParenMethod {
def unapply(term: Term.Name)(
implicit doc: SemanticDocument): Option[MethodSignature] =
def unapply(
term: Term.Name
)(implicit doc: SemanticDocument): Option[MethodSignature] =
PartialFunction.condOpt(term) {
case Term.Name(name) & XSymbol(
XSymbol.Owner(DoesSymbolHaveCorrectType()) & XSignature(
sig: MethodSignature)) if methodNames contains name =>
sig: MethodSignature
)
) if methodNames contains name =>
sig
}
}
Expand Down
38 changes: 23 additions & 15 deletions scalafix/rules/src/main/scala/org/ekrich/sconfig/fix/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ import scala.util.control.NonFatal
*/
package object fix {

/** Allows to apply multiple extractors/matchers to a single expression in a `case` block.
/**
* Allows to apply multiple extractors/matchers to a single expression in a
* `case` block.
*/
object & {
def unapply[A](a: A): Option[(A, A)] = Some((a, a))
Expand Down Expand Up @@ -88,26 +90,31 @@ package object fix {
*/
None
case NonFatal(ex) =>
throw UnexpectedException(sym,
Properties.javaVersion,
Properties.versionString,
ex)
throw UnexpectedException(
sym,
Properties.javaVersion,
Properties.versionString,
ex
)
}
}

object XSemanticType {

/** Tries to infer a result SemanticType of a particular Stat.
/**
* Tries to infer a result SemanticType of a particular Stat.
*
* @note Not all possible cases are handled.
* @note
* Not all possible cases are handled.
*/
def unapply(stat: Stat)(
implicit doc: SemanticDocument): Option[SemanticType] =
def unapply(
stat: Stat
)(implicit doc: SemanticDocument): Option[SemanticType] =
PartialFunction.condOpt(stat) {
case Term.Name(_) & XSymbol(XSignature(ValueSignature(tpe))) => tpe

case Term.Apply(_, _) &
XSymbol(XSignature(ValueSignature(TypeRef(_, funcSym, _ :+ tpe))))
XSymbol(XSignature(ValueSignature(TypeRef(_, funcSym, _ :+ tpe))))
if funcSym.value.startsWith("scala/Function") && funcSym.value
.endsWith("#") =>
tpe
Expand All @@ -117,11 +124,12 @@ package object fix {
}
}

case class UnexpectedException(sym: Symbol,
java: String,
scala: String,
cause: Throwable)
extends RuntimeException(
case class UnexpectedException(
sym: Symbol,
java: String,
scala: String,
cause: Throwable
) extends RuntimeException(
s"""An unexpected exception occurred. Please report this as an issue at https://github.com/ekrich/sconfig/issues and include the following information:
|
|Failing symbol: ${sym.value}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@ package org.ekrich.config
import org.ekrich.config.impl.ConfigBeanImpl

/**
* Factory for automatically creating a Java class from a {@link Config}.
* See {@link ConfigBeanFactory#create}.
* Factory for automatically creating a Java class from a {@link Config}. See
* {@link ConfigBeanFactory#create}.
*
* @since 1.3.0
*/
object ConfigBeanFactory {

/**
* Creates an instance of a class, initializing its fields from a {@link Config}.
* Creates an instance of a class, initializing its fields from a {@link
* Config}.
*
* Example usage:
*
Expand All @@ -20,27 +21,29 @@ object ConfigBeanFactory {
* val config: FooConfig = ConfigBeanFactory.create(configSource, classOf[FooConfig]);
* }}}
*
* The Java class should follow JavaBean conventions. Field types
* can be any of the types you can normally get from a {@link Config},
* including `java.time.Duration` or {@link ConfigMemorySize}.
* Fields may also be another JavaBean-style
* class.
* The Java class should follow JavaBean conventions. Field types can be any
* of the types you can normally get from a {@link Config}, including
* `java.time.Duration` or {@link ConfigMemorySize}. Fields may also be
* another JavaBean-style class.
*
* Fields are mapped to config by converting the config key to
* camel case. So the key `foo-bar` becomes JavaBean
* setter `setFooBar`.
* Fields are mapped to config by converting the config key to camel case. So
* the key `foo-bar` becomes JavaBean setter `setFooBar`.
*
* @since 1.3.0
* @param config source of config information
* @param clazz class to be instantiated
* @param <T> the type of the class to be instantiated
* @return an instance of the class populated with data from the config
* @param config
* source of config information
* @param clazz
* class to be instantiated
* @param [T]
* the type of the class to be instantiated
* @return
* an instance of the class populated with data from the config
* @throws ConfigException#BadBean
* If something is wrong with the JavaBean
* If something is wrong with the JavaBean
* @throws ConfigException#ValidationFailed
* If the config doesn't conform to the bean's implied schema
* If the config doesn't conform to the bean's implied schema
* @throws ConfigException
* Can throw the same exceptions as the getters on {@link Config}
* Can throw the same exceptions as the getters on {@link Config}
*/
def create[T](config: Config, clazz: Class[T]): T =
ConfigBeanImpl.createInternal(config, clazz)
Expand Down
Loading

0 comments on commit 6629bbc

Please sign in to comment.