Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update handling of end-of-line single-line comments #825

Merged
merged 5 commits into from
Dec 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions mdoc/src/main/scala/mdoc/internal/markdown/Renderer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ object Renderer {
sb.appendMultiline(string, N)
}

// Beneath each binding statement, we insert the evaluated variable, e.g., `x: Int = 1`
def renderEvaluatedSection(
doc: EvaluatedDocument,
section: EvaluatedSection,
Expand All @@ -108,15 +109,23 @@ object Renderer {
section.section.statements.zip(section.source.stats).zipWithIndex.foreach {
case ((statement, tree), statementIndex) =>
val pos = tree.pos
val leadingStart = stats(statementIndex - 1) match {
// for each statement, we need to manage:
// 1. the leading trivia: whitespace, newlines
// 2. a footer: empty until filled on the last statement in the section
// 3. and, internally, the trailing single-line comments of the previous statement
val (leading, footer) = stats(statementIndex - 1) match {
case None =>
0
(Position.Range(input, 0, pos.start).text, "")
case Some(previousStatement) =>
previousStatement.pos.end
val Array(prevTrailingSingleLineComment, leadingTrivia) =
section.source.pos.text.substring(previousStatement.pos.end, pos.start).split("\n", 2)
val foot =
if (statementIndex != (totalStats - 1)) ""
else section.source.pos.text.substring(pos.end).split("\n").drop(1).mkString("\n", "\n", "")
("\n" + leadingTrivia, foot)
}
val leadingTrivia = Position.Range(input, leadingStart, pos.start)
if (!section.mod.isFailOrWarn) {
sb.append(leadingTrivia.text)
sb.append(leading)
}
val endOfLinePosition =
Position.Range(pos.input, pos.startLine, pos.startColumn, pos.endLine, Int.MaxValue)
Expand Down Expand Up @@ -179,6 +188,7 @@ object Renderer {
sb.append(printer(variable))
}
}
sb.append(footer)
}
baos.toString.trim
}
Expand Down
18 changes: 12 additions & 6 deletions tests/unit/src/test/scala/tests/markdown/DefaultSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,9 @@ class DefaultSuite extends BaseMarkdownSuite {
|Future.successful(Try(1))
|val (a, b) = Future.successful(Try(1)) -> 2
|
|Future.successful(Try(1))
|Future.successful(Try(1)) // last statement
|
|// penultimate
|```
""".stripMargin,
"""|```scala
Expand All @@ -224,8 +226,10 @@ class DefaultSuite extends BaseMarkdownSuite {
|// a: Future[Try[Int]] = Future(Success(Success(1)))
|// b: Int = 2
|
|Future.successful(Try(1))
|Future.successful(Try(1)) // last statement
|// res1: Future[Try[Int]] = Future(Success(Success(1)))
|
|// penultimate
|```
""".stripMargin
)
Expand Down Expand Up @@ -271,8 +275,9 @@ class DefaultSuite extends BaseMarkdownSuite {
|x + y
|
|/** Docstring */
|class User()
|```
|class User() // Comment 5
|
|// ultimate```
""".stripMargin,
"""|```scala
|/* Comment 1 */
Expand All @@ -289,8 +294,9 @@ class DefaultSuite extends BaseMarkdownSuite {
|// res0: Int = 4
|
|/** Docstring */
|class User()
|```
|class User() // Comment 5
|
|// ultimate```
""".stripMargin
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,44 @@ import mdoc.internal.markdown.ReplVariablePrinter

class VariablePrinterSuite extends BaseMarkdownSuite {

check(
"single-line-comment",
"""
|```scala mdoc
|import scala.Int // an import statement
|val a: Int = 1 // a variable
|val b = 2 // another variable
|```
""".stripMargin,
"""|```scala
|import scala.Int // an import statement
|val a: Int = 1 // a variable
|// a: Int = 1
|val b = 2 // another variable
|// b: Int = 2
|```
""".stripMargin,
baseSettings
)

check(
"single-line-comment:compile-only",
"""|```scala mdoc:compile-only
|// a
|val a = 10
|val b = 20 // b
|val c = 30
|```""".stripMargin,
"""|```scala
|// a
|val a = 10
|val b = 20 // b
|val c = 30
|```
""".stripMargin,
baseSettings
)

val trailingComment = baseSettings.copy(variablePrinter = { variable =>
variable.runtimeValue match {
case n: Int if variable.totalVariablesInStatement == 1 => s" // Number($n)"
Expand Down