Added very basic support for autocompleting modules
Closes #15 Relates #7
This commit is contained in:
parent
a7cf32795c
commit
f1384258dc
5 changed files with 174 additions and 38 deletions
82
autoload/modules.vim
Normal file
82
autoload/modules.vim
Normal file
|
|
@ -0,0 +1,82 @@
|
|||
if exists("s:loaded")
|
||||
finish
|
||||
endif
|
||||
let s:loaded = 1
|
||||
|
||||
|
||||
function! modules#FindImportLocation()
|
||||
return searchpos("^import")
|
||||
endfunction
|
||||
|
||||
function! modules#HasImports()
|
||||
let [l, c] = modules#FindImportLocation()
|
||||
return l != 0 && c == 1
|
||||
endfunction
|
||||
|
||||
function! modules#IsMultilineImport()
|
||||
if modules#HasImports()
|
||||
let [l, c] = modules#FindImportLocation()
|
||||
return indent(l + 1) > 0
|
||||
endif
|
||||
return 0
|
||||
endfunction
|
||||
|
||||
function! modules#AddImport(module)
|
||||
let imports = modules#GetImports()
|
||||
let [begin, end] = modules#ImportLineRange()
|
||||
exec ":" . begin . "," . end . "d"
|
||||
|
||||
let idx = begin - 1
|
||||
call add(imports, a:module)
|
||||
call append(idx, "import")
|
||||
for import in imports
|
||||
let wsstr = ""
|
||||
for wr in range(0, &sw)
|
||||
let wsstr .= " "
|
||||
endfor
|
||||
|
||||
let idx += 1
|
||||
call append(idx, wsstr . import . (idx - begin + 1 < len(imports) ? "," : ""))
|
||||
endfor
|
||||
endfunction
|
||||
|
||||
function! modules#ImportLineRange()
|
||||
let [l, c] = modules#FindImportLocation()
|
||||
if l == 0
|
||||
return [0, 0]
|
||||
endif
|
||||
|
||||
let idx = 2
|
||||
while idx < line("$") && indent(idx) > 0
|
||||
let idx += 1
|
||||
endwhile
|
||||
return [l, idx - 1]
|
||||
endfunction
|
||||
|
||||
function! modules#GetImports()
|
||||
if !modules#HasImports()
|
||||
return []
|
||||
endif
|
||||
|
||||
let [begin, end] = modules#ImportLineRange()
|
||||
let lines = getline(begin, end)
|
||||
return filter(split(substitute(join(lines, ""), ",", "", "g"), " "), 'v:val !~ "^ *$"')[1:-1]
|
||||
endfunction
|
||||
|
||||
function! modules#ImportMap(imports)
|
||||
let result = {}
|
||||
for import in a:imports
|
||||
if import !~? ".*\/system\/.*" && import !~? ".*\/deprecated\/.*"
|
||||
let result[fnamemodify(import, ":t:r")] = import
|
||||
endif
|
||||
endfor
|
||||
return result
|
||||
endfunction
|
||||
|
||||
" function! modules#FindLocalImports()
|
||||
" " return ImportMap(globpath(g:nvim_nim_deps_nim, "**/*.nim", 0, 1))
|
||||
" endfunction
|
||||
|
||||
function! modules#FindGlobalImports()
|
||||
return modules#ImportMap(globpath(g:nvim_nim_deps_nim, "**/*.nim", 0, 1))
|
||||
endfunction
|
||||
|
|
@ -10,29 +10,59 @@ function! omni#item(parsed)
|
|||
\ 'info': a:parsed.doc,
|
||||
\ 'menu': a:parsed.module,
|
||||
\ }
|
||||
" \ 'kind': a:parsed.kindshort . " » " . a:parsed.type,
|
||||
endfunction
|
||||
|
||||
function! omni#nim(findstart, base)
|
||||
if a:findstart && empty(a:base)
|
||||
return col('.')
|
||||
else
|
||||
function! omni#item_module(name, file, type)
|
||||
return {
|
||||
\ 'word': a:name,
|
||||
\ 'kind': a:type,
|
||||
\ 'info': a:file,
|
||||
\ 'menu': "module",
|
||||
\ }
|
||||
endfunction
|
||||
|
||||
let file = expand("%:p")
|
||||
function! omni#nimsuggest(file, l, c)
|
||||
let completions = []
|
||||
let tempfile = util#WriteMemfile()
|
||||
let l = line(".")
|
||||
let c = col(".")
|
||||
|
||||
let query = "sug " . file . ";" . tempfile . ":" . l . ":" . c
|
||||
let jobcmdstr = g:nvim_nim_exec_nimsuggest . " --v2 --stdin " . file
|
||||
let query = "sug " . a:file . ";" . tempfile . ":" . a:l . ":" . a:c
|
||||
let jobcmdstr = g:nvim_nim_exec_nimsuggest . " --v2 --stdin " . a:file
|
||||
let fullcmd = 'echo -e ' . shellescape(query, 1) . '|' . jobcmdstr
|
||||
let completions_raw = split(system(fullcmd), "\n")[4:-2]
|
||||
let completions = []
|
||||
|
||||
for line in completions_raw
|
||||
call add(completions, omni#item(util#ParseV2(line)))
|
||||
endfor
|
||||
|
||||
return completions
|
||||
endfunction
|
||||
|
||||
function! omni#modulesuggest(file, l, c)
|
||||
let modules = modules#FindGlobalImports()
|
||||
let completions = []
|
||||
for module in sort(keys(modules))
|
||||
call add(completions, omni#item_module(module, modules[module], "G"))
|
||||
endfor
|
||||
return completions
|
||||
endfunction
|
||||
|
||||
function! omni#nim(findstart, base)
|
||||
if a:findstart && empty(a:base)
|
||||
return col('.')
|
||||
endif
|
||||
|
||||
let completions = []
|
||||
let file = expand("%:p")
|
||||
let l = line(".")
|
||||
let c = col(".")
|
||||
|
||||
let [istart, iend] = modules#ImportLineRange()
|
||||
if istart != 0 && istart <= l && l < iend
|
||||
let completions = omni#modulesuggest(file, l, c)
|
||||
else
|
||||
let completions = omni#nimsuggest(file, l, c)
|
||||
endif
|
||||
|
||||
return {
|
||||
\ 'words': completions,
|
||||
\ 'refresh': 'always' }
|
||||
|
|
|
|||
|
|
@ -44,12 +44,12 @@ function! util#FirstNonEmpty(lines)
|
|||
endfunction
|
||||
|
||||
|
||||
function! util#CheckDependency(command)
|
||||
if !executable(a:command)
|
||||
echoerr "Not found: " . a:command
|
||||
finish
|
||||
endif
|
||||
return exepath(a:command)
|
||||
function! util#FirstNonEmpty(lines)
|
||||
for line in a:lines
|
||||
if len(line) > 0
|
||||
return line
|
||||
endif
|
||||
endfor
|
||||
endfunction
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue