vim-clangd


Ⅰ. 插件描述

vim plugin for clangd

Ⅱ. 基本信息

创建日期:  2017-06-01
使用用户:  3
Github星:  61
插件作者:  Keyue-Hu

Ⅲ. 安装方法

使用Vundle管理器安装

在你的.vimrc下添加:
Plugin 'chilledheart/vim-clangd'
… 然后在Vim中运行以下命令:
:source %
:PluginInstall

对于Vundle版本 < 0.10.2,请用上面的Bundle替换Plugin。

使用NeoBundle管理器安装

在你的.vimrc下添加:
NeoBundle 'chilledheart/vim-clangd'
… 然后在Vim中运行以下命令:
:source %
:NeoBundleInstall

使用VimPlug管理器安装

在你的.vimrc下添加:
Plug 'chilledheart/vim-clangd'
… 然后在Vim中运行以下命令:
:source %
:PlugInstall

使用Pathogen管理器安装

在终端中运行以下命令:
cd ~/.vim/bundle
git clone https://github.com/chilledheart/vim-clangd

Ⅳ. 文档说明

## vim-clangd
Build Status

One Demo

gif

Features

C/C++ Editor featureClangdvim-clangd
FormattingYesYes
CompletionYesYes
DiagnosticsYesYes
Fix-itsYesYes
Go to DefinitionYesYes
Source hoverYesYes
Signature HelpYesYes
Find ReferencesNoNo
Document HighlightsYesYes
RenameYesYes
Code LensNoNo
Syntax and Semantic ColoringNoNo
Code foldingNoNo
Call hierarchyNoNo
Type hierarchyNoNo
Organize IncludesNoNo
Quick AssistNoNo
Extract Local VariableNoNo
Extract Function/MethodNoNo
Hide MethodNoNo
Implement MethodNoNo
Gen. Getters/SettersNoNo

Note: clangd Editor feature [status]

Installation

Vim-clangd can be used with several package manager:

  • Pathogen

    • git clone https://github.com/hukeyue/vim-clangd.git ~/.vim/bundle/vim-clangd
  • Vundle.vim

    • Plugin 'hukeyue/vim-clangd'

After installing, you still need clangd binary to work with it

:ClangdInstallBinary

or in make style, under project directory:

make

for more information refer to Advanced Usage

Supported Platforms

  • Linux
  • Mac OS X (from 10.11)
  • Windows (both native and cygwin, 64-bit)

Vim version 7.4.1578 with Python 2 or Python3 support is a minimum.
For the case of OS X/macOS case, you can download lastet vim from MacVim,

Windows needs Visual C++ 2015 Runtime which could be downloaded from this link

Feature - Compilation Database

The recommended method to use with compilation flags is using JSON compilation database.
If your project's build system supports it, such as CMake, Ninja. For example,

To generate JSON compilation database, use this option with CMake:

cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=on ...

However, there are many build systems which can't generate JSON compilation database themselves.
By using bear or other special tools, it is able to generate compile_commands.json from most build systems. For
example,

To generate JSON compilation database, use bear:

bear make ...

Feature - Examples

Document-Highlight

gif

Fix-its

gif

Goto-Definition

gif

Rename

gif

Signature-Help

gif

Source-Hover

gif

Advanced Usages

Build clangd from source code

clangd build script are tested under these platforms:

  • Ubuntu 18.04
  • Ubuntu 16.04
  • Ubuntu 14.04
  • Debian 8
  • Debian 9
  • Fedora 25
  • Fedora 26
  • Fedora 27
  • OS X Sierra or later (including macOS Sierra)
  • Windows with VS2015 (32-bit)

You can build clangd from source as well:

./script/build-clangd.sh

vim-clangd will search builtin clangd and then fallback to clangd in the path.
however there is no simple way to get a binary clangd yet including llvm
official apt repo.

Modify clangd binary location

If you have clangd not in the path,
you can specify clangd binary in vimrc file such as

let g:clangd#clangd_executable = '~/build-llvm/bin/clangd'

make sure you have clang headers in the right directory

Turn off auto completion promption

If you find it not handy, you can turn it auto completion manually -- syntanx error detection and other functionalities
remain the same.

Put this in the vimrc file:

let g:clangd#completions_enabled = 0

Adjust auto completion delay interval

Sometimes completion prompt is slow. vim-clangd will detect the slow code completion and skip to let you go.
the condition is tunable where default is 100ms.

Put this in your vimrc file to save your waiting time to 10ms

let g:clangd#codecomplete_timeout = 10

Add key binding to format the selected code

You can use :<C-u> ClangdFormat to code format the specified code

If you prefer to shortcut, here is an example:

au FileType c,cpp,objc,objcpp nnoremap <buffer><Leader>cf :ClangdFormat<CR>
au FileType c,cpp,objc,objcpp vnoremap <buffer><Leader>cf :<C-u>ClangdFormat<CR>

Add key binding to Go to Definition

You can use :ClangdGotoDefinition to jump the definition of current cursor

If you prefer to shortcut, here is an example:

au FileType c,cpp,objc,objcpp nnoremap <leader>j :ClangdGotoDefinition<CR>

Add key binding to Show diagnostics

You can use :ClangdShowDetailedDiagnostic to show the detailed diagnostic for current line.

If you prefer to shortcut, here is an example:

au FileType c,cpp,objc,objcpp nnoremap <leader>d :ClangdShowDetailedDiagnostic<CR>

Add key binding to Show cursor details

You can use :ClangdShowCursorDetail to show the details about current cursor.

If you prefer to shortcut, here is an example:

au FileType c,cpp,objc,objcpp nnoremap <leader>c :ClangdShowCursorDetail<CR>

Add key binding to show Document Highlight

You can use :ClangdDocumentHighlight to search and highlight related pieces about current cursor in the same source
file.

If you prefer to shortcut, here is an example:

au FileType c,cpp,objc,objcpp nnoremap <leader>h :ClangdDocumentHighlight<CR>

Add key bining to do Fix-its

You can use :ClangdFixit to use Fix-its feature.
It will find the possible fix solutions and prompt for your choice if any.

If you prefer to shortcut, here is an example:

au FileType c,cpp,objc,objcpp nnoremap <buffer><Leader>f :ClangdFixit<CR>
au FileType c,cpp,objc,objcpp vnoremap <buffer><Leader>f :<C-u>ClangdFixit<CR>

Use along with neocomplete (experimental)

make sure you have neocomplete installed. you should disable vim-clangd's
autocompletion and configure neocomplete correctly. below is an example:

let g:clangd#completions_enabled = 0
if !exists('g:neocomplete#force_omni_input_patterns')
    let g:neocomplete#force_omni_input_patterns = {}
endif
let g:neocomplete#force_omni_input_patterns.c = '[^.[:digit:] *\t]\%(\.\|->\)\w*'
let g:neocomplete#force_omni_input_patterns.cpp = '[^.[:digit:] *\t]\%(\.\|->\)\w*\|\h\w*::\w*'
autocmd FileType c,cpp,objc,objcpp setlocal omnifunc=clangd#OmniCompleteAt

添加新评论