Skip to content

Commit f35edbb

Browse files
committed
Add and update tests for snippet compiler. Bugfixes found during testing
1 parent 1458e4e commit f35edbb

16 files changed

+300
-99
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
package snippetCompiler
1+
package tests
2+
package snippetCompilerTests
23

34
/**
45
* ```scala sc:compile
@@ -44,15 +45,6 @@ package snippetCompiler
4445
*/
4546
class A {
4647
trait B
47-
val a = new B {
48-
/**
49-
* ```scala sc:compile
50-
* 2 + List()
51-
* ```
52-
*
53-
*/
54-
def a = 3
55-
}
5648
}
5749

5850
/**
@@ -73,52 +65,4 @@ trait Quotes {
7365
*/
7466
def a = 3
7567
}
76-
}
77-
78-
trait Quotes2[A] {
79-
val r1: r1Module[_] = ???
80-
trait r1Module[A] {
81-
type X
82-
object Y {
83-
/**
84-
* ```scala sc:compile
85-
* 2 + List()
86-
* ```
87-
*
88-
*/
89-
type YY
90-
}
91-
val z: zModule = ???
92-
trait zModule {
93-
/**
94-
* ```scala sc:compile
95-
* 2 + List()
96-
* ```
97-
*
98-
*/
99-
type ZZ
100-
}
101-
}
102-
object r2 {
103-
type X
104-
object Y {
105-
/**
106-
* ```scala sc:compile
107-
* 2 + List()
108-
* ```
109-
*
110-
*/
111-
type YY
112-
}
113-
val z: zModule = ???
114-
trait zModule {
115-
/**
116-
* ```scala sc:compile
117-
* 2 + List()
118-
* ```
119-
*
120-
*/
121-
type ZZ
122-
}
123-
}
12468
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package tests.snippetTestcase1
2+
3+
class SnippetTestcase1:
4+
/**
5+
* SNIPPET(OUTERLINEOFFSET:8,OUTERCOLUMNOFFSET:6,INNERLINEOFFSET:3,INNERCOLUMNOFFSET:2)
6+
* ERROR(LINE:8,COLUMN:8)
7+
* ```scala sc:compile
8+
* 2 + List()
9+
* ```
10+
*
11+
*/
12+
def a = 3
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package tests
2+
package snippetTestcase2
3+
4+
trait Quotes2[A] {
5+
val r1: r1Module[_] = ???
6+
trait r1Module[A] {
7+
type X
8+
object Y {
9+
/**
10+
* SNIPPET(OUTERLINEOFFSET:13,OUTERCOLUMNOFFSET:10,INNERLINEOFFSET:6,INNERCOLUMNOFFSET:6)
11+
* ERROR(LINE:13,COLUMN:12)
12+
* ```scala sc:compile
13+
* 2 + List()
14+
* ```
15+
*
16+
*/
17+
type YY
18+
}
19+
val z: zModule = ???
20+
trait zModule {
21+
/**
22+
* SNIPPET(OUTERLINEOFFSET:25,OUTERCOLUMNOFFSET:10,INNERLINEOFFSET:7,INNERCOLUMNOFFSET:6)
23+
* ERROR(LINE:25,COLUMN:12)
24+
* ```scala sc:compile
25+
* 2 + List()
26+
* ```
27+
*
28+
*/
29+
type ZZ
30+
}
31+
}
32+
object r2 {
33+
type X
34+
object Y {
35+
/**
36+
* SNIPPET(OUTERLINEOFFSET:39,OUTERCOLUMNOFFSET:10,INNERLINEOFFSET:5,INNERCOLUMNOFFSET:6)
37+
* ERROR(LINE:39,COLUMN:12)
38+
* ```scala sc:compile
39+
* 2 + List()
40+
* ```
41+
*
42+
*/
43+
type YY
44+
}
45+
val z: zModule = ???
46+
trait zModule {
47+
/**
48+
* SNIPPET(OUTERLINEOFFSET:51,OUTERCOLUMNOFFSET:10,INNERLINEOFFSET:6,INNERCOLUMNOFFSET:6)
49+
* ERROR(LINE:51,COLUMN:12)
50+
* ```scala sc:compile
51+
* 2 + List()
52+
* ```
53+
*
54+
*/
55+
type ZZ
56+
}
57+
}
58+
}

scaladoc/src/dotty/tools/scaladoc/snippets/FlexmarkSnippetProcessor.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ object FlexmarkSnippetProcessor:
3939
val snippetCompilationResult = cf(snippet, lineOffset, argOverride) match {
4040
case result@Some(SnippetCompilationResult(wrapped, _, _, _)) if debug =>
4141
val s = sequence.BasedSequence.EmptyBasedSequence()
42-
.append(wrapped)
42+
.append(wrapped.snippet)
4343
.append(sequence.BasedSequence.EOL)
4444
val content = mdu.BlockContent()
4545
content.add(s, 0)

scaladoc/src/dotty/tools/scaladoc/snippets/SnippetChecker.scala

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,19 @@ import java.io.File
88
class SnippetChecker(val classpath: String, val tastyDirs: Seq[File]):
99
private val sep = System.getProperty("path.separator")
1010
private val cp = List(
11-
System.getProperty("java.class.path"),
12-
Paths.get(classpath).toAbsolutePath,
13-
tastyDirs.map(_.getAbsolutePath()).mkString(sep)
11+
tastyDirs.map(_.getAbsolutePath()).mkString(sep),
12+
classpath,
13+
System.getProperty("java.class.path")
1414
).mkString(sep)
1515

16+
1617
private val compiler: SnippetCompiler = SnippetCompiler(classpath = cp)
1718

19+
// These constants were found empirically to make snippet compiler
20+
// report errors in the same position as main compiler.
21+
private val constantLineOffset = 3
22+
private val constantColumnOffset = 4
23+
1824
def checkSnippet(
1925
snippet: String,
2026
data: Option[SnippetCompilerData],
@@ -27,8 +33,8 @@ class SnippetChecker(val classpath: String, val tastyDirs: Seq[File]):
2733
data.map(_.packageName),
2834
data.fold(Nil)(_.classInfos),
2935
data.map(_.imports).getOrElse(Nil),
30-
lineOffset + data.fold(0)(_.position.line) + 1,
31-
data.fold(0)(_.position.column)
36+
lineOffset + data.fold(0)(_.position.line) + constantLineOffset,
37+
data.fold(0)(_.position.column) + constantColumnOffset
3238
)
3339
val res = compiler.compile(wrapped, arg)
3440
Some(res)

scaladoc/src/dotty/tools/scaladoc/snippets/SnippetCompilationResult.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ case class SnippetCompilerMessage(position: Option[Position], message: String, l
1212
}
1313

1414
case class SnippetCompilationResult(
15-
wrappedSnippet: String,
15+
wrappedSnippet: WrappedSnippet,
1616
isSuccessful: Boolean,
1717
result: Option[AbstractFile],
1818
messages: Seq[SnippetCompilerMessage]

scaladoc/src/dotty/tools/scaladoc/snippets/SnippetCompiler.scala

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import dotty.tools.dotc.util.SourceFile
1919
import dotty.tools.dotc.interfaces.Diagnostic._
2020

2121
class SnippetCompiler(
22-
classpath: String = System.getProperty("java.class.path"), //Probably needs to be done better
22+
classpath: String,
2323
val scalacOptions: String = "",
2424
target: AbstractFile = new VirtualDirectory("(memory)")
2525
):
@@ -46,12 +46,13 @@ class SnippetCompiler(
4646
val line = wrappedSnippet.outerLineOffset
4747
val column = wrappedSnippet.outerColumnOffset
4848
val innerLineOffset = wrappedSnippet.innerLineOffset
49+
val innerColumnOffset = wrappedSnippet.innerColumnOffset
4950
val infos = diagnostics.toSeq.sortBy(_.pos.source.path)
5051
val errorMessages = infos.map {
5152
case diagnostic if diagnostic.position.isPresent =>
5253
val diagPos = diagnostic.position.get
5354
val pos = Some(
54-
Position(diagPos.line + line, diagPos.column + column, diagPos.lineContent, if arg.debug then diagPos.line else diagPos.line - innerLineOffset)
55+
Position(diagPos.line + line - innerLineOffset, diagPos.column + column - innerColumnOffset, diagPos.lineContent, if arg.debug then diagPos.line else diagPos.line - innerLineOffset)
5556
)
5657
val msg = nullableMessage(diagnostic.message)
5758
val level = MessageLevel.fromOrdinal(diagnostic.level)
@@ -92,5 +93,5 @@ class SnippetCompiler(
9293
additionalMessages(wrappedSnippet, arg, context)
9394

9495
val t = Option.when(!context.reporter.hasErrors)(target)
95-
SnippetCompilationResult(wrappedSnippet.snippet, isSuccessful(arg, context), t, messages)
96+
SnippetCompilationResult(wrappedSnippet, isSuccessful(arg, context), t, messages)
9697
}

scaladoc/src/dotty/tools/scaladoc/snippets/SnippetCompilerDataCollector.scala

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class SnippetCompilerDataCollector[Q <: Quotes](val qctx: Q):
1010
object SymOps extends SymOps[qctx.type](qctx)
1111
export SymOps._
1212

13-
def getSnippetCompilerData(sym: Symbol): SnippetCompilerData =
13+
def getSnippetCompilerData(sym: Symbol, originalSym: Symbol): SnippetCompilerData =
1414
val packageName = sym.packageName
1515
if !sym.isPackageDef then sym.tree match {
1616
case c: qctx.reflect.ClassDef =>
@@ -57,9 +57,9 @@ class SnippetCompilerDataCollector[Q <: Quotes](val qctx: Q):
5757
SnippetCompilerData.ClassInfo(classType, classNames, classGenerics)
5858
}
5959
val firstProcessed = allProcessed.head
60-
SnippetCompilerData(packageName, allProcessed.reverse, Nil, position(hackGetPositionOfDocstring(using qctx)(sym)))
61-
case _ => getSnippetCompilerData(sym.maybeOwner)
62-
} else SnippetCompilerData(packageName, Nil, Nil, position(hackGetPositionOfDocstring(using qctx)(sym)))
60+
SnippetCompilerData(packageName, allProcessed.reverse, Nil, position(hackGetPositionOfDocstring(using qctx)(originalSym)))
61+
case _ => getSnippetCompilerData(sym.maybeOwner, originalSym)
62+
} else SnippetCompilerData(packageName, Nil, Nil, position(hackGetPositionOfDocstring(using qctx)(originalSym)))
6363

6464
private def position(p: Option[qctx.reflect.Position]): SnippetCompilerData.Position =
6565
p.fold(SnippetCompilerData.Position(0, 0))(p => SnippetCompilerData.Position(p.startLine, p.startColumn))
@@ -73,7 +73,6 @@ class SnippetCompilerDataCollector[Q <: Quotes](val qctx: Q):
7373
"DocCtx could not be found and documentations are unavailable. This is a compiler-internal error."
7474
)
7575
}
76-
val span = docCtx.docstring(s.asInstanceOf[Symbols.Symbol]).span
7776
s.pos.flatMap { pos =>
7877
docCtx.docstring(s.asInstanceOf[Symbols.Symbol]).map { docstring =>
7978
dotty.tools.dotc.util.SourcePosition(

scaladoc/src/dotty/tools/scaladoc/snippets/WrappedSnippet.scala

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,16 @@ case class WrappedSnippet(snippet: String, outerLineOffset: Int, outerColumnOffs
88

99
object WrappedSnippet:
1010

11+
val indent: Int = 2
12+
1113
def apply(str: String): WrappedSnippet =
1214
val baos = new ByteArrayOutputStream()
1315
val ps = new PrintStream(baos)
1416
ps.println("package snippets")
1517
ps.println("object Snippet {")
16-
str.split('\n').foreach(ps.printlnWithIndent(2, _))
18+
str.split('\n').foreach(ps.printlnWithIndent(indent, _))
1719
ps.println("}")
18-
WrappedSnippet(baos.toString, 0, 0, 2, 2)
20+
WrappedSnippet(baos.toString, 0, 0, indent, indent)
1921

2022
def apply(
2123
str: String,
@@ -31,19 +33,19 @@ object WrappedSnippet:
3133
imports.foreach(i => ps.println(s"import $i"))
3234
val notEmptyClassInfos = if classInfos.isEmpty then Seq(SnippetCompilerData.ClassInfo(None, Nil, None)) else classInfos
3335
notEmptyClassInfos.zipWithIndex.foreach { (info, i) =>
34-
ps.printlnWithIndent(2 * i, s"trait Snippet$i${info.generics.getOrElse("")} { ${info.tpe.fold("")(cn => s"self: $cn =>")}")
36+
ps.printlnWithIndent(indent * i, s"trait Snippet$i${info.generics.getOrElse("")} { ${info.tpe.fold("")(cn => s"self: $cn =>")}")
3537
info.names.foreach{ name =>
36-
ps.printlnWithIndent(2 * i + 2, s"val $name = self")
38+
ps.printlnWithIndent(indent * i + indent, s"val $name = self")
3739
}
3840
}
39-
str.split('\n').foreach(ps.printlnWithIndent(notEmptyClassInfos.size * 2, _))
40-
(0 to notEmptyClassInfos.size -1).reverse.foreach( i => ps.printlnWithIndent(i * 2, "}"))
41+
str.split('\n').foreach(ps.printlnWithIndent(notEmptyClassInfos.size * indent, _))
42+
(0 to notEmptyClassInfos.size -1).reverse.foreach( i => ps.printlnWithIndent(i * indent, "}"))
4143
WrappedSnippet(
4244
baos.toString,
4345
outerLineOffset,
4446
outerColumnOffset,
4547
notEmptyClassInfos.size + notEmptyClassInfos.flatMap(_.names).size + packageName.size,
46-
notEmptyClassInfos.size * 2 + 2
48+
notEmptyClassInfos.size * indent
4749
)
4850

4951
extension (ps: PrintStream) private def printlnWithIndent(indent: Int, str: String) =

scaladoc/src/dotty/tools/scaladoc/tasty/comments/Comments.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ abstract class MarkupConversion[T](val repr: Repr)(using dctx: DocContext) {
129129
(s: qctx.reflect.Symbol) => {
130130
val path = s.source.map(_.path)
131131
val pathBasedArg = dctx.snippetCompilerArgs.get(path)
132-
val data = SnippetCompilerDataCollector[qctx.type](qctx).getSnippetCompilerData(s)
132+
val data = SnippetCompilerDataCollector[qctx.type](qctx).getSnippetCompilerData(s, s)
133133
(str: String, lineOffset: SnippetChecker.LineOffset, argOverride: Option[SCFlags]) => {
134134
val arg = argOverride.fold(pathBasedArg)(pathBasedArg.overrideFlag(_))
135135

scaladoc/test/dotty/tools/scaladoc/ScaladocTest.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ abstract class ScaladocTest(val name: String):
1616
given DocContext = testDocContext(tastyFiles(name))
1717
op(ScalaModuleProvider.mkModule())
1818

19-
private def getTempDir() : TemporaryFolder =
19+
protected def getTempDir() : TemporaryFolder =
2020
val folder = new TemporaryFolder()
2121
folder.create()
2222
folder

scaladoc/test/dotty/tools/scaladoc/site/TemplateFileTests.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import org.junit.Test
88
import java.nio.file.Files
99

1010
class TemplateFileTests:
11+
given staticSiteContext: StaticSiteContext = testDocContext().staticSiteContext.get
1112
private def testTemplate(code: String, ext: String = "html")(op: TemplateFile => Unit): Unit =
1213
val tmpFile = Files.createTempFile("headerTests", s".${ext}").toFile()
1314
try

0 commit comments

Comments
 (0)