diff --git a/Documentation/NvimConfiguration.md b/Documentation/NvimConfiguration.md new file mode 100644 index 0000000000..a3c720aa36 --- /dev/null +++ b/Documentation/NvimConfiguration.md @@ -0,0 +1,174 @@ +# 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"], + "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`. + +# 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 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 = ' (), ' +"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 re (coc-codeaction-refactor) +xmap r (coc-codeaction-refactor-selected) +nmap r (coc-codeaction-refactor-selected) + +"outline for file (\o) +nmap o :CocList outline + +"goto definition etc. +nmap gd (coc-definition) +nmap gt (coc-type-definition) +nmap gi (coc-implementation) +nmap gr (coc-references) + +"coc rename (\rn) +nmap rn (coc-rename) + +"prev or next error +nmap [g (coc-diagnostic-prev) +nmap ]g (coc-diagnostic-next) + +"confirm coc-suggestion with enter +imap coc#pum#visible() ? coc#pum#confirm() : "\" + +"ctrl+space for completion +imap coc#refresh() + +"show documentation with ctrl+k +nmap :call ShowDocumentation() + +"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 gs :CocCommand clangd.switchSourceHeader vsplit +"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.