Added very basic support for autocompleting modules

Closes #15
Relates #7
This commit is contained in:
baabelfish 2016-02-07 15:33:00 +02:00
commit f1384258dc
5 changed files with 174 additions and 38 deletions

82
autoload/modules.vim Normal file
View 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

View file

@ -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' }

View file

@ -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

View file

@ -5,16 +5,3 @@ let s:loaded = 1
au BufNewFile,BufRead *.nim set filetype=nim
au BufNewFile,BufRead *.nims set filetype=nims
let g:nvim_nim_highlighter_enable = 1
let g:nvim_nim_highlighter_semantic = 1
let g:nvim_nim_enable_async = 1
let g:nvim_nim_highlight_builtin = 1
let g:nvim_nim_highlight_use_unite = 0
let g:nvim_nim_autocomplete = "omni" " omni, deoplete, ycm
let g:nvim_nim_outline = "quickfix" " quickfix, outline
let g:nvim_nim_outline_buffer = 1
let g:nvim_nim_outline_buffer_width = 30
let g:nvim_nim_highlighter_semantics = ["skConst", "skForVar", "skGlobalVar", "skGlobalLet", "skLet", "skModule", "skParam", "skTemp", "skVar"]

View file

@ -1,9 +1,46 @@
" if exists("s:loaded")
" finish
" endif
" let s:loaded = 1
if exists("s:loaded")
finish
endif
let s:loaded = 1
let g:nvim_nim_exec_nim = util#CheckDependency("nim")
let g:nvim_nim_exec_nimble = util#CheckDependency("nimble")
let g:nvim_nim_exec_nimsuggest = util#CheckDependency("nimsuggest")
let g:nvim_nim_exec_bash = util#CheckDependency("bash")
function! CheckDependency(command)
if !executable(a:command)
echoerr "Not found: " . a:command
finish
endif
return exepath(a:command)
endfunction
" FIXME
function! FindNimbleModulesPath()
return "~/.nimble/pkgs/"
endfunction
" FIXME
function! FindNimModulesPath()
return "/usr/lib/nim/"
endfunction
let g:nvim_nim_exec_nim = CheckDependency("nim")
let g:nvim_nim_exec_nimble = CheckDependency("nimble")
let g:nvim_nim_exec_nimsuggest = CheckDependency("nimsuggest")
let g:nvim_nim_exec_bash = CheckDependency("bash")
let g:nvim_nim_deps_nim = FindNimModulesPath()
let g:nvim_nim_deps_nimble = FindNimbleModulesPath()
let g:nvim_nim_highlighter_enable = 1
let g:nvim_nim_highlighter_enable = 1
let g:nvim_nim_highlighter_semantic = 1
let g:nvim_nim_enable_async = 1
let g:nvim_nim_highlight_builtin = 1
let g:nvim_nim_highlight_use_unite = 0
let g:nvim_nim_outline_buffer = 1
let g:nvim_nim_outline_buffer_width = 30
let g:nvim_nim_highlighter_semantics = ["skConst", "skForVar", "skGlobalVar", "skGlobalLet", "skLet", "skModule", "skParam", "skTemp", "skVar"]