Improve file name completion (#308)

This commit is contained in:
Karl Yngve Lervåg 2016-01-04 23:28:58 +01:00
parent bb04215684
commit 33daacfed8
2 changed files with 59 additions and 30 deletions

View File

@ -10,13 +10,14 @@ function! vimtex#complete#init_options() " {{{1
call vimtex#util#set_default('g:vimtex_complete_close_braces', 0) call vimtex#util#set_default('g:vimtex_complete_close_braces', 0)
call vimtex#util#set_default('g:vimtex_complete_recursive_bib', 0) call vimtex#util#set_default('g:vimtex_complete_recursive_bib', 0)
call vimtex#util#set_default('g:vimtex_complete_img_use_tail', 0)
endfunction endfunction
" }}}1 " }}}1
function! vimtex#complete#init_script() " {{{1 function! vimtex#complete#init_script() " {{{1
if !g:vimtex_complete_enabled | return | endif if !g:vimtex_complete_enabled | return | endif
let s:completers = [s:bib, s:ref, s:img] let s:completers = [s:bib, s:ref, s:img, s:inc]
endfunction endfunction
" }}}1 " }}}1
@ -55,7 +56,7 @@ function! vimtex#complete#omnifunc(findstart, base) " {{{1
endfor endfor
return -3 return -3
else else
return s:completer.complete(a:base) return s:close_braces(s:completer.complete(a:base))
endif endif
endfunction endfunction
@ -97,7 +98,7 @@ function! s:bib.init() dict " {{{2
endfunction endfunction
function! s:bib.complete(regexp) dict " {{{2 function! s:bib.complete(regexp) dict " {{{2
let res = [] let l:res = []
let self.type_length = 4 let self.type_length = 4
for m in self.search(a:regexp) for m in self.search(a:regexp)
@ -110,21 +111,14 @@ function! s:bib.complete(regexp) dict " {{{2
let auth = substitute(auth, '\~', ' ', 'g') let auth = substitute(auth, '\~', ' ', 'g')
let auth = substitute(auth, ',.*\ze', ' et al. ', '') let auth = substitute(auth, ',.*\ze', ' et al. ', '')
let w = { call add(l:res, {
\ 'word': m['key'], \ 'word': m['key'],
\ 'abbr': type . auth . year, \ 'abbr': type . auth . year,
\ 'menu': m['title'] \ 'menu': m['title']
\ } \ })
" Close braces if desired
if g:vimtex_complete_close_braces && !s:next_chars_match('^\s*[,}]')
let w.word = w.word . '}'
endif
call add(res, w)
endfor endfor
return res return l:res
endfunction endfunction
function! s:bib.search(regexp) dict " {{{2 function! s:bib.search(regexp) dict " {{{2
@ -256,16 +250,11 @@ function! s:ref.complete(regex) dict " {{{2
let suggestions = [] let suggestions = []
for m in matches for m in matches
let entry = { call add(suggestions, {
\ 'word' : m[0], \ 'word' : m[0],
\ 'abbr' : m[0],
\ 'menu' : printf('%7s [p. %s]', '('.m[1].')', m[2]) \ 'menu' : printf('%7s [p. %s]', '('.m[1].')', m[2])
\ } \ })
if g:vimtex_complete_close_braces && !s:next_chars_match('^\s*[,}]')
let entry = copy(entry)
let entry.abbr = entry.word
let entry.word = entry.word . '}'
endif
call add(suggestions, entry)
endfor endfor
return suggestions return suggestions
@ -335,13 +324,25 @@ function! s:img.complete(regex) dict " {{{2
let l:output = b:vimtex.out() let l:output = b:vimtex.out()
call filter(l:candidates, 'v:val !=# l:output') call filter(l:candidates, 'v:val !=# l:output')
call map(l:candidates, 'strpart(v:val, len(b:vimtex.root)+1)')
if g:vimtex_complete_close_braces && !s:next_chars_match('^\s*[,}]') if g:vimtex_complete_img_use_tail
call map(l:candidates, '{ ''abbr'' : v:val, ''word'' : v:val . ''}'' }') return uniq(sort(map(l:candidates, 'fnamemodify(v:val, '':t'')')))
else
return map(l:candidates, 'strpart(v:val, len(b:vimtex.root)+1)')
endif endif
endfunction
return l:candidates " }}}1
" {{{1 Include completion
let s:inc = {
\ 'pattern' : '\v\\%(include|input)\s*\{[^{}]*',
\ 'enabled' : 1,
\}
function! s:inc.complete(regex) dict " {{{2
let l:candidates = globpath(b:vimtex.root, '**/*.tex', 1, 1)
return map(l:candidates, 'strpart(v:val, len(b:vimtex.root)+1)')
endfunction endfunction
" }}}1 " }}}1
@ -349,8 +350,24 @@ endfunction
" "
" Utility functions " Utility functions
" "
function! s:next_chars_match(regex) " {{{1 function! s:close_braces(candidates) " {{{1
return strpart(getline('.'), col('.') - 1) =~ a:regex if empty(a:candidates) | return [] | endif
if !g:vimtex_complete_close_braces
\ || strpart(getline('.'), col('.') - 1) =~# '^\s*[,}]'
return a:candidates
endif
" Candidates may be either strings or dictionaries
if type(a:candidates[0]) == type({})
let l:candidates = a:candidates
for l:cand in l:candidates
let l:cand.word .= '}'
endfor
return l:candidates
else
return map(a:candidates, '{ ''abbr'' : v:val, ''word'' : v:val . ''}'' }')
endif
endfunction endfunction
" }}}1 " }}}1

View File

@ -351,6 +351,12 @@ Options~
Default value: 0 Default value: 0
*g:vimtex_complete_img_use_tail*
If enabled, only the tail part of file names are completed. This is useful
if one uses the `graphicx` package and the `\graphicspath` command.
Default value: 0
*g:vimtex_change_complete_envs* *g:vimtex_change_complete_envs*
Define a list of environments that will be completed when changing the Define a list of environments that will be completed when changing the
surrounding environments (see |<plug>(vimtex-change-env)|). surrounding environments (see |<plug>(vimtex-change-env)|).
@ -1285,11 +1291,17 @@ Complete file names for figures~
File name completion for figures is triggered by '\includegraphic{' commands. File name completion for figures is triggered by '\includegraphic{' commands.
As an example: > By default, the paths are completed relative to the root path of the main
file. One can use the option |g:vimtex_complete_img_use_tail| to instead
complete only the tail part of file names. This is useful if one uses the
`graphicx` package and the `\graphicspath` command.
\includegraphic{fig<CTRL-X><CTRL-O> ------------------------------------------------------------------------------
Complete input/include files~
*vimtex-complete-input*
offers a list of all matching file names. File name completion for input/include files is triggered by '\input{' or
'\include{' commands.
------------------------------------------------------------------------------ ------------------------------------------------------------------------------
Autocomplete~ Autocomplete~