您的位置:首页 > 编程语言

Windows下Vim代码补全YouCompleteMe设置指南

2014-07-09 15:20 309 查看

简介

YouCompleteMe 是一个快速,即时,允许模糊搜索的 Vim 代码补全引擎。除了支持缓冲区关键字补全外,还支持 C/C++/Objective-C/Objective-C++,Python(Jedi-based),C#(OmniSharp-based),Go(Gocode-based)的语意补全。本文主要为 Clang-base 的 C-family 补全的安装指南。

感受下来自官方的介绍图:



GitHub项目地址:https://github.com/Valloric/YouCompleteMe

准备

YCM 和以下插件冲突,安装前需要先删除:
 clang_complete
 AutoComplPop
 Supertab
 neocomplcache

准备软件

 Vim(支持python2)
 Python 2.7:https://www.python.org/download/releases/2.7.8/
 git:http://git-scm.com/downloads
 CMake:http://cmake.org/cmake/resources/software.html
 可用的C++编译器
 Clang:http://llvm.org/releases/download.html
需要把 Python 的根目录和 git 的 bin 目录放在环境变量中。

注意Vim,Pyhton 以及之后编译的 YCM 位数要对应,如果想要 32 位的,三者应均为 32 位。

安装

推荐使用 Vundle,只需要将

Plugin 'Valloric/YouCompleteMe'


加入到 vimrc 中,重启 Vim 后运行 :PluginInstall 即可

配置YouCompleteMe

1.编译

官方的 Pre-built Binaries 来自 VC++,所以想要省事的话也要用 VC 编译 YCM。CMake 直接 Generate

cd YouCompleteMe\third_party\ycmd\cpp\build
cmake -G "Visual Studio 12 2013" -DPATH_TO_LLVM_ROOT=<LLVM_PATH> -DUSE_CLANG_COMPLETER=ON ./../
<LLVM_PATH>部分换成LLVM的根目录,替换 -G 后对应的 Target 为你想要使用的构建环境

之后进入YouCompleteMe\third_party\ycmd\cpp\build 打开解决方案,输出目标换为 MinSizeRel,右键 ycm_support_libs,生成,然后等 VS 为你安排好一切。

编译完成后 YouCompleteMe\third_party\ycmd 下就会多出三个文件:libclang.dll,ycm_client_support.pyd,ycm_core.pyd。

2.配置.ycm_extra_conf.py

这个文件决定了 YCM 对 C 系语言的补全行为,样板文件位于 YouCompleteMe\third_party\ycmd\cpp\ycm

当打开一个代码文件时,YCM 将顺着文件所在的路径一直向上查找,搜索到第一个配置文件将立刻读入。如果搜索不到配置文件将加载全局配置文件。这时如果全局配置文件也没有找到则语法不全不会被启用。

找到以下内容:
# NOTE: This is just for YouCompleteMe; it's highly likely that your project
# does NOT need to remove the stdlib flag. DO NOT USE THIS IN YOUR
# ycm_extra_conf IF YOU'RE NOT 100% YOU NEED IT.
try:
final_flags.remove( '-stdlib=libc++' )
except ValueError:
pass
将之注释后 YCM 才会补全 C++ 标准库的内容。

若需要补全其他库可以参考注释修改,flags 中添加
isystem 即可

例如:
flags = [
'-Wall',
'-Wextra',
'-Werror',
'-Wc++98-compat',
'-Wno-long-long',
'-Wno-variadic-macros',
'-fexceptions',
'-DNDEBUG',
# You 100% do NOT need -DUSE_CLANG_COMPLETER in your flags; only the YCM
# source code needs it.
'-DUSE_CLANG_COMPLETER',
# THIS IS IMPORTANT! Without a "-std=<something>" flag, clang won't know which
# language to use when compiling headers. So it will guess. Badly. So C++
# headers will be compiled as C headers. You don't want that so ALWAYS specify
# a "-std=<something>".
# For a C project, you would set this to something like 'c99' instead of
# 'c++11'.
'-std=c++11',
# ...and the same thing goes for the magic -x option which specifies the
# language that the files to be compiled are written in. This is mostly
# relevant for c++ headers.
# For a C project, you would set this to 'c' instead of 'c++'.
'-x',
'c++',
'-isystem',
'../BoostParts',
'-isystem',
# This path will only work on OS X, but extra paths that don't exist are not
# harmful
'/System/Library/Frameworks/Python.framework/Headers',
'-isystem',
'../llvm/include',
'-isystem',
'../llvm/tools/clang/include',
'-I',
'.',
'-I',
'./ClangCompleter',
'-isystem',
'./tests/gmock/gtest',
'-isystem',
'./tests/gmock/gtest/include',
'-isystem',
'./tests/gmock',
'-isystem',
'./tests/gmock/include',
'-isystem',
'/usr/include',
'-isystem',
'/usr/local/include',
'-isystem',
'/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1',
'-isystem',
'/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include',
'-isystem',
'E:/developertools/TDM-GCC/lib/gcc/mingw32/4.8.1/include/c++',
'-isystem',
'E:/developertools/TDM-GCC/lib/gcc/mingw32/4.8.1/include/c++/mingw32',
'-isystem',
'E:/developertools/TDM-GCC/lib/gcc/mingw32/4.8.1/include/c++/backward',
'-isystem',
'E:/developertools/TDM-GCC/lib/gcc/mingw32/4.8.1/include',
'-isystem',
'E:/developertools/TDM-GCC/lib/gcc/mingw32/4.8.1/include-fixed',
'-isystem',
'E:/developertools/TDM-GCC/include',
'-isystem',
'E:/DeveloperTools/wxWidgets-3.0.0/include',
'-isystem',
'C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v6.0/include/CL',
'-isystem',
'C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v6.0/include',
'-isystem',
'E:/DeveloperTools/wxWidgets-3.0.0/include/wx',
]

若需支持 C++11,也需要在此处添加  '-std=c++11'

3.YCM变量配置

在_vimrc中添加以下设置
let g:ycm_global_ycm_extra_conf = '~/.ycm_extra_conf.py' "此处是全局配置文件路径
let g:ycm_confirm_extra_conf = 0 "关闭每次导入配置文件前的询问
let g:syntastic_always_populate_loc_list = 1 "方便使用syntastic进行语法检查
let g:ycm_seed_identifiers_with_syntax=1 " 开启语法关键字补全
更多的变量请参考YCM README

其他相关问题


非ASCII字符后YCM不再弹出补全菜单

文件中出现 Unicode 导致补全框不能显示,需要在 _vimrc 的起始位置添加如下语句
set encoding=utf-8
set langmenu=zh_CN.UTF-8
language messages zh_CN.utf-8

第一行改变 VIM 的默认编码。为了保证 GUI 不会出现乱码所以还要指定 GUI 的编码

配合Ultsnips

嗯,问题是这样的:YCM 弹出的补全菜单只能显示 all.snippets 的代码片补全

官方编译的 Vim 是不会出现这样的问题的,因为它不支持 Python3

对的,问题就在这。因为 Ultsnips 默认使用 Python3 ,YCM 只能用 Python2,灵异问题也就接踵而至

所以,解决方案很简单,强制让 US 使用 Python2 就好了:

let g:UltiSnipsUsePythonVersion = 2
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  vim 编译器 YCM