Skip to content

Commit

Permalink
fix(intellij): only attempt to start the Contextive language server i…
Browse files Browse the repository at this point in the history
…f a contextive definitions file is present. (fixes #64)
  • Loading branch information
chrissimon-au committed Mar 16, 2024
1 parent 779e21d commit 0f707eb
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<ContextiveLspServerSupportProvider>()

Expand All @@ -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))
}
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<VirtualFile>() {
every { findFileByRelativePath(".contextive/definitions.yml") } returns
mockk {
every { exists() } returns isContextiveFilePresent
}
}

val project = mockk<Project> {
return mockk<Project> {
every { getService<BaseProjectDirectories>(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<VirtualFile>()
val serverStarter = mockk<LspServerSupportProvider.LspServerStarter> {
every { ensureServerStarted(any()) } returns Unit
}
val serverStarter = mockk<LspServerSupportProvider.LspServerStarter>(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()) }
}

}

0 comments on commit 0f707eb

Please sign in to comment.