From e10f811e38c75f830a4a3b5698a42ef2443b2f5b Mon Sep 17 00:00:00 2001 From: Thomas Bruyelle Date: Fri, 27 Oct 2023 20:09:13 +0200 Subject: [PATCH] docs(contributing): add ViM instructions to setup gnols (#1282) Relates to #1274 What works with this setup: - code completion after `.` but only for stdlibs packages - code hover tooltip with `:LspHover` command (again limited to stdlibs) - format on save (only gofmt for now) - code Lens with `:LspCodeLens` command (only when the buffer is `_test.gno` file) which display a list of possible `gno test` executions with different scopes (package, files or function). Note that test executions are using the LSP `workspace/executeCommand` and actually `vim-lsp` doesn't handle well the output of that. That means, tests are running, but you don't get the ouput. I'm still unsure about how to setup that properly (see https://github.com/prabirshrestha/vim-lsp/issues/1461). The good thing is the limitations can be removed by contributing to [gnols](https://github.com/gno-playground/gnols), for instance : - Expand code completion to all imported types - Expand code hover to all imported types - Add the *go-to-definition* feature - Add the *rename* feature - ... --------- Co-authored-by: Morgan --- CONTRIBUTING.md | 61 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 60 insertions(+), 1 deletion(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 279b7869152..1739d50f034 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -80,7 +80,7 @@ There currently is an unofficial [Visual Studio Code](https://marketplace.visual extension (primarily developed by a core team member) for working with `*.gno` files. -#### ViM Support +#### ViM Support (without LSP) Add to your `.vimrc` file: @@ -104,9 +104,68 @@ To use *gofumpt* instead of *gofmt*, as hinted in the comment, you may either ha cexpr system('go run -modfile /misc/devdeps/go.mod mvdan.cc/gofumpt -w ' . expand('%')) ``` +### ViM Support (with LSP) + There is an experimental and unofficial [Gno Language Server](https://github.com/jdkato/gnols) developed by the community, with an installation guide for Neovim. +For ViM purists, you have to install the [`vim-lsp`](https://github.com/prabirshrestha/vim-lsp) +plugin and then register the LSP server in your `.vimrc` file: + +```vim +augroup gno_autocmd + autocmd! + autocmd BufNewFile,BufRead *.gno + \ set filetype=gno | + \ set syntax=go +augroup END + +if (executable('gnols')) + au User lsp_setup call lsp#register_server({ + \ 'name': 'gnols', + \ 'cmd': ['gnols'], + \ 'allowlist': ['gno'], + \ 'config': {}, + \ 'workspace_config': { + \ 'root' : '/path/to/gno_repo', + \ 'gno' : '/path/to/gno_bin', + \ 'precompileOnSave' : v:true, + \ 'buildOnSave' : v:false, + \ }, + \ 'languageId': {server_info->'gno'}, + \ }) +else + echomsg 'gnols binary not found: LSP disabled for Gno files' +endif + +function! s:on_lsp_buffer_enabled() abort + " Autocompletion + setlocal omnifunc=lsp#complete + " Format on save + autocmd BufWritePre LspDocumentFormatSync + " Some optionnal mappings + nmap i (lsp-hover) + " Following mappings are not supported yet by gnols + " nmap gd (lsp-definition) + " nmap rr (lsp-rename) +endfunction +augroup lsp_install + au! + autocmd User lsp_buffer_enabled call s:on_lsp_buffer_enabled() +augroup END +``` + +Note that unlike the previous ViM setup without LSP, here it is required by +`vim-lsp` to have a specific `filetype=gno`. Syntax highlighting is preserved +thanks to `syntax=go`. + +Inside `lsp#register_server()`, you also have to replace +`workspace_config.root` and `workspace_config.gno` with the correct directories +from your machine. + +Additionaly, it's not possible to use `gofumpt` for code formatting with +`gnols` for now. + #### Emacs Support 1. Install [go-mode.el](https://github.com/dominikh/go-mode.el).