1
0
mirror of https://github.com/SpaceVim/SpaceVim.git synced 2025-02-02 21:20:05 +08:00

feat(bookmark): add virt_text support

This commit is contained in:
Eric Wong 2024-03-29 01:55:02 +08:00
parent f759826426
commit 3a22f555dd
2 changed files with 38 additions and 6 deletions

View File

@ -26,11 +26,11 @@ function! bookmarks#annotate() abort
if !empty(annotation)
let file = s:FILE.unify_path(expand('%'), ':p')
let lnum = line('.')
call bookmarks#add(file, lnum, annotation)
call bookmarks#add(file, lnum, annotation, 1)
else
call s:NT.notify('empty annotation, skipped!')
endif
endfunction
function! bookmarks#get_all_bookmarks() abort
@ -42,18 +42,28 @@ function! bookmarks#add(file, lnum, text, ...) abort
if !has_key(s:bookmarks, a:file)
let s:bookmarks[a:file] = {}
endif
if has_key(s:bookmarks[a:file], a:lnum) && has_key(s:bookmarks[a:file][a:lnum], 'vtextid')
call bookmarks#vtext#delete(a:file, s:bookmarks[a:file][a:lnum].vtextid)
endif
let s:bookmarks[a:file][a:lnum] = {
\ 'text' : a:text,
\ 'file' : a:file,
\ 'lnum' : a:lnum,
\ 'signid' : bookmarks#sign#add(a:file, a:lnum)
\ }
if get(a:000, 0, 0)
let s:bookmarks[a:file][a:lnum].vtextid = bookmarks#vtext#add(a:file, a:lnum, a:text)
let s:bookmarks[a:file][a:lnum].annotation = a:text
endif
call bookmarks#cache#write(s:bookmarks)
endfunction
function! bookmarks#delete(file, lnum) abort
if has_key(s:bookmarks, a:file) && has_key(s:bookmarks[a:file], a:lnum)
exe 'sign unplace ' . s:bookmarks[a:file][a:lnum].signid
if has_key(s:bookmarks[a:file][a:lnum], 'vtextid')
call bookmarks#vtext#delete(a:file, s:bookmarks[a:file][a:lnum].vtextid)
endif
unlet s:bookmarks[a:file][a:lnum]
call bookmarks#cache#write(s:bookmarks)
endif
@ -63,7 +73,7 @@ function! s:jump_to_bookmark(bookmark) abort
exe 'e ' . a:bookmark.file
exe a:bookmark.lnum
endfunction
function! bookmarks#next() abort
@ -76,7 +86,7 @@ function! bookmarks#next() abort
endif
endfor
endif
endfunction
function! bookmarks#showall() abort
@ -104,7 +114,10 @@ function! bookmarks#on_enter_buffer() abort
let file = s:FILE.unify_path(expand('%'), ':p')
if has_key(s:bookmarks, file)
for lnum in keys(s:bookmarks[file])
let s:bookmarks[file][lnum].signid = bookmarks#sign#add(file, lnum)
let s:bookmarks[file][lnum].signid = bookmarks#sign#add(file, lnum)
if has_key(s:bookmarks[file][lnum], 'annotation') && !empty(s:bookmarks[file][lnum].annotation)
call bookmarks#vtext#add(file, lnum, s:bookmarks[file][lnum].annotation)
endif
endfor
endif
@ -115,7 +128,7 @@ function! bookmarks#clear() abort
let file = s:FILE.unify_path(expand('%'), ':p')
if has_key(s:bookmarks, file)
for lnum in keys(s:bookmarks[file])
call bookmarks#delete(file, lnum)
call bookmarks#delete(file, lnum)
endfor
endif

View File

@ -0,0 +1,19 @@
if exists('*nvim_create_namespace')
let s:ns_id = nvim_create_namespace('bookmarks')
endif
function! bookmarks#vtext#add(file, lnum, text) abort
if exists('*nvim_buf_set_extmark')
return nvim_buf_set_extmark(bufnr(a:file), s:ns_id, a:lnum - 1, 0, {'virt_text' : [[a:text, 'Comment']]})
endif
endfunction
function! bookmarks#vtext#delete(file, id) abort
if exists('*nvim_buf_del_extmark')
call nvim_buf_del_extmark(bufnr(a:file), s:ns_id, a:id)
endif
endfunction