Language Server Protocol and Text Editors

01 Oct 2022

The Language Server Protocol

According to Wikipedia the The Language Server Protocol (LSP) is an open, JSON-RPC-based protocol for use between source code editors or integrated development environments (IDEs) and servers that provide programming language-specific features. The goal of the protocol is to allow programming language support to be implemented and distributed independently of any given editor or IDE.

Helix

Helix is a Kakoune / Neovim inspired editor, written in Rust. Once installed you can ask for help:

% hx --help              
helix-term 22.08.1 (66276ce6)
Blaž Hrastnik <blaz@mxxn.io>
A post-modern text editor.

USAGE:
    hx [FLAGS] [files]...

ARGS:
    <files>...    Sets the input file to use, position can also be specified via
                  file[:row[:col]]

FLAGS:
    -h, --help                     Prints help information
    --tutor                        Loads the tutorial
    --health [CATEGORY]            Checks for potential errors in editor setup
                                   CATEGORY can be a language or one of
                                   'clipboard', 'languages' or 'all'.
                                   'all' is the default if not specified.
    -g, --grammar {fetch|build}    Fetches or builds tree-sitter grammars listed
                                   in languages.toml
    -c, --config <file>            Specifies a file to use for configuration
    -v                             Increases logging verbosity each use for up
                                   to 3 times
                                   (default file: ~/.cache/helix/helix.log)
    -V, --version                  Prints version information
    --vsplit                       Splits all given files vertically into
                                   different windows
    --hsplit                       Splits all given files horizontally into
                                   different windows

I recommend starting with hx --tutor to get a feeling for the editor and how it’s supposed to work. I did install Helix from source and therefore had to create a softlink in my ~/.config/helix directory. See link if you use another OS or prefer another install method.

% hx --health
Config file: default
Language file: default
Log file: /Users/jan/.cache/helix/helix.log
Runtime directory: /usr/local/Cellar/helix/22.08.1/libexec/runtime
Clipboard provider: pbpaste+pbcopy
System clipboard provider: pbpaste+pbcopy
...

A list of supported languages etc. is provided (which I skipped above via the ... output), but you can ask for each language (in this case Zig) like this:

% hx --health zig
Configured language server: zls
Binary for language server: /Users/jan/zls/zls
Configured debug adapter: None
Highlight queries: ✓
Textobject queries: ✘
Indent queries: ✓

How to install the default language servers is answered here.

Once you installed some of those language servers and played around with it within Helix you can as well use them somehwere else, like in my prefered text editor Emacs.

Emacs

If you don’t know much about Emacs you can have a look at this free documentation or, if you are more serious about it, read this excellent book to get started. Two main reasons to get started with Emacs are mentioned there as well:

  1. Org Mode - A GNU Emacs major mode for keeping notes, authoring documents, computational notebooks, literate programming, maintaining to-do lists, planning projects, and more — in a fast and effective plain text system.

  2. Magit - Magit is a complete text-based user interface to Git. It fills the glaring gap between the Git command-line interface and various GUIs, letting you perform trivial as well as elaborate version control tasks with just a couple of mnemonic key presses.

  3. And I give you another reason (beside playing around with language servers). If you bought for example the book mentioned above in EPUB format, you can read it within Emacs via the nov-mode (or nov.el - because it was written in Emacs Lisp)

For Emacs I recommend looking into those packages:

  1. Eglot - Emacs Polyglot: an Emacs LSP client that stays out of your way.

  2. lsp-mode - lsp-mode aims to provide IDE-like experience by providing optional integration with the most popular Emacs packages like company, flycheck and projectile.

  3. emacs-tree-sitter - tree-sitter is an Emacs binding for Tree-sitter (see below), an incremental parsing system.

Tree-Sitter

Tree-sitter was already mentioned in the Helix --help message (and on the Helix web-site).

Tree-sitter is a parser generator tool and an incremental parsing library.
It can build a concrete syntax tree for a source file and efficiently update
the syntax tree as the source file is edited.

Watch this video (Strange Loop 2018) to get started …

I don’t want to overwhelm you with too much information, but basically I had to write this down (not just for myself).