51 lines
1.5 KiB
VimL
51 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 = getline(".")[:col('.')]
|
|
" Take the shortest prefix of a denote link. This may be any of
|
|
" \<d$
|
|
" ..
|
|
" \<denote:$
|
|
" \<denote:\f$
|
|
" \<denote:\f\f$
|
|
" etc.
|
|
let res = l->matchstrpos('\<denote:\f*$')
|
|
if res[1] >= 0
|
|
return res[1]
|
|
endif
|
|
let res = l->matchstrpos('\<d\f*$')
|
|
if res[1] == -1
|
|
return -3
|
|
endif
|
|
return 'denote:' =~ '^' .. res[0] ? res[1] : -3
|
|
endfunction
|
|
|
|
" Return completion items given by the base
|
|
function s:suggestions(base)
|
|
let prefix = a:base->matchstr('^denote:\zs.*$')
|
|
let flist = glob(t:denote_directory .. (prefix ? "*" .. prefix .. "*" : "*"), 0, v:true)
|
|
let res = []
|
|
for filename in flist
|
|
let noteId = denote#meta#noteIdFromFile(filename)
|
|
echom "Gote noteId: " .. noteId
|
|
let noteTitle = denote#meta#noteTitleFromFile(filename)
|
|
if noteId == v:false || (noteId !~ '^' .. prefix && noteTitle !~ prefix)
|
|
continue
|
|
endif
|
|
let noteTitle = noteTitle ?? '(no title)'
|
|
let noteTags = denote#meta#noteTagsFromFile(filename)
|
|
call add(res, {
|
|
\ 'word' : 'denote:' .. noteId,
|
|
\ 'abbr' : noteTitle,
|
|
\ 'menu' : noteTags->join(', ')
|
|
\ })
|
|
endfor
|
|
return res
|
|
endfunction
|
|
|
|
" Completion function for denote links
|
|
function denote#completion#get(findstart, base)
|
|
return a:findstart == 1 ? s:column() : s:suggestions(a:base)
|
|
endfunction
|