Skip to content

Instantly share code, notes, and snippets.

@MartyLake
Created November 7, 2018 13:13
Show Gist options
  • Save MartyLake/9c073808901de94c6339431226a17a50 to your computer and use it in GitHub Desktop.
Save MartyLake/9c073808901de94c6339431226a17a50 to your computer and use it in GitHub Desktop.
compile_commands.json

install tools

pip install scan-build

included bellow

create the compile_commands.json

intercept-build xcodebuild move the compile_commands.json to the base of your directory


Clangd <https://clang.llvm.org/extra/clangd.html)>_

Clangd is an implementation of the Language Server Protocol leveraging Clang. Clangd’s goal is to provide language “smartness” features like code completion, find references, etc. for clients such as C/C++ Editors.

Installation

Most package managers and major distributions provide a way to install tools from clang-tools-extra which includes Clangd. If you can't find instructions for your workspace below, consider checking Installing Clangd <https://clang.llvm.org/extra/clangd.html#installing-clangd>_ section.

macOS Homebrew:

.. code-block:: console

brew install --with-toolchain llvm

For Arch Linux you can just install Clang package <https://www.archlinux.org/packages/extra/x86_64/clang/>_ which comes with clang-tools-extra:

.. code-block:: console

pacman -Syu clang

Debian-based Linux distributions provide clang-tools package (e.g. Debian package <https://packages.debian.org/sid/devel/clang-tools>_ and Ubuntu package <https://packages.ubuntu.com/search?keywords=clang-tools>_):

.. code-block:: console

apt-get install clang-tools

Configuration

.. code-block:: vim

let g:LanguageClient_serverCommands = { \ 'cpp': ['clangd'], \ }

Recommended Settings

LanguageClient-neovim has multiple options for the completion management:

  • deoplete <https://github.com/Shougo/deoplete.nvim>_
  • ncm2 <https://github.com/ncm2/ncm2>_
  • Vim's omnifunc (used by default)

For best experience, it is advised to use deoplete. This is one of the most advanced completion manager which is very flexible and has numerous nice-to-have features such as snippets integration (if you are a UltiSnips <https://github.com/SirVer/ultisnips>_ user please proceed to UltiSnips Integration <https://github.com/autozimu/LanguageClient-neovim/wiki/UltiSnips-Integration>_). You can install it via

.. code-block:: vim

if has('nvim') Plug 'Shougo/deoplete.nvim', { 'do': ':UpdateRemotePlugins' } else Plug 'Shougo/deoplete.nvim' Plug 'roxma/nvim-yarp' Plug 'roxma/vim-hug-neovim-rpc' endif

let g:deoplete#enable_at_startup = 1

Also, by default LanguageClient-neovim comes without key bindings. Since it is easier to use key bindings for the source code navigation, it is advised to set up a custom set of shortcuts. Suggested way of a consistent setup with the <leader> key <http://vim.wikia.com/wiki/Mapping_keys_in_Vim_-_Tutorial_(Part_3)#Map_leader>_:

.. code-block:: vim

function SetLSPShortcuts() nnoremap ld :call LanguageClient#textDocument_definition() nnoremap lr :call LanguageClient#textDocument_rename() nnoremap lf :call LanguageClient#textDocument_formatting() nnoremap lt :call LanguageClient#textDocument_typeDefinition() nnoremap lx :call LanguageClient#textDocument_references() nnoremap la :call LanguageClient_workspace_applyEdit() nnoremap lc :call LanguageClient#textDocument_completion() nnoremap lh :call LanguageClient#textDocument_hover() nnoremap ls :call LanguageClient_textDocument_documentSymbol() nnoremap lm :call LanguageClient_contextMenu() endfunction()

augroup LSP autocmd! autocmd FileType cpp,c call SetLSPShortcuts() augroup END

This will ensure that LSP shortcuts are enabled only for source files in C++ or C. If you use other language servers for LanguageClient-neovim, just add more filetypes to the autocmd.

Another great addition to the setup is FZF <https://github.com/junegunn/fzf>_ integration. It will enable user to use fuzzy matching for something like searching through workspace or document symbols. For FZF, LanguageClient-neovim will take care of the configuration. All user has to do is to install the plugin:

.. code-block:: vim

Plug 'junegunn/fzf', { 'dir': '~/.fzf', 'do': './install --all' } Plug 'junegunn/fzf.vim'

echodoc <https://github.com/Shougo/echodoc.vim>_ can handle the function signatures displaying:

.. code-block:: vim

Plug 'Shougo/echodoc.vim'

set cmdheight=2 let g:echodoc#enable_at_startup = 1 let g:echodoc#type = 'signature'

signcolumn will appear each time Clangd sends a warning or provides a diagnostic and the text will be shifted by one column each time signcolumn is triggered. To prevent this shift and always show the signcolumn, use

.. code-block:: vim

" Always draw the signcolumn. set signcolumn=yes

For documentation, see LanguageClient.txt <https://github.com/autozimu/LanguageClient-neovim/blob/next/doc/LanguageClient.txt>_ or simply call :help LanguageClient.

Troubleshooting

If you encounter a bug and you are able to reproduce it, Clangd developers would appreciate a bug submission. Please use LLVM Bugzilla <https://bugs.llvm.org/buglist.cgi?component=Other&product=clang-tools-extra&resolution=--->_ for the bug reports. To help diagnose the problem, you can attach log files and the Mirror File (which stores the LSP input passed to Clangd).

To store stderr output, you can redirect stderr to a file and use LanguageClient#debugInfo() function:

.. code-block:: vim

let g:LanguageClient_serverStderr = '/tmp/clangd.stderr'

function SetLSPShortcuts() " ... " Previous bindings " ... nnoremap ll :call LanguageClient#debugInfo() endfunction()

Bonus: Building Global Static Index

Clangd can provide global completion when given a global static index. This index is not rebuilt during the editing process which means that if a significant part of the codebase is changed you would have to rebuild it to reflect these changes.

To build the global static index, you can use experimental clangd-indexer tool. If have copied the compile_commands.json to the source directory, call:

.. code-block:: console

clangd-indexer -executor=all-TUs /path/to/project > index.yaml

After the index is produced, you can pass it to Clangd via -yaml-symbol-file:

.. code-block:: vim

let g:LanguageClient_serverCommands = { \ 'cpp': ['clangd', '-yaml-symbol-file=/path/to/index.yaml',], \ }

The global static index infrastructure is experimental, so please be aware that you might encounter some bugs. If you do, please submit a bug report to help make the infrastructure better.

Getting Involved

If you are interested in Clangd development or have any questions, consider using clangd-dev Mailing List <http://lists.llvm.org/mailman/listinfo/clangd-dev>_.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment