Skip to content

Commit 39e7fec

Browse files
committed
Fix snippet compiler reporting line numbers in specific cases
1 parent 310769a commit 39e7fec

File tree

6 files changed

+16
-13
lines changed

6 files changed

+16
-13
lines changed

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

+1-1
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, snippetCheckingFunc, withContext = false)(using ssctx.outerCtx)
109+
val processed = FlexmarkSnippetProcessor.processSnippets(parsedMd, None, snippetCheckingFunc, withContext = false)(using ssctx.outerCtx)
110110
HtmlRenderer.builder(defaultMarkdownOptions).build().render(processed)
111111

112112
layoutTemplate match

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

+3-2
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,18 @@ import com.vladsch.flexmark.util.options.MutableDataSet
88
import collection.JavaConverters._
99

1010
import dotty.tools.scaladoc.tasty.comments.markdown.ExtendedFencedCodeBlock
11+
import dotty.tools.scaladoc.tasty.comments.PreparsedComment
1112

1213
object FlexmarkSnippetProcessor:
13-
def processSnippets(root: mdu.Node, checkingFunc: => SnippetChecker.SnippetCheckingFunc, withContext: Boolean)(using CompilerContext): mdu.Node = {
14+
def processSnippets(root: mdu.Node, preparsed: Option[PreparsedComment], checkingFunc: => SnippetChecker.SnippetCheckingFunc, withContext: Boolean)(using CompilerContext): mdu.Node = {
1415
lazy val cf: SnippetChecker.SnippetCheckingFunc = checkingFunc
1516

1617
val nodes = root.getDescendants().asScala.collect {
1718
case fcb: mda.FencedCodeBlock => fcb
1819
}.toList
1920

2021
nodes.foldLeft[Map[String, String]](Map()) { (snippetMap, node) =>
21-
val lineOffset = node.getStartLineNumber
22+
val lineOffset = node.getStartLineNumber + preparsed.fold(0)(_.strippedLinesBeforeNo)
2223
val info = node.getInfo.toString.split(" ")
2324
if info.contains("scala") then {
2425
val argOverride = info

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class SnippetChecker(val args: Scaladoc.Args)(using cctx: CompilerContext):
3333

3434
// These constants were found empirically to make snippet compiler
3535
// report errors in the same position as main compiler.
36-
private val constantLineOffset = 3
36+
private val constantLineOffset = 2
3737
private val constantColumnOffset = 4
3838

3939
def checkSnippet(

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ import dotty.tools.scaladoc.DocContext
55
import dotty.tools.dotc.config.Settings._
66
import dotty.tools.dotc.config.ScalaSettings
77

8-
case class SnippetCompilerSetting[T](setting: Setting[T], value: T)
8+
case class SnippetCompilerSetting[T](setting: Setting[T], value: T)

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

+6-5
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ case class PreparsedComment(
6767
hideImplicitConversions: List[String],
6868
shortDescription: List[String],
6969
syntax: List[String],
70+
strippedLinesBeforeNo: Int,
7071
)
7172

7273
case class DokkaCommentBody(summary: Option[DocPart], body: DocPart)
@@ -78,7 +79,7 @@ abstract class MarkupConversion[T](val repr: Repr)(using dctx: DocContext) {
7879
protected def markupToDokkaCommentBody(t: T): DokkaCommentBody
7980
protected def filterEmpty(xs: List[String]): List[T]
8081
protected def filterEmpty(xs: SortedMap[String, String]): SortedMap[String, T]
81-
protected def processSnippets(t: T): T
82+
protected def processSnippets(t: T, preparsed: PreparsedComment): T
8283

8384
lazy val snippetChecker = dctx.snippetChecker
8485

@@ -141,7 +142,7 @@ abstract class MarkupConversion[T](val repr: Repr)(using dctx: DocContext) {
141142

142143
final def parse(preparsed: PreparsedComment): Comment =
143144
val markup = stringToMarkup(preparsed.body)
144-
val body = markupToDokkaCommentBody(processSnippets(markup))
145+
val body = markupToDokkaCommentBody(processSnippets(markup, preparsed))
145146
Comment(
146147
body = body.body,
147148
short = body.summary,
@@ -193,8 +194,8 @@ class MarkdownCommentParser(repr: Repr)(using dctx: DocContext)
193194
.filterNot { case (_, v) => v.isEmpty }
194195
.mapValues(stringToMarkup).to(SortedMap)
195196

196-
def processSnippets(root: mdu.Node): mdu.Node =
197-
FlexmarkSnippetProcessor.processSnippets(root, snippetCheckingFunc(owner), withContext = true)
197+
def processSnippets(root: mdu.Node, preparsed: PreparsedComment): mdu.Node =
198+
FlexmarkSnippetProcessor.processSnippets(root, Some(preparsed), snippetCheckingFunc(owner), withContext = true)
198199
}
199200

200201
class WikiCommentParser(repr: Repr)(using DocContext)
@@ -249,6 +250,6 @@ class WikiCommentParser(repr: Repr)(using DocContext)
249250
xs.view.mapValues(stringToMarkup).to(SortedMap)
250251
.filterNot { case (_, v) => v.blocks.isEmpty }
251252

252-
def processSnippets(root: wiki.Body): wiki.Body =
253+
def processSnippets(root: wiki.Body, preparsed: PreparsedComment): wiki.Body =
253254
// Currently not supported
254255
root

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

+4-3
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ object Preparser {
3232
tags: Map[TagKey, List[String]],
3333
lastTagKey: Option[TagKey],
3434
remaining: List[String],
35-
inCodeBlock: Boolean
36-
): PreparsedComment = remaining match {
35+
inCodeBlock: Boolean,
36+
)(using strippedLinesBeforeNo: Int = 0): PreparsedComment = remaining match {
3737
case CodeBlockStartRegex(before, marker, after) :: ls if !inCodeBlock =>
3838
if (!before.trim.isEmpty && !after.trim.isEmpty && marker == "```")
3939
go(docBody, tags, lastTagKey, before :: (marker + after) :: ls, inCodeBlock = false)
@@ -108,7 +108,7 @@ object Preparser {
108108
case line :: ls =>
109109
if docBody.length > 0 then docBody.append(endOfLine)
110110
docBody.append(line)
111-
go(docBody, tags, lastTagKey, ls, inCodeBlock)
111+
go(docBody, tags, lastTagKey, ls, inCodeBlock)(using strippedLinesBeforeNo + (if line.isEmpty && docBody.length == 0 then 1 else 0))
112112

113113

114114
case Nil =>
@@ -177,6 +177,7 @@ object Preparser {
177177
hideImplicitConversions = allTags(SimpleTagKey("hideImplicitConversion")),
178178
shortDescription = allTags(SimpleTagKey("shortDescription")),
179179
syntax = allTags(SimpleTagKey("syntax")),
180+
strippedLinesBeforeNo = strippedLinesBeforeNo
180181
)
181182

182183
cmt

0 commit comments

Comments
 (0)