Skip to content

Commit

Permalink
Align error underlines properly when tabs are involved
Browse files Browse the repository at this point in the history
  • Loading branch information
drewd committed May 2, 2024
1 parent 8d8eabc commit 403ed18
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -229,13 +229,15 @@ class SqlDelightEnvironment(
val maxDigits = (log10(context.lineEnd.toDouble()) + 1).toInt()
for (line in context.lineStart..context.lineEnd) {
if (!tokenizer.hasMoreTokens()) break
result.append(("%0${maxDigits}d %s\n").format(line, tokenizer.nextToken()))
val lineValue = tokenizer.nextToken()
result.append(("%0${maxDigits}d %s\n").format(line, lineValue))
if (element.lineStart == element.lineEnd && element.lineStart == line) {
// If its an error on a single line highlight where on the line.
// If it's an error on a single line highlight where on the line.
result.append(("%${maxDigits}s ").format(""))
if (element.charPositionInLine > 0) {
result.append(("%${element.charPositionInLine}s").format(""))
}
// Print tabs when you see it, spaces for everything else.
lineValue.subSequence(0 until element.charPositionInLine)
.map { char -> if (char != '\t') " " else char }
.forEach(result::append)
result.append(("%s\n").format("^".repeat(element.textLength)))
}
}
Expand Down
12 changes: 12 additions & 0 deletions sqldelight-gradle-plugin/src/test/bad-underlines/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
plugins {
alias(libs.plugins.kotlin.jvm)
alias(libs.plugins.sqldelight)
}

sqldelight {
databases {
Database {
packageName = "com.example"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
pluginManagement {
includeBuild("../build-logic-tests")
}

plugins {
id("sqldelightTests")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
CREATE TABLE test1(
value BIGINT
);
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,23 @@ class FailureTest {
""".trimMargin(),
)
}

@Test fun `errors with tabs in line underline properly`() {
val fixtureRoot = File("src/test/bad-underlines")

val output = GradleRunner.create()
.withCommonConfiguration(fixtureRoot)
.withArguments("clean", "generateMainDatabaseInterface", "--stacktrace")
.buildAndFail()

assertThat(output.output).contains(
"""
|Test1.sq: (2, 7): <type name real> expected, got 'BIGINT'
|1 CREATE TABLE test1(
|2 value BIGINT
| ^^^^^^
|3 )
""".trimMargin(),
)
}
}

0 comments on commit 403ed18

Please sign in to comment.