/
DiagnosticsParser.kt
62 lines (53 loc) · 1.78 KB
/
DiagnosticsParser.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package org.jetbrains.bsp.bazel.server.diagnostics
class DiagnosticsParser {
fun parse(bazelOutput: String, target: String, onlyKnownFiles: Boolean): List<Diagnostic> {
val output = prepareOutput(bazelOutput, target)
val diagnostics = collectDiagnostics(output, onlyKnownFiles)
return deduplicate(diagnostics)
}
private fun prepareOutput(bazelOutput: String, target: String): Output {
val lines = bazelOutput.lines()
val relevantLines = lines.filterNot { line -> IgnoredLines.any { it.matches(line) } }
return Output(relevantLines, target)
}
private fun collectDiagnostics(output: Output, onlyKnownFiles: Boolean): List<Diagnostic> {
val diagnostics = mutableListOf<Diagnostic>()
while (output.nonEmpty()) {
for (parser in Parsers) {
val result = parser.tryParse(output)
if (result.isNotEmpty()) {
diagnostics.addAll(result)
break
}
}
}
if (diagnostics.isEmpty() && !onlyKnownFiles) {
diagnostics.add(
Diagnostic(
position = Position(0, 0),
message = output.fullOutput(),
level = Level.Error,
fileLocation = "<unknown>",
targetLabel = output.targetLabel
)
)
}
return diagnostics.toList()
}
private fun deduplicate(parsedDiagnostics: List<Diagnostic>): List<Diagnostic> =
parsedDiagnostics
.groupBy { Triple(it.fileLocation, it.message, it.position) }
.values
.map { it.first() }
companion object {
private val Parsers = listOf(
BazelRootMessageParser,
CompilerDiagnosticParser,
AllCatchParser
)
private val IgnoredLines = listOf(
"^$".toRegex(),
"Use --sandbox_debug to see verbose messages from the sandbox".toRegex()
)
}
}