Skip to content

Commit

Permalink
feat: add vimtex#cite#get_key
Browse files Browse the repository at this point in the history
refer: #2908
  • Loading branch information
lervag committed Apr 29, 2024
1 parent 23cf764 commit 0157240
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 26 deletions.
35 changes: 35 additions & 0 deletions autoload/vimtex/cite.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
" VimTeX - LaTeX plugin for Vim
"
" Maintainer: Karl Yngve Lervåg
" Email: [email protected]
"

function! vimtex#cite#get_key(...) abort " {{{1
let l:cmd = a:0 > 0 ? a:1 : vimtex#cmd#get_current()
if empty(l:cmd)
\ || l:cmd.name[1:] !~# g:vimtex#re#cite_cmd
\ || len(l:cmd.args) < 1
\ || len(l:cmd.args) > 2
return ''
endif

let l:current_word = a:0 > 1 ? a:2 : expand('<cword>')
let l:cites = l:cmd.args->map({_, x -> x.text})->join(',')->split(',\s*')

return index(l:cites, l:current_word) >= 0
\ ? l:current_word
\ : l:cites[0]
endfunction

" }}}1
function! vimtex#cite#get_key_at(line, col) abort " {{{1
let l:pos_saved = vimtex#pos#get_cursor()

call vimtex#pos#set_cursor(a:line, a:col)
let l:key = vimtex#cite#get_key()
call vimtex#pos#set_cursor(l:pos_saved)

return l:key
endfunction

" }}}1
28 changes: 2 additions & 26 deletions autoload/vimtex/context/cite.vim
Original file line number Diff line number Diff line change
Expand Up @@ -12,34 +12,10 @@ endfunction

let s:handler = {
\ 'name': 'citation handler',
\ 're': '\v%(%(\a*cite|Cite)\a*|bibentry|%(text|block|%(for|hy)\w+)cquote)',
\}
function! s:handler.match(cmd, word) abort dict " {{{1
if a:cmd.name[1:] !~# self.re
return v:false
endif

if len(a:cmd.args) < 1 || len(a:cmd.args) > 2
return v:false
endif

let l:text = a:cmd.args[0].text
if len(a:cmd.args) == 2
let l:text .= ',' .. a:cmd.args[1].text
endif

let self.cites = split(l:text, ',\s*')
if index(self.cites, a:word) >= 0
let self.selected = a:word
else
let self.selected = self.cites[0]
endif

if empty(self.selected)
return v:false
endif

return v:true
let self.selected = vimtex#cite#get_key(a:cmd, a:word)
return !empty(self.selected)
endfunction

" }}}1
Expand Down
2 changes: 2 additions & 0 deletions autoload/vimtex/re.vim
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ let g:vimtex#re#tex_include = g:vimtex#re#tex_input_root
\ . '|' . g:vimtex#re#tex_input . '\zs[^\}]*\ze\}?'
\ . '|' . g:vimtex#re#tex_input_package

let g:vimtex#re#cite_cmd = '\v%(%(\a*cite|Cite)\a*|bibentry|%(text|block|%(for|hy)\w+)cquote)'

" {{{1 Completion regexes
let g:vimtex#re#neocomplete =
\ '\v\\%('
Expand Down
14 changes: 14 additions & 0 deletions test/test-cite/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
MYVIM ?= nvim --clean --headless

INMAKE := 1
export INMAKE

TESTS := $(wildcard test*.vim)
TESTS := $(TESTS:.vim=)

.PHONY: test $(TESTS)

test: $(TESTS)

$(TESTS):
@$(MYVIM) -u $@.vim
17 changes: 17 additions & 0 deletions test/test-cite/test-cite.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
set nocompatible
let &rtp = '../..,' . &rtp
filetype plugin indent on
syntax enable

set nomore

nnoremap q :qall!<cr>
silent edit ../test-context-cite/test-cites.tex

if empty($INMAKE) | finish | endif

call assert_equal("Hemingway1940", vimtex#cite#get_key_at(11, 35))
call assert_equal("wilcox.e:2021", vimtex#cite#get_key_at(14, 56))

call vimtex#test#finished()

0 comments on commit 0157240

Please sign in to comment.