Vim Cmake integration
Asked Answered
H

4

7

I have a cmake project. I want to do the following easily

  • search the declaration, definition and references of any variable, function, etc. under the cursor, which may be declared in an external header file whose path is added using INCLUDE_DIRECTORIES in CMakeLists.txt
  • rename a variable, function, etc. that is declared in the project

How can I set this up?

Huntington answered 31/8, 2018 at 6:3 Comment(0)
C
2

You can try to use vim plugin cmake4vim in order to integrate CMake to Vim. This plugin helps to work with cmake targets and allows to generate compilation database file (compile_commands.json). A lot of plugins use this file for code completion, jump to definition and etc. (for example YCM)

Also you can use vim lsp plugins (for example vim-lsp) these plugins use language servers for code completion, refactoring and another good features.

But CMake project integration (cmake cache generation, project compilation, etc.) and search the declaration, definition and etc are different tasks. And different plugins and tools solve these tasks.

Cretinism answered 13/3, 2019 at 8:29 Comment(0)
L
0
  • You can tell Vim where to look for includes by adding entries to the path option. I don't have enough experience with Cmake to know how to pull paths from CMakeLists.txt, though.

    See :help 'path'.

  • Assuming a properly set path, it is possible to use the built-in :dsearch and related commands to search for definitions across includes.

    The define option has a prescriptive name but it could be used to find any specific pattern so you could alter it to match declarations, too, or really anything.

    See :help include-search and :help 'define'.

  • Vim has no built-in concept of "reference". :isearch and friends should work for that but they will probably be too noisy.

  • Renaming is usually done with something like:

    :grep foo paths
    :cwindow
    :cdo s/foo/bar/gc
    
Laborious answered 31/8, 2018 at 9:44 Comment(3)
Is it renaming by the token (as provided by all modern IDEs)? Or is it a dumb string search/replace which can tamper with another names and parts of text damagingly?Echelon
@bloody, Vim isn't an IDE. It doesn't know anything about your code so there is no reason whatsoever to expect IDE levels of smartness. That said, the /c flag prompts the user for confirmation so, if you pay attention to what you are doing, you don't risk much.Laborious
Thank's for the feedback. Since our exchange I've realized that :grep -w foo paths is better for renaming in the first place. It's not like IDE's rename but it won't at least include substrings from longer names sparing some review time possibly. Surely closer to "real" renaming.Echelon
G
0

YouCompleteMe will help you. It uses compilation_database.json, witch can be generated by cmake.

This plugin also provides autocompetion for many languages.

Gambado answered 22/4, 2019 at 9:24 Comment(1)
Please Take the Tour , and be sure with your answer link While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. how to answerCercaria
Q
0

I use functions in vim and assign them to a hotkey.

https://developer.ibm.com/tutorials/l-vim-script-2/

it gives you more an IDE feel. but at the end of the day you get a bit more control.

Quan answered 22/4, 2019 at 12:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.