user122072
user122072

Reputation: 91

vim - plugins for import suggestions

Are there any good plugins that will suggest or automate imports based on the code you write?

I would prefer to use vim over an ide, and use it for all tasks. I intend to use it as my sole development environment and master it, tailor it to my needs and use it for however long I may end up programming for. I'd rather use one editor with some good plugins and a terminal, than half a dozen ide's which I will never really master, or be forced to emulate vim with.

Upvotes: 0

Views: 338

Answers (1)

Luc Hermitte
Luc Hermitte

Reputation: 32956

In C and C++, I have a few C++ snippets that automatically add the related header files. Other snippets can easily be defined. I add them on a case by case basis (i.e. when I'm tired of always typing the same things over and over). The framework can be used with other languages, I did a first experience with Python as well.

VimL:" mu-template python-snippet for os.path.exists()
VimL:" hint: os.path.exists()
VimL: let s:value_start = '¡'
VimL: let s:value_end   = s:value_start
VimL: let s:marker_open  = '<+'
VimL: let s:marker_close = '+>'
VimL: call s:AddPostExpandCallback('lh#dev#import#add("os", {"symbol": "path"})')
os.path.exists(¡s:Surround(1, '<+type+>')¡)<++>

For other languages, the framework defined in lh-dev may need to be tweaked -- but don't worry variation points are already there to be specialized.

The cons are:

  • The automated inclusion of related header file is not smart. For instance, if foo.h includes <vector>, expanding vector (to std::vector<placeholder> placeholder) in foo.cpp will also include <vector>. It won't be able to know when a forward declaration is enough, either.
  • The snippet engine is mu-template, which while very powerful is not the most trendy one -- everyone will push you towards snipmate & cie ; I have to admit the explicit placeholder characters used are very noisy. Note however, that lh#dev#import#add() could be used from other snippet engines -- I just don't know how they'll react to function that execute vim append() function.

lh-cpp has another (related) feature. When the cursor is on an identifier declared in another file, CTRL-X_i will have lh-cpp look into the ctags database where the identifier is declared and try to add the proper include statement at the beginning of the file. So far, this feature is dedicated to C and C++, but it's be possible to open it to other languages -- I'll have to add it to my todo list.

Upvotes: 1

Related Questions