]> Sergey Matveev's repositories - dotfiles.git/blob - vim/.vim/ftplugin/python/importcompl.vim
Changed git web frontend
[dotfiles.git] / vim / .vim / ftplugin / python / importcompl.vim
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
4 "
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.
10 "
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.
15 "
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.
20
21 let s:git_grep_cmd = "git grep -H --line-number --ignore-case --no-color "
22
23 if exists('g:loaded_importcompl') | finish | endif
24 let g:loaded_importcompl = 1
25
26 function! SortByLen(s1, s2)
27     if len(a:s1) == len(a:s2) | return a:s1 > a:s2 | endif
28     return 1 ? len(a:s1) > len(a:s2) : -1
29 endfunction
30
31 function! ImportCompl()
32     normal diw
33     let output = system(s:git_grep_cmd . '"^from .* import .*' . @" . '" -- "*.py" "**/*.py"')
34     let suggestions = []
35     for line in split(output, "\n")
36         if stridx(line, "unused-import") != -1 | continue | endif
37         let m = matchlist(line, '^.*:\d\+:\(.*\)$')
38         if len(m) == 0 | continue | endif
39         call insert(suggestions, m[1])
40     endfor
41     call sort(suggestions, "SortByLen")
42     call uniq(suggestions)
43     call reverse(suggestions)
44     call complete(col('.'), suggestions)
45     return ''
46 endfunction
47
48 inoremap <F3> <C-R>=ImportCompl()<CR>
49
50 function! AllImportCompl()
51     let output = system(s:git_grep_cmd . '"^from .* import" -- "*.py" "**/*.py"')
52     let imports = {}
53     for line in split(output, "\n")
54         if stridx(line, "unused-import") != -1 | continue | endif
55         for regexp in [
56             \'^.*:\d\+:\(from .* import \(\w\+\).*\)$',
57             \'^.*:\d\+:\(from .* import \w\+ as \(\w\+\).*\)$',
58         \]
59             let m = matchlist(line, regexp)
60             if len(m) == 0 | break | endif
61             let imports[m[2]] = m[1]
62         endfor
63     endfor
64     let lines = getloclist(winnr())
65     if len(lines) == 0 | let lines = getqflist() | endif
66     let result = []
67     for line in lines
68         let m = matchlist(line.text, '\(E0602\|F821\).*' . "'" . '\(\w\+\)' . "'$")
69         if len(m) == 0 || !has_key(imports, m[2]) | continue | endif
70         call insert(result, imports[m[2]])
71     endfor
72     call sort(result, "i")
73     call uniq(result)
74     call append(".", result)
75 endfunction