seineo
seineo

Reputation: 93

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?

Upvotes: 0

Views: 1874

Answers (2)

Kohirus
Kohirus

Reputation: 1

If you are using Neovim, you can try my plugin: cppassist.nvim, which can basically be used normally, but there are still many problems. Welcome to ask me questions!

This plugin will recursively search in your working directory until a file matching the same name with the counterpart extension is discovered. Note that the search also respects your .gitignore if one exists and any file ignored in git will be ignored in the results. As such, I suggest working from the root of your project. Once that file is found, it will automatically be opened in the current buffer. If no corresponding file is found, a message will be logged to the messages buffer -- use :messages to review your recent messages.

It uses regular expressions instead of LSP to generate function definition. Currently, it supports most keywords, as well as template types, and can generate multiple function definitions simultaneously in view mode.

However, the definition of nested classes is not currently supported. Also, if the function definition is already generated, press the shortcut key again and it will generate the function definition again.

Upvotes: 0

Luc Hermitte
Luc Hermitte

Reputation: 32966

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.

Upvotes: 1

Related Questions