Search code examples
vimvim-plugin

Vim generate function definition from the declaration


I'm learning to use vim, but I think it's inconvenient to generate function definition in .cpp file from its declaration in .h file.

For example, if I declare a function void print(const vector<int>& arr); in A.h, I have to open A.cpp and type the following:

void print(const vector<int>& arr) {

}

(or use yy copy the declaration line, then delete ; and add {}...) When some derived classes need to override function in base class, it can be a heavy job...

Is there any convenient plugin or command to help me deal with it?


Solution

  • My lh-cpp plugin has been providing this feature for quite some time now.

    Go on the function declaration, type :GOTOIMPL et voilà!. It either moves the cursor to a function definition (from its declaration), or if none exists, it generates an empty shell to define that function.

    Note: I'm currently in the process of improving the feature to support any kind of function declaration. To support template functions, you'd have to use the gotoimpl_with_libclang branch and the support plugin vim-clang (in V2Upgrade branch).

    At this precise moment the sister command :MOVETOIMPL doesn't work as expected with constructors defined with initializer-lists, which has side effects on the :Constructor command. :MOVETOIMPL is meant to change an inline definition into a declaration plus a separate definition in a .cpp file typically.

    Note: lh-cpp is a complex plugin that provides many things and that has many dependencies. Regarding overriding, it provides an :Override command to let us select which function we want to override -- this feature requires my current working branches of lh-cpp and vim-clang.