Skip to content

YouCompleteMe: semantic auto-completion and JumpToDefinition for VIM on webkitgtk+

Prelude

Jump to the next session if you don’t want to read lullabies

There was a time when I was a Qt application developer working with vim. It worked pretty well until I started to work with large applications such as Meego Application Framework.
Hundreds of thousands of files, classes, I couldn’t memorize everything. Then I discovered QtCreator with VI-mode.

I’d never really liked big/heavy IDEs but QtCreator wasn’t that heavy, it had a decent vi-mode and I was able to use the cool things such as auto-completion and jump-to-definition.

Three years later here I’m working with raw VIM again, on top of webkitgtk+ so QtCreator is not an option anymore. What to do?

You can tell me about ctags and cscope… Sorry, ctags and cscope sux for c++ code, really! I tried hard, I wrote scripts to parse the GNUMakefile.list.am and use only the important files but it wasn’t enough. Ctags can’t really deal with classes, doesn’t know much about scopes and VIM auto-completion wasn’t semantic. How to solve that problem all-in-once?

YouCompleteMe

YouCompleteMe GIF

Description

From the original website: “YouCompleteMe is a fast, as-you-type, fuzzy-search code completion engine for Vim.”

Installation

The installation procedures are quite simple and well defined on https://github.com/Valloric/YouCompleteMe#readme
Basically, you need to use Vundle (why are you not using it yet?!) and drop the YCM files in the right place. The installer will do the rest.

vimrc and vundle

if YouCompleteMe is your first Vundle plugin, your .vimrc file should start like this:


set nocompatible               " be iMproved
filetype off                   " required!

set rtp+=~/.vim/bundle/vundle/
call vundle#rc()

Bundle 'gmarik/vundle'
Bundle 'Valloric/YouCompleteMe'

Remember to use those lines at the beginning of the file, other rules should appear after that.

How it works

Basically YouCompleteMe uses a .ycm_extra_conf.py, which is a python script that helps YouCompleteMe plugin to find the compiling flags. Those flags are required so clang can interpret #includes and #defines from other files. It might be required a .ycm_extra_conf.py for each project you work on (Although it would be awesome to have a generic Makefile parser).

For webkitGtk, you can drop this file into the root folder and you’re done. Or you can help me to add traction to Ycm and push this patch upstream.

Tips&Tricks

Add this to your .vimrc


nnoremap <leader>y :YcmForceCompileAndDiagnostics<cr>                                                            
nnoremap <leader>pg :YcmCompleter GoToDefinitionElseDeclaration<CR>
nnoremap <leader>pd :YcmCompleter GoToDefinition<CR>
nnoremap <leader>pc :YcmCompleter GoToDeclaration<CR>

If you’re used to the webkit codebase you know how painful it is when you touch one file, wait two minutes to build and find out that you forgot a semicolon.
You can type \y to check if the file is compilable (clang will actually compile the file for you) and give you warnings/errors for that specific file.
\pc and \pd are also usefull for jump to definition/declaration.

So, if you’re working on WebkitGtk now (or any other project that might benefit from YouCompleteMe), give it a try!

Post a Comment

Your email is never published nor shared. Required fields are marked *
*
*