Skip to content

Commit

Permalink
Fix snippet compiler reporting line numbers in specific cases
Browse files Browse the repository at this point in the history
  • Loading branch information
KacperFKorban committed Sep 14, 2021
1 parent 5300646 commit 7f1a8a2
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 13 deletions.
2 changes: 1 addition & 1 deletion scaladoc/src/dotty/tools/scaladoc/site/templates.scala
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ case class TemplateFile(
// Snippet compiler currently supports markdown only
val parser: Parser = Parser.builder(defaultMarkdownOptions).build()
val parsedMd = parser.parse(rendered)
val processed = FlexmarkSnippetProcessor.processSnippets(parsedMd, snippetCheckingFunc, withContext = false)(using ssctx.outerCtx)
val processed = FlexmarkSnippetProcessor.processSnippets(parsedMd, None, snippetCheckingFunc, withContext = false)(using ssctx.outerCtx)
HtmlRenderer.builder(defaultMarkdownOptions).build().render(processed)

layoutTemplate match
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,18 @@ import com.vladsch.flexmark.util.options.MutableDataSet
import collection.JavaConverters._

import dotty.tools.scaladoc.tasty.comments.markdown.ExtendedFencedCodeBlock
import dotty.tools.scaladoc.tasty.comments.PreparsedComment

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

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

nodes.foldLeft[Map[String, String]](Map()) { (snippetMap, node) =>
val lineOffset = node.getStartLineNumber
val lineOffset = node.getStartLineNumber + preparsed.fold(0)(_.strippedLinesBeforeNo)
val info = node.getInfo.toString.split(" ")
if info.contains("scala") then {
val argOverride = info
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class SnippetChecker(val args: Scaladoc.Args)(using cctx: CompilerContext):

// These constants were found empirically to make snippet compiler
// report errors in the same position as main compiler.
private val constantLineOffset = 3
private val constantLineOffset = 2
private val constantColumnOffset = 4

def checkSnippet(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ import dotty.tools.scaladoc.DocContext
import dotty.tools.dotc.config.Settings._
import dotty.tools.dotc.config.ScalaSettings

case class SnippetCompilerSetting[T](setting: Setting[T], value: T)
case class SnippetCompilerSetting[T](setting: Setting[T], value: T)
11 changes: 6 additions & 5 deletions scaladoc/src/dotty/tools/scaladoc/tasty/comments/Comments.scala
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ case class PreparsedComment(
hideImplicitConversions: List[String],
shortDescription: List[String],
syntax: List[String],
strippedLinesBeforeNo: Int,
)

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

lazy val snippetChecker = dctx.snippetChecker

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

final def parse(preparsed: PreparsedComment): Comment =
val markup = stringToMarkup(preparsed.body)
val body = markupToDokkaCommentBody(processSnippets(markup))
val body = markupToDokkaCommentBody(processSnippets(markup, preparsed))
Comment(
body = body.body,
short = body.summary,
Expand Down Expand Up @@ -193,8 +194,8 @@ class MarkdownCommentParser(repr: Repr)(using dctx: DocContext)
.filterNot { case (_, v) => v.isEmpty }
.mapValues(stringToMarkup).to(SortedMap)

def processSnippets(root: mdu.Node): mdu.Node =
FlexmarkSnippetProcessor.processSnippets(root, snippetCheckingFunc(owner), withContext = true)
def processSnippets(root: mdu.Node, preparsed: PreparsedComment): mdu.Node =
FlexmarkSnippetProcessor.processSnippets(root, Some(preparsed), snippetCheckingFunc(owner), withContext = true)
}

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

def processSnippets(root: wiki.Body): wiki.Body =
def processSnippets(root: wiki.Body, preparsed: PreparsedComment): wiki.Body =
// Currently not supported
root
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ object Preparser {
tags: Map[TagKey, List[String]],
lastTagKey: Option[TagKey],
remaining: List[String],
inCodeBlock: Boolean
): PreparsedComment = remaining match {
inCodeBlock: Boolean,
)(using strippedLinesBeforeNo: Int = 0): PreparsedComment = remaining match {
case CodeBlockStartRegex(before, marker, after) :: ls if !inCodeBlock =>
if (!before.trim.isEmpty && !after.trim.isEmpty && marker == "```")
go(docBody, tags, lastTagKey, before :: (marker + after) :: ls, inCodeBlock = false)
Expand Down Expand Up @@ -108,7 +108,7 @@ object Preparser {
case line :: ls =>
if docBody.length > 0 then docBody.append(endOfLine)
docBody.append(line)
go(docBody, tags, lastTagKey, ls, inCodeBlock)
go(docBody, tags, lastTagKey, ls, inCodeBlock)(using strippedLinesBeforeNo + (if line.isEmpty && docBody.length == 0 then 1 else 0))


case Nil =>
Expand Down Expand Up @@ -177,6 +177,7 @@ object Preparser {
hideImplicitConversions = allTags(SimpleTagKey("hideImplicitConversion")),
shortDescription = allTags(SimpleTagKey("shortDescription")),
syntax = allTags(SimpleTagKey("syntax")),
strippedLinesBeforeNo = strippedLinesBeforeNo
)

cmt
Expand Down

0 comments on commit 7f1a8a2

Please sign in to comment.