Vim: Use the tags file to extend YCM's code auto-completion function for third-party libraries in C-family languages

foreword

Among the many auto-completion plug-ins of the Vim editor, YouCompleteMe (YCM) is definitely one of the best plug-ins to use, but its configuration process is often daunting for beginners. After many years of tossing by the author, it has basically reached the level of daily use.

If readers are not familiar with the YouCompleteMe plugin, it is recommended to refer to the author's blog Vim: Configure Python and C++ common plugins first .

In order to better understand this article, readers are suggested to read my previous blog Vim: Extending YCM's semantic analysis function for third-party libraries of C-family languages . In this blog, we solved the problem of YCM's semantic analysis error, but the improvement of autocompletion function is limited.

When using Vim+YCM, YCM usually cannot automatically complete functions and definitions in third-party software libraries (such as OpenCV in c++ mode). For this reason, this article mainly introduces how to use ctags to generate tags files, and further improves the automatic completion kinetic energy of YCM in third-party software libraries on the basis of the previous blog.

configuration process

This article takes opencv as an example to introduce how to expand the automatic completion function of YCM by generating tags files.

Generation of Tags file

1 install ctags

sudo apt-get install ctags

Note that according to the requirements of YCM, exuberant-ctags needs to be installed [ 1 ] ^{[1]}[ 1 ] . Fortunately, the above command will install exuberant-ctags by default. You can verify the version of ctags with the following command:

ctags --version

My output here is as follows:

Exuberant Ctags 5.9~svn20110310, Copyright (C) 1996-2009 Darren Hiebert
Addresses: <[email protected]>, http://ctags.sourceforge.net
Optional compiled features: +wildcards, +regex

2 Generate Tags file

This article takes opencv as an example, first download the opencv source code, and then execute the following command in the moudles path:

# 注意=+l中是小写的L,而不是1。
ctags -R --fields=+l *

Note that the parameters required by '–fields=+l' c++, if you need to use c language, you also need to add --langmap=c:.c.hparameters

Then a tags file will appear under the current folder. Let's observe some of the contents of the Tags file, as follows:

imread	imgcodecs/src/loadsave.cpp	/^Mat imread( const String& filename, int flags )$/;"	f	language:C++	namespace:cv
imread_	imgcodecs/src/loadsave.cpp	/^imread_( const String& filename, int flags, Mat& mat )$/;"	f	language:C++	namespace:cv
imreadmulti	imgcodecs/src/loadsave.cpp	/^bool imreadmulti(const String& filename, std::vector<Mat>& mats, int flags)$/;"	f	language:C++	namespace:cv
imreadmulti	imgcodecs/src/loadsave.cpp	/^bool imreadmulti(const String& filename, std::vector<Mat>& mats, int start, int count, int flags)$/;"	f	language:C++	namespace:cv
imreadmulti_	imgcodecs/src/loadsave.cpp	/^imreadmulti_(const String& filename, int flags, std::vector<Mat>& mats, int start, int count)$/;"	f	language:C++	namespace:cv
imshow	highgui/misc/java/src/java/highgui+HighGui.java	/^    public static void imshow(String winname, Mat img) {$/;"	m	language:Java	class:HighGui
imshow	highgui/src/window.cpp	/^void cv::imshow( const String& winname, InputArray _img )$/;"	f	language:C++	class:cv
imshow	highgui/src/window.cpp	/^void cv::imshow(const String& winname, const ogl::Texture2D& _tex)$/;"	f	language:C++	class:cv
imshow	videoio/src/cap_winrt_bridge.cpp	/^void VideoioBridge::imshow()$/;"	f	language:C++	class:VideoioBridge
imwrite	imgcodecs/src/loadsave.cpp	/^bool imwrite( const String& filename, InputArray _img,$/;"	f	language:C++	namespace:cv
imwrite_	imgcodecs/src/loadsave.cpp	/^static bool imwrite_( const String& filename, const std::vector<Mat>& img_vec,$/;"	f	language:C++	namespace:cv
imwritemulti	imgcodecs/include/opencv2/imgcodecs.hpp	/^bool imwritemulti(const String& filename, InputArrayOfArrays img,$/;"	f	language:C++	namespace:cv

It can be seen that the tags file contains the function name and basic definition, and which file it is in, and YCM can parse them very well.

3. Configure the tags file path

For this step, you only need to:

  1. Move the tags file to a custom location

  2. point to it in the vimrc file

For example, I changed the name of the tags file I just generated and put it in the following location: ~/.vim/tagfiles/opencv.tags, and then added the following settings in vimrc:

# 以下二选一即可
set tags=opencv.tags #相对位置,需要将tags文件放在当前工程下
set tags+=~/.vim/tagfiles/opencv.tags #绝对位置,tags文件就放在`~/.vim/tagfiles`内

YCM configuration

configuration

Add the following configuration under vimrc:

let g:ycm_collect_identifiers_from_tags_files = 1

In this way, YCM will automatically find the tags file defined in vimrc and parse it. You can run the following command in vim to see which tags file YCM has loaded in the current buffer:

:echo tagfiles()

The results of my computer running are as follows:

['/home/xuyangcao/.vim/tagfiles/opencv.tags']

Precautions

According to the documentation of YCM, it is found that using clangd completer in the compilation process is smarter than libclang completer, and parsing tags files is more comprehensive. The advantages of Clangd completer can be found in reference [3], so I won’t introduce too much here.

Therefore, in the process of compiling YCM, it is recommended to use --clanged-completerparameters.

Summarize

This article takes opencv as an example to introduce how to expand YCM's auto-completion and syntax highlighting functions by generating tags files.

References

1 YCM does not read identifiers from my tags files

2 Vim configuration associates multiple tags files

3 C-family Semantic Completion

Guess you like

Origin blog.csdn.net/xuyangcao123/article/details/125930185