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
|
||||
Loading…
Add table
Add a link
Reference in a new issue