feat!: update setup,add read/write marks json

This commit is contained in:
tomasky 2022-07-01 16:28:09 +08:00
parent 810abbfa32
commit 1ce6fdb40d
8 changed files with 106 additions and 24 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
draft.md

View File

@ -35,13 +35,13 @@ Here is an example with most of the default settings:
```lua
require('bookmarks').setup {
save_file = "~/.bookmarks"
}
```
## Credits
- [gitsigns.nvim] most lua functions come from this plugin
- [gitsigns.nvim] most of lua functions come from this plugin
- [vim-bookmarks](https://github.com/MattesGroeger/vim-bookmarks) inspired by this vim plugin
[gitsigns.nvim]: https://github.com/lewis6991/gitsigns.nvim

View File

@ -1,12 +1,15 @@
local void = require('gitsigns.async').void
local scheduler = require('gitsigns.async').scheduler
local void = require("bookmarks.async").void
local scheduler = require("bookmarks.async").scheduler
local api = vim.api
local uv = vim.loop
local current_buf = api.nvim_get_current_buf
local config = require "bookmarks.config"
local signs = require "bookmarks.signs"
local M = {}
M.setup = void(function(cfg)
config.build(cfg)
signs.setup()
end)
return M

View File

@ -1,5 +1,9 @@
local config = require("bookmarks.config").config
local void = require("gitsigns.async").void
local void = require("bookmarks.async").void
local signs = require "bookmarks.signs"
local utils = require "bookmarks.util"
local api = vim.api
local current_buf = api.nvim_get_current_buf
local M = {}
M.toggle_signs = function(value)
@ -12,8 +16,44 @@ M.toggle_signs = function(value)
return config.signcolumn
end
M.bookmark_add = function() end
M.bookmark_add = function(lnum)
local signlines = {}
local bufnr = current_buf()
signlines[0] = {
type = "add",
count = 1,
lnum = lnum,
}
signs:add(bufnr, signlines)
end
M.bookmark_rm = function() end
M.bookmark_clean = function() end
M.bookmark_ann = function() end
M.bookmark_prev = function() end
M.bookmark_next = function() end
M.refresh = function() end
local function saveBookmarks(filepath)
local content = {
index = 1,
data = {
[filepath] = {
{
i = 1, -- index
l = 1, -- line num
c = "", -- mark content
a = "", -- mark annotation
},
},
},
}
local data = vim.json.encode(content)
utils.write_file(config.save_file, data)
end
local function loadBookmarks()
utils.read_file(config.save_file, function(data)
local marks = vim.json.decode(data)
end)
end

View File

@ -1,13 +1,26 @@
local M = {}
M.config = {}
M.schema = {}
M.schema = {
save_file = {
type = "string",
default = vim.fn.expand "~/.bookmarks",
},
signs = {
type = "table",
deep_extend = true,
default = {
add = { hl = "BookMarksAdd", text = "", numhl = "BookMarksAddNr", linehl = "BookMarksAddLn" },
ann = { hl = "BookMarksAnn", text = "", numhl = "BookMarksAnnNr", linehl = "BookMarksAnnLn" },
},
},
}
local function validate_config(config)
for k, v in pairs(config) do
local kschema = M.schema[k]
if kschema == nil then
warn("gitsigns: Ignoring invalid configuration field '%s'", k)
warn("bookmarks: Ignoring invalid configuration field '%s'", k)
elseif kschema.type then
if type(kschema.type) == "string" then
vim.validate {

View File

@ -5,6 +5,7 @@ local M = {}
local hls = {
{ BookMarksAdd = { "MarkAdd" } },
{ BookMarksAnn = { "MarkAnn" } },
}
local function is_hl_set(hl_name)

View File

@ -68,4 +68,16 @@ 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

View File

@ -1,3 +1,4 @@
local uv = vim.loop
local M = {}
function M.path_exists(path)
@ -106,21 +107,32 @@ function M.readfile(filename)
return data:gsub("\r", "")
end
function M.writefile(filename, data)
local ok
local fh, err, code = io.popen(filename, "w")
if fh then
ok, err, code = fh:write(data)
if ok then
ok, err, code = fh:close()
else
fh:close()
end
end
if not ok then
return err .. code
end
return data
M.write_file = function(path, content)
uv.fs_open(path, "w", 438, function(open_err, fd)
assert(not open_err, open_err)
uv.fs_write(fd, content, -1, function(write_err)
assert(not write_err, write_err)
uv.fs_close(fd, function(close_err)
assert(not close_err, close_err)
end)
end)
end)
end
M.read_file = function(path, callback)
uv.fs_open(path, "r", 438, function(err, fd)
assert(not err, err)
uv.fs_fstat(fd, function(err, stat)
assert(not err, err)
uv.fs_read(fd, stat.size, 0, function(err, data)
assert(not err, err)
uv.fs_close(fd, function(err)
assert(not err, err)
callback(data)
end)
end)
end)
end)
end
function M.dump(o)