Files
vim-denote/autoload/denote/completion.vim
2026-02-25 16:11:35 +01:00

50 lines
1.5 KiB
VimL

" Find the column where the completion starts. This must be between 1 and
" col('.'). Denote links are of this form: `denote:<identifier>`.
function s:column()
" Get the substring from the start of the line until col('.')
let l:l = getline('.')[:col('.')]
" Take the shortest prefix of a denote link. This may be any of
" \<d$
" ..
" \<denote:$
" \<denote:\f$
" \<denote:\f\f$
" etc.
let l:res = l:l->matchstrpos('\<denote:\f*$')
if l:res[1] >= 0
return l:res[1]
endif
let l:res = l:l->matchstrpos('\<d\f*$')
if l:res[1] == -1
return -3
endif
return 'denote:' =~ '^' .. l:res[0] ? l:res[1] : -3
endfunction
" Return completion items given by the base
function s:suggestions(base)
let l:prefix = a:base->matchstr('^denote:\zs.*$')
let l:flist = glob(g:denote_directory .. '/' .. (l:prefix ? '*' .. l:prefix .. '*' : '*'), 0, v:true)
let l:res = []
for filename in l:flist
let l:noteId = denote#meta#noteIdFromFile(filename)
let l:noteTitle = denote#meta#noteTitleFromFile(filename)
if l:noteId == v:false || (l:noteId !~ '^' .. l:prefix && l:noteTitle !~ l:prefix)
continue
endif
let l:noteTitle = l:noteTitle ?? '(no title)'
let l:noteTags = denote#meta#noteTagsFromFile(filename)
call add(l:res, {
\ 'word' : 'denote:' .. l:noteId,
\ 'abbr' : l:noteTitle,
\ 'menu' : l:noteTags->join(', ')
\ })
endfor
return l:res
endfunction
" Completion function for denote links
function denote#completion#get(findstart, base)
return a:findstart == 1 ? s:column() : s:suggestions(a:base)
endfunction