refactor(signs): move some method to actions

This commit is contained in:
tomasky 2022-07-03 17:26:19 +08:00
parent e71384a5ce
commit fe7779ffcb
4 changed files with 93 additions and 53 deletions

View File

@ -35,7 +35,14 @@ Here is an example with most of the default settings:
```lua
require('bookmarks').setup {
save_file = "~/.bookmarks"
save_file = "$HOME/.bookmarks",
on_attach = function(bufnr)
local bm = require "bookmarks"
local map = vim.keymap.set
map("n","mm",bm.bookmark_toggle)
map("n","mc",bm.bookmark_clean)
map("n","mi",bm.bookmark_ann)
end
}
```

View File

@ -29,27 +29,51 @@ local function autocmd(event, opts)
nvim.autocmd(event, opts0)
end
M.attach = void(function(bufnr, aucmd)
local function on_detach(_, bufnr)
M.detach(bufnr, true)
end
M.attach = void(function(bufnr)
bufnr = bufnr or current_buf()
scheduler()
actions.loadBookmarks()
if config.on_attach then
config.on_attach(bufnr)
if config.config.on_attach then
config.config.on_attach(bufnr)
end
api.nvim_buf_attach(bufnr, false, {
on_detach = on_detach,
})
end)
M.detach_all = void(function(bufnr)
bufnr = bufnr or current_buf()
scheduler()
signs.detach(bufnr)
actions.detach(bufnr)
actions.saveBookmarks()
end)
local function on_or_after_vimenter(fn)
if vim.v.vim_did_enter == 1 then
fn()
else
nvim.autocmd("VimEnter", {
callback = wrap_func(fn),
once = true,
})
end
end
M.setup = void(function(cfg)
config.build(cfg)
signs.setup()
actions.setup()
nvim.augroup "bookmarks"
autocmd("VimLeavePre", M.detach_all)
autocmd("ColorScheme", hl.setup_highlights)
autocmd("BufRead", wrap_func(M.attach, nil, "BufRead"))
on_or_after_vimenter(function()
hl.setup_highlights()
M.attach()
autocmd("BufWinEnter", actions.refresh)
end)
end)
return setmetatable(M, {

View File

@ -1,32 +1,50 @@
local config = require("bookmarks.config").config
local uv = vim.loop
local signs = require "bookmarks.signs"
local Signs = require "bookmarks.signs"
local utils = require "bookmarks.util"
local api = vim.api
local current_buf = api.nvim_get_current_buf
local M = {}
local signs
M.setup = function()
signs = Signs.new(config.signs)
end
M.detach = function(bufnr, keep_signs)
if not keep_signs then
signs:remove(bufnr)
end
end
local function updateBookmarks(bufnr, line, mark, ann)
local filepath = uv.fs_realpath(api.nvim_buf_get_name(bufnr))
local marks = config.cache[filepath]
if filepath == nil then
return
end
local data = config.cache["data"]
local marks = data[filepath]
local isIns = false
if line == -1 then
marks = nil
isIns = true
-- check buffer auto_save to file
return
end
for i = 1, #marks do
if marks[i].l == line and mark == "" then
table.remove(marks, i)
elseif marks[i].l == line then
for i = 1, #(marks or {}) do
if marks[i].l == line then
isIns = true
if mark == "" then
table.remove(marks, i)
end
break
end
end
if isIns == false then
marks = marks or {}
table.insert(marks, ann and { l = line, m = mark, a = ann } or { l = line, m = mark })
-- check buffer auto_save to file
-- M.saveBookmarks()
end
data[filepath] = marks
end
M.toggle_signs = function(value)
@ -39,60 +57,61 @@ M.toggle_signs = function(value)
return config.signcolumn
end
M.bookmark_add = function()
M.bookmark_toggle = function()
local lnum = api.nvim_win_get_cursor(0)[1]
local bufnr = current_buf()
local signlines = { {
type = "add",
lnum = lnum,
} }
signs:add(bufnr, signlines)
updateBookmarks(bufnr, lnum, "line ")
end
M.bookmark_rm = function()
local lnum = api.nvim_win_get_cursor(0)[1]
local bufnr = current_buf()
signs:remove(bufnr, lnum)
updateBookmarks(bufnr, lnum, "")
local isExt = signs:add(bufnr, signlines)
if isExt then
signs:remove(bufnr, lnum)
updateBookmarks(bufnr, lnum, "")
else
local line = api.nvim_buf_get_lines(bufnr, lnum, lnum + 1, false)[1]
updateBookmarks(bufnr, lnum, line)
end
end
M.bookmark_clean = function()
local bufnr = current_buf()
signs:remove(bufnr)
updateBookmarks(bufnr, -1, "")
end
M.bookmark_ann = function(old_annotation)
local new_annotation = ""
local input_msg = old_annotation ~= "" and "Edit" or "Enter"
vim.ui.input(input_msg, old_annotation, function(answer)
new_annotation = answer
end)
M.bookmark_ann = function()
local lnum = api.nvim_win_get_cursor(0)[1]
local bufnr = current_buf()
local signlines = { {
type = "ann",
lnum = lnum,
} }
signs:add(bufnr, signlines)
updateBookmarks(bufnr, lnum, "line ", new_annotation)
local isExt = signs:add(bufnr, signlines)
local input_msg = isExt and "Edit:" or "Enter:"
vim.ui.input({ prompt = input_msg, default = "" }, function(answer)
local line = api.nvim_buf_get_lines(bufnr, lnum, lnum + 1, false)[1]
updateBookmarks(bufnr, lnum, line, answer)
end)
end
M.bookmark_prev = function() end
M.bookmark_next = function() end
M.bookmark_showall = function() end
M.refresh = function()
local cache = config.cache
local bufnr = current_buf()
M.refresh = function(bufnr)
bufnr = bufnr or current_buf()
local file = uv.fs_realpath(api.nvim_buf_get_name(bufnr))
local marks = cache.data[file]
if file == nil then
return
end
local marks = config.cache.data[file]
local signlines = {}
if marks then
for mark in marks do
for _, m in ipairs(marks) do
local ma = {
type = mark.a and "ann" or "add",
lnum = mark.l,
type = m.a and "ann" or "add",
lnum = m.l,
}
signs:remove(bufnr, ma.lnum)
table.insert(signlines, ma)

View File

@ -27,9 +27,10 @@ end
function M:add(bufnr, signs)
local cfg = self.config
local isExt = true
for _, s in ipairs(signs) do
if not self:contains(bufnr, s.lnum) then
isExt = false
local cs = cfg[s.type]
local text = cs.text
@ -43,11 +44,12 @@ function M:add(bufnr, signs)
})
end
end
return isExt
end
function M:contains(bufnr, start)
local marks = api.nvim_buf_get_extmarks(bufnr, self.ns, { start - 1, 0 }, { start, 0 }, { limit = 1 })
return #marks > 0
return marks and #marks > 0
end
function M:reset()
@ -56,16 +58,4 @@ function M:reset()
end
end
local signs
M.setup = function()
signs = M.new(config.signs)
end
M.detach = function(bufnr, keep_signs)
if not keep_signs then
signs:remove(bufnr)
end
end
return M