1 " Python imports insert completion
2 " Maintainer: Sergey Matveev <stargrave@stargrave.org>
3 " License: GNU General Public License version 3 of the License or later
5 " This plugin is intended for quick import-line insertion.
6 " For example you have got "from foo.bar import Baz" import somewhere in
7 " your Git-versioned code. In another file you can write "Baz" and then
8 " press <F3> in insert mode. Completion menu will show you suggestions
9 " of all import lines from your code containing Baz import.
11 " If you have Pylint or Pyflakes output in quickfix window, containing
12 " unfedined variables errors, then you can you :call AllImportCompl()
13 " function. It will go through the quickfix list and find all possible
14 " imports, sort them and insert under current cursor position.
16 " It uses external "git grep" call and assumes that "canonical" Python
17 " import format is used (single import per line).
18 " You can use http://www.git.stargrave.org/?p=pyimportcan.git;a=blob;f=pyimportcan.pl
19 " utility to convert existing imports to that format.
21 let s:git_grep_cmd = "git grep -H --line-number --ignore-case --no-color "
23 function! python#importcompl#sortByLen(s1, s2)
24 if len(a:s1) == len(a:s2) | return a:s1 > a:s2 | endif
25 return 1 ? len(a:s1) > len(a:s2) : -1
28 function! python#importcompl#do() abort
30 let output = system(s:git_grep_cmd . '"^from .* import .*' . @" . '" -- "*.py" "**/*.py"')
32 for line in split(output, "\n")
33 if stridx(line, "unused-import") != -1 | continue | endif
34 let m = matchlist(line, '^.*:\d\+:\(.*\)$')
35 if len(m) == 0 | continue | endif
36 call insert(suggestions, m[1])
38 call sort(suggestions, "python#importcompl#sortByLen")
39 call uniq(suggestions)
40 call reverse(suggestions)
41 call complete(col('.'), suggestions)
45 function! python#importcompl#all() abort
46 let output = system(s:git_grep_cmd . '"^from .* import" -- "*.py" "**/*.py"')
48 for line in split(output, "\n")
49 if stridx(line, "unused-import") != -1 | continue | endif
51 \'^.*:\d\+:\(from .* import \(\w\+\).*\)$',
52 \'^.*:\d\+:\(from .* import \w\+ as \(\w\+\).*\)$',
54 let m = matchlist(line, regexp)
55 if len(m) == 0 | break | endif
56 let imports[m[2]] = m[1]
59 let lines = getloclist(winnr())
60 if len(lines) == 0 | let lines = getqflist() | endif
63 let m = matchlist(line.text, '\(E0602\|F821\).*' . "'" . '\(\w\+\)' . "'$")
64 if len(m) == 0 || !has_key(imports, m[2]) | continue | endif
65 call insert(result, imports[m[2]])
67 call sort(result, "i")
69 call append(".", result)