GitHub - nim-lang/langserver: The Nim language server implementation (based on nimsuggest) (original) (raw)

Nim Language Server

Nim Language Server, or nimlangserver, is a language server for Nim.

Installation

IMPORTANT you might want to use latest build of the nimlangserver and/or build it from source.

nimlangserver requires nimble >= 0.16.1

Installing binaries

NB: nimlangserver requires nimsuggest version that supports --v3:

You can install the latest release into $HOME/.nimble/bin using e.g.:

nimble install nimlangserver

From Source

VSCode

Sublime Text

Install LSP-nimlangserver from Package Control.

Zed Editor

Install Nim Extenstion from the Zed Editor extensions.

Helix

Just install the langserver with Nimble and make sure it's on your PATH.

To verify that everything is set up, run:

$ hx --health nim Configured language servers: ✓ nimlangserver: /home/username/.nimble/bin/nimlangserver Configured debug adapter: None Configured formatter: ✓ /home/username/.nimble/bin/nph Tree-sitter parser: ✓ Highlight queries: ✓ Textobject queries: ✓ Indent queries: ✓

Neovim

lua <<EOF

require'lspconfig'.nim_langserver.setup{ settings = { nim = { nimsuggestPath = "~/.nimble/bin/custom_lang_server_build" } } }

EOF

Change configuration to your liking (most probably you don't need to provide any settings at all, defaults should work fine for the majority of the users). You might also want to read lsp-config documentation to setup key bindings, autocompletion and so on.

IMPORTANT you might want to use latest build of the nimlangserver and/or build it from source.

VIM/Neovim

{ "languageserver": { "nim": { "command": "nimlangserver", "filetypes": ["nim"], "trace.server": "verbose", "settings": { "nim": { "nimsuggestPath": "~/.nimble/bin/nimsuggest" } } } } }

Of course, change the configuration to your liking. You might also want to read coc.nvim documentation to setup key bindings, autocompletion and so on.

Configuration Options

{ "nim.projectMapping": [{ // open files under tests using one nimsuggest instance started with root = test/all.nim "projectFile": "tests/all.nim", "fileRegex": "tests/.*\.nim" }, { // everything else - use main.nim as root. "projectFile": "main.nim", "fileRegex": ".*\.nim" }] }

Note when in a nimble project, nimble will drive the entry points for nimsuggest.

Inlay Hints

Inlay hints are visual snippets displayed by the editor inside your code.

Typical usage for inlay hints is displaying type information but it can go beyond that and provide all sorts of useful context.

nimlangserver offers three kinds of inlay hints:

Here's how inlay hints look like in VSCode

Here are the same hints in Helix:

In VSCode, inlay hints are enabled by default. You can toggle different kinds of hints in the settings:

  1. Go to Settings.
  2. Search inlay.
  3. Go to Nim configuration.

To enable inlay hints in Neovim, configure lspconfig in your init.vim:

lua << EOF

lspconfig.nim_langserver.setup({ settings = { nim = { inlayHints = { typeHints = true, exceptionHints = true, parameterHints = true, } } },

-- Enable hints on LSP attach on_attach = function(client, bufnr) if client.server_capabilities.inlayHintProvider then vim.lsp.inlay_hint.enable(true, { bufnr = bufnr }) end end })

EOF

In Vim, use the coc configuration to enable inlay hints (see VIM/Neovim).

To enable inlay hints in Helix, add this langserver configuration to your languages.toml (you can toggle different hint kinds individually):

[language-server.nimlangserver.config.nim] inlayHints = { typeHints = true, exceptionHints = true, parameterHints = true }

Features

nimlangserver supports the following LSP features:

You can install nimlangserver using the instructions for your text editor below:

Extension methods

In addition to the standard LSP methods, nimlangserver provides additional nim specific methods.

extension/macroExpand

type ExpandTextDocumentPositionParams* = ref object of RootObj textDocument*: TextDocumentIdentifier position*: Position level*: Option[int]

Where:

type ExpandResult* = ref object of RootObj range*: Range content*: string

Where:

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"
}

VSCode

Install the vscode-nim extension from here

Emacs

(add-hook 'nim-mode-hook #'lsp)

Transport

nimlangserver supports two transport modes:

To use socket mode, start nimlangserver with the --socket flag. You can set the port using the --port flag. If you don't specify the port, nimlangserver will automatically find an open port and print it in the console.

Test Runner

In order to list and run tests the test library unittest2 >= 0.2.4 must be used. An entry point for the tests must be provided via the vsc extension nim.test.entryPoint or testEntryPoint in future versions of nimble

License

MIT