Skip to content

Commit

Permalink
feat(lsp): Use "node" in command-line, read package.json for tsp version
Browse files Browse the repository at this point in the history
Closes #3
  • Loading branch information
siketyan committed Nov 16, 2024
1 parent 5e2feb3 commit 1675aa1
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 5 deletions.
4 changes: 4 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ plugins {
id("java")
id("org.jetbrains.kotlin.jvm") version "2.0.21"
id("org.jetbrains.intellij.platform") version "2.1.0"

kotlin("plugin.serialization") version "2.0.21"
}

group = "jp.s6n.idea"
Expand All @@ -20,6 +22,8 @@ repositories {
}

dependencies {
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.7.3")

intellijPlatform {
intellijIdeaUltimate("2024.3")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@ import jp.s6n.idea.typespec.lang.TypeSpecLanguage

@Suppress("UnstableApiUsage")
class TypeSpecLspServerDescriptor(
project: Project, root: VirtualFile, private val tspServerFile: VirtualFile
) : LspServerDescriptor(project, "TypeSpec", root) {
project: Project,
root: VirtualFile,
version: String,
private val tspServerFile: VirtualFile
) : LspServerDescriptor(project, "TypeSpec $version", root) {
override fun isSupportedFile(file: VirtualFile) = file.fileType == TypeSpecFileType
override fun getLanguageId(file: VirtualFile) = TypeSpecLanguage.id
override fun createCommandLine() = GeneralCommandLine(tspServerFile.path, "--stdio")
override fun createCommandLine() = GeneralCommandLine("node", tspServerFile.path, "--stdio")
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,14 @@ import com.intellij.notification.NotificationType
import com.intellij.notification.Notifications
import com.intellij.openapi.project.Project
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.openapi.vfs.findDirectory
import com.intellij.openapi.vfs.findFile
import com.intellij.platform.lsp.api.LspServerSupportProvider
import jp.s6n.idea.typespec.lang.TypeSpecFileType
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.decodeFromStream

@Suppress("UnstableApiUsage")
class TypeSpecLspServerSupportProvider : LspServerSupportProvider {
Expand All @@ -29,9 +34,27 @@ class TypeSpecLspServerSupportProvider : LspServerSupportProvider {
}

private fun findTspServer(project: Project, directory: VirtualFile): TypeSpecLspServerDescriptor? {
val tspServerFile = directory.findFile("node_modules/@typespec/compiler/cmd/tsp-server.js")
val tspDirectory = directory.findDirectory("node_modules/@typespec/compiler")
?: return findTspServer(project, directory.parent ?: return null)

return TypeSpecLspServerDescriptor(project, directory, tspServerFile)
val tspServerFile = tspDirectory.findFile("cmd/tsp-server.js") ?: return null
val version = tspDirectory.findFile("package.json")
?.let { PackageJson.parseFile(it).version }
?: return null

return TypeSpecLspServerDescriptor(project, directory, version, tspServerFile)
}

@Serializable
class PackageJson(val version: String?) {
companion object {
private val decoder = Json {
ignoreUnknownKeys = true
}

@OptIn(ExperimentalSerializationApi::class)
fun parseFile(file: VirtualFile): PackageJson =
decoder.decodeFromStream(file.inputStream)
}
}
}

0 comments on commit 1675aa1

Please sign in to comment.