Fixed #668: Improve graphics completion

This commit is contained in:
Karl Yngve Lervåg 2017-01-21 21:40:48 +01:00
parent 05697bddb0
commit 85cf4215ea

View File

@ -381,27 +381,14 @@ let s:img = {
\} \}
function! s:img.complete(regex) dict " {{{2 function! s:img.complete(regex) dict " {{{2
call self.get_graphicspaths()
call self.gather_candidates() call self.gather_candidates()
call filter(self.candidates, 'v:val =~# a:regex') call filter(self.candidates, 'v:val.word =~# a:regex')
call map(self.candidates, '{
\ ''abbr'' : v:val,
\ ''word'' : v:val,
\ ''menu'' : '' [graphics]'',
\ }')
if !empty(self.graphicspaths)
for l:cand in self.candidates
let l:cand.word = fnamemodify(l:cand.word, ':t')
endfor
endif
return self.candidates return self.candidates
endfunction endfunction
function! s:img.get_graphicspaths() dict " {{{2 function! s:img.graphicspaths() dict " {{{2
" Get preamble text and remove comments " Get preamble text and remove comments
let l:preamble = vimtex#parser#tex(b:vimtex.tex, { let l:preamble = vimtex#parser#tex(b:vimtex.tex, {
\ 're_stop': '\\begin{document}', \ 're_stop': '\\begin{document}',
@ -409,26 +396,42 @@ function! s:img.get_graphicspaths() dict " {{{2
\}) \})
call map(l:preamble, 'substitute(v:val, ''\\\@<!%.*'', '''', '''')') call map(l:preamble, 'substitute(v:val, ''\\\@<!%.*'', '''', '''')')
" Parse preamble for graphicspath command " Parse preamble for graphicspaths
let l:graphicspath = matchstr(join(l:preamble, ' '), let l:graphicspaths = []
\ '\\graphicspath{\s*{\s*\zs.\{-}\ze\s*}\s*}') for l:path in split(matchstr(join(l:preamble, ' '),
let self.graphicspaths = map(split(l:graphicspath, '}\s*{'), \ '\\graphicspath{\s*{\s*\zs.\{-}\ze\s*}\s*}'), '}\s*{')
\ 'v:val[0] ==# ''/'' ? v:val : simplify(b:vimtex.root . ''/'' . v:val)') if l:path[0] ==# '/'
call add(l:graphicspaths, l:path[:-2])
else
call add(l:graphicspaths, simplify(b:vimtex.root . '/' . l:path[:-2]))
endif
endfor
" Project root is always valid
return l:graphicspaths + [b:vimtex.root]
endfunction endfunction
" }}}2
function! s:img.gather_candidates() dict " {{{2 function! s:img.gather_candidates() dict " {{{2
if !empty(self.graphicspaths) let l:added_files = []
let self.candidates = split(globpath(expand('%:p:h'), '*.*'), '\n') let l:generated_pdf = b:vimtex.out()
for l:path in self.graphicspaths
let self.candidates += split(globpath(l:path, '*.*'), '\n')
endfor
else
let self.candidates = split(globpath(b:vimtex.root, '**/*.*'), '\n')
endif
call filter(self.candidates, 'v:val !=# b:vimtex.out()') let self.candidates = []
call filter(self.candidates, 'v:val =~? self.ext_re') for l:path in self.graphicspaths()
call map(self.candidates, 'vimtex#paths#shorten_relative(v:val)') for l:file in split(globpath(l:path, '**/*.*'), '\n')
if l:file !~? self.ext_re
\ || l:file ==# l:generated_pdf
\ || index(l:added_files, l:file) >= 0 | continue | endif
call add(l:added_files, l:file)
call add(self.candidates, {
\ 'abbr': vimtex#paths#shorten_relative(l:file),
\ 'word': vimtex#paths#relative(l:file, l:path),
\ 'menu': '[graphics]',
\})
endfor
endfor
endfunction endfunction
" }}}1 " }}}1