Skip to content

Commit

Permalink
Merge pull request #72 from olafurpg/fail-pos
Browse files Browse the repository at this point in the history
Fix position bug in mdoc:fail
  • Loading branch information
olafurpg authored Sep 9, 2018
2 parents 0419af2 + 7bb150b commit 9756bb0
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 17 deletions.
22 changes: 7 additions & 15 deletions mdoc/src/main/scala/mdoc/internal/markdown/Renderer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -113,25 +113,17 @@ object Renderer {
section.mod match {
case Modifier.Fail =>
binder.value match {
case CompileResult.TypecheckedOK(_, tpe, pos) =>
val mpos = Position
.Range(
section.input,
pos.startLine,
pos.startColumn,
pos.endLine,
pos.endColumn
)
.toUnslicedPosition
case CompileResult.TypecheckedOK(_, tpe, tpos) =>
reporter.error(
mpos,
tpos.toMeta(section),
s"Expected compile error but statement type-checked successfully"
)
appendMultiline(sb, tpe)
case CompileResult.ParseError(msg, pos) =>
appendFreshMultiline(sb, pos.formatMessage(doc.edit, msg))
case CompileResult.TypeError(msg, pos) =>
appendFreshMultiline(sb, pos.formatMessage(doc.edit, msg))
case CompileResult.ParseError(msg, tpos) =>
appendFreshMultiline(sb, tpos.formatMessage(section, msg))
case CompileResult.TypeError(msg, tpos) =>
val mpos = tpos.toMeta(section)
appendFreshMultiline(sb, tpos.formatMessage(section, msg))
case _ =>
val obtained = pprint.PPrinter.BlackWhite.apply(binder).toString()
throw new IllegalArgumentException(
Expand Down
15 changes: 13 additions & 2 deletions mdoc/src/main/scala/mdoc/internal/pos/PositionSyntax.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import scala.meta.io.RelativePath
import scalafix.internal.util.PositionSyntax._
import mdoc.document.RangePosition
import mdoc.internal.cli.Settings
import mdoc.internal.markdown.EvaluatedSection

object PositionSyntax {
implicit class XtensionInputMdoc(input: Input) {
Expand All @@ -29,8 +30,8 @@ object PositionSyntax {
}
}
implicit class XtensionRangePositionMdoc(pos: RangePosition) {
def formatMessage(edit: TokenEditDistance, message: String): String = {
val mpos = pos.toMeta(edit)
def formatMessage(section: EvaluatedSection, message: String): String = {
val mpos = pos.toMeta(section)
new StringBuilder()
.append(message)
.append("\n")
Expand All @@ -40,6 +41,16 @@ object PositionSyntax {
.append("\n")
.toString()
}
def toMeta(section: EvaluatedSection): Position = {
val mpos = Position.Range(
section.input,
pos.startLine,
pos.startColumn,
pos.endLine,
pos.endColumn
)
mpos.toUnslicedPosition
}
def toMeta(edit: TokenEditDistance): Position = {
Position
.Range(
Expand Down
7 changes: 7 additions & 0 deletions runtime/src/main/scala/mdoc/document/RangePosition.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ final class RangePosition(
val endLine: Int,
val endColumn: Int
) {
def add(other: RangePosition): RangePosition =
new RangePosition(
other.startLine + startLine,
other.startColumn + startColumn,
other.endLine + endLine,
other.endColumn + endColumn
)
def isEmpty: Boolean =
startLine == -1 &&
startColumn == -1 &&
Expand Down
27 changes: 27 additions & 0 deletions tests/unit/src/test/scala/tests/markdown/FailSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,31 @@ class FailSuite extends BaseMarkdownSuite {
|""".stripMargin
)

check(
"order",
"""
|```scala mdoc
|println(42)
|```
|```scala mdoc:fail
|val x: Int = "String"
|```
""".stripMargin,
"""
|```scala
|println(42)
|// 42
|```
|
|```scala
|val x: Int = "String"
|// type mismatch;
|// found : String("String")
|// required: Int
|// val x: Int = "String"
|// ^
|```
""".stripMargin
)

}

0 comments on commit 9756bb0

Please sign in to comment.