Skip to content

Commit 77980fe

Browse files
pikinier20tanishiking
authored andcommitted
Remove redundant debug mode from snippet compiler.
1 parent 6c7a1dc commit 77980fe

15 files changed

+58
-55
lines changed

scaladoc-testcases/src/tests/snippetTestcase1.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package tests.snippetTestcase1
22

33
class SnippetTestcase1:
44
/**
5-
* SNIPPET(OUTERLINEOFFSET:8,OUTERCOLUMNOFFSET:6,INNERLINEOFFSET:3,INNERCOLUMNOFFSET:2)
5+
* SNIPPET(OUTERLINEOFFSET:8,OUTERCOLUMNOFFSET:6,INNERLINEOFFSET:5,INNERCOLUMNOFFSET:2)
66
* ERROR(LINE:8,COLUMN:8)
77
* ```scala sc:fail
88
* 2 + List()
@@ -11,17 +11,17 @@ class SnippetTestcase1:
1111
*/
1212
def a = 3
1313
/**
14-
* SNIPPET(OUTERLINEOFFSET:16,OUTERCOLUMNOFFSET:6,INNERLINEOFFSET:3,INNERCOLUMNOFFSET:2)
14+
* SNIPPET(OUTERLINEOFFSET:16,OUTERCOLUMNOFFSET:6,INNERLINEOFFSET:5,INNERCOLUMNOFFSET:2)
1515
* ```scala sc:compile sc-name:1
1616
* val xs: List[Int] = List()
1717
* ```
1818
*
19-
* SNIPPET(OUTERLINEOFFSET:21,OUTERCOLUMNOFFSET:6,INNERLINEOFFSET:3,INNERCOLUMNOFFSET:2)
19+
* SNIPPET(OUTERLINEOFFSET:21,OUTERCOLUMNOFFSET:6,INNERLINEOFFSET:5,INNERCOLUMNOFFSET:2)
2020
* ```scala sc:compile sc-compile-with:1 sc-name:2
2121
* val ys = xs.map(x => x * 2)
2222
* ```
2323
*
24-
* SNIPPET(OUTERLINEOFFSET:26,OUTERCOLUMNOFFSET:6,INNERLINEOFFSET:3,INNERCOLUMNOFFSET:2)
24+
* SNIPPET(OUTERLINEOFFSET:26,OUTERCOLUMNOFFSET:6,INNERLINEOFFSET:5,INNERCOLUMNOFFSET:2)
2525
* ```scala sc:compile sc-compile-with:2
2626
* xs ++ ys
2727
* ```

scaladoc-testcases/src/tests/snippetTestcase2.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ trait Quotes2[A] {
77
type X
88
object Y {
99
/**
10-
* SNIPPET(OUTERLINEOFFSET:13,OUTERCOLUMNOFFSET:10,INNERLINEOFFSET:6,INNERCOLUMNOFFSET:6)
10+
* SNIPPET(OUTERLINEOFFSET:13,OUTERCOLUMNOFFSET:10,INNERLINEOFFSET:8,INNERCOLUMNOFFSET:6)
1111
* ERROR(LINE:13,COLUMN:12)
1212
* ```scala sc:fail
1313
* 2 + List()
@@ -19,7 +19,7 @@ trait Quotes2[A] {
1919
val z: zModule = ???
2020
trait zModule {
2121
/**
22-
* SNIPPET(OUTERLINEOFFSET:25,OUTERCOLUMNOFFSET:10,INNERLINEOFFSET:7,INNERCOLUMNOFFSET:6)
22+
* SNIPPET(OUTERLINEOFFSET:25,OUTERCOLUMNOFFSET:10,INNERLINEOFFSET:9,INNERCOLUMNOFFSET:6)
2323
* ERROR(LINE:25,COLUMN:12)
2424
* ```scala sc:fail
2525
* 2 + List()
@@ -33,7 +33,7 @@ trait Quotes2[A] {
3333
type X
3434
object Y {
3535
/**
36-
* SNIPPET(OUTERLINEOFFSET:39,OUTERCOLUMNOFFSET:10,INNERLINEOFFSET:5,INNERCOLUMNOFFSET:6)
36+
* SNIPPET(OUTERLINEOFFSET:39,OUTERCOLUMNOFFSET:10,INNERLINEOFFSET:7,INNERCOLUMNOFFSET:6)
3737
* ERROR(LINE:39,COLUMN:12)
3838
* ```scala sc:fail
3939
* 2 + List()
@@ -45,7 +45,7 @@ trait Quotes2[A] {
4545
val z: zModule = ???
4646
trait zModule {
4747
/**
48-
* SNIPPET(OUTERLINEOFFSET:51,OUTERCOLUMNOFFSET:10,INNERLINEOFFSET:6,INNERCOLUMNOFFSET:6)
48+
* SNIPPET(OUTERLINEOFFSET:51,OUTERCOLUMNOFFSET:10,INNERLINEOFFSET:8,INNERCOLUMNOFFSET:6)
4949
* ERROR(LINE:51,COLUMN:12)
5050
* ```scala sc:fail
5151
* 2 + List()

scaladoc/src/dotty/tools/scaladoc/DocContext.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ case class NavigationNode(name: String, dri: DRI, nested: Seq[NavigationNode])
7474
case class DocContext(args: Scaladoc.Args, compilerContext: CompilerContext):
7575
lazy val sourceLinks = SourceLinks.load(args.sourceLinks, args.revision)(using compilerContext)
7676

77-
lazy val snippetCompilerArgs = snippets.SnippetCompilerArgs.load(args.snippetCompiler, args.snippetCompilerDebug)(using compilerContext)
77+
lazy val snippetCompilerArgs = snippets.SnippetCompilerArgs.load(args.snippetCompiler)(using compilerContext)
7878

7979
lazy val snippetChecker = snippets.SnippetChecker(args)(using compilerContext)
8080

scaladoc/src/dotty/tools/scaladoc/Scaladoc.scala

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ object Scaladoc:
5555
docCanonicalBaseUrl: String = "",
5656
documentSyntheticTypes: Boolean = false,
5757
snippetCompiler: List[String] = Nil,
58-
snippetCompilerDebug: Boolean = false,
5958
noLinkWarnings: Boolean = false,
6059
versionsDictionaryUrl: Option[String] = None,
6160
generateInkuire : Boolean = false,
@@ -222,7 +221,6 @@ object Scaladoc:
222221
YdocumentSyntheticTypes.get,
223222
snippetCompiler.get,
224223
noLinkWarnings.get,
225-
snippetCompilerDebug.get,
226224
versionsDictionaryUrl.nonDefault,
227225
generateInkuire.get,
228226
apiSubdirectory.get,

scaladoc/src/dotty/tools/scaladoc/ScaladocSettings.scala

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,14 +114,11 @@ class ScaladocSettings extends SettingGroup with AllScalaSettings:
114114
val snippetCompiler: Setting[List[String]] =
115115
MultiStringSetting("-snippet-compiler", "snippet-compiler", snippets.SnippetCompilerArgs.usage)
116116

117-
val snippetCompilerDebug: Setting[Boolean] =
118-
BooleanSetting("-Ysnippet-compiler-debug", snippets.SnippetCompilerArgs.debugUsage, false)
119-
120117
val generateInkuire: Setting[Boolean] =
121118
BooleanSetting("-Ygenerate-inkuire", "Generates InkuireDB and enables Hoogle-like searches", false)
122119

123120
val apiSubdirectory: Setting[Boolean] =
124121
BooleanSetting("-Yapi-subdirectory", "Put the API documentation pages inside a directory `api/`", false)
125122

126123
def scaladocSpecificSettings: Set[Setting[_]] =
127-
Set(sourceLinks, syntax, revision, externalDocumentationMappings, socialLinks, skipById, skipByRegex, deprecatedSkipPackages, docRootContent, snippetCompiler, snippetCompilerDebug, generateInkuire)
124+
Set(sourceLinks, syntax, revision, externalDocumentationMappings, socialLinks, skipById, skipByRegex, deprecatedSkipPackages, docRootContent, snippetCompiler, generateInkuire)

scaladoc/src/dotty/tools/scaladoc/site/templates.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ case class TemplateFile(
106106
// Snippet compiler currently supports markdown only
107107
val parser: Parser = Parser.builder(defaultMarkdownOptions).build()
108108
val parsedMd = parser.parse(rendered)
109-
val processed = FlexmarkSnippetProcessor.processSnippets(parsedMd, ssctx.snippetCompilerArgs.debug, snippetCheckingFunc)(using ssctx.outerCtx)
109+
val processed = FlexmarkSnippetProcessor.processSnippets(parsedMd, snippetCheckingFunc)(using ssctx.outerCtx)
110110
HtmlRenderer.builder(defaultMarkdownOptions).build().render(processed)
111111

112112
layoutTemplate match

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import collection.JavaConverters._
1010
import dotty.tools.scaladoc.tasty.comments.markdown.ExtendedFencedCodeBlock
1111

1212
object FlexmarkSnippetProcessor:
13-
def processSnippets(root: mdu.Node, debug: Boolean, checkingFunc: => SnippetChecker.SnippetCheckingFunc)(using CompilerContext): mdu.Node = {
13+
def processSnippets(root: mdu.Node, checkingFunc: => SnippetChecker.SnippetCheckingFunc)(using CompilerContext): mdu.Node = {
1414
lazy val cf: SnippetChecker.SnippetCheckingFunc = checkingFunc
1515

1616
val nodes = root.getDescendants().asScala.collect {
@@ -67,7 +67,7 @@ object FlexmarkSnippetProcessor:
6767

6868
val fullSnippet = Seq(snippetImports, snippet).mkString("\n").trim
6969
val snippetCompilationResult = cf(fullSnippet, lineOffset, argOverride) match {
70-
case result@Some(SnippetCompilationResult(wrapped, _, _, _)) if debug =>
70+
case result@Some(SnippetCompilationResult(wrapped, _, _, _)) =>
7171
node.setContentString(wrapped.snippet)
7272
result
7373
case result =>

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class SnippetCompiler(
5959
case diagnostic if diagnostic.position.isPresent =>
6060
val diagPos = diagnostic.position.get
6161
val pos = Some(
62-
Position(diagPos.line + line - innerLineOffset, diagPos.column + column - innerColumnOffset, diagPos.lineContent, if arg.debug then diagPos.line else diagPos.line - innerLineOffset)
62+
Position(diagPos.line + line - innerLineOffset, diagPos.column + column - innerColumnOffset, diagPos.lineContent, diagPos.line)
6363
)
6464
val dmsg = Try(diagnostic.message) match {
6565
case Success(msg) => msg

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

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package snippets
33

44
import java.nio.file.Path
55

6-
case class SnippetCompilerArg(flag: SCFlags, debug: Boolean):
6+
case class SnippetCompilerArg(flag: SCFlags):
77
def overrideFlag(f: SCFlags): SnippetCompilerArg = copy(flag = f)
88

99
sealed trait SCFlags(val flagName: String)
@@ -15,16 +15,16 @@ object SCFlags:
1515

1616
def values: Seq[SCFlags] = Seq(Compile, NoCompile, Fail)
1717

18-
case class SnippetCompilerArgs(scFlags: PathBased[SCFlags], val debug: Boolean, defaultFlag: SCFlags):
18+
case class SnippetCompilerArgs(scFlags: PathBased[SCFlags], defaultFlag: SCFlags):
1919
def get(member: Member): SnippetCompilerArg =
2020
member.sources
2121
.flatMap(s => scFlags.get(s.path).map(_.elem))
22-
.fold(SnippetCompilerArg(defaultFlag, debug))(SnippetCompilerArg(_, debug))
22+
.fold(SnippetCompilerArg(defaultFlag))(SnippetCompilerArg(_))
2323

2424
def get(path: Option[Path]): SnippetCompilerArg =
2525
path
2626
.flatMap(p => scFlags.get(p).map(_.elem))
27-
.fold(SnippetCompilerArg(defaultFlag, debug))(SnippetCompilerArg(_, debug))
27+
.fold(SnippetCompilerArg(defaultFlag))(SnippetCompilerArg(_))
2828

2929

3030
object SnippetCompilerArgs:
@@ -46,11 +46,7 @@ object SnippetCompilerArgs:
4646
|
4747
""".stripMargin
4848

49-
val debugUsage = """
50-
|Setting this option causes snippet compiler to print snippet as it is compiled (after wrapping).
51-
""".stripMargin
52-
53-
def load(args: List[String], debug: Boolean, defaultFlag: SCFlags = SCFlags.NoCompile)(using CompilerContext): SnippetCompilerArgs = {
49+
def load(args: List[String], defaultFlag: SCFlags = SCFlags.NoCompile)(using CompilerContext): SnippetCompilerArgs = {
5450
PathBased.parse[SCFlags](args)(using SCFlagsParser) match {
5551
case PathBased.ParsingResult(errors, res) =>
5652
if errors.nonEmpty then report.warning(s"""
@@ -60,7 +56,7 @@ object SnippetCompilerArgs:
6056
|$usage
6157
|""".stripMargin
6258
)
63-
SnippetCompilerArgs(res, debug, defaultFlag)
59+
SnippetCompilerArgs(res, defaultFlag)
6460
}
6561
}
6662

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

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,15 @@ object WrappedSnippet:
1313
def apply(str: String): WrappedSnippet =
1414
val baos = new ByteArrayOutputStream()
1515
val ps = new PrintStream(baos)
16+
ps.startHide()
1617
ps.println("package snippets")
1718
ps.println("object Snippet {")
19+
ps.endHide()
1820
str.split('\n').foreach(ps.printlnWithIndent(indent, _))
21+
ps.startHide()
1922
ps.println("}")
20-
WrappedSnippet(baos.toString, 0, 0, indent, indent)
23+
ps.endHide()
24+
WrappedSnippet(baos.toString, 0, 0, indent + 2 /*Hide tokens*/, indent)
2125

2226
def apply(
2327
str: String,
@@ -29,6 +33,7 @@ object WrappedSnippet:
2933
): WrappedSnippet =
3034
val baos = new ByteArrayOutputStream()
3135
val ps = new PrintStream(baos)
36+
ps.startHide()
3237
ps.println(s"package ${packageName.getOrElse("snippets")}")
3338
imports.foreach(i => ps.println(s"import $i"))
3439
val notEmptyClassInfos = if classInfos.isEmpty then Seq(SnippetCompilerData.ClassInfo(None, Nil, None)) else classInfos
@@ -38,17 +43,23 @@ object WrappedSnippet:
3843
ps.printlnWithIndent(indent * i + indent, s"val $name = self")
3944
}
4045
}
46+
ps.endHide()
4147
str.split('\n').foreach(ps.printlnWithIndent(notEmptyClassInfos.size * indent, _))
48+
ps.startHide()
4249
(0 to notEmptyClassInfos.size -1).reverse.foreach( i => ps.printlnWithIndent(i * indent, "}"))
50+
ps.endHide()
4351
WrappedSnippet(
4452
baos.toString,
4553
outerLineOffset,
4654
outerColumnOffset,
47-
notEmptyClassInfos.size + notEmptyClassInfos.flatMap(_.names).size + packageName.size,
55+
notEmptyClassInfos.size + notEmptyClassInfos.flatMap(_.names).size + packageName.size + 2 /*Hide tokens*/,
4856
notEmptyClassInfos.size * indent
4957
)
5058

51-
extension (ps: PrintStream) private def printlnWithIndent(indent: Int, str: String) =
52-
ps.println((" " * indent) + str)
59+
extension (ps: PrintStream)
60+
private def printlnWithIndent(indent: Int, str: String) =
61+
ps.println((" " * indent) + str)
62+
private def startHide() = ps.println(raw"//{")
63+
private def endHide() = ps.println(raw"//}")
5364

5465

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ class MarkdownCommentParser(repr: Repr)(using dctx: DocContext)
197197
.mapValues(stringToMarkup).to(SortedMap)
198198

199199
def processSnippets(root: mdu.Node): mdu.Node =
200-
FlexmarkSnippetProcessor.processSnippets(root, dctx.snippetCompilerArgs.debug, snippetCheckingFunc(owner))
200+
FlexmarkSnippetProcessor.processSnippets(root, snippetCheckingFunc(owner))
201201
}
202202

203203
class WikiCommentParser(repr: Repr)(using DocContext)

scaladoc/src/dotty/tools/scaladoc/tasty/comments/markdown/SnippetRenderer.scala

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,18 @@ object SnippetRenderer:
5858
}
5959
mRes.getOrElse(snippetLines)
6060

61+
private def wrapCommonIndent(snippetLines: Seq[SnippetLine]): Seq[SnippetLine] =
62+
val nonHiddenSnippetLines = snippetLines.filter(l => !l.classes.contains("hideable"))
63+
nonHiddenSnippetLines.headOption.map(_.content.takeWhile(_ == ' ')).map { prefix =>
64+
val maxCommonIndent = nonHiddenSnippetLines.foldLeft(prefix) { (currPrefix, elem) =>
65+
if elem.content.startsWith(currPrefix) then currPrefix else elem.content.takeWhile(_ == ' ')
66+
}
67+
snippetLines.map { line =>
68+
if line.classes.contains("hideable") then line
69+
else line.copy(content = span(cls := "hideable")(maxCommonIndent).toString + line.content.stripPrefix(maxCommonIndent))
70+
}
71+
}.getOrElse(snippetLines)
72+
6173
private def wrapLineInBetween(startSymbol: Option[String], endSymbol: Option[String], line: SnippetLine): SnippetLine =
6274
val startIdx = startSymbol.map(s => line.content.indexOf(s))
6375
val endIdx = endSymbol.map(s => line.content.indexOf(s))
@@ -90,6 +102,7 @@ object SnippetRenderer:
90102
}
91103
wrapImportedSection
92104
.andThen(wrapHiddenSymbols)
105+
.andThen(wrapCommonIndent)
93106
.apply(snippetLines)
94107

95108
private def addCompileMessages(messages: Seq[SnippetCompilerMessage])(codeLines: Seq[SnippetLine]): Seq[SnippetLine] =

scaladoc/test/dotty/tools/scaladoc/snippets/SnippetCompilerTest.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class SnippetCompilerTest {
1818
0
1919
)
2020

21-
def runTest(str: String) = compiler.compile(wrapFn(str), SnippetCompilerArg(SCFlags.Compile, false))
21+
def runTest(str: String) = compiler.compile(wrapFn(str), SnippetCompilerArg(SCFlags.Compile))
2222

2323
private def assertSuccessfulCompilation(res: SnippetCompilationResult): Unit = res match {
2424
case r @ SnippetCompilationResult(_, isSuccessful, _, messages) => assert(isSuccessful, r.getSummary)

scaladoc/test/dotty/tools/scaladoc/snippets/SnippetsE2eTest.scala

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import collection.JavaConverters._
1818

1919
import dotty.tools.scaladoc.tasty.comments.markdown.ExtendedFencedCodeBlock
2020

21-
abstract class SnippetsE2eTest(testName: String, flag: SCFlags, debug: Boolean) extends ScaladocTest(testName):
21+
abstract class SnippetsE2eTest(testName: String, flag: SCFlags) extends ScaladocTest(testName):
2222

2323
import SnippetsE2eTest._
2424

@@ -29,16 +29,13 @@ abstract class SnippetsE2eTest(testName: String, flag: SCFlags, debug: Boolean)
2929
def report(str: String) = s"""|In test $testName:
3030
|$str""".stripMargin
3131

32-
println(BuildInfo.test_testcasesOutputDir.map(_ + s"/tests/$testName"))
33-
3432
override def args = Scaladoc.Args(
3533
name = "test",
3634
tastyDirs = BuildInfo.test_testcasesOutputDir.map(java.io.File(_)).toSeq,
3735
tastyFiles = tastyFiles(testName),
3836
output = getTempDir().getRoot,
3937
projectVersion = Some("1.0"),
40-
snippetCompiler = List(s"${BuildInfo.test_testcasesSourceRoot}/tests=${flag.flagName}"),
41-
snippetCompilerDebug = debug
38+
snippetCompiler = List(s"${BuildInfo.test_testcasesSourceRoot}/tests=${flag.flagName}")
4239
)
4340

4441
override def withModule(op: DocContext ?=> Module => Unit) =
@@ -85,17 +82,10 @@ abstract class SnippetsE2eTest(testName: String, flag: SCFlags, debug: Boolean)
8582

8683
def checkRelativeLines(msg: Message, cmsg: SnippetCompilerMessage): Seq[String] =
8784
val pos = cmsg.position.get
88-
if debug then {
89-
if !(pos.relativeLine == pos.line - ws.outerLineOffset + ws.innerLineOffset) then Seq(
90-
s"Expected ${msg.level.text} message at relative line: ${pos.line - ws.outerLineOffset + ws.innerLineOffset} " +
91-
s"but found at ${pos.relativeLine}"
92-
) else Nil
93-
} else {
94-
if !(pos.relativeLine == pos.line - ws.outerLineOffset) then Seq(
95-
s"Expected ${msg.level.text} message at relative line: ${pos.line - ws.outerLineOffset} " +
96-
s"but found at ${pos.relativeLine}"
97-
) else Nil
98-
}
85+
if !(pos.relativeLine == pos.line - ws.outerLineOffset + ws.innerLineOffset) then Seq(
86+
s"Expected ${msg.level.text} message at relative line: ${pos.line - ws.outerLineOffset + ws.innerLineOffset} " +
87+
s"but found at ${pos.relativeLine}"
88+
) else Nil
9989

10090
val mResult = compilationMessagesWithPos.flatMap { cmsg =>
10191
messages
Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
package dotty.tools.scaladoc
22
package snippets
33

4-
class SnippetE2eTestcase1 extends SnippetsE2eTest("snippetTestcase1", SCFlags.Compile, false)
4+
class SnippetE2eTestcase1 extends SnippetsE2eTest("snippetTestcase1", SCFlags.Compile)
55

6-
class SnippetE2eTestcase1Debug extends SnippetsE2eTest("snippetTestcase1", SCFlags.Compile, true)
76

8-
class SnippetE2eTestcase2 extends SnippetsE2eTest("snippetTestcase2", SCFlags.Compile, false)
7+
class SnippetE2eTestcase2 extends SnippetsE2eTest("snippetTestcase2", SCFlags.Compile)
98

10-
class SnippetE2eTestcase2Debug extends SnippetsE2eTest("snippetTestcase2", SCFlags.Compile, true)

0 commit comments

Comments
 (0)