This repository has been archived by the owner on Aug 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bugfix: Fix showing Scala 3 diagnostics (#544)
* bugfix: Fix showing Scala 3 diagnostics Previously, Scala 3 diagnsotics would not be shown since they currently use rendered message, which is a pretty printed diagnotic. Now, I added a separate parser for Scala 3 compiler diagnostics. * chore: Update changelog with information about the change
- Loading branch information
Showing
5 changed files
with
159 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
.../main/kotlin/org/jetbrains/bsp/bazel/server/diagnostics/Scala3CompilerDiagnosticParser.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package org.jetbrains.bsp.bazel.server.diagnostics | ||
|
||
object Scala3CompilerDiagnosticParser : Parser { | ||
|
||
override fun tryParse(output: Output): List<Diagnostic> = | ||
listOfNotNull(tryParseOne(output)) | ||
|
||
private val DiagnosticHeader = """ | ||
^--\ # "-- " diagnostic start | ||
\[E\d+\] # "[E008]" code | ||
([^:]+): # (1) type of diagnostic | ||
([^:]+):(\d+):(\d+) # (2) path, (3) line, (4) column | ||
\ \-+$ # " -----------------" ending | ||
""".toRegex(RegexOption.COMMENTS) | ||
|
||
// Scala 3 diagnostics have additional color printed, since Bazel uses renderedMessage field | ||
private val colorRegex = "\u001b\\[1A\u001b\\[K|\u001B\\[[;\\d]*m".toRegex() | ||
|
||
fun tryTake(output: Output, regex: Regex): MatchResult? = | ||
output.peek()?.let { regex.matchEntire(it.replace(colorRegex, "")) }?.also { output.take() } | ||
|
||
fun tryParseOne(output: Output): Diagnostic? { | ||
return tryTake(output, DiagnosticHeader) | ||
?.let { match -> | ||
val level = if (match.groupValues[1].contains("Error")) Level.Error else Level.Warning | ||
val path = match.groupValues[2].trim() | ||
val line = match.groupValues[3].toInt() | ||
val messageLines = collectMessageLines(match.groupValues[1].trim(), output) | ||
val column = match.groupValues[4].toIntOrNull() ?: tryFindColumnNumber(messageLines) ?: 1 | ||
val message = messageLines.joinToString("\n") | ||
Diagnostic(Position(line, column), message, level, path, output.targetLabel) | ||
} | ||
} | ||
|
||
private fun collectMessageLines(header: String, output: Output): List<String> { | ||
val lines = mutableListOf<String>() | ||
fun String.cleanLine(): String = | ||
this.replace(colorRegex, "").trim() | ||
|
||
// skip lines with numbers which show the source and skip the next ^^^^ line | ||
if (output.peek()?.cleanLine()?.startsWith('|') == false) output.take(2) | ||
while (output.nonEmpty() && output.peek()?.cleanLine()?.startsWith('|') == true) { | ||
lines.add(output.take().cleanLine().removePrefix("|").trim()) | ||
} | ||
lines.add(0, header) | ||
return lines | ||
} | ||
|
||
private val IssuePositionMarker = """^[\s\|]*\^\s*$""".toRegex() // ^ surrounded by whitespace only | ||
|
||
private fun tryFindColumnNumber(messageLines: List<String>): Int? { | ||
val line = messageLines.find { IssuePositionMarker.matches(it.replace(colorRegex, "")) } | ||
return line?.indexOf("^")?.let { it + 1 } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters