Skip to content

Commit

Permalink
Remove dependency on pprint for Scala 3 and make it use default type …
Browse files Browse the repository at this point in the history
…printer an toString
  • Loading branch information
tgodzik committed Sep 9, 2020
1 parent 29dc715 commit 4b51a63
Show file tree
Hide file tree
Showing 27 changed files with 3,236 additions and 103 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ jobs:
- "'++2.11.12 test'"
- "'++2.12.11 test' scripted"
- "'++2.13.2 test'"
- "'++0.26.0-RC1 test'"
- "'++0.26.0 test'"
- "'++0.27.0-RC1 test'"
steps:
- uses: actions/checkout@v1
- uses: olafurpg/setup-scala@v7
Expand Down
4 changes: 4 additions & 0 deletions .scalafmt.conf
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,8 @@ assumeStandardLibraryStripMargin = true

project.excludeFilters = [
"mdoc/src/main/scala-3"
"runtime/src/main/scala-3"
"printing/src/main/scala-3"
"printing/src/main/scala-0.26"
"printing/src/main/scala-0.27"
]
12 changes: 7 additions & 5 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ def crossSetting[A](
inThisBuild(
List(
scalaVersion := scala212,
version := "2.2.7-SNAPSHOT",
crossScalaVersions := List(scala212, scala211, scala213) ::: scala3,
organization := "org.scalameta",
licenses := Seq(
Expand Down Expand Up @@ -110,7 +111,7 @@ lazy val sharedSettings = List(

val V = new {
val scalameta = "4.3.20"
val munit = "0.7.11"
val munit = "0.7.12"
val coursier = "0.0.25"
}

Expand All @@ -121,7 +122,7 @@ lazy val pprintVersion = Def.setting {

lazy val fansiVersion = Def.setting {
if (scalaVersion.value.startsWith("2.11")) "0.2.6"
else "0.2.7"
else "0.2.9"
}

lazy val interfaces = project
Expand All @@ -143,11 +144,13 @@ lazy val runtime = project
.settings(
sharedSettings,
moduleName := "mdoc-runtime",
libraryDependencies += "com.lihaoyi" %% "pprint" % pprintVersion.value,
unmanagedSourceDirectories.in(Compile) ++= multiScalaDirectories("runtime").value,
libraryDependencies ++= crossSetting(
scalaVersion.value,
if2 = List(
"org.scala-lang" % "scala-reflect" % scalaVersion.value % Provided
"com.lihaoyi" %% "pprint" % pprintVersion.value,
"org.scala-lang" % "scala-reflect" % scalaVersion.value % Provided,
"org.scala-lang" % "scala-compiler" % scalaVersion.value % Provided
)
)
)
Expand Down Expand Up @@ -247,7 +250,6 @@ val jsdocs = project
scalaJSLinkerConfig ~= {
_.withModuleKind(ModuleKind.CommonJSModule)
},
crossScalaVersions -= scala3,
libraryDependencies ++= List(
"org.scala-js" %%% "scalajs-dom" % scalajsDom
),
Expand Down
11 changes: 11 additions & 0 deletions mdoc/src/main/scala-2/mdoc/internal/io/ConsoleColors.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package mdoc.internal.io

import fansi.Attrs
import fansi.Color._

case class ConsoleColors(
green: Attrs = Green,
blue: Attrs = Blue,
yellow: Attrs = Yellow,
red: Attrs = Red
)
9 changes: 9 additions & 0 deletions mdoc/src/main/scala-3/mdoc/internal/io/ConsoleColors.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package mdoc.internal.io

case class ConsoleColors(
green: String => String = identity,
blue: String => String = identity,
yellow: String => String = identity,
red: String => String = identity
)

Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import java.nio.file.Path

import dotty.tools.dotc.interfaces.SourcePosition
import dotty.tools.dotc.ast.untpd._
import dotty.tools.dotc.core.Contexts.Context
import dotty.tools.dotc.ast.Trees
import dotty.tools.dotc.core.Flags

Expand Down Expand Up @@ -57,6 +58,7 @@ class Instrumenter(
private def printAsScript(): Unit = {
sections.zipWithIndex.foreach {
case (section, i) =>
import section.ctx
printlnWithIndent("")
printlnWithIndent("$doc.startSection();")
section.stats.foreach { stat =>
Expand All @@ -79,7 +81,7 @@ class Instrumenter(
m: Modifier,
sb: PrintStream,
section: SectionInput
): Unit = {
)(using ctx: Context): Unit = {
val binders = stat match {
case Instrumenter.Binders(names) =>
names
Expand Down Expand Up @@ -140,13 +142,13 @@ object Instrumenter {
}

object Binders {
private def fromPat(trees: List[Tree]) = {
private def fromPat(trees: List[Tree])(using ctx: Context) = {
trees.map {
case id: Ident =>
id.name.toString -> id.sourcePos
}
}
def unapply(tree: Tree): Option[List[(String, SourcePosition)]] =
def unapply(tree: Tree)(using ctx: Context): Option[List[(String, SourcePosition)]] =
tree match {
case df: Trees.ValDef[_] if df.mods.is(Flags.Lazy) => Some(Nil)
case df: Trees.ValDef[_] =>
Expand Down
22 changes: 12 additions & 10 deletions mdoc/src/main/scala-3/mdoc/internal/markdown/SectionInput.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import scala.meta.Source
import scala.meta.inputs.Input
import scala.meta.Name
import mdoc.internal.pos.TokenEditDistance

import mdoc.internal.BuildInfo
import dotty.tools.dotc.interactive.InteractiveDriver

/* The class uses Scala 3 parser.
Expand All @@ -28,9 +28,9 @@ case class SectionInput(input : Input, mod : Modifier, context : MContext){
|}
|""".stripMargin
private val filename = "Section.scala"

driver.run(java.net.URI.create("file:///Section.scala"), SourceFile.virtual(filename, sourceCode))
val source = driver.currentCtx.run.units.head.untpdTree
given ctx as Context = driver.currentCtx
def stats : List[Tree] = {
source match {
case PackageDef(_, List(module @ _ : ModuleDef)) =>
Expand All @@ -42,14 +42,16 @@ case class SectionInput(input : Input, mod : Modifier, context : MContext){
def show(tree : Tree, currentIdent : Int) = {
val str = tree.sourcePos.start
val end = tree.sourcePos.end
// https://github.com/lampepfl/dotty/issues/9495
val prefix = tree match {
case ext: ExtMethods if ext.tparams.nonEmpty =>
"extension ["
case _: ExtMethods =>
"extension ("
case _ => ""
}
// workaround should be removed once support for 0.26.0 is dropped
val prefix = if (BuildInfo.scalaBinaryVersion == "0.26")
tree match {
case ext: ExtMethods if ext.tparams.nonEmpty =>
"extension ["
case _: ExtMethods =>
"extension ("
case _ => ""
}
else ""
val realIdent = " " * (currentIdent - wrapIdent.size)
prefix + sourceCode.substring(str, end).replace("\n", "\n" + realIdent)
}
Expand Down
9 changes: 3 additions & 6 deletions mdoc/src/main/scala/mdoc/internal/io/ConsoleReporter.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package mdoc.internal.io

import fansi.Attrs
import fansi.Color._
import java.io.PrintStream
import java.util.concurrent.atomic.AtomicBoolean
import scala.meta.Position
Expand All @@ -11,12 +9,11 @@ import coursierapi.Logger

class ConsoleReporter(
ps: PrintStream,
green: Attrs = Green,
blue: Attrs = Blue,
yellow: Attrs = Yellow,
red: Attrs = Red
colors: ConsoleColors = ConsoleColors()
) extends Reporter {

import colors._

def formatMessage(pos: Position, severity: String, message: String): String =
pos.formatMessage("", message)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,11 @@ object MarkdownBuilder {
val pathString = path.toString
pathString.contains("scala-library") ||
pathString.contains("scala-reflect") ||
pathString.contains("sourcecode") ||
pathString.contains("fansi") ||
pathString.contains("pprint") ||
pathString.contains("mdoc-interfaces") ||
(pathString.contains("mdoc") && pathString.contains("runtime"))
(pathString.contains("mdoc") && pathString.contains("runtime")) ||
(pathString.contains("mdoc") && pathString.contains("printing"))
})
base ++ runtime
}
Expand All @@ -99,7 +99,8 @@ object MarkdownBuilder {
.iterator
.map(url => AbsolutePath(Paths.get(url.toURI)))
.filter(p => fn(p.toNIO))
Classpath(paths.toList)
.toList
Classpath(paths)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@ import mdoc.internal.markdown.SectionInput
import mdoc.internal.markdown.Modifier
import mdoc.internal.markdown.Instrumenter
import mdoc.internal.markdown.MarkdownBuilder
import mdoc.internal.document.Printing
import mdoc.document.Statement
import mdoc.document.RangePosition
import mdoc.internal.cli.Settings
import pprint.TPrintColors
import pprint.PPrinter.BlackWhite
import mdoc.internal.io.StoreReporter
import mdoc.{interfaces => i}
import java.{util => ju}
Expand Down Expand Up @@ -94,9 +93,7 @@ class WorksheetProvider(settings: Settings) {
.append(": ")
.append(binder.tpeString)
.append(" = ")
BlackWhite
.tokenize(binder.value, width = settings.screenWidth, height = settings.screenHeight)
.foreach(text => out.appendAll(text.getChars))
Printing.print(binder.value, out, settings.screenWidth, settings.screenHeight)
}
}
statement.out.linesIterator.foreach { line =>
Expand Down Expand Up @@ -140,16 +137,8 @@ class WorksheetProvider(settings: Settings) {
.append(": ")
.append(binder.tpeString)
.append(" = ")
val chunk = BlackWhite
.tokenize(binder.value, width = margin - out.length)
.map(_.getChars)
.filterNot(_.iterator.forall(_.isWhitespace))
.flatMap(_.iterator)
.filter {
case '\n' => false
case _ => true
}
out.appendAll(chunk)

Printing.printOneLine(binder.value, out, width = margin - out.length)
out.length > margin
}
}
Expand Down
Loading

0 comments on commit 4b51a63

Please sign in to comment.