Installation of vim plug-in YouCompleteMe for Mac

Keywords: vim Python SDK cmake

System: macOS Mojave 10.14.6

vim version: 8.1 (higher than 7.4.1578 and supports Python 2 or Python 3)

Installation of CMake

CMake is a cross-platform project management tool. It can be obtained directly through brew

brew install cmake

Installation and Use of Vundle

Vundle is an automated vim plug-in manager. Due to version updates, the configuration of Vundle is no longer consistent with half of the tutorials given online. Reference to Vundle's Official Documents To install Vundle, you can use the following commands

git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim

Once the download is complete, you can use Vundle to download and manage other plug-ins. First, specify the plug-in to use Vundle management in ~/.vimrc

set nocompatible
filetype off
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()

Plugin 'VundleVim/Vundle.vim'
Plugin 'scrooloose/nerdtree'
Plugin 'jistr/vim-nerdtree-tabs'
Plugin 'vim-scripts/taglist.vim'
Plugin 'tomasr/molokai'
Plugin 'vim-syntastic/syntastic'
Plugin 'Valloric/YouCompleteMe'

call vundle#end()
filetype plugin indent on

The parameter after the Plugin command is the address of the plug-in to be managed at github. Once saved, open vim and run the command

:PluginInstall

Vundle checks whether these plug-ins are installed, and if not, it automatically downloads and installs them to github.

Installation of YCM

YouCompleteMe is also a plug-in, so it can be installed through Vundle. The difference is that

  1. YCM has a large volume and slow download speed. It's better to find a place to visit github fast to download.
  2. YCM needs to be compiled, so it will report an error after downloading, regardless of him.

Switch to YCM's download directory to run the installation script

cd ~/.vim/bundle/YouCompleteMe/
./install.py

If the C family needs to be semantically complemented, use parameters

./install.py --clang-completer

If you're lucky enough, it should be ready. But in general, it's not possible (the hardest thing in history to install a plug-in is not for nothing). So YCM Official Documents The method of full manual installation is given in this paper.

1. Download YCM

This step can be done by pulling the git warehouse either by Vundle or manually.

cd ~/.vim/bundle/
git clone https://github.com/ycm-core/YouCompleteMe.git
git submodule update --init --recursive

It should be noted that many third-party warehouses are introduced in YCM warehouse. In especial

~/.vim/bundle/YouCompleteMe/third_party/ycmd/third_party/cregex/

In this warehouse, the blogger did not succeed in two reloads (I don't know if this is the design). According to the official website, the installation of cregex is optional, and it may not matter if it is not downloaded. But after all, all the big heads are downloaded, and that's not bad.

cd ~/.vim/bundle/YouCompleteMe/third_party/ycmd/third_party/
rm -rf ./cregex
git submodule update --init --recursive

2. Download libclang

This step is only necessary for C family semantic completion and can be skipped by unnecessary users. Direct access llvm official website Download llvm binary file

YCM officially requires llvm version not less than 8.0.0, but the maximum version 8.0.1 currently does not have macOS version, so you have to download this version first. If you have the latest version in the future, it is better to download the new version, so as not to recompile because of YCM updates.

Downloading precompiled llvm binaries is YCM's official recommendation, but using native llvm is also feasible

brew install llvm

Because the blogger's network is so moving that it can't survive, and the result is unknown. Installed buddies can try.

Backup!!!

The two steps mentioned earlier are probably the longest in the installation process. Therefore, before proceeding with the installation, it is better to backup the downloaded files, so as to avoid the subsequent installation failure and re-download. Bloggers put downloaded files on Baidu's web disk

Links: https://pan.baidu.com/s/1y05NTT0PKpiDe-PXCKRs3w Password: zd3o

Compressed after about 600 MB, you can consider the speed of selective download.

Compile ycm_core

Next is the core of compiling YCM. First, you need to put the decompressed llvm in the ~/ycm_temp directory.

/Users/lutingwang
└── ycm_temp
	└── llvm_root_dir
	    ├── bin
	    ├── include
	    ├── lib
	    ├── libexec
	    └── share

Then switch to the compiler directory

cd ~
mkdir ycm_build
cd ycm_build/

Start compiling

cmake -G "Unix Makefiles" -DPATH_TO_LLVM_ROOT=~/ycm_temp/llvm_root_dir . ~/.vim/bundle/YouCompleteMe/third_party/ycmd/cpp
cmake --build . --target ycm_core --config Release

If it works properly, your YCM should already be available. Deleting the temporary path created above will not affect YCM work

cd ~
rm -r ycm_temp
rm -r ycm_build

Compile regex

This step is optional in order to speed up the analysis of regular expressions. The compilation process is similar to that above.

cd ~
mkdir regex_build
cd regex_build
cmake -G "Unix Makefiles" . ~/.vim/bundle/YouCompleteMe/third_party/ycmd/third_party/cregex
cmake --build . --target _regex --config Release
cd ~
rm -r regex_build

YCM Configuration

After installation, YCM needs to be configured. If you use Vundle for management (YCM is in the ~/.vim/bundle/ directory), please confirm whether there is ~/.vimrc first.

set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()
" ...
Plugin 'Valloric/YouCompleteMe'
" ...
call vundle#end()

Note that YCM declarations should be placed between vundle begin () and vundle end ().

Then I changed the location of the file. ycm_extra_conf.py, which I found in the YouCompleteMe / directory when I installed it. Estimation is another change in the process of renewal. In short, find the file and define it after the import statement

# import os.path as p
# import subprocess

flags = [
        'std=c++11',
        '-x',
        'c++',
        '-I', '.',
        '-isystem',
        '/usr/local/include',
        '-isystem',
        '/Library/Developer/CommandLineTools/usr/include/c++/v1',
        '-isystem',
        '/Library/Developer/CommandLineTools/usr/lib/clang/10.0.1/include',
        '-isystem',
        '/Library/Developer/CommandLineTools/usr/include',
        '-isystem',
        '/Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/usr/include',
        '-isystem',
        '/Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/System/Library/Frameworks'
]

def FlagsForFile(filename):
    return { 'flags': flags }

# DIR_OF_THIS_SCRIPT = p.abspath( p.dirname( __file__ ) )
# DIR_OF_THIRD_PARTY = p.join( DIR_OF_THIS_SCRIPT, 'third_party' )

The annotated part is original. As you can see here, we define a list of flags as the return value of the FlagsForFile function. This list acts as a parameter following the gcc command. Look first at a series of values of - isystem, which contain paths that the compiler searches for when # include <...>. Although they are roughly the same, the paths of different computers may be slightly different and can be queried by command.

Lutings-MacBook-Air:ycm4mac lutingwang$ echo | clang -v -E -x c++ -
Apple LLVM version 10.0.1 (clang-1001.0.46.4)
Target: x86_64-apple-darwin18.7.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
 "/Library/Developer/CommandLineTools/usr/bin/clang" -cc1 -triple x86_64-apple-macosx10.14.0 -Wdeprecated-objc-isa-usage -Werror=deprecated-objc-isa-usage -E -disable-free -disable-llvm-verifier -discard-value-names -main-file-name - -mrelocation-model pic -pic-level 2 -mthread-model posix -mdisable-fp-elim -fno-strict-return -masm-verbose -munwind-tables -target-sdk-version=10.14 -target-cpu penryn -dwarf-column-info -debugger-tuning=lldb -target-linker-version 450.3 -v -resource-dir /Library/Developer/CommandLineTools/usr/lib/clang/10.0.1 -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk -I/usr/local/include -stdlib=libc++ -Wno-atomic-implicit-seq-cst -Wno-framework-include-private-from-public -Wno-atimport-in-framework-header -Wno-quoted-include-in-framework-header -fdeprecated-macro -fdebug-compilation-dir /Users/lutingwang/.Trash/ycm4mac -ferror-limit 19 -fmessage-length 80 -stack-protector 1 -fblocks -fencode-extended-block-signature -fregister-global-dtors-with-atexit -fobjc-runtime=macosx-10.14.0 -fcxx-exceptions -fexceptions -fmax-type-align=16 -fdiagnostics-show-option -fcolor-diagnostics -o - -x c++ -
clang -cc1 version 10.0.1 (clang-1001.0.46.4) default target x86_64-apple-darwin18.7.0
ignoring nonexistent directory "/Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/usr/include/c++/v1"
ignoring nonexistent directory "/Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/usr/local/include"
ignoring nonexistent directory "/Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/Library/Frameworks"
#include "..." search starts here:
#include <...> search starts here:
 /usr/local/include
 /Library/Developer/CommandLineTools/usr/include/c++/v1
 /Library/Developer/CommandLineTools/usr/lib/clang/10.0.1/include
 /Library/Developer/CommandLineTools/usr/include
 /Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/usr/include
 /Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/System/Library/Frameworks (framework directory)
End of search list.
# 1 "<stdin>"
# 1 "<built-in>" 1
# 1 "<built-in>" 3
# 373 "<built-in>" 3
# 1 "<command line>" 1
# 1 "<built-in>" 2
# 1 "<stdin>" 2

Notice that there is a line # include <...> search start here: the next few lines are the information we want.

After editing. ycm_extra_conf.py, you need to modify ~/.vimrc. Write after YCM's Plugin statement

let g:ycm_global_ycm_extra_conf='~/.vim/bundle/YouCompleteMe/.ycm_extra_conf.py'

It is equivalent to setting the newly modified file to the global YCM configuration. So YCM should be able to work. Open a source file at will, and the completion option will pop up automatically when the identifier is entered. If there is no response, or if you prompt The ycmd server SHUT DOWN on the bottom command line like a blogger, you need a little more work.

But if you install it successfully, congratulations, your vim already has IDE-level auto-completion. If necessary, it can also be configured in ~/.vimrc

let g:ycm_key_list_select_completion=['<c-n>']
let g:ycm_key_list_previous_completion=['<c-p>']
let g:ycm_collect_indentifiers_from_tags_files=1
let g:ycm_seed_identifiers_with_syntax=1
let g:ycm_confirm_extra_conf=0	" avoid YCM Whether to load or not is prompted by the user each time the load is loaded
let g:ycm_autoclose_preview_window_after_completion=1
let g:ycm_complete_in_comments = 1  " It can also be completed in comment input
let g:ycm_complete_in_strings = 1   " It can also be completed in string input
let g:ycm_collect_identifiers_from_comments_and_strings = 1   " Texts in comments and strings are also supplemented by revenue.

Common Problems in YCM Installation

According to the blogger's three-day experience of installing YCM, the main reason why this plug-in is difficult to install is that the download speed is slow. If you download the YouCompleteMe repository and llvm binary in advance, it will not be slow to install (bloggers will run out within five minutes of the last reload). So it's not difficult to reinstall as long as the backup is done before installation. Now let's talk about the problems I encountered during installation.

vim flashback, report python's Momsory Error

This problem occurred after I first used Vundle to install YCM. At that time, everything went amazingly well, Vundle downloaded YCM in less than half an hour, and reported an error Memory Error. But at that time, I thought that this mistake was the "normal" mistake in the legend. I didn't care about him. When a set of installation process came down, I found that my vim was no longer in use.

Later, I found that the Vundle I installed was an old version, and all the commands I used were Bundle'Valloric/YouCompleteMe'. It may be compatibility issues and so on. In short, vim triggers MemoryError when it handles call vundle#rc(). Finally, delete the Vundle and reinstall it.

The ycmd server SHUT DOWN ... Unexpected exit code -11 ...

According to the prompt to check the log found to be empty, searched the Internet for a long time and no one said - 11 code corresponding to what the problem is... Maybe it's Unexpected. So the blogger used the omnipotent remedy.

cd ~/.vim/bundle/
rm -rf ./YouCompleteMe
cp -r ~/Desktop/YouCompleteMe ./
cd YouCompleteMe/
./install.py --clang-completer

Re-install. However, I gave up after two reinstallations. Generally speaking, installation errors are not so frequent. Later, I saw the ideas provided by netizens on a website.

  1. Use the command: YcmDiag to get specific information
  2. Comment let g:ycm_confirm_extra_conf=0 in ~/.vimrc to confirm compilation

The first method is actually equivalent to viewing the log, except that the log does not record information, and the command actually returns an error that the built-in completion function cannot recognize the file type. It's also because of this error message that I suspect the installation of llvm (install.py was used all the time before, and I didn't think that automatic downloading would cause problems).

The second method is a little more complicated. Normally, after entering vim, YCM will ask at the bottom command line if a. ycm_extra_conf.py file is loaded unless this function is disabled in ~/. vimrc (that is, the function of the command above). So if compiled properly, there should be similar queries. Even if there is a mistake, it should collapse after asking. But the blogger's VIM prompts server shutdown as soon as it enters, so it's doubtful that there's something wrong with the compilation process.

As a result, reinstallation is necessary, but it's no longer reliable to install with scripts, so I chose to install manually. It has to be said that official documents should be taken seriously, even if there are problems after installation, it is generally not fatal.

Python version

The most common question asked online is the inconsistency of versions. There are two main inconsistencies: Python version and llvm version. Since llvm is downloaded manually from the official website, there is no such problem. As for the Python version, bloggers haven't figured out what the problem is... But as a result, ycmd server shutdown again. This time, instead of Unexpected, it reminds me that the Python version used at compile time is different from the Python version used at run time, and that the installation path of Python 2 needs to be given in ~/.vimrc. So there's a sentence like this.

let g:ycm_server_python_interpreter='/usr/bin/python2.7'

Use of YCM

As mentioned above, a flags list is added when modifying ~/.vim/bundle/YouCompleteMe/.ycm_extra_conf.py. The - isystem option in this list specifies the search path for the system header file. For the header file inside the project, there is only the phrase'-I','.'. If you use header files that are not in this directory in your project, you also need to modify this file. But obviously this is inefficient. Therefore, it is better to copy a configuration file to the root directory of the project source code and add the project header directory to the configuration file. Because YCM searches for. ycm_extra_conf.py first for the current directory, then one by one for the parent directory, and finally for the global configuration file. Therefore, the configuration file of the project root directory can overwrite the global file. This is equivalent to specifying a separate configuration for each project, without polluting the system settings.

Posted by cyberrate on Tue, 13 Aug 2019 22:24:20 -0700