Skip to content

Commit

Permalink
fix: bug in gls completion
Browse files Browse the repository at this point in the history
refer: #3046
  • Loading branch information
lervag committed Dec 13, 2024
1 parent 2b7784f commit 3de8ca5
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 11 deletions.
35 changes: 26 additions & 9 deletions autoload/vimtex/complete.vim
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,7 @@ function! s:completer_gls.init() dict abort " {{{2
if !has_key(b:vimtex.packages, 'glossaries-extra') | return | endif

" Detect stuff like this:
" \GlsXtrLoadResources[src={glossary}]
" \GlsXtrLoadResources[src=glossary.bib]
" \GlsXtrLoadResources[src={glossary.bib}, selection={all}]
" \GlsXtrLoadResources[selection={all},src={glossary.bib}]
Expand All @@ -544,6 +545,7 @@ function! s:completer_gls.init() dict abort " {{{2
" ]

let l:do_search = 0
let b:vimtex.complete.glsbib = []
for l:line in vimtex#parser#preamble(b:vimtex.tex)
if line =~# '^\s*\\GlsXtrLoadResources\s*\['
let l:do_search = 1
Expand All @@ -560,7 +562,13 @@ function! s:completer_gls.init() dict abort " {{{2
let l:value = trim(remove(l:matches, 0))
let l:value = substitute(l:value, '^{', '', '')
let l:value = substitute(l:value, '[]}]\s*', '', 'g')
let b:vimtex.complete.glsbib = l:value
if !vimtex#paths#is_abs(l:value)
let l:value = vimtex#paths#join(b:vimtex.root, l:value)
endif
if !filereadable(l:value)
let l:value .= '.bib'
endif
call add(b:vimtex.complete.glsbib, l:value)
break
endif
endwhile
Expand Down Expand Up @@ -592,15 +600,24 @@ function! s:completer_gls.parse_glsentries() dict abort " {{{2
endfunction

function! s:completer_gls.parse_glsbib() dict abort " {{{2
let l:filename = get(b:vimtex.complete, 'glsbib', '')
if empty(l:filename) | return [] | endif

let l:candidates = []
for l:entry in vimtex#parser#bib(l:filename, {'backend': 'vim'})
call add(l:candidates, {
\ 'word': l:entry.key,
\ 'menu': get(l:entry, 'name', '--'),
\})

for l:filename in get(b:vimtex.complete, 'glsbib', [])
for l:entry in vimtex#parser#bib(l:filename, {'backend': 'vim'})
let l:menu = ''
for l:c in ['name', 'long', 'title']
if l:entry->has_key(l:c)
let l:menu = ' ' .. l:entry[l:c]
break
endif
endfor

call add(l:candidates, {
\ 'word': l:entry.key,
\ 'kind': '[gls]',
\ 'menu': l:menu
\})
endfor
endfor

return l:candidates
Expand Down
4 changes: 3 additions & 1 deletion test/test-completion-glossary/glossaries-extra-1.tex
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
\documentclass{minimal}
\usepackage[german]{babel}
\usepackage{csquotes}
\usepackage[record,style=long]{glossaries-extra}

\GlsXtrLoadResources[
src={glossaries-extra.bib},
selection={all},
]
\GlsXtrLoadResources[src={glossary}]

\begin{document}

Hello World!

\gls{isbn}

\gls{API}

\printunsrtglossaries

\end{document}
2 changes: 1 addition & 1 deletion test/test-completion-glossary/glossaries-extra.vim
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ nnoremap q :qall!<cr>
silent edit $TEXFILE

let s:candidates = vimtex#test#completion('\gls{', '')
call assert_equal(len(s:candidates), 9)
call assert_equal(len(s:candidates), 10)

call vimtex#test#finished()
7 changes: 7 additions & 0 deletions test/test-completion-glossary/glossary.bib
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
@abbreviation{API,
title = {Application {{Programming Interface}}},
long = {Application Programming Interface},
prefix = {an\space},
prefixfirst = {an\space},
short = {API},
}

0 comments on commit 3de8ca5

Please sign in to comment.