From 0f707eb216cbd6b0fcd613a925259c12acec8031 Mon Sep 17 00:00:00 2001 From: Chris Simon Date: Sun, 17 Mar 2024 09:36:21 +1100 Subject: [PATCH] fix(intellij): only attempt to start the Contextive language server if a contextive definitions file is present. (fixes #64) --- .../ContextiveLspServerSupportProvider.kt | 7 +++- .../ContextiveLspServerSupportProviderTest.kt | 38 ++++++++++++------- 2 files changed, 31 insertions(+), 14 deletions(-) diff --git a/src/intellij/contextive/src/main/kotlin/tech/contextive/contextive/ContextiveLspServerSupportProvider.kt b/src/intellij/contextive/src/main/kotlin/tech/contextive/contextive/ContextiveLspServerSupportProvider.kt index 28ee2f4e..045335d2 100644 --- a/src/intellij/contextive/src/main/kotlin/tech/contextive/contextive/ContextiveLspServerSupportProvider.kt +++ b/src/intellij/contextive/src/main/kotlin/tech/contextive/contextive/ContextiveLspServerSupportProvider.kt @@ -2,10 +2,12 @@ package tech.contextive.contextive import com.intellij.execution.configurations.GeneralCommandLine import com.intellij.openapi.diagnostic.logger +import com.intellij.openapi.project.BaseProjectDirectories.Companion.getBaseDirectories import com.intellij.openapi.project.Project import com.intellij.openapi.vfs.VirtualFile import com.intellij.platform.lsp.api.LspServerSupportProvider import com.intellij.platform.lsp.api.ProjectWideLspServerDescriptor +import com.intellij.testFramework.utils.vfs.getFile private val LOG = logger() @@ -15,7 +17,10 @@ class ContextiveLspServerSupportProvider : LspServerSupportProvider { file: VirtualFile, serverStarter: LspServerSupportProvider.LspServerStarter ) { - serverStarter.ensureServerStarted(ContextiveLspServerDescriptor(project)) + val contextiveDefinitionsFile = project.getBaseDirectories().first().findFileByRelativePath(".contextive/definitions.yml"); + if (contextiveDefinitionsFile?.exists() == true) { + serverStarter.ensureServerStarted(ContextiveLspServerDescriptor(project)) + } } } diff --git a/src/intellij/contextive/src/test/kotlin/tech/contextive/contextive/ContextiveLspServerSupportProviderTest.kt b/src/intellij/contextive/src/test/kotlin/tech/contextive/contextive/ContextiveLspServerSupportProviderTest.kt index b66aef7a..7c40a0f6 100644 --- a/src/intellij/contextive/src/test/kotlin/tech/contextive/contextive/ContextiveLspServerSupportProviderTest.kt +++ b/src/intellij/contextive/src/test/kotlin/tech/contextive/contextive/ContextiveLspServerSupportProviderTest.kt @@ -7,30 +7,42 @@ import org.junit.jupiter.api.Test import com.intellij.openapi.project.Project import com.intellij.openapi.vfs.VirtualFile import com.intellij.platform.lsp.api.LspServerSupportProvider +import com.jetbrains.rd.generator.nova.PredefinedType import io.mockk.* +import org.junit.jupiter.params.ParameterizedTest +import org.junit.jupiter.params.provider.CsvSource class ContextiveLspServerSupportProviderTest { - @Test - fun ShouldAlwaysStartLanguageServer() { - val lspServerSupportProvider = ContextiveLspServerSupportProvider() + private fun getMockedProject(isContextiveFilePresent: Boolean): Project { + val baseDirectory = mockk() { + every { findFileByRelativePath(".contextive/definitions.yml") } returns + mockk { + every { exists() } returns isContextiveFilePresent + } + } - val project = mockk { + return mockk { every { getService(any()) } returns - mockk { - every { getBaseDirectories() } returns emptySet() - } + mockk { + every { getBaseDirectories() } returns setOf(baseDirectory) + } } + } + + @ParameterizedTest + @CsvSource("true,1", "false,0") + fun onlyEnsureServerStartedIfContextiveFileIsPresent(isContextiveFilePresent: Boolean, expectedServerStartInvocationCount: Int) { + val project = getMockedProject(isContextiveFilePresent) val file = mockk() - val serverStarter = mockk { - every { ensureServerStarted(any()) } returns Unit - } + val serverStarter = mockk(relaxed = true) - lspServerSupportProvider.fileOpened(project, file, serverStarter); + val lspServerSupportProvider = ContextiveLspServerSupportProvider() - verify { serverStarter.ensureServerStarted(any()) } + lspServerSupportProvider.fileOpened(project, file, serverStarter); - confirmVerified(serverStarter) + verify(exactly = expectedServerStartInvocationCount) { serverStarter.ensureServerStarted(any()) } } + } \ No newline at end of file