Skip to content

Commit 586207a

Browse files
authored
feat: Add kotlin lsp integration (anomalyco#6601)
1 parent a58dbb3 commit 586207a

3 files changed

Lines changed: 86 additions & 0 deletions

File tree

packages/opencode/src/lsp/language.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ export const LANGUAGE_EXTENSIONS: Record<string, string> = {
4545
".ini": "ini",
4646
".java": "java",
4747
".js": "javascript",
48+
".kt": "kotlin",
49+
".kts": "kotlin",
4850
".jsx": "javascriptreact",
4951
".json": "json",
5052
".tex": "latex",

packages/opencode/src/lsp/server.ts

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1209,6 +1209,89 @@ export namespace LSPServer {
12091209
},
12101210
}
12111211

1212+
export const KotlinLS: Info = {
1213+
id: "kotlin-ls",
1214+
extensions: [".kt", ".kts"],
1215+
root: NearestRoot(["build.gradle", "build.gradle.kts", "settings.gradle.kts", "pom.xml"]),
1216+
async spawn(root) {
1217+
const distPath = path.join(Global.Path.bin, "kotlin-ls")
1218+
const launcherScript =
1219+
process.platform === "win32" ? path.join(distPath, "kotlin-lsp.cmd") : path.join(distPath, "kotlin-lsp.sh")
1220+
const installed = await Bun.file(launcherScript).exists()
1221+
if (!installed) {
1222+
if (Flag.OPENCODE_DISABLE_LSP_DOWNLOAD) return
1223+
log.info("Downloading Kotlin Language Server from GitHub.")
1224+
1225+
const releaseResponse = await fetch("https://api.github.com/repos/Kotlin/kotlin-lsp/releases/latest")
1226+
if (!releaseResponse.ok) {
1227+
log.error("Failed to fetch kotlin-lsp release info")
1228+
return
1229+
}
1230+
1231+
const release = await releaseResponse.json()
1232+
const version = release.name?.replace(/^v/, '')
1233+
1234+
if (!version) {
1235+
log.error("Could not determine Kotlin LSP version from release")
1236+
return
1237+
}
1238+
1239+
const platform = process.platform
1240+
const arch = process.arch
1241+
1242+
let kotlinArch: string = arch
1243+
if (arch === "arm64") kotlinArch = "aarch64"
1244+
else if (arch === "x64") kotlinArch = "x64"
1245+
1246+
let kotlinPlatform: string = platform
1247+
if (platform === "darwin") kotlinPlatform = "mac"
1248+
else if (platform === "linux") kotlinPlatform = "linux"
1249+
else if (platform === "win32") kotlinPlatform = "win"
1250+
1251+
const supportedCombos = [
1252+
"mac-x64", "mac-aarch64",
1253+
"linux-x64", "linux-aarch64",
1254+
"win-x64", "win-aarch64"
1255+
]
1256+
1257+
const combo = `${kotlinPlatform}-${kotlinArch}`
1258+
1259+
if (!supportedCombos.includes(combo)) {
1260+
log.error(`Platform ${platform}/${arch} is not supported by Kotlin LSP`)
1261+
return
1262+
}
1263+
1264+
const assetName = `kotlin-lsp-${version}-${kotlinPlatform}-${kotlinArch}.zip`
1265+
const releaseURL = `https://download-cdn.jetbrains.com/kotlin-lsp/${version}/${assetName}`
1266+
1267+
await fs.mkdir(distPath, { recursive: true })
1268+
const archivePath = path.join(distPath, "kotlin-ls.zip")
1269+
await $`curl -L -o '${archivePath}' '${releaseURL}'`.quiet().nothrow()
1270+
const ok = await Archive.extractZip(archivePath, distPath)
1271+
.then(() => true)
1272+
.catch((error) => {
1273+
log.error("Failed to extract Kotlin LS archive", { error })
1274+
return false
1275+
})
1276+
if (!ok) return
1277+
await fs.rm(archivePath, { force: true })
1278+
if (process.platform !== "win32") {
1279+
await $`chmod +x ${launcherScript}`.quiet().nothrow()
1280+
}
1281+
log.info("Installed Kotlin Language Server", { path: launcherScript })
1282+
}
1283+
if (!(await Bun.file(launcherScript).exists())) {
1284+
log.error(`Failed to locate the Kotlin LS launcher script in the installed directory: ${distPath}.`)
1285+
return
1286+
}
1287+
return {
1288+
process: spawn(launcherScript, ["--stdio"], {
1289+
cwd: root,
1290+
}),
1291+
}
1292+
},
1293+
}
1294+
12121295
export const YamlLS: Info = {
12131296
id: "yaml-ls",
12141297
extensions: [".yaml", ".yml"],

packages/web/src/content/docs/lsp.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ OpenCode comes with several built-in LSP servers for popular languages:
2626
| gleam | .gleam | `gleam` command available |
2727
| gopls | .go | `go` command available |
2828
| jdtls | .java | `Java SDK (version 21+)` installed |
29+
| kotlin-ls | .kt, .kts | Auto-installs for Kotlin projects |
2930
| lua-ls | .lua | Auto-installs for Lua projects |
3031
| nixd | .nix | `nixd` command available |
3132
| ocaml-lsp | .ml, .mli | `ocamllsp` command available |

0 commit comments

Comments
 (0)