X-Git-Url: http://www.git.stargrave.org/?a=blobdiff_plain;f=vim%2F.vim%2Fpack%2Fstargrave%2Fstart%2Fpy-importcompl%2Fautoload%2Fpython%2Fimportcompl.vim;h=48c166a4499eaf2c3a5c7ca432bf4fe76ca595ce;hb=9cbfa59ca421fac5422cbf0c2c52f48f8f2adb4c;hp=8bb62b83936bb9de021bb9f0cb2009fedfb2e686;hpb=efec8ecc6aea7f853aba30893b1c85b48b8b9a49;p=dotfiles.git diff --git a/vim/.vim/pack/stargrave/start/py-importcompl/autoload/python/importcompl.vim b/vim/.vim/pack/stargrave/start/py-importcompl/autoload/python/importcompl.vim index 8bb62b8..48c166a 100644 --- a/vim/.vim/pack/stargrave/start/py-importcompl/autoload/python/importcompl.vim +++ b/vim/.vim/pack/stargrave/start/py-importcompl/autoload/python/importcompl.vim @@ -1,69 +1,75 @@ -" Python imports insert completion -" Maintainer: Sergey Matveev -" License: GNU General Public License version 3 of the License or later -" -" This plugin is intended for quick import-line insertion. -" For example you have got "from foo.bar import Baz" import somewhere in -" your Git-versioned code. In another file you can write "Baz" and then -" press in insert mode. Completion menu will show you suggestions -" of all import lines from your code containing Baz import. -" -" If you have Pylint or Pyflakes output in quickfix window, containing -" unfedined variables errors, then you can you :call python#importcompl#all() -" function. It will go through the quickfix list and find all possible -" imports, sort them and insert under current cursor position. -" -" It uses external "git grep" call and assumes that "canonical" Python -" import format is used (single import per line). -" You can use http://www.git.stargrave.org/?p=pyimportcan.git;a=blob;f=pyimportcan.pl -" utility to convert existing imports to that format. +vim9script -let s:git_grep_cmd = "git grep -H --line-number --ignore-case --no-color " +# Python imports insert completion +# Maintainer: Sergey Matveev +# License: GNU General Public License version 3 of the License or later +# +# This plugin is intended for quick import-line insertion. +# For example you have got "from foo.bar import Baz" import somewhere in +# your Git-versioned code. In another file you can write "Baz" and then +# press in insert mode. Completion menu will show you suggestions +# of all import lines from your code containing Baz import. +# +# If you have Pylint or Pyflakes output in quickfix window, containing +# unfedined variables errors, then you can you :call python#importcompl#all() +# function. It will go through the quickfix list and find all possible +# imports, sort them and insert under current cursor position. +# +# It uses external "git grep" call and assumes that "canonical" Python +# import format is used (single import per line). +# You can use http://www.git.stargrave.org/?p=pyimportcan.git;a=blob;f=pyimportcan.pl +# utility to convert existing imports to that format. -function! python#importcompl#sortByLen(s1, s2) - if len(a:s1) == len(a:s2) | return a:s1 > a:s2 | endif - return 1 ? len(a:s1) > len(a:s2) : -1 -endfunction +const gitGrepCmd = "git grep -H --line-number --ignore-case --no-color " -function! python#importcompl#do() abort +export def Do(): string normal diw - let output = system(s:git_grep_cmd . '"^from .* import .*' . @" . '" -- "*.py" "**/*.py"') - let suggestions = [] + var output = system(gitGrepCmd .. '"^from .* import .*' .. @" .. '" -- "*.py" "**/*.py"') + var suggestions: list + var m: list for line in split(output, "\n") if stridx(line, "unused-import") != -1 | continue | endif - let m = matchlist(line, '^.*:\d\+:\(.*\)$') + m = matchlist(line, '^.*:\d\+:\(.*\)$') if len(m) == 0 | continue | endif - call insert(suggestions, m[1]) + insert(suggestions, m[1]) endfor - call sort(suggestions, "python#importcompl#sortByLen") - call uniq(suggestions) - call reverse(suggestions) - call complete(col('.'), suggestions) - return '' -endfunction + sort(suggestions, (s1: string, s2: string): number => { + if len(s1) == len(s2) | return 0 | endif + if len(s1) > len(s2) | return 1 | endif + return -1 + }) + uniq(suggestions) + reverse(suggestions) + complete(col("."), suggestions) + return "" +enddef -function! python#importcompl#all() abort - let output = system(s:git_grep_cmd . '"^from .* import" -- "*.py" "**/*.py"') - let imports = {} +export def All() + var output = system(gitGrepCmd .. '"^from .* import" -- "*.py" "**/*.py"') + var imports = {} + var ms: list for line in split(output, "\n") if stridx(line, "unused-import") != -1 | continue | endif for regexp in [ - \'^.*:\d\+:\(from .* import \(\w\+\).*\)$', - \'^.*:\d\+:\(from .* import \w\+ as \(\w\+\).*\)$', - \] - let m = matchlist(line, regexp) - if len(m) == 0 | break | endif - let imports[m[2]] = m[1] + '^.*:\d\+:\(from .* import \(\w\+\).*\)$', + '^.*:\d\+:\(from .* import \w\+ as \(\w\+\).*\)$', + ] + ms = matchlist(line, regexp) + if len(ms) == 0 | break | endif + imports[ms[2]] = ms[1] endfor endfor - let result = [] - for diag in lsp#internal#diagnostics#movement#get_all_buffer_diagnostics() - if diag["message"] !~ "^undefined name" | continue | endif - let m = diag["message"][strridx(diag["message"][:-2], "'")+1:-2] + var lines = getloclist(winnr()) + if len(lines) == 0 | lines = getqflist() | endif + var result: list + var m: string + for line in lines + if line.text !~ "^undefined name" | continue | endif + m = line.text[strridx(line.text[: -2], "'") + 1 : -2] if len(m) == 0 || !has_key(imports, m) | continue | endif - call insert(result, imports[m]) + insert(result, imports[m]) endfor - call sort(result, "i") - call uniq(result) - call append(".", result) -endfunction + sort(result, "i") + uniq(result) + append(".", result) +enddef