summaryrefslogtreecommitdiff
path: root/Documentation/NvimConfiguration.md
blob: 86a4a8977a44d8b7556d3b9695bf125e21dc32c8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# NVim Project Configuration

NVim can be configured to use the [COC-clangd](https://github.com/clangd/coc-clangd)
plugin to provide code-completion as well as inline
[git blame](https://github.com/f-person/git-blame.nvim) using [vim-plug](https://github.com/junegunn/vim-plug).

Make sure you ran `./Meta/serenity.sh rebuild-toolchain` as well as
`./Meta/serenity.sh run`.

# Install vim-plug

See [https://github.com/junegunn/vim-plug](https://github.com/junegunn/vim-plug).

# Install coc.nvim

The config file for neovim is at `~/.config/nvim/init.vim` or if
set `$XDG_CONFIG_HOME/nvim/init.vim`.

Add the Plugin:

```vim
Plug 'neoclide/coc.nvim', { 'branch': 'release' }
```

Run `:PlugInstall` inside nvim.

# Install coc-clangd via CocInstall

```vim
:CocInstall coc-clangd
```

> **Note**: This guide is tested with clangd version 14.0.6 and 15.0.6.

In case you have not installed clangd already install it with
`:CocCommand clangd.install`.

This will install a separate version of clangd just for neovim.

# Configure coc-clangd in coc-settings.json

Use the following settings to ensure that coc-clangd works out of the box.

> **Note**: You might want to adjust the `clangd.fallbackFlags` depending on your build
system and customize the `inlayHints.sep` based on your preference.

```json
{
    "clangd.fallbackFlags": ["-std=c++20"],
    "clangd.arguments": ["--query-driver=${workspaceFolder}/Toolchain/Local/**/*"],
    "semanticTokens.enable": true,
    "inlayHint.subSeparator": "︴",
    "inlayHints.enableParameter": true,
    "clangd.inlayHints.sep": "⇝"
}
```

To change the coc-settings.json go to the file `~/.config/nvim/coc-settings.json`
or type `:CocConfig` in the command line.

> **Note**: In case you already had another c++ language server configured in the
`coc-settings.json` you might want to nuke it first and
work towards your desired config by adding the other parts back in to avoid
conflicts.

> **Note**: If you have configured `clangd` as a languageServer in
`coc-settings.json`, you should remove it to avoid running clangd twice!

> **Note**: `clangd.inlayHints.sep` breaks on `clangd 15.0.6`.

# Formatting
For code formatting the formatter plugin can be used.
```vim
Plug 'mhartington/formatter.nvim'
```
### Configuration
To use the formatter plugin one needs to opt-in to specific formatters. An example lua configuration which uses clang-format for cpp files:
```lua
require("formatter").setup{
    filetype = {
        cpp = {
            require("formatter.filetypes.cpp").clangformat
        }
    }
}
```

# Install git blame (Optional)

```vim
Plug 'f-person/git-blame.nvim'
```

Run `:PlugInstall` inside nvim.

# Configure your init.vim

The config file for neovim is at `~/.config/nvim/init.vim` or if
set `$XDG_CONFIG_HOME/nvim/init.vim`.

The `init.vim` excerpt:

```vim
"IMPORTANT: the leader key for <leader> keycombos
let mapleader = "\\"

"BEGIN: git blame (optional)
hi GitBlame guifg=#7b7b7b
let g:gitblame_date_format = '%d.%m.%y %H:%M'
let g:gitblame_highlight_group = 'GitBlame'
let g:gitblame_message_when_not_committed = 'You: Uncommitted changes'
let g:gitblame_message_template = '   <author> (<committer>), <date> <sha> • <summary>'
"END: git blame

"BEGIN: coc
"inline hints (depending on clangd version one or another gets used)
hi CocHintVirtualText guifg=#84afe0
hi CocInlayHint guifg=#84afe0 guibg=#393939
hi CocInlayHintParameter guifg=#84afe0 guibg=#393939
hi CocInlayHintType guifg=#89ddff guibg=#393939

"semantic highlighting
hi CocSemMethod guifg=#bfaa87 gui=bold
hi CocSemFunction guifg=#bfaaf7 gui=bold
hi CocSemParameter guifg=#a9bfd1 gui=underline
hi CocSemVariable guifg=#8edbdb
hi CocSemProperty guifg=#23ce6d
hi link CocSemEnumMember Constant
hi link CocSemEnum CocSemClass
hi Constant guifg=#f78c6c
hi CocSemClass guifg=#89ddff
hi Statement guifg=#c792ea
hi Type guifg=#db954a


"remap keys for applying refactor code actions (on warnings) (\re)
nmap <silent> <leader>re <Plug>(coc-codeaction-refactor)
xmap <silent> <leader>r  <Plug>(coc-codeaction-refactor-selected)
nmap <silent> <leader>r  <Plug>(coc-codeaction-refactor-selected)

"outline for file (\o)
nmap <silent><nowait> <leader>o :<C-u>CocList outline<cr>

"goto definition etc.
nmap <silent> gd <Plug>(coc-definition)
nmap <silent> gt <Plug>(coc-type-definition)
nmap <silent> gi <Plug>(coc-implementation)
nmap <silent> gr <Plug>(coc-references)

"coc rename (\rn)
nmap <leader>rn <Plug>(coc-rename)

"prev or next error
nmap <silent> [g <Plug>(coc-diagnostic-prev)
nmap <silent> ]g <Plug>(coc-diagnostic-next)

"confirm coc-suggestion with enter
imap <silent><expr> <CR> coc#pum#visible() ? coc#pum#confirm() : "\<CR>"

"ctrl+space for completion
imap <silent><expr> <c-space> coc#refresh()

"show documentation with ctrl+k
nmap <silent><c-k> :call ShowDocumentation()<CR>

"show documentation if it's available
function! ShowDocumentation()
  if CocAction('hasProvider', 'hover')
    call CocActionAsync('doHover')
  else
    call feedkeys('K', 'in')
  endif
endfunction

"coc-clangd switch between header and source
nmap <silent>gs :CocCommand clangd.switchSourceHeader vsplit<CR>
"END: coc
```

# Configure .clangd

> **Note**: Every time a new source is added or the compilation commands get adjusted
(through CMake) you need to rerun `./Meta/serenity.sh rebuild`.

Link `ln -s /path/to/serenity/Build/x86_64/compile_commands.json /path/to/serenity/compile_commands.json`.

Create `/path/to/serenity/.clangd` (replace `/path/to/serenity`
with your SerenityOS directory) with content of the clangd section in the
[VSCodeConfiguration.md](./VSCodeConfiguration.md).

> **Note**: You can add a `Remove` part, where you can remove unwanted flags
such as those that aren't supported by the current version of clang.