Vim Autocomplete for Django

2015 May 11 at 12:12 » Tagged as :python, django, vim, mynt,

This is the continuation of a journey that began by switching from eclipse/pydev to vim for python/django development via python mode. Looking to setup auto completion for python and django. Let's jump straight in

Rope

Originally rope was installed as part of python-mode and I quickly tired of it and switched to jedi it was only after installation that I discovered it to be incompatible with python-mode. That was what prompted my decision to give up on python mode (it has far too many blackboxes) and install everything manually. I managed to get auto complete working beautifully with rope or so I thought.

autocomplete django code with vim and rope

and this is the configuration that was used

let ropevim_vim_completion = 1
let ropevim_extended_complete = 1
let ropevim_enable_autoimport = 1
let g:ropevim_autoimport_modules = ["os.*","traceback","django.*"]
imap <C-space> <C-R>=RopeCodeAssistInsertMode()<CR>

The trouble with this setup is that auto completion would add the package name after the class name!. For example if you were to select HttpResponse from the dialog, what actually gets added is HttpResponse : django.http.HttpResponse. Using the let ropevim_vim_completion = 1 also means taking vim auto import out of the picture. The meaning of Auto import here is to automatically add the import statement to the top of the code without moving the cursor. This is one of the most usefull features of pydev

If you set let ropevim_vim_completion = 0 auto complete still works but you lose the popup dialog. Instead you get a line near the bottom of the screen with the choice. You need to cycle though the options with the tab key.

Jedi

The real problem with jedi is that you need to know that full class name including it's package for auto complete to work. For example just typing Http<Ctrl Space> will not give you HttpResponse or any of the related packages from django.http. For me this is a real problem, I have never bothered to remember the full package names for classes in any of the languages I have ever used. There isn't a need for it. That's what IDEs are for!

Conclusion.

So it seems that both Jedi and Rope have their short comings. Where Jedi is strong rope is lacking and vice verce. Hay hang on a second. Can't we use both? The answer my dear madam is that you can! This is the relevent section of the .vimrc

" Use both ropevim and jedi-vim for auto completion.Python-mode 
" let ropevim_vim_completion = 1
let ropevim_extended_complete = 1
let ropevim_enable_autoimport = 1 
let g:ropevim_autoimport_modules = ["os.*","traceback","django.*"] 
imap <s-space> <C-R>=RopeCodeAssistInsertMode()<CR>

let g:jedi#completions_command = "<C-Space>"
let g:jedi#auto_close_doc = 1

Now when you are not sure of the package name you hit Shift-Space to do an rope auto import + auto complete action. You don't get a pop up but have to tab through the options. When the class has already been imported or you know the full name, hit Ctrl Space and get jedi to do the dirty work for you.