Skip to content

Commit

Permalink
Installs nimlangserver locally (#59)
Browse files Browse the repository at this point in the history
* Installs the langserver locally #9

* Changes the order so it prioritises the local install over the global

Maybe we can use an option for those users that prefer to use the global installation

* fixes win support
  • Loading branch information
jmgomez committed Apr 10, 2024
1 parent d9d28fe commit ac159fa
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
35 changes: 25 additions & 10 deletions src/nimLsp.nim
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import std/[jsconsole, strutils, jsfetch, asyncjs, sugar, sequtils, options, strformat]
import platform/[vscodeApi, languageClientApi]

import platform/js/[jsNodeFs, jsNodePath, jsNodeCp, jsNodeUtil]
import platform/js/[jsNodeFs, jsNodePath, jsNodeCp, jsNodeUtil, jsNodeOs]
import nimutils
from std/strformat import fmt
from tools/nimBinTools import getNimbleExecPath, getBinPath
Expand Down Expand Up @@ -123,24 +123,38 @@ proc isValidLspPath(lspPath: cstring): bool =
else:
console.log(fmt"isValidLspPath({lspPath}) = {result}".cstring)


proc getLocalLspDir(): cstring =
#The lsp is installed inside the user directory because the user
#storage of the extension seems to be too long and the installation fails
result = path.join(nodeOs.homedir, ".vscode-nim")
if not fs.existsSync(result):
fs.mkdirSync(result)


proc getLspPath(state: ExtensionState): cstring =
#[
We first try to use the path from the nim.lsp.path setting.
If path is not set, we try to use the global nimlangserver binary.
If the global binary is not found, (TODO) we try to use the local nimlangserver binary.
If path is not set, we try to use the local nimlangserver binary.
If the local binary is not found, we try to use the global nimlangserver binary.
]#
result = vscode.workspace.getConfiguration("nim").getStr("lsp.path")
if not isValidLspPath(result):
result = getBinPath("nimlangserver")
var langserverExec = "nimlangserver"
if process.platform == "win32":
langserverExec.add ".cmd"
result = path.join(getLocalLspDir(), "nimbledeps", "bin", langserverExec)
if not isValidLspPath(result):
result = path.join(state.ctx.storagePath, "bin", "nimlangserver")
result = getBinPath("nimlangserver")

outputLine(("Using nimlangserver from path: " & result))

proc startLanguageServer(tryInstall: bool, state: ExtensionState) {.async.} =
let rawPath = getLspPath(state)
if not isValidLspPath(rawPath):
console.log("nimlangserver not found on path")
if tryInstall and not state.installPerformed:
let command = getNimbleExecPath() & " install nimlangserver --accept"
let command = getNimbleExecPath() & " install nimlangserver --accept -l"
vscode.window.showInformationMessage(
cstring(fmt "Unable to find nimlangserver. Do you want me to attempt to install it via '{command}'?"),
VscodeMessageOptions(
Expand All @@ -156,10 +170,10 @@ proc startLanguageServer(tryInstall: bool, state: ExtensionState) {.async.} =
state.installPerformed = true
vscode.window.showInformationMessage(
cstring(fmt "Trying to install nimlangserver via '{command}'"))
let args: seq[cstring] = @["install nimlangserver", "--accept"]
let args: seq[cstring] = @["install nimlangserver", "--accept", "-l"]
var process = cp.spawn(
getNimbleExecPath(), args,
SpawnOptions(shell: true))
SpawnOptions(shell: true, cwd: getLocalLspDir()))
process.stdout.onData(proc(data: Buffer) =
outputLine(data.toString())
)
Expand Down Expand Up @@ -188,8 +202,8 @@ proc startLanguageServer(tryInstall: bool, state: ExtensionState) {.async.} =
vscode.window.showInformationMessage(cantInstallInfoMesssage)
else:
let nimlangserver = path.resolve(rawPath);
console.log(fmt"nimlangserver found: {nimlangserver}".cstring)
console.log("Starting nimlangserver.")
outputLine(fmt"nimlangserver found: {nimlangserver}".cstring)
outputLine("Starting nimlangserver.")
let latestVersion = await getLatestReleasedLspVersion(MinimalLSPVersion)
handleLspVersion(nimlangserver, latestVersion)

Expand All @@ -209,6 +223,7 @@ proc startLanguageServer(tryInstall: bool, state: ExtensionState) {.async.} =
serverOptions,
clientOptions)
await state.client.start()
outputLine("Nim Language Server started")

export startLanguageServer

Expand Down
2 changes: 2 additions & 0 deletions src/platform/js/jsNodeOs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ type

proc tmpdir*(os: OsModule): cstring {.importcpp.}

proc homedir*(os: OsModule): cstring {.importcpp.}

var nodeOs* = require("os").to(OsModule)

0 comments on commit ac159fa

Please sign in to comment.