Skip to content

Commit 6d4485a

Browse files
julianpeeterstgodzik
authored andcommitted
Refactor for clarity, simplify test
1 parent b70e5a4 commit 6d4485a

File tree

3 files changed

+14
-26
lines changed

3 files changed

+14
-26
lines changed

mdoc/src/main/scala/mdoc/internal/markdown/Renderer.scala

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package mdoc.internal.markdown
22

33
import java.io.ByteArrayOutputStream
44
import java.io.PrintStream
5-
import java.util.regex.Pattern
65
import mdoc.Reporter
76
import mdoc.Variable
87
import mdoc.document.CompileResult
@@ -118,26 +117,15 @@ object Renderer {
118117
case None =>
119118
(Position.Range(input, 0, pos.start).text, "")
120119
case Some(previousStatement) =>
121-
val betweenStatements =
122-
section.source.pos.text.substring(previousStatement.pos.end, pos.start)
123120
val Array(prevTrailingSingleLineComment, leadingTrivia) =
124-
betweenStatements.split(Pattern.quote("\n"), 2)
125-
val footerAtEnd = {
121+
section.source.pos.text.substring(previousStatement.pos.end, pos.start).split("\n", 2)
122+
val foot =
126123
if (statementIndex != (totalStats - 1)) ""
127-
else section.source.pos.text.substring(pos.end)
128-
}
129-
val lead =
130-
// if no trailing single-line comments, then we can use the established `Position.Range` system
131-
if (prevTrailingSingleLineComment.length == 0)
132-
Position.Range(input, previousStatement.pos.end, pos.start).text
133-
else "\n" + leadingTrivia
134-
val footer = footerAtEnd.split(Pattern.quote("\n")).drop(1)
135-
if (footer.nonEmpty)
136-
(lead, footer.mkString("\n", "\n", ""))
137-
else (lead, "")
124+
else section.source.pos.text.substring(pos.end).split("\n").drop(1).mkString("\n", "\n", "")
125+
("\n" + leadingTrivia, foot)
138126
}
139127
if (!section.mod.isFailOrWarn) {
140-
sb.append(leading )
128+
sb.append(leading)
141129
}
142130
val endOfLinePosition =
143131
Position.Range(pos.input, pos.startLine, pos.startColumn, pos.endLine, Int.MaxValue)

tests/unit/src/test/scala/tests/markdown/DefaultSuite.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ class DefaultSuite extends BaseMarkdownSuite {
210210
|Future.successful(Try(1))
211211
|val (a, b) = Future.successful(Try(1)) -> 2
212212
|
213-
|Future.successful(Try(1))
213+
|Future.successful(Try(1)) // last statement
214214
|
215215
|// penultimate
216216
|```
@@ -226,7 +226,7 @@ class DefaultSuite extends BaseMarkdownSuite {
226226
|// a: Future[Try[Int]] = Future(Success(Success(1)))
227227
|// b: Int = 2
228228
|
229-
|Future.successful(Try(1))
229+
|Future.successful(Try(1)) // last statement
230230
|// res1: Future[Try[Int]] = Future(Success(Success(1)))
231231
|
232232
|// penultimate
@@ -275,7 +275,7 @@ class DefaultSuite extends BaseMarkdownSuite {
275275
|x + y
276276
|
277277
|/** Docstring */
278-
|class User()
278+
|class User() // Comment 5
279279
|
280280
|// ultimate```
281281
""".stripMargin,
@@ -294,7 +294,7 @@ class DefaultSuite extends BaseMarkdownSuite {
294294
|// res0: Int = 4
295295
|
296296
|/** Docstring */
297-
|class User()
297+
|class User() // Comment 5
298298
|
299299
|// ultimate```
300300
""".stripMargin

tests/unit/src/test/scala/tests/markdown/VariablePrinterSuite.scala

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ class VariablePrinterSuite extends BaseMarkdownSuite {
77
"single-line-comment",
88
"""
99
|```scala mdoc
10-
|import scala.None // an import statement
11-
|val a = None // a variable
10+
|import scala.Int // an import statement
11+
|val a: Int = 1 // a variable
1212
|val b = 2 // another variable
1313
|```
1414
""".stripMargin,
1515
"""|```scala
16-
|import scala.None // an import statement
17-
|val a = None // a variable
18-
|// a: None.type = None
16+
|import scala.Int // an import statement
17+
|val a: Int = 1 // a variable
18+
|// a: Int = 1
1919
|val b = 2 // another variable
2020
|// b: Int = 2
2121
|```

0 commit comments

Comments
 (0)