xref: /aosp_15_r20/external/bazelbuild-rules_go/docs/go/editors/vim.md (revision 9bb1b549b6a84214c53be0924760be030e66b93a)
1# Vim + `rules_go`
2
3`rules_go` has support for integration with text editors, see [Editor and tool
4integration](https://github.com/bazelbuild/rules_go/wiki/Editor-and-tool-integration).
5This document describes integration with Vim, which will provide semantic
6autocompletion for Bazel-generated Go files, among other things.
7
8## Setup
9
101. Install [vim-go](https://github.com/fatih/vim-go), a Vim plugin for Go
11   development with support for `gopls`.
12
132. Follow the instructions from [Editor
14   setup](https://github.com/bazelbuild/rules_go/wiki/Editor-setup#3-editor-setup)
15   for installing `gopls` and adding a launcher script.
16   * Note that `gopls` should already be installed as part of installing `vim-go`.
17
183. Add the following to your `.vimrc`:
19
20      ```vim
21      function! MaybeSetGoPackagesDriver()
22        " Start at the current directory and see if there's a WORKSPACE file in the
23        " current directory or any parent. If we find one, check if there's a
24        " gopackagesdriver.sh in a tools/ directory, and point our
25        " GOPACKAGESDRIVER env var at it.
26        let l:dir = getcwd()
27        while l:dir != "/"
28          if filereadable(simplify(join([l:dir, 'WORKSPACE'], '/')))
29            let l:maybe_driver_path = simplify(join([l:dir, 'tools/gopackagesdriver.sh'], '/'))
30            if filereadable(l:maybe_driver_path)
31              let $GOPACKAGESDRIVER = l:maybe_driver_path
32              break
33            end
34          end
35          let l:dir = fnamemodify(l:dir, ':h')
36        endwhile
37      endfunction
38
39      call MaybeSetGoPackagesDriver()
40
41      " See https://github.com/golang/tools/blob/master/gopls/doc/settings.md
42      let g:go_gopls_settings = {
43        \ 'build.directoryFilters': [
44          \ '-bazel-bin',
45          \ '-bazel-out',
46          \ '-bazel-testlogs',
47          \ '-bazel-mypkg',
48        \ ],
49        \ 'ui.completion.usePlaceholders': v:true,
50        \ 'ui.semanticTokens': v:true,
51        \ 'ui.codelenses': {
52          \ 'gc_details': v:false,
53          \ 'regenerate_cgo': v:false,
54          \ 'generate': v:false,
55          \ 'test': v:false,
56          \ 'tidy': v:false,
57          \ 'upgrade_dependency': v:false,
58          \ 'vendor': v:false,
59        \ },
60      \ }
61      ```
62
63    * You'll want to replace `-bazel-mypkg` with your package.
64    * If you've put your `gopackagesdriver.sh` script somewhere other than
65      `tools/gopackagesdriver.sh`, you'll need to update
66      `MaybeSetGoPackagesDriver` accordingly.
67