feat: denote link completion

This commit is contained in:
2026-02-19 15:56:08 +01:00
parent 00af1c53b9
commit 83ea4f2bcf
3 changed files with 63 additions and 2 deletions

View File

@@ -1,3 +1,6 @@
" Link completion {{{1
set omnifunc=denote#completion#get
" Go to file command |gf| adjustments {{{1 " Go to file command |gf| adjustments {{{1
" This resolves denote links. The function has access to the variable v:fname, " This resolves denote links. The function has access to the variable v:fname,
" which corresponds to the filename under the cursor. " which corresponds to the filename under the cursor.

View File

@@ -0,0 +1,59 @@
" 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(prefix ? "*" .. prefix .. "*" : "*", 0, v:true)
let res = []
for filename in flist
let noteId = denote#meta#noteIdFromFile(filename)
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)
let res = res->add({
\ 'word' : 'denote:' .. noteId,
\ 'abbr' : noteTitle,
\ 'menu' : noteTags->join(', ')
\ })
endfor
return res
endfunction
function! Myomni(findstart, base)
if a:findstart == 1
let tmp = s:column()
return tmp
else
let tmp = s:suggestions(a:base)
return tmp
endif
endfunction
" Completion function for denote links
function denote#completion#get(findstart, base)
return a:findstart == 1 ? s:column() : s:suggestions(a:base)
endfunction

View File

@@ -23,10 +23,9 @@ function denote#meta#noteIdFromFile(filename)
\ ?? v:false \ ?? v:false
endfunction endfunction
" Return the note title from the filename. On failure, v:false is returned. " Return the note title from the filename.
function denote#meta#noteTitleFromFile(filename) function denote#meta#noteTitleFromFile(filename)
return a:filename->matchstr("--\\zs.\\{-\\}\\ze\\(==\\|@@\\|__\\|\\..\\)")->substitute("-", " ", "g") return a:filename->matchstr("--\\zs.\\{-\\}\\ze\\(==\\|@@\\|__\\|\\..\\)")->substitute("-", " ", "g")
\ ?? v:false
endfunction endfunction
" Return the note tags from the filename as a list. " Return the note tags from the filename as a list.