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

Scalafmt #303

Merged
merged 1 commit into from
Jan 4, 2022
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
18 changes: 5 additions & 13 deletions .scalafmt.conf
Original file line number Diff line number Diff line change
@@ -1,23 +1,16 @@
version = "3.0.8"
version = "3.3.1"
runner.dialect = scala213
maxColumn = 120
align.preset = most
align.multiline = false
continuationIndent.defnSite = 2
assumeStandardLibraryStripMargin = true
docstrings.style = Asterisk
lineEndings = preserve
includeCurlyBraceInSelectChains = false
danglingParentheses.preset = true
spaces {
inImportCurlyBraces = true
}
optIn.annotationNewlines = true
newlines.alwaysBeforeMultilineDef = false
optIn.configStyleArguments = false
runner.optimizer.forceConfigStyleMinArgCount = 4
danglingParentheses.defnSite = false
danglingParentheses.callSite = false
newlines.implicitParamListModifierPrefer = before

rewrite.rules = [RedundantBraces]
rewrite.rules = [RedundantBraces, AvoidInfix, SortModifiers]

project.excludeFilters = [
"input/",
Expand All @@ -26,5 +19,4 @@ project.excludeFilters = [
"scalafix/output"
]

rewrite.redundantBraces.generalExpressions = false
project.git = true
3 changes: 1 addition & 2 deletions migrate/src/main/scala/migrate/Scala3Migrate.scala
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,7 @@ object Scala3Migrate {
}
val allParsedLibs = libsCompatibleWith213.values.flatten.toSeq
val filteredLibs = allParsedLibs.filterNot(l =>
InitialLib.filteredLibs.exists { case (org, name) => org == l.organization && name == l.name }
)
InitialLib.filteredLibs.exists { case (org, name) => org == l.organization && name == l.name })
MigratedLibsImpl.from(filteredLibs.map(lib => (lib, lib.toCompatible)).toMap)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package migrate.interfaces

import java.nio.file.Path
import java.{ util => jutil }
import java.{util => jutil}

import scala.jdk.CollectionConverters._

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package migrate.interfaces

import java.{ util => jutil }
import java.{util => jutil}

import scala.jdk.CollectionConverters._

Expand Down Expand Up @@ -31,7 +31,7 @@ case class MigratedLibsImpl(
compatible match {
case keptInitialLib: CompatibleWithScala3.Lib =>
initialLib.crossVersion == keptInitialLib.crossVersion &&
initialLib.revision == keptInitialLib.revision
initialLib.revision == keptInitialLib.revision
case _ => false
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package migrate.interfaces

import java.{ util => ju }
import java.{util => ju}

import scala.jdk.CollectionConverters._

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ private[migrate] class FileMigration(fileToMigrate: FileMigrationState.Initial,
// We first try to remove all candidates
val initialStep = CompilationStep(kept = Seq.empty, removed = candidates, necessary = None)

loopUntilCompile(Success(initialStep)) map { case CompilationStep(kept, _, necessary) =>
loopUntilCompile(Success(initialStep)).map { case CompilationStep(kept, _, necessary) =>
CompilingState(kept, necessaryPatches ++ necessary)
}
}
Expand Down
3 changes: 1 addition & 2 deletions migrate/src/main/scala/migrate/internal/InitialLib.scala
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ object InitialLib {
(Organization("org.scalameta"), Name("semanticdb-scalac")) -> Specific3.SemanticDB
)

val macroLibs: Set[(Organization, Name)] = {
val macroLibs: Set[(Organization, Name)] =
// need to complete the list
// the other solution would be to download the src-jar and look for =\w*macro\w
Set(
Expand Down Expand Up @@ -197,7 +197,6 @@ object InitialLib {
Organization("dev.zio") -> Name("zio-macros-core"),
Organization("com.michaelpollmeier") -> Name("macros")
)
}

// Those libs are correctly handled by sbt or scalajs plugin
// Showing them would confuse the user.
Expand Down
3 changes: 1 addition & 2 deletions migrate/src/main/scala/migrate/utils/ScalafixService.scala
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,9 @@ final case class ScalafixService(
if (oneFile.isSuccessful) {
oneFile.previewPatchesAsUnifiedDiff().asScala match {
case None => scribe.debug(s"Nothing to fix in $absPath)")
case Some(_) => {
case Some(_) =>
oneFile.applyPatches()
scribe.info(s"Syntax fixed for $absPath)")
}
}
} else {
val errorMsg = oneFile.getErrorMessage.asScala.getOrElse("Unknown Error")
Expand Down
17 changes: 9 additions & 8 deletions plugin/src/main/scala/migrate/LibsMigration.scala
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package migrate

import ScalaMigratePlugin.{ migrateAPI, scala3Version }
import ScalaMigratePlugin.{migrateAPI, scala3Version}
import ScalaMigratePlugin.Keys._
import Messages._
import interfaceImpl.LibImpl
import migrate.interfaces.{ Lib, MigratedLib, MigratedLibs }
import migrate.interfaces.{Lib, MigratedLib, MigratedLibs}
import sbt.Keys._
import sbt._

import scala.io.AnsiColor._
import scala.util.{ Failure, Success, Try }
import scala.util.{Failure, Success, Try}
import scala.collection.JavaConverters._

private[migrate] object LibsMigration {
Expand Down Expand Up @@ -79,11 +79,12 @@ private[migrate] object LibsMigration {

val spacesForHelp = computeLongestValue(Seq(removedSign, validSign, toBeUpdated))

val help = s"""
|${formatValueWithSpace(removedSign, spacesForHelp)} $RED: Cannot be updated to scala 3$RESET
|${formatValueWithSpace(validSign, spacesForHelp)} $CYAN: Already a valid version for Scala 3$RESET
|${formatValueWithSpace(toBeUpdated, spacesForHelp)} $BLUE: Need to be updated to the following version$RESET
|""".stripMargin
val help =
s"""
|${formatValueWithSpace(removedSign, spacesForHelp)} $RED: Cannot be updated to scala 3$RESET
|${formatValueWithSpace(validSign, spacesForHelp)} $CYAN: Already a valid version for Scala 3$RESET
|${formatValueWithSpace(toBeUpdated, spacesForHelp)} $BLUE: Need to be updated to the following version$RESET
|""".stripMargin

Seq(help, formatIncompatibleLibs, formatValid, formatLibToUpdate)
.filterNot(_.isEmpty)
Expand Down
2 changes: 2 additions & 0 deletions plugin/src/main/scala/migrate/ScalacOptionsMigration.scala
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ private[migrate] object ScalacOptionsMigration {
private val pluginSign = s"""${BOLD}${CYAN}Plugin${RESET}"""
private val spacesHelp = computeLongestValue(Seq(removedSign, renamedSign, sameSign, pluginSign))

// format: off
private val help =
s"""
|${formatValueWithSpace(removedSign, spacesHelp)} $RED: the option is not available is Scala 3$RESET
Expand All @@ -87,6 +88,7 @@ private[migrate] object ScalacOptionsMigration {
|${formatValueWithSpace(pluginSign, spacesHelp)} $CYAN: the option is related to a plugin, previously handled by migrate-libs$RESET
|
|""".stripMargin
// format: on

private def message(
removed: Seq[String],
Expand Down
2 changes: 1 addition & 1 deletion plugin/src/main/scala/migrate/SyntaxMigration.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import sbt.Keys._
import sbt._

import scala.io.AnsiColor._
import scala.util.{ Try, Success, Failure }
import scala.util.{Try, Success, Failure}
import scala.collection.JavaConverters._

private[migrate] object SyntaxMigration {
Expand Down
6 changes: 3 additions & 3 deletions plugin/src/main/scala/migrate/TypeInferenceMigration.scala
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package migrate

import migrate.interfaces.CompilationException
import ScalaMigratePlugin.{ Keys, inputsStore, migrateAPI, scala3Version }
import migrate.TypeInferenceMigration.{ errorMessage, successMessage }
import ScalaMigratePlugin.{Keys, inputsStore, migrateAPI, scala3Version}
import migrate.TypeInferenceMigration.{errorMessage, successMessage}
import sbt.Keys._
import sbt._

import scala.io.AnsiColor._
import scala.util.{ Failure, Success, Try }
import scala.util.{Failure, Success, Try}
import scala.collection.JavaConverters._
import java.nio.file.Files

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,9 @@ class ExplicitImplicitsRule[G <: Global](g: G) {
// at the same position we are supposed to have maximum one ApplyToImplicitArgs
// except it there is also an implicit conversion that takes implicits.
// See Mix.scala example
case g.Apply(fun, args) if !fun.isInstanceOf[g.ApplyImplicitView] => {
case g.Apply(fun, args) if !fun.isInstanceOf[g.ApplyImplicitView] =>
val listOfArgs = args.map(_.symbol.asInstanceOf[g.Symbol])
(fun, listOfArgs)
}
}

}
Expand All @@ -104,7 +103,7 @@ class ExplicitImplicitsRule[G <: Global](g: G) {
// we don't wont to handle this rewrite.
case g.Apply(g.Apply(_, _), _) if original.isInstanceOf[Name] || original.isInstanceOf[Term.Select] =>
None
case g.Apply(_, args) if original.isInstanceOf[Name] || original.isInstanceOf[Term.Select] => {
case g.Apply(_, args) if original.isInstanceOf[Name] || original.isInstanceOf[Term.Select] =>
val isTermEta = original.parent.exists(_.isInstanceOf[Term.Eta])
val etaExpansionArgs = (1 to args.size).map(_ => "_")
if (isTermEta) {
Expand All @@ -120,7 +119,6 @@ class ExplicitImplicitsRule[G <: Global](g: G) {
Patch
.addRight(original, s"(${etaExpansionArgs.mkString(", ")})" + "(" + implicitsParams.mkString(", ") + ")")
)
}
case _ => Some(Patch.addRight(original, "(" + implicitsParams.mkString(", ") + ")"))
}
}
3 changes: 1 addition & 2 deletions scalafix/rules/src/main/scala/migrate/InferTypes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,8 @@ class InferTypes[G <: Global](g: G) {

private def addExplicitResultType()(implicit doc: SemanticDocument, compilerSrv: CompilerService[g.type]): Patch =
doc.tree.collect {
case t @ Defn.Val(_, Pat.Var(name) :: Nil, None, body) => {
case t @ Defn.Val(_, Pat.Var(name) :: Nil, None, body) =>
fixDefinition(t, name, body)
}
case t @ Defn.Var(_, Pat.Var(name) :: Nil, None, Some(body)) =>
fixDefinition(t, name, body)

Expand Down
2 changes: 1 addition & 1 deletion scalafix/rules/src/main/scala/migrate/MigrateRule.scala
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class MigrateRule(g: Global) extends SemanticRule("MigrationRule") {
}

override def fix(implicit doc: SemanticDocument): Patch = {
lazy implicit val compilerService: CompilerService[g.type] = new CompilerService(g, doc)
implicit lazy val compilerService: CompilerService[g.type] = new CompilerService(g, doc)

val inferType = new InferTypes[g.type](g)
val explicitImplicit = new ExplicitImplicitsRule[g.type](g)
Expand Down
2 changes: 1 addition & 1 deletion scalafix/rules/src/main/scala/utils/CompilerService.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package utils
import java.io.File

import scala.reflect.internal.util.SourceFile
import scala.reflect.internal.util.{ Position => ReflectPos }
import scala.reflect.internal.util.{Position => ReflectPos}
import scala.reflect.io.VirtualDirectory
import scala.tools.nsc.Settings
import scala.tools.nsc.interactive.Global
Expand Down
19 changes: 7 additions & 12 deletions scalafix/rules/src/main/scala/utils/PrettyPrinter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package scala.meta.internal.pc

import scala.collection.mutable.ListBuffer
import scala.tools.nsc.interactive.Global
import scala.{ meta => m }
import scala.{meta => m}

import utils.ScalaExtensions.TraversableOnceOptionExtension

Expand All @@ -15,7 +15,7 @@ class PrettyPrinter[G <: Global](val g: G) {
if (filterType(t)) None
else {
t match {
case TypeRef(pre, sym, args) => {
case TypeRef(pre, sym, args) =>
if (sym.isExistentialSkolem) None
else {
val lookUp = context.lookupSymbol(sym.name, _ => true)
Expand All @@ -24,14 +24,12 @@ class PrettyPrinter[G <: Global](val g: G) {
argsOpt.map(a => TypeRef(g.NoPrefix, sym, a))
} else argsOpt.map(a => TypeRef(loop(pre).get, sym, a))
}
}
case SingleType(pre, sym) => {
case SingleType(pre, sym) =>
val lookUp = context.lookupSymbol(sym.name, _ => true)
if (isTheSameSymbol(sym, lookUp, pre))
Some(SingleType(NoPrefix, sym))
else Some(SingleType(loop(pre).get, sym))

}
case ThisType(sym) =>
Some(new PrettyType(lookUpName(sym, context)))
case ConstantType(Constant(_: TermSymbol)) => Some(t)
Expand All @@ -45,7 +43,7 @@ class PrettyPrinter[G <: Global](val g: G) {
scala.util
.Try(ExistentialType(quantified.map(sym => sym.setInfo(loop(sym.info).get)), loop(underlying).get))
.toOption
case PolyType(typeParams, resultType) => {
case PolyType(typeParams, resultType) =>
scala.util
.Try(resultType.map(t => loop(t).get))
.toOption match {
Expand All @@ -56,7 +54,6 @@ class PrettyPrinter[G <: Global](val g: G) {
Some(PolyType(typeParams, otherType))
case None => None
}
}
case NullaryMethodType(resultType) =>
loop(resultType)
case TypeBounds(lo, hi) =>
Expand Down Expand Up @@ -88,11 +85,9 @@ class PrettyPrinter[G <: Global](val g: G) {
private def lookUpName(sym: g.Symbol, context: g.Context): String = {
// first get all owners
val owners = getOwnersFor(sym)
val necessaryOwners = owners.iterator.takeWhile {
case sym => {
val lookUp = context.lookupSymbol(sym.name.toTermName, _ => true)
!isTheSameSymbol(sym, lookUp)
}
val necessaryOwners = owners.iterator.takeWhile { case sym =>
val lookUp = context.lookupSymbol(sym.name.toTermName, _ => true)
!isTheSameSymbol(sym, lookUp)
}.toSeq

val size = necessaryOwners.size
Expand Down