Hi,
First, this is an awesome plugin, thank you for making it. One feature that would be useful is to have an option to sort the list of Import completions by keywords in the current buffer. An often used coding pattern (at least for me) is to use a name in the code then after wards go to the top of the file and import it. It would be nice if the import completion list put names that are already in the current buffer at the top.
For example, suppose I am using PyQt
I have some code:
class MyDialog(QDialog):
blah...
and later I do
from PyQt4.Qt import QD
I currently get a very long list in which QDialog is far from the top.
Oh, that's actually a very good idea. But it may take a while...
What about an "import this" shortcut instead (or additionally)? That would work at least for direct imports, not for from spam import ham
or import spam as ham
though... But that already covers many use cases, like this one:
d = {'spam': 1, 'ham': 2}
print json.dumps(d)
Putting the cursor over json
(or highlighting it in visual mode) and pressing <leader>i
would then import json.
Certainly an "import this" shortcut would be useful for importing builtin modules, but at least for my workflow, I tend to mostly be using from ... import ...
When implementing "import this", another consideration is coding style. PEP8 recommends putting each import statement on a separate line, to make debugging ImportErrors easier, but some people (I for one) disagree with that. In all my years of writing python, I've never had a problem interpreting a ImportError when importing a builtin module, so I like to put all import builtin_modules on the same line. And then there are considerations like import statement organization. In my projects I follow a convention of grouping different categories of imports, builtin modules first, third party libraries second and project modules third. I'm sure there are many other conventions.
Finally there is the problem of delayed imports. Many larger projects dont import at the top level of a module but only in the context where the imported name is used, to reduce startup time.
If you do want to implement "import this", you can, theoretically, implement it for from style imports as well by scanning sys.path on startup in a separate thread to find all importable names. This will also avoid the delay when doing import completion for the first time from a large namespace like PyQt4.Qt. And maybe have either a set of options to allow various coding conventions, or let people supply their own algorithm in the form of a python/vimscript function to determine import placement. Of course, this is a lot of work :)
@kovidgoyal If you really want this, feel free to do it yourself (It's probably not even that hard). I'm really busy for the next months. And there are a lot of fundamental problems within Jedi, that I need to address. Maybe I'll do it. But really, it might take some time. So if anyone really wants this, please contribute :-)
Won't happen. I think that alphanumeric sorting is good enough for now. If someone implements this in Jedi, feel free. But there's no priority.
For reference in case anyone takes a stab at it in the future:
It's not @kovidgoyal's original feature request in this issue, but the notion of an "organize imports" feature is also implied in the discussion—an implementation could quite possibly borrow from flake8-import-order which supports several styles.
Although maybe only in inspiration and not directly in code, because it's GPL…
For sorting check out https://github.com/timothycrosley/isort/, too.
It can be easily integrated into Vim: https://github.com/blueyed/dotfiles/blob/f0aa2c334c86c6ec9fe9598012eaa052c7e6449c/vimrc#L2528.
I also found this snippet a while ago (which uses rope), not sure how good it is: http://pastebin.com/dzjexWGu.
Most helpful comment
For sorting check out https://github.com/timothycrosley/isort/, too.
It can be easily integrated into Vim: https://github.com/blueyed/dotfiles/blob/f0aa2c334c86c6ec9fe9598012eaa052c7e6449c/vimrc#L2528.
I also found this snippet a while ago (which uses rope), not sure how good it is: http://pastebin.com/dzjexWGu.