简体   繁体   English

如何在 YouCompleteMe C 代码上自动完成?

[英]How to get autocomplete on YouCompleteMe C code?

Question Edited问题已编辑

I mostly code in C and have been using vscode.我主要在 C 中编码,并且一直在使用 vscode。 I recently decided to try out vim and am trying to get code autocompletion to something resembling vscode.我最近决定尝试 vim 并试图让代码自动完成类似于 vscode 的东西。 Youcompleteme is popular vim plugin. Youcompleteme是流行的 vim 插件。 On using it I noticed that the code completion out of the box is really primitive and only completed the symbols in the current file and inbuild libraries.在使用它时,我注意到开箱即用的代码完成非常原始,只完成了当前文件和内置库中的符号。

My software versions我的软件版本

WSL Ubuntu 20.04.
GCC: gcc (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0
vim: VIM - Vi IMproved 8.1 (2018 May 18, compiled Apr 15 2020 06:40:31) Included patches: 1-2269

I installed it with python3 install.py --clangd-completer but its only partially suggesting symbols.我用python3 install.py --clangd-completer安装了它,但它只是部分暗示了符号。

How do I get the autocomplete engine to show me symbols from my header files across my project?如何让自动完成引擎向我显示项目中 header 文件中的符号?

I am providing an answer my own question so that others may find this.我正在提供我自己的问题的答案,以便其他人可以找到这个。 Here is how you get YouCompleteMe to autocomplete their C code in vim across your project.这是让 YouCompleteMe 在整个项目中自动完成vim中的 C 代码的方法。

python3 install.py --clangd-completer

YCM claims that using will enable the above command to install YCM will enable C semantic completion but it did not do so in my case. YCM 声称使用将启用上述命令来安装 YCM 将启用 C 语义完成,但在我的情况下并没有这样做。 The code completion only worked for inbuilt C libraries and for the current file being worked on.代码完成仅适用于内置 C 库和正在处理的当前文件。

Versions I am using:我正在使用的版本:

WSL Ubuntu 20.04.
GCC: gcc (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0
vim: VIM - Vi IMproved 8.1 (2018 May 18, compiled Apr 15 2020 06:40:31) Included patches: 1-2269

Its current behavior is to only complete standard library files and the symbols from the current file which is obviously not enough.它当前的行为是仅完成标准库文件和当前文件中的符号,这显然是不够的。 This is happening because the clangd that YCM is bundling during installation is not up-to-date and therefore doesnt have the dynamic indexing support they claim to have.发生这种情况是因为 YCM 在安装期间捆绑的 clangd 不是最新的,因此没有他们声称拥有的动态索引支持。 Fortunately, we have the ability to provide it YCM with our own clangd binary which it will use for code completion from files across your project.幸运的是,我们有能力为 YCM 提供我们自己的 clangd 二进制文件,它将用于完成项目中文件的代码。

In order to do this, you must download clang 13 from the here为此,您必须从此处下载 clang 13

On downloading and extracting it, you must add the path of <clang-for-your-respective-distribution>/bin/clangd to your PATH.在下载和解压它时,您必须将<clang-for-your-respective-distribution>/bin/clangd的路径添加到您的 PATH 中。

Now add the following settings to your vimrc file that allow it to use your clangd over the bundled one.现在将以下设置添加到您的 vimrc 文件中,以允许它在捆绑的文件上使用您的 clangd。

let g:ycm_clangd_uses_ycmd_caching=0 let g:ycm_clangd_binary_path='/home/shiva/clang+llvm-13.0.0-x86_64-linux-gnu-ubuntu-20.04/bin/clangd' let g:ycm_clangd_uses_ycmd_caching=0 let g:ycm_clangd_binary_path='/home/shiva/clang+llvm-13.0.0-x86_64-linux-gnu-ubuntu-20.04/bin/clangd'

save and source your vimrc.保存并获取您的 vimrc。 Enjoy full code completion.享受完整的代码完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM