Skip to content

Commit 90c8e09

Browse files
authored
Merge pull request #1127 from tgodzik/fix-3.10
bugfix: Make mdoc work with Scala 3.10.x
2 parents d91ef2a + 800735e commit 90c8e09

7 files changed

Lines changed: 44 additions & 17 deletions

File tree

build.sbt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ addCommandAlias(
77
"interfaces/test;runtime/test;parser/test;cli/test;mdoc/test;testsInput/test;tests/test;jsdocs/test;worksheets/test;unit/test;unitJS/test;jsApi/test;jsWorker/test;js/test;"
88
)
99

10+
Global / resolvers += "scala-nightlies" at
11+
"https://repo.scala-lang.org/artifactory/maven-nightlies"
12+
1013
def scala212 = "2.12.21"
1114
def scala213 = "2.13.18"
1215
def scala3 = "3.3.8"
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package mdoc.modifiers
22

3-
import dotty.tools.io.{AbstractFile, VirtualDirectory}
3+
import dotty.tools.io.{AbstractFile, VirtualDirectory, VirtualDirectoryCompat}
44

55
import mdoc.internal.markdown.MarkdownCompiler
66
import scala.meta.inputs.Input
@@ -9,5 +9,5 @@ import mdoc.internal.pos.TokenEditDistance
99
import mdoc.internal.markdown.FileImport
1010

1111
private[modifiers] object CompilerCompat {
12-
def abstractFile(tg: String) = new VirtualDirectory(tg, None)
12+
def abstractFile(tg: String) = VirtualDirectoryCompat.virtualDirectory(tg)
1313
}

mdoc-js/src/main/scala/mdoc/modifiers/JsModifier.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ class JsModifier extends mdoc.PreModifier {
157157

158158
val hasErrors = ctx.reporter.errorCount > oldErrors
159159

160-
val sjsirFiles = target.toList
160+
val sjsirFiles = target.iterator
161161
.filter(_.name.endsWith(".sjsir"))
162162
.map(file => scalajsApi.get.inMemory(file.path, file.toByteArray))
163163

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package dotty.tools.io
2+
3+
import dotty.tools.io.VirtualDirectory
4+
5+
object VirtualDirectoryCompat {
6+
def virtualDirectory(name: String): VirtualDirectory = new VirtualDirectory(name)
7+
}

mdoc/src/main/scala-3/mdoc/internal/markdown/AbstractClassFileLoader.scala

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,39 @@ import java.util.Collections
1919

2020
class AbstractFileClassLoader(val root: AbstractFile, parent: ClassLoader)
2121
extends ClassLoader(parent):
22-
private def findAbstractFile(name: String) =
23-
root.lookupPath(name.split('/').toIndexedSeq, directory = false)
22+
23+
/** Splits the given path using the given separator char, and finds the corresponding file through
24+
* subdirectories. Optionally adds the given suffix to the last component. This is intended to
25+
* make it easy to find files in formats such as "java/lang/Object" or "java.lang.Object".
26+
*/
27+
final def lookupPath(
28+
path: String,
29+
separator: Char,
30+
lastSuffix: String = "",
31+
directory: Boolean = false
32+
): Option[AbstractFile] =
33+
var file: AbstractFile = root
34+
var idx = 0
35+
var nextStepIdx = -1
36+
while
37+
nextStepIdx = path.indexOf(separator, idx)
38+
nextStepIdx != -1
39+
do
40+
file.lookupName(path.substring(idx, nextStepIdx), directory = true) match
41+
case null => return None
42+
case f =>
43+
file = f
44+
idx = nextStepIdx + 1
45+
Option(file.lookupName(path.substring(idx) + lastSuffix, directory = directory))
46+
end lookupPath
2447

2548
// on JDK 20 the URL constructor we're using is deprecated,
2649
// but the recommended replacement, URL.of, doesn't exist on JDK 8
2750
@annotation.nowarn("cat=deprecation")
2851
override protected def findResource(name: String): URL | Null =
29-
findAbstractFile(name) match
30-
case null => null
31-
case file => new URL(
52+
lookupPath(name, '/') match
53+
case None => null
54+
case Some(file) => new URL(
3255
null,
3356
s"memory:${file.path}",
3457
new URLStreamHandler {

mdoc/src/main/scala-3/mdoc/internal/markdown/Instrumenter.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ object Instrumenter {
198198
def unapply(tree: Tree): Option[List[Name]] =
199199
tree match {
200200
case t: Defn.Val if t.mods.exists(_.isInstanceOf[Lazy]) => Some(Nil)
201-
case t: Tree.WithPats with Defn => Some(t.pats.flatMap(binders))
201+
case t: (Tree.WithPats & Defn) => Some(t.pats.flatMap(binders))
202202
case _: Defn => Some(Nil)
203203
case _: Import => Some(Nil)
204204
case _ => None

mdoc/src/main/scala-3/mdoc/internal/markdown/MarkdownCompiler.scala

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ import dotty.tools.dotc.interfaces.{Diagnostic => IDiagnostic}
3939
import dotty.tools.dotc.reporting._
4040
import dotty.tools.dotc.parsing.Parsers.Parser
4141
import dotty.tools.dotc.Compiler
42-
import dotty.tools.io.{AbstractFile, VirtualDirectory}
42+
import dotty.tools.io.{AbstractFile, VirtualDirectory, VirtualDirectoryCompat}
4343
import dotty.tools.dotc.util.SourceFile
4444

4545
import scala.annotation.implicitNotFound
@@ -64,7 +64,7 @@ class MarkdownDriver(val settings: List[String]) extends Driver {
6464
class MarkdownCompiler(
6565
classpath: String,
6666
val scalacOptions: List[String],
67-
target: AbstractFile = new VirtualDirectory("(memory)")
67+
target: AbstractFile = VirtualDirectoryCompat.virtualDirectory("(memory)")
6868
) {
6969

7070
private val defaultFlags =
@@ -119,11 +119,6 @@ class MarkdownCompiler(
119119
this.getClass.getClassLoader
120120
)
121121

122-
private def clearTarget(): Unit = target match {
123-
case vdir: VirtualDirectory => vdir.clear()
124-
case _ =>
125-
}
126-
127122
private def toSource(input: Input): SourceFile = {
128123
SourceFile.virtual(input.filename, new String(input.chars))
129124
}
@@ -142,7 +137,6 @@ class MarkdownCompiler(
142137
fileImports: List[FileImport]
143138
): Unit = {
144139
reset()
145-
clearTarget()
146140
val compiler = new Compiler
147141
val run = compiler.newRun(using context)
148142
val inputs = List(input)

0 commit comments

Comments
 (0)