From 36d98f4738db07adad9452315c0e17572726d2da Mon Sep 17 00:00:00 2001 From: Marcel Joss Date: Sun, 7 May 2023 16:32:45 +0200 Subject: [PATCH] fix(lsp): don't crash if files outside of workspace are opened --- .../kotlin/tools/samt/ls/SamtTextDocumentService.kt | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/language-server/src/main/kotlin/tools/samt/ls/SamtTextDocumentService.kt b/language-server/src/main/kotlin/tools/samt/ls/SamtTextDocumentService.kt index cd6ab1d2..0a11aad0 100644 --- a/language-server/src/main/kotlin/tools/samt/ls/SamtTextDocumentService.kt +++ b/language-server/src/main/kotlin/tools/samt/ls/SamtTextDocumentService.kt @@ -30,7 +30,7 @@ class SamtTextDocumentService(private val workspaces: Map) : val path = params.textDocument.uri.toPathUri() val newText = params.contentChanges.single().text val fileInfo = parseFile(SourceFile(path, newText)) - val workspace = getWorkspace(path) + val workspace = getWorkspace(path) ?: return workspace.add(fileInfo) workspace.buildSemanticModel() @@ -58,7 +58,7 @@ class SamtTextDocumentService(private val workspaces: Map) : val path = params.textDocument.uri.toPathUri() val workspace = getWorkspace(path) - val fileInfo = workspace[path] ?: return@supplyAsync Either.forRight(emptyList()) + val fileInfo = workspace?.get(path) ?: return@supplyAsync Either.forRight(emptyList()) val fileNode: FileNode = fileInfo.fileNode ?: return@supplyAsync Either.forRight(emptyList()) val globalPackage: Package = workspace.samtPackage ?: return@supplyAsync Either.forRight(emptyList()) @@ -89,7 +89,7 @@ class SamtTextDocumentService(private val workspaces: Map) : override fun references(params: ReferenceParams): CompletableFuture> = CompletableFuture.supplyAsync { val path = params.textDocument.uri.toPathUri() - val workspace = getWorkspace(path) + val workspace = getWorkspace(path) ?: return@supplyAsync emptyList() val relevantFileInfo = workspace[path] ?: return@supplyAsync emptyList() val relevantFileNode = relevantFileInfo.fileNode ?: return@supplyAsync emptyList() @@ -120,7 +120,7 @@ class SamtTextDocumentService(private val workspaces: Map) : val path = params.textDocument.uri.toPathUri() val workspace = getWorkspace(path) - val fileInfo = workspace[path] ?: return@supplyAsync SemanticTokens(emptyList()) + val fileInfo = workspace?.get(path) ?: return@supplyAsync SemanticTokens(emptyList()) val tokens: List = fileInfo.tokens val fileNode: FileNode = fileInfo.fileNode ?: return@supplyAsync SemanticTokens(emptyList()) @@ -158,6 +158,6 @@ class SamtTextDocumentService(private val workspaces: Map) : this.client = client } - private fun getWorkspace(filePath: URI): SamtWorkspace = - workspaces.values.first { filePath in it } + private fun getWorkspace(filePath: URI): SamtWorkspace? = + workspaces.values.firstOrNull { filePath in it } }