mdoc icon indicating copy to clipboard operation
mdoc copied to clipboard

Trailing comments getting duplicated

Open BalmungSan opened this issue 5 years ago • 3 comments

Hello, I am getting a funny behavior with comments at the end of a line.

For example, consider this:

```scala mdoc:compile-only
// a
val a = 10
val b = 20 // b
val c = 30
```

It produces this:

```scala
// a
val a = 10
val b = 20 // b // b
val c = 30
```

You can see that the // b comment was duplicated. Also, without the compile-only modifier this happens:

```scala
// a
val a = 10
// a: Int = 10
val b = 20 // b
// b: Int = 20 // b
val c = 30
// c: Int = 30
```

After playing with it for a while, it seems it only happens with comments at the end of a line, since // a wasn't affected. And that it requires more statements after; if it is the last one the error doesn't happen (but it may be any arbitrary number of blank lines between b and c).

Using mdoc v2.1.1 through sbt-microsite 1.1.5

BalmungSan avatar Apr 08 '20 18:04 BalmungSan

Just checked - it's still there in 2.2.10:

✗ cs launch org.scalameta::mdoc:2.2.10 -- --in test-trailing.md --out test-trailing-out.md && cat test-trailing-out.md

test-trailing.md

```scala mdoc:compile-only
val (a, b) = (1, 2) // comment

println(1)
```

result

info: Compiling 1 file to /root/projects/mdoc/test-trailing-out.md
info: Compiled in 2.17s (0 errors)
```scala
val (a, b) = (1, 2) // comment // comment

println(1)
```

I'll see what's up

keynmol avatar Nov 02 '20 18:11 keynmol

I can trace it to this line: https://github.com/scalameta/mdoc/blob/master/mdoc/src/main/scala-2/mdoc/internal/markdown/Renderer.scala#L124

And the issue is that the position of the statement is reported up to the comment section, so if you have 2 statements in a row, then this "leadingStart" will start from the end of the previous scalameta-recognised statement (which ends before the comment), so the whole comment will be added. I hope this sentence is as confusing to read as it was to write

@olafurpg I'm happy to take a stab at fixing this, but there's a multitude of ways this can be handled - any usecases in particular you'd want covered in the tests before I change this behaviour?

keynmol avatar Nov 02 '20 20:11 keynmol

@keynmol you are on the right track! The bug lies in the renderer. Would love to see this get fixed.

any usecases in particular you'd want covered in the tests before I change this behaviour?

Just make sure to check what happens with leading+trailing comments and the sibling whitespace characters.

olafurpg avatar Nov 03 '20 07:11 olafurpg