in src/main/kotlin/org/jetbrains/qodana/requests.kt [146:209]
override suspend fun execute(state: SarifLanguageServer.ServerState) {
val pathPrefix = state.pathPrefix
if (pathPrefix == null) {
logger.error("Path prefix for the project was not set")
return
}
val actual = state.openFileCache[uri]
if (actual == null) {
logger.error("Trying to remap diagnostics for closed file")
return
}
val base = state.repositoryFileCache?.get(uri) ?: return
val relPathToFile = getRelativePath(uri, pathPrefix) ?: return
val diagnostics = state.diagnostic?.get(relPathToFile) ?: emptyList()
if (!diagnostics.any()) return // nothing to remap
val diffs = DiffLocator.computeDiff(base, actual, diagnostics.map { it.range.start.line })
val announce = mutableListOf<Diagnostic>()
for (diagnostic in diagnostics) {
val oldLine = diagnostic.range.start.line
val length = diagnostic.range.end.character - diagnostic.range.start.character // we don't compute lines in initial pass
val matchingDiff = diffs.firstOrNull { it.idx == oldLine && it.status != DiffStatus.DELETE && it.newIdx != null } ?: continue // diagnostics got removed
val newLine = matchingDiff.newIdx ?: continue
val oldString = matchingDiff.str ?: continue
val newString = matchingDiff.newStr ?: continue
val oldColumn = diagnostic.range.start.character
if (matchingDiff.str == matchingDiff.newStr) {
announce.add(Diagnostic(
Range(
Position(newLine, oldColumn),
Position(newLine, oldColumn + length)
),
diagnostic.message.intern(),
diagnostic.severity,
diagnostic.source,
diagnostic.code.left.intern()
))
} else {
val firstLineHighlight = DiffLocator.computeFirstLine(diagnostic.highlightedText)
if (firstLineHighlight != null && !newString.contains(firstLineHighlight)) continue
val inlineDiff = DiffLocator.computeLineDiff(
oldString,
newString,
listOf(oldColumn)
)
val matchingInlineDiff =
inlineDiff.firstOrNull { it.idx == oldColumn && it.newIdx != null }
val newColumn = matchingInlineDiff?.newIdx ?: continue
announce.add(
Diagnostic(
Range(
Position(newLine, newColumn),
Position(newLine, newColumn + length)
),
diagnostic.message.intern(),
diagnostic.severity,
diagnostic.source,
diagnostic.code.left.intern()
)
)
}
}
state.languageClient?.publishDiagnostics(PublishDiagnosticsParams(uri, announce))
}