Skip to content

Commit

Permalink
large scale refactoring of PluginSettings to minimise boilerplate
Browse files Browse the repository at this point in the history
Aggregate reduction & position into TypeDetail setting
  • Loading branch information
tribbloid committed Nov 4, 2023
1 parent aec96de commit e962bd5
Show file tree
Hide file tree
Showing 17 changed files with 214 additions and 90 deletions.
25 changes: 25 additions & 0 deletions .scalafix.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
runner.dialect = scala213

// Built in rules
rules = [
NoAutoTupling // Rewrite that inserts explicit tuples for adapted argument lists for compatibility with -Yno-adapted-args
NoValInForComprehension
ProcedureSyntax
RemoveUnused
LeakingImplicitClassVal
// TODO: unrealiable due to lack of speculative modification, disabled
// ExplicitResultTypes
]

RemoveUnused {
imports = true
privates = true
locals = true
patternvars = true
params = true
}

ExplicitResultTypes {
rewriteStructuralTypesToNamedSubclass = false
skipSimpleDefinitions = false
}
9 changes: 6 additions & 3 deletions .scalafmt.conf
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
version = "3.7.14"
runner.dialect = scala213


maxColumn = 120
lineEndings = unix

# Only format files tracked by git.
project.git = true
Expand All @@ -28,11 +28,14 @@ newlines {
rewrite {
rules = [
# // AvoidInfix,
Imports,
RedundantBraces,
RedundantParens,
SortModifiers,
SortImports
SortModifiers
]
imports {
sort=original
}
redundantBraces {
generalExpressions = false
methodBodies = false
Expand Down
8 changes: 8 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ plugins {
id("io.github.gradle-nexus.publish-plugin") version "1.3.0"

id("com.github.ben-manes.versions") version "0.49.0"

id("io.github.cosmicsilence.scalafix") version "0.1.14"
}

val sonatypeApiUser = providers.gradleProperty("sonatypeApiUser")
Expand Down Expand Up @@ -233,6 +235,12 @@ allprojects {
}
}

apply(plugin = "io.github.cosmicsilence.scalafix")
scalafix {
semanticdb.autoConfigure.set(true)
semanticdb.version.set("4.8.11")
}

idea {

module {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,11 @@ trait ImplicitsExtension extends TyperCompatViews with typechecker.Implicits {
): SearchResult = {

import ImplicitsHistory._
import PluginSettings.Keys._

def getResult = super.inferImplicit(tree, pt, reportAmbiguous, isView, context, saveAmbiguousDivergent, pos)

if (settings.Vimplicits.value && pluginSettings.implicitDiverging) {
if (settings.Vimplicits.value && `Vimplicits-diverging`.isEnabled) {

val posII = PositionIndex(
tree.pos
Expand All @@ -80,7 +81,7 @@ trait ImplicitsExtension extends TyperCompatViews with typechecker.Implicits {
}

val previousSimilarErrorsN = previousSimilarErrors.size
if (previousSimilarErrorsN >= pluginSettings.implicitDivergingMaxDepth) {
if (previousSimilarErrorsN >= `Vimplicits-diverging-max-depth`.get) {

local.DivergingImplicitErrors.logs +=
s"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ class SplainAnalyzer(val global: Global, val pluginSettings: PluginSettings)
extends typechecker.Analyzer
with SplainFormattingExtension
with ImplicitsExtension
with SplainAnalyzerShim {
with SplainAnalyzerShim
with PluginSettings.Implicits {

override val specialFormatters: List[SpecialFormatter] =
List(
Expand All @@ -24,7 +25,7 @@ class SplainAnalyzer(val global: Global, val pluginSettings: PluginSettings)

val extra = mutable.Buffer.empty[String]

if (pluginSettings.debug) {
if (PluginSettings.Keys.debug.isEnabled) {

extra += "===[ ORIGINAL ERROR ]===" +
builtinFoundReqMsg(found, req) +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ trait SplainFormattingExtension extends typechecker.splain.SplainFormatting with

import SplainFormattingExtension._
import global._
import PluginSettings._

case class SplainImplicitErrorLink(
fromTree: ImplicitError,
Expand Down Expand Up @@ -456,8 +457,9 @@ trait SplainFormattingExtension extends typechecker.splain.SplainFormatting with

def index(): Unit = {

if (pluginSettings.showTypeReduction)
if (TypeDetail.reduction.isEnabled) {
Based += FormattedIndex(element) -> this
}
}

override protected def formattedHeader_Body(break: Boolean): (String, Seq[TypeRepr]) = {
Expand All @@ -474,7 +476,7 @@ trait SplainFormattingExtension extends typechecker.splain.SplainFormatting with

def index(): Unit = {

if (pluginSettings.TypeDiffsDetail.builtInMsg)
if (TypeDiffsDetail.`builtin-msg`.isEnabled)
Based += FormattedIndex(element) -> this
}

Expand All @@ -500,7 +502,7 @@ trait SplainFormattingExtension extends typechecker.splain.SplainFormatting with

def index(): Unit = {

if (pluginSettings.showTypeDefPosition)
if (TypeDetail.position.isEnabled)
Based += FormattedIndex(element) -> this
}

Expand All @@ -519,7 +521,7 @@ trait SplainFormattingExtension extends typechecker.splain.SplainFormatting with
tpe.typeArgs match {
case List(t1, t2) =>
val result =
if (pluginSettings.TypeDiffsDetail.disambiguation) {
if (TypeDiffsDetail.disambiguation.isEnabled) {

withDisambiguation(Nil, t1, t2) {
formatTypeImplNoDisambiguation(tpe, top)
Expand All @@ -533,7 +535,7 @@ trait SplainFormattingExtension extends typechecker.splain.SplainFormatting with
case Infix(ii, left, right, _) =>
val noApparentDiff = (left == right) && (t1 != t2)

if (noApparentDiff || pluginSettings.TypeDiffsDetail.builtInMsgAlways) {
if (noApparentDiff || TypeDiffsDetail.`builtin-msg-always`.isEnabled) {

BuiltInDiffMsg(
result,
Expand Down Expand Up @@ -578,7 +580,7 @@ trait SplainFormattingExtension extends typechecker.splain.SplainFormatting with

override def formatDiffImpl(found: Type, req: Type, top: Boolean): Formatted = {

if (pluginSettings.TypeDiffsDetail.disambiguation) {
if (TypeDiffsDetail.disambiguation.isEnabled) {

val result = withDisambiguation(Nil, found, req) {
formatDiffImplNoDisambiguation(found, req, top)
Expand All @@ -588,7 +590,7 @@ trait SplainFormattingExtension extends typechecker.splain.SplainFormatting with
case diff: Diff =>
val noApparentDiff = (diff.left == diff.right) && (found != req)

if (noApparentDiff || pluginSettings.TypeDiffsDetail.builtInMsgAlways) {
if (noApparentDiff || TypeDiffsDetail.`builtin-msg-always`.isEnabled) {

BuiltInDiffMsg(
diff,
Expand Down
12 changes: 7 additions & 5 deletions core/src/main/scala-2.13.7+/latest/splain/SplainPlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package splain
import scala.tools.nsc._
import scala.tools.nsc.typechecker.{Analyzer, MacroAnnotationNamers}

class SplainPlugin(val global: Global) extends SplainPluginLike {
class SplainPlugin(val global: Global) extends SplainPluginLike with PluginSettings.Implicits {

lazy val pluginSettings: PluginSettings = PluginSettings(this.opts)
override lazy val pluginSettings: PluginSettings = PluginSettings(this.opts)

lazy val splainAnalyzer: SplainAnalyzer =
if (global.settings.YmacroAnnotations.value)
Expand Down Expand Up @@ -47,8 +47,10 @@ class SplainPlugin(val global: Global) extends SplainPluginLike {
}

override def init(options: List[String], error: String => Unit): Boolean = {
def invalid(opt: String) = error(s"splain: invalid option `$opt`")
def setOpt(key: String, value: String) =
def invalid(opt: String): Unit = error(
s"splain: invalid option `$opt`, supported options are ${PluginSettings.nameToKey.map(kv => "`" + kv._1 + "`").mkString(", ")}"
)
def setOpt(key: String, value: String): Unit =
if (opts.contains(key))
opts.update(key, value)
else
Expand All @@ -63,6 +65,6 @@ class SplainPlugin(val global: Global) extends SplainPluginLike {
invalid(opt)
}
}
pluginSettings.enabled
PluginSettings.Keys.enabled.get
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ trait SplainPluginLike extends plugins.Plugin {
val description = "better types and implicit errors"
val components: List[PluginComponent] = Nil

val opts: mutable.Map[String, String] = PluginSettings.inits.to(mutable.Map)
val opts: mutable.Map[String, String] = PluginSettings.nameToInitValue.to(mutable.Map)
}
30 changes: 18 additions & 12 deletions core/src/main/scala-2.13.7+/latest/splain/TyperCompatViews.scala
Original file line number Diff line number Diff line change
Expand Up @@ -92,25 +92,31 @@ trait TyperCompatViews {

def typeToString: String = {

val typeDetail = pluginSettings.typeDetail
import PluginSettings.TypeDetail

def short = self.safeToString

def long = scala.util.Try(self.toLongString).getOrElse(short)
val base = {
if (TypeDetail.long.isEnabled)
scala.util.Try(self.toLongString).getOrElse(short)
else
short
}

def maybeContext = scala.util.Try(existentialContext(self)).toOption
val extraExistential =
if (TypeDetail.existential.isEnabled)
scala.util.Try(existentialContext(self)).toOption
else
None

def maybeAlias = scala.util.Try(explainAlias(self)).toOption
val extraAlias =
if (TypeDetail.alias.isEnabled)
scala.util.Try(explainAlias(self)).toOption
else
None

typeDetail match {
case i if i <= 1 => short
case 2 => long
case 3 =>
(Seq(long) ++ maybeContext).mkString("")
(Seq(base) ++ extraExistential ++ extraAlias).mkString("")

case i if i >= 4 =>
(Seq(long) ++ maybeContext ++ maybeAlias).mkString("")
}
}
}

Expand Down
Loading

0 comments on commit e962bd5

Please sign in to comment.