Skip to content

Commit

Permalink
feat: validate cache files
Browse files Browse the repository at this point in the history
refer: #2097
  • Loading branch information
lervag committed Jul 15, 2021
1 parent 431f520 commit 8b1ad51
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 17 deletions.
9 changes: 9 additions & 0 deletions autoload/vimtex/cache.vim
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,15 @@ function! s:cache.init(name, opts) dict abort " {{{1
let new.ftime = -1
let new.modified = 0

if has_key(a:opts, 'validate')
call new.read()
if get(new.data, '__validate', {}) != a:opts.validate
call new.clear()
let new.data.__validate = deepcopy(a:opts.validate)
call new.write()
endif
endif

return new
endfunction

Expand Down
1 change: 1 addition & 0 deletions autoload/vimtex/complete.vim
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ function! s:completer_bib.gather_candidates() dict abort " {{{2

let l:cache = vimtex#cache#open('bibcomplete', {
\ 'local': 1,
\ 'validate': g:vimtex_complete_bib,
\ 'default': {'result': [], 'ftime': -1}
\})

Expand Down
21 changes: 4 additions & 17 deletions doc/vimtex.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1195,19 +1195,13 @@ OPTIONS *vimtex-options*
Since the author list can be large, the `@author_all` is truncated to 20
characters. This can be modified with the `auth_len` key (see below).

Note: If you change this option, you should clear the related cache
files: `:VimtexClearCache bibcomplete` (|VimtexClearCache|)

menu_fmt~
Default value: `'[@type] @author_short (@year), "@title"'`
The format used for the `menu` entry for bib completion candidates (see
|complete-items|). If the key is set to an empty string, then the `menu`
entry is not added to the completion candidates. See the description of
`match_str_fmt` for the allowed keys.

Note: If you change this option, you should clear the related cache
files: `:VimtexClearCache bibcomplete` (|VimtexClearCache|)

abbr_fmt~
Default value: `''`
The format used for the `abbr` entry for bib completion candidates (see
Expand All @@ -1219,9 +1213,6 @@ OPTIONS *vimtex-options*
Truncation length for author list with the `@author_all` format key in the
format strings for `match_str_fmt`, `menu_fmt`, and `abbr_fmt`.

Note: If you change this option, you should clear the related cache
files: `:VimtexClearCache bibcomplete` (|VimtexClearCache|)

custom_patterns~
Default value: []
List of custom trigger patterns that may be used to allow completion for
Expand Down Expand Up @@ -3192,15 +3183,11 @@ COMMANDS *vimtex-commands*
|g:vimtex_subfile_start_local|.

*:VimtexClearCache*
:VimtexClearCache {name} Clear all cache files that matches `name`. This is
useful for instance to clear the `bibcomplete` cache
when you change the completion options, see
|g:vimtex_complete_bib.match_str_fmt|.

To clear all cache files: `:VimtexClearCache ALL`
:VimtexClearCache {name} Clear cache files that matches `name`. The cache
files are located at |g:vimtex_cache_root| and can
also be deleted manually.

The cache files can be cleared manually. They are
located at |g:vimtex_cache_root|.
`:VimtexClearCache ALL` clears all cache files.

------------------------------------------------------------------------------
MAP DEFINITIONS *vimtex-mappings*
Expand Down
31 changes: 31 additions & 0 deletions test/test-cache/test-validate.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
set nocompatible
let &rtp = '../..,' . &rtp
filetype plugin on

nnoremap q :qall!<cr>
let g:vimtex_cache_root = '.'

" Create test files
let s:files = {
\ 'test-validate-pass.json': {'__validate': 1, 'a': 1},
\ 'test-validate-fail.json': {'__validate': 0, 'a': 1},
\}
for [s:file, s:content] in items(s:files)
call writefile([json_encode(s:content)], s:file)
endfor


let s:cache = vimtex#cache#open('test-validate-pass', {'validate': 1})
call assert_true(s:cache.has('a'))

let s:cache = vimtex#cache#open('test-validate-fail', {'validate': 1})
call assert_false(s:cache.has('a'))


" Clean up
for s:file in keys(s:files)
if filereadable(s:file) | call delete(s:file) | endif
endfor

call vimtex#test#finished()

0 comments on commit 8b1ad51

Please sign in to comment.