Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions autoload/wiki/complete.vim
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ function! wiki#complete#link(lead, line, pos) abort " {{{1
let l:candidates = s:completer_wikilink.complete(l:input)
call map(l:candidates, 'v:val.word')
if !s:completer_wikilink.is_anchor
return filter(l:candidates, 'stridx(v:val, l:input) == 0')
return s:filter_candidates(l:candidates, '^' . l:input)
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, that's smart!

endif

return map(l:candidates, 'l:base . v:val')
Expand Down Expand Up @@ -99,10 +99,10 @@ function! s:completer_wikilink.complete_anchor(regex) dict abort " {{{2
let l:length = strlen(l:base)

let l:anchors = wiki#toc#gather_anchors(l:url)
call filter(l:anchors, 'v:val =~# ''^'' . wiki#u#escape(l:base) . ''[^#]*$''')
call s:filter_candidates(l:anchors, '^' . wiki#u#escape(l:base) . '[^#]*$')
call map(l:anchors, 'strpart(v:val, l:length)')
if !empty(a:regex)
call filter(l:anchors, 'v:val =~# ''' . a:regex . '''')
call s:filter_candidates(l:anchors, a:regex)
endif

return l:anchors
Expand All @@ -122,7 +122,7 @@ function! s:completer_wikilink.complete_page(regex) dict abort " {{{2
\ empty(b:wiki.link_extension)
\ ? 'l:pre . fnamemodify(v:val, '':r'')'
\ : 'l:pre . v:val')
call filter(l:cands, 'stridx(v:val, a:regex) >= 0')
call s:filter_candidates(l:cands, a:regex)

call sort(l:cands)

Expand Down Expand Up @@ -207,7 +207,7 @@ endfunction

function! s:completer_tags.complete(regex) dict abort " {{{2
let l:candidates = keys(wiki#tags#get_all())
call filter(l:candidates, {_, x -> stridx(x, a:regex) >= 0})
call s:filter_candidates(l:candidates, a:regex)
return map(sort(l:candidates), {_, x -> {
\ 'word': x,
\ 'kind': '[tag]'
Expand All @@ -216,6 +216,18 @@ endfunction

" }}}1

"
" Utility
"
function s:filter_candidates(cands, regex) " {{{1
" Filter list a:cands for a match with a:regex anywhere in items, with
" case-sensitivity depending on the value of g:wiki_completion_case_sensitive
call filter(a:cands, {_,x -> match(x,
\ (g:wiki_completion_case_sensitive ? '\C' : '\c')
\ . a:regex) >= 0})
endfunction
" }}}1

"
" Initialize module
"
Expand Down
6 changes: 6 additions & 0 deletions doc/wiki.txt
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,12 @@ OPTIONS *wiki-config-options*

Default value: 1

*g:wiki_completion_case_sensitive*
Specify whether to use case-sensitive matching for omnicompletion of links
and tags.

Default value: 1

*g:wiki_date_exe*
Specify the executable for `date`, which is used "behind the scenes" to
manipulate and calculate dates for the journal features.
Expand Down
1 change: 1 addition & 0 deletions plugin/wiki.vim
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ call wiki#init#option('wiki_cache_root',
\ : (empty($XDG_CACHE_HOME)
\ ? $HOME . '/.cache'
\ : $XDG_CACHE_HOME) . '/wiki.vim')
call wiki#init#option('wiki_completion_case_sensitive', 1)
call wiki#init#option('wiki_export', {
\ 'args' : '',
\ 'from_format' : 'markdown',
Expand Down
6 changes: 6 additions & 0 deletions test/test-complete/test-tags.vim
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ silent edit ../wiki-basic/index.wiki

let s:candidates = wiki#test#completion(':t', 'tag')
call assert_equal(7, len(s:candidates))
let s:candidates = wiki#test#completion(':T', 'Tag')
call assert_equal(0, len(s:candidates))
let g:wiki_completion_case_sensitive = 0
let s:candidates = wiki#test#completion(':T', 'Tag')
call assert_equal(7, len(s:candidates))
let g:wiki_completion_case_sensitive = 1

let s:candidates = wiki#test#completion(':m', 'mar')
call assert_equal(1, len(s:candidates))
Expand Down
13 changes: 13 additions & 0 deletions test/test-complete/test-wikilinks.vim
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,23 @@ let s:candidates = wiki#test#completion('[[/', 'in')
call assert_equal(7, len(s:candidates))
call assert_equal('/', s:candidates[0].word[0])

let s:candidates = wiki#test#completion('[[', 'new')
call assert_equal(0, len(s:candidates))
let g:wiki_completion_case_sensitive = 0
let s:candidates = wiki#test#completion('[[', 'new')
call assert_equal(1, len(s:candidates))
let g:wiki_completion_case_sensitive = 1

silent edit ../wiki-basic/ToC-reference.wiki

let s:candidates = wiki#test#completion('[[#2 Next chapter#', '')
call assert_equal(3, len(s:candidates))
let s:candidates = wiki#test#completion('[[#2 n', '2 n')
call assert_equal(0, len(s:candidates))
let g:wiki_completion_case_sensitive = 0
let s:candidates = wiki#test#completion('[[#2 n', '2 n')
call assert_equal(2, len(s:candidates))
let g:wiki_completion_case_sensitive = 1

" Profiling on @lervag's personal wiki
" silent edit ~/.local/wiki/index.wiki
Expand Down
2 changes: 2 additions & 0 deletions test/wiki-basic/ToC-reference.wiki
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,5 @@
## 1.2 Level 2

## 1.3 Level 2

# 2 Nothing here
2 changes: 2 additions & 0 deletions test/wiki-basic/ToC.wiki
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,5 @@
## 1.2 Level 2

## 1.3 Level 2

# 2 Nothing here