• Stars
    star
    410
  • Rank 105,468 (Top 3 %)
  • Language
    Nim
  • License
    MIT License
  • Created over 6 years ago
  • Updated 7 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Language Server Protocol implementation for Nim

Nim Language Server Protocol

This is a Language Server Protocol implementation in Nim, for Nim. It is based on nimsuggest, which means that every editor that supports LSP will now have the same quality of suggestions that has previously only been available in supported editors.

Installing nimlsp

If you have installed Nim through choosenim (recommended) the easiest way to install nimlsp is to use nimble with:

nimble install nimlsp

This will compile and install it in the nimble binary directory, which if you have set up nimble correctly it should be in your path. When compiling and using nimlsp it needs to have Nim's sources available in order to work. With Nim installed through choosenim these should already be on your system and nimlsp should be able to find and use them automatically. However if you have installed nimlsp in a different way you might run into issues where it can't find certain files during compilation/running. To fix this you need to grab a copy of Nim sources and then point nimlsp at them on compile-time by using -d:explicitSourcePath=PATH, where PATH is where you have your Nim sources. You can also pass them at run-time (if for example you're working with a custom copy of the stdlib by passing it as an argument to nimlsp. How exectly to do that will depend on the LSP client.

Compile nimlsp

If you want more control over the compilation feel free to clone the repository. nimlsp depends on the nimsuggest sources which are in the main Nim repository, so make sure you have a copy of that somewhere. Manually having a copy of Nim this way means the default source path will not work so you need to set it explicitly on compilation with -d:explicitSourcePath=PATH and point to it at runtime (technically the runtime should only need the stdlib, so omitting it will make nimlsp try to find it from your Nim install). As of Nim 2.0.0 you must run the 'build_all' script in the Nim repository first (nimsuggest expects to import a file that is not otherwise present).

To do the standard build run:

nimble build

Or if you want debug output when nimlsp is running:

nimble debug

Or if you want even more debug output from the LSP format:

nimble debug -d:debugLogging

Supported Protocol features

Status LSP Command
☑ DONE textDocument/didChange
☑ DONE textDocument/didClose
☑ DONE textDocument/didOpen
☑ DONE textDocument/didSave
☐ TODO textDocument/codeAction
☑ DONE textDocument/completion
☑ DONE textDocument/definition
☐ TODO textDocument/documentHighlight
☑ DONE textDocument/documentSymbol
☐ TODO textDocument/executeCommand
☐ TODO textDocument/format
☑ DONE textDocument/hover
☑ DONE textDocument/rename
☑ DONE textDocument/references
☑ DONE textDocument/signatureHelp
☑ DONE textDocument/publishDiagnostics
☐ TODO workspace/symbol

Setting up nimlsp

Sublime Text

Install the LSP plugin. Install the NimLime plugin for syntax highlighting.

To set up LSP, run Preferences: LSP settings from the command palette and add the following:

{
   "clients": {
      "nimlsp": {
         "command": ["nimlsp"],
         "enabled": true,

         // ST4 only
         "selector": "source.nim",

         // ST3 only
         "languageId": "nim",
         "scopes": ["source.nim"],
         "syntaxes": ["Packages/NimLime/Syntaxes/Nim.sublime-syntax"]
      }
   }
}

Note: Make sure ``<path/to>/.nimble/bin`` is added to your ``PATH``.

To enable syntax highlighting in popups, run Preferences: settings and add the following:

"mdpopups.use_sublime_highlighter": true,
"mdpopups.sublime_user_lang_map": {
   "nim":
   [
      [
         "nim"
      ],
      [
         "NimLime/Syntaxes/Nim"
      ]
   ]
}

Vim

To use nimlsp in Vim install the prabirshrestha/vim-lsp plugin and dependencies:

Plugin 'prabirshrestha/asyncomplete.vim'
Plugin 'prabirshrestha/async.vim'
Plugin 'prabirshrestha/vim-lsp'
Plugin 'prabirshrestha/asyncomplete-lsp.vim'

Then set it up to use nimlsp for Nim files:

let s:nimlspexecutable = "nimlsp"
let g:lsp_log_verbose = 1
let g:lsp_log_file = expand('/tmp/vim-lsp.log')
" for asyncomplete.vim log
let g:asyncomplete_log_file = expand('/tmp/asyncomplete.log')

let g:asyncomplete_auto_popup = 0

if has('win32')
   let s:nimlspexecutable = "nimlsp.cmd"
   " Windows has no /tmp directory, but has $TEMP environment variable
   let g:lsp_log_file = expand('$TEMP/vim-lsp.log')
   let g:asyncomplete_log_file = expand('$TEMP/asyncomplete.log')
endif
if executable(s:nimlspexecutable)
   au User lsp_setup call lsp#register_server({
   \ 'name': 'nimlsp',
   \ 'cmd': {server_info->[s:nimlspexecutable]},
   \ 'whitelist': ['nim'],
   \ })
endif

function! s:check_back_space() abort
    let col = col('.') - 1
    return !col || getline('.')[col - 1]  =~ '\s'
endfunction

inoremap <silent><expr> <TAB>
  \ pumvisible() ? "\<C-n>" :
  \ <SID>check_back_space() ? "\<TAB>" :
  \ asyncomplete#force_refresh()
inoremap <expr><S-TAB> pumvisible() ? "\<C-p>" : "\<C-h>"

This configuration allows you to hit Tab to get auto-complete, and to call various functions to rename and get definitions. Of course you are free to configure this any way you'd like.

Emacs

With lsp-mode and use-package:

(use-package nim-mode
  :ensure t
  :hook
  (nim-mode . lsp))

Or with Eglot

(add-to-list 'eglot-server-programs
          '(nim-mode "nimlsp"))

Intellij

You will need to install the LSP support plugin. For syntax highlighting i would recommend the "official" nim plugin (its not exactly official, but its developed by an intellij dev), the plugin will eventually use nimsuggest and have support for all this things and probably more, but since its still very new most of the features are still not implemented, so the LSP is a decent solution (and the only one really).

To use it:

  1. Install the LSP and the nim plugin.
  2. Go into settings > Language & Frameworks > Language Server Protocol > Server Definitions.
  3. Set the LSP mode to executable, the extension to nim and in the Path, the path to your nimlsp executable.
  4. Hit apply and everything should be working now.

Kate

The LSP plugin has to be enabled in the Kate (version >= 19.12.0) plugins menu:

  1. In Settings > Configure Kate > Application > Plugins, check box next to LSP Client to enable LSP functionality.
  2. Go to the now-available LSP Client menu (Settings > Configure Kate > Application) and enter the following in the User Server Settings tab:
{
    "servers": {
        "nim": {
            "command": [".nimble/bin/nimlsp"],
            "url": "https://github.com/PMunch/nimlsp",
            "highlightingModeRegex": "^Nim$"
        }
    }
}

This assumes that nimlsp was installed through nimble. Note: Server initialization may fail without full path specified, from home directory, under the ``"command"`` entry, even if nimlsp is in system's ``PATH``.

Run Tests

Not too many at the moment unfortunately, but they can be run with:

nimble test

More Repositories

1

futhark

Automatic wrapping of C headers in Nim
Nim
341
star
2

protobuf-nim

Protobuf implementation in pure Nim that leverages the power of the macro system to not depend on any external tools
Nim
166
star
3

ratel

Nim
122
star
4

nimcr

Nim
81
star
5

badger

Keyboard firmware written from scratch using Nim
Nim
73
star
6

binaryparse

Binary parser for Nim
Nim
69
star
7

macroutils

A package that makes creating macros easier
Nim
59
star
8

nancy

Nancy - Nim fancy ANSI tables
Nim
53
star
9

nim-playground-frontend

The front-end for https://play.nim-lang.org
Nim
44
star
10

jsonschema

Schema validation of JSON for Nim
Nim
42
star
11

nim-optionsutils

Utility macros for easier handling of options in Nim
Nim
35
star
12

superlog

Nim
28
star
13

i3ColourChanger

Python
28
star
14

termstyle

Easy to use styles for terminal output
Nim
28
star
15

nim-persistent-vector

Implementation of Clojures persistent vector in Nim for easy immutable lists.
Nim
25
star
16

SDLGamelib

A library of functions to make creating games using Nim and SDL2 easier. This does not intend to be a full blown engine and tries to keep all the components loosely coupled so that individual parts can be used separately.
Nim
25
star
17

notifishower

Nim
21
star
18

nim-electron

Nim
18
star
19

notificatcher

Simple program to read freedesktop notifications and format them as strings
Nim
18
star
20

Configuration

Shell
18
star
21

combparser

A parser combinator library for easy generation of complex parsers
Nim
18
star
22

strslice

Nim
17
star
23

nim-pcap

Tiny pure Nim library to read PCAP files
Nim
16
star
24

labeltry

A new approach to dealing with exceptions
Nim
16
star
25

drawille-nim

Drawing in terminal with Unicode Braille characters. This is the Nim version of the Python original.
Nim
16
star
26

plotter

Simple tool to plot input piped to it
Nim
14
star
27

ikeahomesmart

IKEA Home Smart library for Nim
Nim
13
star
28

libkeepass

Library for reading KeePass files and decrypt the passwords within it
Nim
12
star
29

autotemplate

Nim
12
star
30

nim-cache

Simple cache module for Nim, supports LRU and max-count pruning
Nim
12
star
31

genui

This is what might become a really kick-ass cross-platform native UI toolkit
Nim
10
star
32

getmac

Package to get MAC addresses from an IP address in Nim
Nim
9
star
33

ansiparse

Nim library to parse ANSI escape codes
Nim
9
star
34

xevloop

Library to more easily create X11 event loops
Nim
9
star
35

aoc2021

Advent of Code 2021 solutions in Nim
Nim
9
star
36

stacklang

A stack based calculator/minimal language
Nim
8
star
37

gtkgenui

The genui DSL for creating GUIs implemented for the gtk2 bindings in nimble.
Nim
7
star
38

nimscriptexamples

Examples for my article on embedding NimScript
Nim
7
star
39

nimbleutils

A Nimble package to inspect Nimble packages
Nim
7
star
40

mapm-nim

Matt's Arbitrary Precision Math library - Nim wrapper
Nim
6
star
41

deriveables

Deriveable types in Nim
Nim
6
star
42

ratel-bme280

BME280 implementation for Ratel
Nim
6
star
43

libcoap

Nim wrapper for libcoap
Nim
6
star
44

skeletal

HTML
6
star
45

gtk3genui

The genui DSL for creating GUIs implemented for the gtk3 bindings by StefanSalewski/ngtk3.
Nim
6
star
46

libfuse-nim

Nim
5
star
47

nimtours

A tour of Nim, in multiple parts. Available online at https://play.nim-lang.org/tours/index.html
HTML
5
star
48

MannBarSchwein-arduboy

Arduboy game jam game
C++
4
star
49

aoc2020

Nim
4
star
50

Imlib2

Nim
4
star
51

termfm

Nim
4
star
52

aoc2022

Nim
4
star
53

webexperiment

Nim
3
star
54

TromsoGameJam2017

Nim
3
star
55

fibers

Fibers in Nim, experiment
Nim
3
star
56

zhangshasha

This module is a port of the Java implementation of the Zhang-Shasha algorithm for tree edit distance
Nim
3
star
57

statusbar

Libfuse based statusbar for Nimdow and other xsetroot -name WMs
Nim
3
star
58

ansitohtml

Converts ANSI colour codes to HTML span tags with style tags
Nim
2
star
59

conf.nim-lang.org

Simple conference information page for NimConf2020
CSS
2
star
60

nim-coroutines

A simple coroutines library for Nim based in iterators, untested and mostly for experimentation
Nim
2
star
61

ArcticGameJam2014

JavaScript
2
star
62

libbuilder

Tool to create condensed Nim standard libraries for NimScript integration
Nim
2
star
63

nim-homeassistant

Nim
1
star
64

aoc2023

Nim
1
star
65

echooverride

Simple test to override the Nim standard output in the entire project
Nim
1
star
66

colourfinder

Create nice spectrum images of colours
Nim
1
star
67

femtozip

Nim
1
star
68

pangoterm-altfonts

A copy of the original pangoterm found here: https://www.leonerd.org.uk/code/pangoterm/ but with improved support for alternate fonts
C
1
star
69

data.vm

Experiment into a data based VM
Nim
1
star