zls/README.md

260 lines
9.3 KiB
Markdown
Raw Normal View History

# Zig Language Server
2020-05-14 15:07:14 +01:00
2020-05-07 18:54:58 +01:00
Zig Language Server, or `zls`, is a language server for Zig. The Zig wiki states that "The Zig community is decentralized" and "There is no concept of 'official' or 'unofficial'", so instead of calling `zls` unofficial, and I'm going to call it a cool option, one of [many](https://github.com/search?q=zig+language+server).
2020-04-24 23:19:03 +01:00
2020-05-15 05:01:54 +01:00
<!-- omit in toc -->
## Table Of Contents
2020-05-15 05:00:44 +01:00
- [Installation](#installation)
- [Build Options](#build-options)
- [Configuration Options](#configuration-options)
- [Usage](#usage)
- [VSCode](#vscode)
2020-05-24 16:56:51 +01:00
- [Sublime Text 3](#sublime-text-3)
2020-05-27 08:48:22 +01:00
- [Kate](#kate)
2020-05-28 18:16:37 +01:00
- [Neovim/Vim8](#neovimvim8)
2020-06-08 09:21:30 +01:00
- [Emacs](#emacs)
- [Doom Emacs](#doom-emacs)
2020-05-15 05:00:44 +01:00
- [Related Projects](#related-projects)
- [License](#license)
2020-04-24 23:19:03 +01:00
## Installation
2020-11-22 20:09:31 +00:00
Installation starts with downloading an official release from the [Releases page](https://github.com/zigtools/zls/releases).
See [Downloading and Building ZLS](https://github.com/zigtools/zls/wiki/Downloading-and-Building-ZLS) on the Wiki, or the page about [using ZLS with Visual Studio Code](https://github.com/zigtools/zls/wiki/Installing-for-Visual-Studio-Code) for a guide to help get `zls` running in your editor.
### Installing binaries
#### MacOS
You can install the latest release into `$HOME/zls` using e.g.:
```sh
brew install xz
mkdir $HOME/zls && cd $HOME/zls && curl -L https://github.com/zigtools/zls/releases/download/0.1.0/x86_64-macos.tar.xz | tar -xJ --strip-components=1 -C .
```
#### Linux
You can install the latest release into `$HOME/zls` using e.g.:
```
sudo apt install xz-utils
mkdir $HOME/zls && cd $HOME/zls && curl -L https://github.com/zigtools/zls/releases/download/0.1.0/x86_64-linux.tar.xz | tar -xJ --strip-components=1 -C .
```
2020-11-22 20:09:31 +00:00
### From Source
Building `zls` is very easy. You will need [a build of Zig master](https://ziglang.org/download/) to build zls.
2020-05-07 18:54:58 +01:00
2020-04-24 23:19:03 +01:00
```bash
git clone --recurse-submodules https://github.com/zigtools/zls
2020-04-24 23:19:03 +01:00
cd zls
2020-11-22 20:17:48 +00:00
zig build -Drelease-safe
zig build config # Configure ZLS
2020-04-24 23:19:03 +01:00
```
2020-11-22 20:09:31 +00:00
*For detailed building instructions, see the Wiki page about [Cloning With Git](https://github.com/zigtools/zls/wiki/Downloading-and-Building-ZLS#cloning-with-git).*
2020-05-07 18:54:58 +01:00
2020-11-22 20:09:31 +00:00
#### Build Options
<!-- When updating this table, be sure to copy changes to the Wiki page about building from source. -->
<!-- If this table grows too large, then delete this one and move it all over to the Wiki page about building from source. -->
2020-05-18 17:56:43 +01:00
| Option | Type | Default Value | What it Does |
| --- | --- | --- | --- |
| `-Ddata_version` | `string` (master or 0.7.0) | master | The data file version. This selects the files in the `src/data` folder that correspond to the Zig version being served.|
2020-05-07 18:54:58 +01:00
2020-05-15 05:00:44 +01:00
### Configuration Options
2020-05-19 20:22:02 +01:00
You can configure zls by providing a zls.json file.
zls will look for a zls.json configuration file in multiple locations with the following priority:
- In the local configuration folder of your OS (as provided by [known-folders](https://github.com/ziglibs/known-folders#folder-list))
- In the same directory as the executable
The following options are currently available.
| Option | Type | Default value | What it Does |
| --- | --- | --- | --- |
| `enable_snippets` | `bool` | `false` | Enables snippet completions when the client also supports them. |
| `zig_lib_path` | `?[]const u8` | `null` | zig library path, e.g. `/path/to/zig/lib/zig`, used to analyze std library imports. |
2020-05-30 21:39:57 +01:00
| `zig_exe_path` | `?[]const u8` | `null` | zig executable path, e.g. `/path/to/zig/zig`, used to run the custom build runner. If `null`, zig is looked up in `PATH`. Will be used to infer the zig standard library path if none is provided. |
2020-05-15 20:10:53 +01:00
| `warn_style` | `bool` | `false` | Enables warnings for style *guideline* mismatches |
| `build_runner_path` | `?[]const u8` | `null` | Path to the build_runner.zig file provided by zls. `null` is equivalent to `${executable_directory}/build_runner.zig` |
| `build_runner_cache_path` | `?[]const u8` | `null` | Path to a directroy that will be used as zig's cache when running `zig run build_runner.zig ...`. `null` is equivalent to `${KnownFloders.Cache}/zls` |
2020-11-15 22:07:35 +00:00
| `enable_semantic_tokens` | `bool` | `true` | Enables semantic token support when the client also supports it. |
| `operator_completions` | `bool` | `true` | Enables `*` and `?` operators in completion lists. |
2021-03-07 17:45:37 +00:00
| `skip_std_references` | `bool` | `false` | When true, skips searching for references in std. Improves lookup speed for functions in user's code. Renaming and go-to-definition will continue to work as is.
## Features
2020-04-24 23:19:03 +01:00
`zls` supports most language features, including simple type function support, usingnamespace, payload capture type resolution, custom packages and others.
Notable language features that are not currently implemented include `@cImport` as well as most forms of compile time evaluation.
The following LSP features are supported:
- Completions
- Hover
- Goto definition/declaration
- Document symbols
- Find references
- Rename symbol
- Formatting using `zig fmt`
- Semantic token highlighting (LSP 3.16 proposed feature, implemented by a few clients including VSCode, kak and emacs lsp-mode)
You can install `zls` using the instuctions for your text editor below:
2020-04-24 23:19:03 +01:00
### VSCode
Install the `zls-vscode` extension from [here](https://github.com/zigtools/zls-vscode/releases) and provide a path to the build `zls` executable.
2020-05-07 18:54:58 +01:00
### Sublime Text 3
- Install the `LSP` package from [here](https://github.com/sublimelsp/LSP/releases) or via Package Control.
- Add this snippet to `LSP's` user settings:
```json
{
"clients": {
"zig":{
"command": ["zls"],
"enabled": true,
"languageId": "zig",
2020-05-26 11:06:13 +01:00
"scopes": ["source.zig"],
"syntaxes": ["Packages/Zig Language/Syntaxes/Zig.tmLanguage"]
}
}
}
```
2020-05-27 08:48:22 +01:00
### Kate
- Enable `LSP client` plugin in Kate settings.
- Add this snippet to `LSP client's` user settings (e.g. /$HOME/.config/kate/lspclient)
(or paste it in `LSP client's` GUI settings)
2020-05-27 08:48:22 +01:00
```json
{
"servers": {
"zig": {
"command": ["zls"],
"url": "https://github.com/zigtools/zls",
"highlightingModeRegex": "^Zig$"
}
}
}
```
2020-05-28 18:14:59 +01:00
### Neovim/Vim8
#### CoC
2020-05-28 18:14:59 +01:00
- Install the CoC engine from [here](https://github.com/neoclide/coc.nvim).
- Issue `:CocConfig` from within your Vim editor, and the following snippet:
2020-05-28 18:14:59 +01:00
```json
{
2020-06-08 12:18:46 +01:00
"languageserver": {
2020-05-28 18:14:59 +01:00
"zls" : {
"command": "command_or_path_to_zls",
"filetypes": ["zig"]
}
}
}
```
#### nvim-lspconfig
Requires Nvim 0.5 (HEAD)!
- Install nvim-lspconfig from [here](https://github.com/neovim/nvim-lspconfig).
- Install zig.vim from [here](https://github.com/ziglang/zig.vim).
2020-12-24 00:04:45 +00:00
nvim-lspconfig already ships a configuration for zls. A simple `init.vim` might look like this:
```vim
call plug#begin('~/.config/nvim/plugged')
Plug 'neovim/nvim-lspconfig'
Plug 'nvim-lua/completion-nvim'
Plug 'ziglang/zig.vim'
call plug#end()
:lua << EOF
local lspconfig = require('lspconfig')
local on_attach = function(_, bufnr)
vim.api.nvim_buf_set_option(bufnr, 'omnifunc', 'v:lua.vim.lsp.omnifunc')
require('completion').on_attach()
end
local servers = {'zls'}
for _, lsp in ipairs(servers) do
lspconfig[lsp].setup {
on_attach = on_attach,
}
end
EOF
" Set completeopt to have a better completion experience
set completeopt=menuone,noinsert,noselect
" Enable completions as you type
let g:completion_enable_auto_popup = 1
```
#### LanguageClient-neovim
- Install the LanguageClient-neovim from [here](https://github.com/autozimu/LanguageClient-neovim)
- Edit your neovim configuration and add `zls` for zig filetypes:
```vim
let g:LanguageClient_serverCommands = {
\ 'zig': ['~/code/zls/zig-cache/bin/zls'],
\ }
```
2020-06-08 09:21:30 +01:00
### Emacs
- Install [lsp-mode](https://github.com/emacs-lsp/lsp-mode) from melpa
- [zig mode](https://github.com/ziglang/zig-mode) is also useful
```elisp
(require 'lsp)
(add-to-list 'lsp-language-id-configuration '(zig-mode . "zig"))
(lsp-register-client
(make-lsp-client
:new-connection (lsp-stdio-connection "<path to zls>")
:major-modes '(zig-mode)
:server-id 'zls))
```
### Doom Emacs
- Enable the `lsp` module
2021-03-17 23:51:39 +00:00
- Install the [zig-mode](https://github.com/ziglang/zig-mode) package (add `(package! zig-mode)` to your `packages.el` file
```elisp
(require 'lsp)
(use-package! zig-mode
:hook ((zig-mode . lsp))
:custom (zig-format-on-save nil)
:init
(add-to-list 'lsp-language-id-configuration '(zig-mode . "zig"))
(lsp-register-client
(make-lsp-client
:new-connection (lsp-stdio-connection "<path to zls>")
:major-modes '(zig-mode)
:server-id 'zls)))
```
2020-05-07 18:54:58 +01:00
## Related Projects
2020-05-07 18:54:58 +01:00
- [`sublime-zig-language` by @prime31](https://github.com/prime31/sublime-zig-language)
- Supports basic language features
- Uses data provided by `src/data` to perform builtin autocompletion
2020-05-15 05:00:44 +01:00
- [`zig-lsp` by @xackus](https://github.com/xackus/zig-lsp)
- Inspiration for `zls`
2020-05-19 20:22:02 +01:00
- [`known-folders` by @ziglibs](https://github.com/ziglibs/known-folders)
2020-06-05 17:29:34 +01:00
- Provides API to access known folders on Linux, Windows and Mac OS
2020-05-07 18:54:58 +01:00
## License
2020-05-07 18:54:58 +01:00
MIT