Skip to content

Commit

Permalink
Report some inconsistent findByPosition results as warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Azpillaga Aldalur committed Oct 9, 2023
1 parent 42dc546 commit 3139c00
Showing 1 changed file with 19 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import com.strumenta.kolasu.traversing.findByPosition
import com.strumenta.kolasu.traversing.walk
import org.eclipse.lsp4j.DefinitionParams
import org.eclipse.lsp4j.Diagnostic
import org.eclipse.lsp4j.DiagnosticSeverity
import org.eclipse.lsp4j.DidChangeConfigurationParams
import org.eclipse.lsp4j.DidChangeTextDocumentParams
import org.eclipse.lsp4j.DidChangeWatchedFilesParams
Expand Down Expand Up @@ -106,9 +107,27 @@ open class KolasuServer<R : Node>(private val parser: ASTParser<R>, private val
params?.apply {
assert(this.contentChanges.size == 1)
parseAndPublishDiagnostics(this.contentChanges.first().text, params.textDocument.uri)
showLeaves(params)
}
}

private fun showLeaves(params: DidChangeTextDocumentParams) {
val uri = params.textDocument.uri
val tree = uriToParsingResult[uri]?.root ?: return

val diagnostics = mutableListOf<Diagnostic>()
for (node in tree.walk()) {
if (node.children.isEmpty() || node.position == null) continue
if (tree.findByPosition(node.position!!) != node) {
val diagnostic = Diagnostic(toLSPRange(node.position!!), "Leaf type: ${node.simpleNodeType} but findByPositionType: ${tree.findByPosition(node.position!!)?.simpleNodeType}")
diagnostic.severity = DiagnosticSeverity.Warning
diagnostics.add(diagnostic)
}
}

client.publishDiagnostics(PublishDiagnosticsParams(params.textDocument.uri, diagnostics))
}

private fun parseAndPublishDiagnostics(text: String, uri: String) {
val parsingResult = parser.parse(text)
parsingResult.root?.let {
Expand Down

0 comments on commit 3139c00

Please sign in to comment.