Search code examples
qtvimqt-creatorctags

Qt Creator tag file


I use vim for C++ code editing. But its code completion isn't so good (although I have tried many plugins, such as OmniCppComplete). The Qt Creator code completion is awesome, and it also has vim style editing which functionality is full enough for me. Only thing that isn't so good for me is that I cannot use ctags functionality inside Qt Creator (although Qt Creator has functionality to go to class definition, but it takes a lot more time to parse the source code).

Is it possible to create the source code tag file and use it with in Qt Creator in fake vim mode?


Solution

  • Code completion for C++ in Vim is actually superior. I'll outline the steps you have to take in order to make it work. However, I won't go into much detail here (such as building huge open-source code base, cloning a repository, installing plugins for Vim, etc.) because otherwise it would be worth writing a large tutorial for novices. So, assuming that you are well-aware and well-prepared software developer, there you go a fluent guide:

    1. Get and install clang_complete (plugin for Vim);

    2. Get and install neocomplcache (plugin for Vim);

    3. If you are on Unix, then you are lucky because you probably have LLVM and Clang either installed on your system already or you have to use package manager and install them with a single command. If so, then you can immediately jump to the last step (#7).

      If you are on Windows, then you are less lucky, but that's actually better from the practical point of view - you'll have a great experience of building huge stuff on your own and getting things to work no matter what. :)

      So, if you're on Windows (and using Qt Creator as I can see), then you might have MinGW already installed on your system. If that's not the case, then I strongly suggest that you to install a bleeding-edge MinGW-w64. Fortunately, you don't have to compile it yourself as rubenvb has kindly built MinGW-w64 toolchain in both variants: targeting 64-bit Windows (aka x64) and targeting 32-bit Windows (aka x86). Just download one depending on your OS. NOTE: These links are pointing to the latest (at the time of writing this answer) stable builds of MinGW-w64, i.e. based on GCC 4.7.2. IMPORTANT: Make sure that MinGW-w64 is in the %PATH% environment variable.

    4. Another piece of software needed is CMake (a popular build system). Again, if you are on Unix it might be installed already. If you are on Windows, then just download and install it. IMPORTANT: Make sure that CMake is in the %PATH% environment variable.

    5. The last thing we'll need is Python. Once again, if you are on Unix, then it is already installed on your system. Otherwise, you know what to do already. :) Officially, there are 2 versions of Python: 2.7.3 and 3.x.x - you should definitely download and install both. Python is an essential piece of software on any developer's machine. IMPORTANT: Make sure that Python 2.7.3 (not 3.x.x!) is in the %PATH% environment variable.

    6. Now that we have MinGW-w64, CMake, Python installed, we are ready to build LLVM and Clang. To ease the pain go to my Out-of-Source Builders project and scroll down to Guide: Build 64-bit LLVM and Clang for Windows (64-bit) Using MinGW-w64. If you are on 32-bit Windows, don't pay attention to 64-bit (it does not matter) in the title, just follow the instructions there. Wait for about an hour until LLVM and Clang are built.

    7. We are almost done, all that is left is to configure Vim properly. Here I'll simply provide my configuration which would most likely satisfy your needs.

    Configure neocomplcache:


    let g:neocomplcache_enable_at_startup            = 1
    let g:neocomplcache_enable_smart_case            = 1
    let g:neocomplcache_enable_camel_case_completion = 1
    let g:neocomplcache_enable_underbar_completion   = 1
    let g:neocomplcache_min_syntax_length            = 2
    
    if !exists('g:neocomplcache_force_omni_patterns')
      let g:neocomplcache_force_omni_patterns = {}
    endif
    
    let g:neocomplcache_force_overwrite_completefunc = 1
    
    let g:neocomplcache_force_omni_patterns.c      = '[^.[:digit:] *\t]\%(\.\|->\)'
    let g:neocomplcache_force_omni_patterns.cpp    = '[^.[:digit:] *\t]\%(\.\|->\)'
        \ . '\|\h\w*::'
    let g:neocomplcache_force_omni_patterns.objc   = '[^.[:digit:] *\t]\%(\.\|->\)'
        \ . '\|\h\w*::'
    let g:neocomplcache_force_omni_patterns.objcpp = '[^.[:digit:] *\t]\%(\.\|->\)'
        \ . '\|\h\w*::'
    
    inoremap <expr> <Tab>   pumvisible() ? "\<C-n>" : "\<Tab>"
    inoremap <expr> <S-Tab> pumvisible() ? "\<C-p>" : "\<S-Tab>"
    

    Configure clang_complete:


    let g:clang_use_library      = 1
    let g:clang_auto_select      = 0
    let g:clang_complete_auto    = 0
    let g:clang_complete_copen   = 1
    let g:clang_complete_macros  = 1
    let g:clang_complete_patters = 1
    
    let g:clang_library_path = 'D:/Toolchains/x64/LLVM/3.3/bin'
    
    let g:clang_auto_user_options = 'path, .clang_complete'
    

    For more information on both of these plugins use Vim's help documentation: :h clang_complete and :h neocomplcache. There are lots of options to tweak, especially in neocomplcache. From my point of view both plugins are must have for any C++ developer who uses Vim.

    NOTE: If you don't know how to complete some of the steps listed here, you would have to either ask additional questions here on StackOverflow or look elsewhere for the reason described in the beginning of this answer.

    I hope this helps and you would favor Vim more for your development efforts from now. :)