Nim Language Server, or nimlangserver
, is a language server for Nim.
- vscode-nim has support for
nimlangserver
. Follow the instructions at: https://github.com/saem/vscode-nim#nim-lanugage-server-integration-experimental
NB: nimlangserver
requires nimsuggest
version that supports --v3
:
You can install the latest release into $HOME/.nimble/bin
using e.g.:
nimble install nimlangserver
nimble build
nim.projectMapping
- configure hownimsuggest
should be started. Here it is sample configuration forVScode
. We don't wantnimlangserver
to startnimsuggest
for each file and this configuration will allow configuring pairprojectPath
/fileRegex
so when one of the regexp in the list matches current file thennimls
will useroot
to startnimsuggest
. In case there are no matchesnimlangserver
will try to guess the most suitable project root.nim.timeout
- the request timeout in ms after whichnimlangserver
will restart the language server. If not specified the default is 2 minutes.nim.nimsuggestPath
- the path to thenimsuggest
. The default is"nimsuggest"
.nim.autoCheckFile
- check the file on the flynim.autoCheckProject
- check the project after saving the filenim.autoRestart
- auto restart once in case ofnimsuggest
crash. Note that the server won't restart if there weren't any successful calls after the last restart.
{
"nim.projectMapping": [{
// open files under tests using one nimsuggest instance started with root = test/all.nim
"projectPath": "tests/all.nim",
"fileRegex": "tests/.*\\.nim"
}, {
// everything else - use main.nim as root.
"projectPath": "main.nim",
"fileRegex": ".*\\.nim"
}]
}
nimlangserver
supports the following LSP features:
- Completions
- Hover
- Goto definition
- Document symbols
- Find references
- Workspace symbols
- Rename symbols
You can install nimlangserver
using the instuctions for your text editor below:
In addition to the standard LSP
methods, nimlangserver
provides additional nim specific methods.
- Request:
type
ExpandTextDocumentPositionParams* = ref object of RootObj
textDocument*: TextDocumentIdentifier
position*: Position
level*: Option[int]
Where:
position
is the position in the document.textDocument
is the document.level
is the how much levels to expand from the current position
- Response:
type
ExpandResult* = ref object of RootObj
range*: Range
content*: string
Where:
content
is the expand resultrange
is the original range of the request.
Here it is sample request/response:
[Trace - 11:10:09 AM] Sending request 'extension/macroExpand - (141)'.
Params: {
"textDocument": {
"uri": "file:///.../tests/projects/hw/hw.nim"
},
"position": {
"line": 27,
"character": 2
},
"level": 1
}
[Trace - 11:10:10 AM] Received response 'extension/macroExpand - (141)' in 309ms.
Result: {
"range": {
"start": {
"line": 27,
"character": 0
},
"end": {
"line": 28,
"character": 19
}
},
"content": " block:\n template field1(): untyped =\n a.field1\n\n template field2(): untyped =\n a.field2\n\n field1 = field2"
}
Install the vscode-nim
extension from here
- Install lsp-mode and
nim-mode
from melpa and add the following to your config:
(add-hook 'nim-mode-hook #'lsp)
-
Both
nimlangserver
andnimlsp
are based onnimsuggest
, but the main difference is thatnimlsp
has a single specific version ofnimsuggest
embedded in the server executable, whilenimlangserver
launchesnimsuggest
as an external process. This allowsnimlangserver
to handle anynimsuggest
crashes more gracefully.
MIT