Skip to content

Commit

Permalink
add showParsingErrors and includeErrorNodeFoundIssues configuration s…
Browse files Browse the repository at this point in the history
…ettings
  • Loading branch information
Martin Azpillaga Aldalur committed Oct 11, 2023
1 parent 22cda72 commit e97d76c
Showing 1 changed file with 30 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,7 @@ import kotlin.reflect.full.isSubtypeOf
import kotlin.reflect.jvm.javaField
import kotlin.reflect.typeOf

data class DidChangeConfigurationRegistrationOptions(val section: String)
open class KolasuServer<R : Node>(private val parser: ASTParser<R>, private val name: String = "kolasuServer", private val includeErrorNodeIssues: Boolean = false) : LanguageServer, TextDocumentService, WorkspaceService, LanguageClientAware {
open class KolasuServer<R : Node>(private val parser: ASTParser<R>, private val language: String = "kolasuServer", private val includeErrorNodeIssues: Boolean = false) : LanguageServer, TextDocumentService, WorkspaceService, LanguageClientAware {

protected lateinit var client: LanguageClient
protected val uriToParsingResult: MutableMap<String, ParsingResult<R>> = mutableMapOf()
Expand Down Expand Up @@ -99,67 +98,63 @@ open class KolasuServer<R : Node>(private val parser: ASTParser<R>, private val
}

override fun initialized(params: InitializedParams?) {
val registrationParams = DidChangeConfigurationRegistrationOptions(name)
val registrationParams = DidChangeConfigurationRegistrationOptions(language)
client.registerCapability(RegistrationParams(listOf(Registration("myID", "workspace/didChangeConfiguration", registrationParams))))
}

override fun didChangeConfiguration(params: DidChangeConfigurationParams?) {
val settings = params?.settings as? JsonObject ?: return
configuration = settings[name].asJsonObject
configuration = settings[language].asJsonObject
}

override fun didOpen(params: DidOpenTextDocumentParams?) {
val uri = params?.textDocument?.uri ?: return
val text = params.textDocument.text

parseAndPublishDiagnostics(text, uri)
diagnoseAST(uri)
}

override fun didChange(params: DidChangeTextDocumentParams?) {
val uri = params?.textDocument?.uri ?: return
val change = params.contentChanges.first() ?: return

parseAndPublishDiagnostics(change.text, uri)
diagnoseAST(uri)
}

private fun diagnoseAST(uri: String) {
val tree = uriToParsingResult[uri]?.root ?: return
private fun parseAndPublishDiagnostics(text: String, uri: String) {
val parsingResult = parser.parse(text)
parsingResult.root?.let {
resolveSymbols(it)
}
uriToParsingResult[uri] = parsingResult

val diagnoseAST = configuration["diagnoseAST"]?.asBoolean ?: return
val showLeaves = configuration["showLeaves"]?.asBoolean ?: return
val tree = parsingResult.root ?: return

if (!diagnoseAST && !showLeaves) return
val showASTWarnings = configuration["showASTWarnings"]?.asBoolean ?: false
val showLeafPositions = configuration["showLeafPositions"]?.asBoolean ?: false
val showParsingErrors = configuration["showParsingErrors"]?.asBoolean ?: true
val includeErrorNodeFoundIssues = configuration["includeErrorNodeFoundIssues"]?.asBoolean ?: false

val diagnostics = mutableListOf<Diagnostic>()
for (node in tree.walk()) {
if (node.children.isNotEmpty() || node.position == null) continue
val diagnostics = ArrayList<Diagnostic>()

if (diagnoseAST && tree.findByPosition(node.position!!) != node) {
diagnostics.add(Diagnostic(toLSPRange(node.position!!), "Leaf type: ${node.simpleNodeType} but findByPositionType: ${tree.findByPosition(node.position!!)?.simpleNodeType}").apply { severity = DiagnosticSeverity.Warning })
}
if (showParsingErrors) {
for (issue in parsingResult.issues) {
if (!includeErrorNodeFoundIssues && issue.message.startsWith("Error node found") || issue.position == null) continue

if (showLeaves) {
diagnostics.add(Diagnostic(toLSPRange(node.position!!), "Leaf position: ${node.position}, Source text: ${node.sourceText}").apply { severity = DiagnosticSeverity.Information })
diagnostics.add(Diagnostic(toLSPRange(issue.position!!), issue.message))
}
}
client.publishDiagnostics(PublishDiagnosticsParams(uri, diagnostics))
}
if (showASTWarnings || showLeafPositions) {
for (node in tree.walk()) {
if (node.children.isNotEmpty() || node.position == null) continue

private fun parseAndPublishDiagnostics(text: String, uri: String) {
val parsingResult = parser.parse(text)
parsingResult.root?.let {
resolveSymbols(it)
}
uriToParsingResult[uri] = parsingResult
if (showASTWarnings && tree.findByPosition(node.position!!) != node) {
diagnostics.add(Diagnostic(toLSPRange(node.position!!), "Leaf type: ${node.simpleNodeType} but findByPositionType: ${tree.findByPosition(node.position!!)?.simpleNodeType}").apply { severity = DiagnosticSeverity.Warning })
}

val diagnostics = ArrayList<Diagnostic>()
parsingResult.issues.filter { issue ->
includeErrorNodeIssues || !issue.message.startsWith("Error node found")
}.forEach { issue ->
issue.position?.let {
diagnostics.add(Diagnostic(toLSPRange(it), issue.message))
if (showLeafPositions) {
diagnostics.add(Diagnostic(toLSPRange(node.position!!), "Leaf position: ${node.position}, Source text: ${node.sourceText}").apply { severity = DiagnosticSeverity.Information })
}
}
}
client.publishDiagnostics(PublishDiagnosticsParams(uri, diagnostics))
Expand Down Expand Up @@ -328,3 +323,5 @@ open class KolasuServer<R : Node>(private val parser: ASTParser<R>, private val
}

data class Symbol(val definition: PossiblyNamed, val references: MutableList<Node>)

data class DidChangeConfigurationRegistrationOptions(val section: String)

0 comments on commit e97d76c

Please sign in to comment.