1
0
mirror of https://github.com/SpaceVim/SpaceVim.git synced 2025-01-23 13:50:05 +08:00

feat(bookmarks): update bookmarks lnums

This commit is contained in:
Eric Wong 2024-03-30 21:38:08 +08:00
parent 5cb566b359
commit 7c80fe4369
4 changed files with 56 additions and 6 deletions

View File

@ -11,14 +11,40 @@ let s:bookmarks = bookmarks#cache#read()
function! s:skip_current_buf() abort
if empty(bufname())
call s:NT.notify('skip empty bufname.')
return v:true
elseif !empty(&buftype)
call s:NT.notify('skip buftype: ' . &buftype)
return v:true
endif
endfunction
function! bookmarks#on_leave_buffer() abort
if s:skip_current_buf()
return
endif
let file = s:FILE.unify_path(expand('%'), ':p')
if has_key(s:bookmarks, file)
let sign_lnum_map = bookmarks#sign#get_lnums(bufnr('%'))
let new_file_bms = {}
for lnum in keys(s:bookmarks[file])
let signid = s:bookmarks[file][lnum].signid
if has_key(sign_lnum_map, signid)
let new_lnum = sign_lnum_map[signid]
let new_file_bms[new_lnum] = s:bookmarks[file][lnum]
let new_file_bms[new_lnum].lnum = new_lnum
else
" the signid does not exist, maybe that line has been removed
if has_key(s:bookmarks[file][lnum], 'vtextid')
call bookmarks#vtext#delete(file, s:bookmarks[file][lnum].vtextid)
endif
endif
endfor
let s:bookmarks[file] = new_file_bms
call bookmarks#cache#write(s:bookmarks)
endif
endfunction
function! bookmarks#toggle() abort
if s:skip_current_buf()
return
@ -70,6 +96,11 @@ endfunction
function! bookmarks#add(file, lnum, text, ...) abort
call bookmarks#logger#info('add bookmarks:')
call bookmarks#logger#info(' file:' .. a:file)
call bookmarks#logger#info(' lnum:' .. a:lnum)
call bookmarks#logger#info(' text:' .. a:text)
call bookmarks#logger#info(' a:000:' .. string(a:000))
if !has_key(s:bookmarks, a:file)
let s:bookmarks[a:file] = {}
endif

View File

@ -1,8 +1,13 @@
let s:LOGGER = SpaceVim#logger#derive('bookmarks')
let s:LOGGER = SpaceVim#logger#derive('bookmark')
function! bookmarks#logger#info(msg) abort
call s:LOGGER.info(a:msg)
endfunction
function! bookmarks#logger#debug(msg) abort
call s:LOGGER.debug(a:msg)
endfunction
function! bookmarks#logger#warn(msg) abort
call s:LOGGER.warn(a:msg)
endfunction

View File

@ -17,3 +17,16 @@ call sign_define(s:sign_name, {
function! bookmarks#sign#add(file, lnum) abort
return sign_place(0, '', s:sign_name, a:file, {'lnum':a:lnum} )
endfunction
function! bookmarks#sign#get_lnums(buf) abort
let signs = filter(sign_getplaced(a:buf)[0].signs, 'v:val.name == "bookmarks"')
let map = {}
for sign in signs
call extend(map, { sign.id : sign.lnum })
endfor
return map
endfunction

View File

@ -21,4 +21,5 @@ command! BookmarkShowAll call bookmarks#showall()
augroup bookmarks
autocmd!
autocmd BufEnter * call bookmarks#on_enter_buffer()
autocmd BufLeave * call bookmarks#on_leave_buffer()
augroup END