feat(marks)!: show bookmarks list in quickfix and telescope

This commit is contained in:
tomasky 2022-07-04 14:00:06 +08:00
parent b4aa5e96e3
commit e2f6e4a48d
5 changed files with 128 additions and 10 deletions

View File

@ -35,7 +35,13 @@ Here is an example with most of the default settings:
```lua
require('bookmarks').setup {
save_file = "$HOME/.bookmarks", -- bookmarks save file path
save_file = vim.fn.expand "$HOME/.bookmarks", -- bookmarks save file path
keywords = {
["@t"] = "☑️ ", -- mark annotation startswith @t signs this icon of todo
["@w"] = "⚠️ ", -- mark annotation startswith @w signs this icon of warn
["@f"] = "⛏ ", -- mark annotation startswith @f signs this icon of fix
["@n"] = " ", -- mark annotation startswith @n signs this icon of note
},
on_attach = function(bufnr)
local bm = require "bookmarks"
local map = vim.keymap.set
@ -44,10 +50,19 @@ require('bookmarks').setup {
map("n","mc",bm.bookmark_clean) -- clean all marks in local buffer
map("n","mn",bm.bookmark_next) -- jump to next mark in local buffer
map("n","mp",bm.bookmark_prev) -- jump to previous mark in local buffer
map("n","ml",bm.bookmark_list) -- show marked file list in quickfix window
end
}
```
## Telescope
```lua
require('telescope').load_extension('bookmarks')
```
Then use `:Telescope bookmarks list` or `require('telescope').extensions.bookmarks.list()`
## Credits
- [gitsigns.nvim] most of lua functions come from this plugin

View File

@ -127,7 +127,7 @@ local jump_line = function(prev)
api.nvim_win_set_cursor(0, { lnum, 0 })
local mark = marks[tostring(lnum)]
if mark.a ~= "" then
print(mark.a)
api.nvim_echo({ { mark.a, "WarningMsg" } }, true, {})
end
end
@ -139,7 +139,19 @@ M.bookmark_next = function()
jump_line(false)
end
M.bookmark_showall = function() end
M.bookmark_list = function()
local allmarks = config.cache.data
local marklist = {}
for k, ma in pairs(allmarks) do
if utils.path_exists(k) == false then
allmarks[k] = nil
end
for l, v in pairs(ma) do
table.insert(marklist, k .. "|" .. l .. "|" .. v.m .. "|" .. v.a)
end
end
utils.setqflist(marklist)
end
M.refresh = function(bufnr)
bufnr = bufnr or current_buf()
@ -163,14 +175,12 @@ M.refresh = function(bufnr)
end
function M.loadBookmarks()
local file = uv.fs_realpath(config.save_file)
if file == nil then
return
if utils.path_exists(config.save_file) then
utils.read_file(config.save_file, function(data)
config.cache = vim.json.decode(data)
config.marks = data
end)
end
utils.read_file(file, function(data)
config.cache = vim.json.decode(data)
config.marks = data
end)
end
function M.saveBookmarks()

View File

@ -2,6 +2,10 @@ local M = {}
M.config = {}
M.schema = {
keywords = {
type = "table",
default = { ["@t"] = "☑️ ", ["@w"] = "⚠️ ", ["@f"] = "", ["@n"] = "" },
},
cache = {
type = "table",
deep_extend = true,

View File

@ -152,4 +152,22 @@ function M.lazy(fn)
end
end
function M.setqflist(content, opts)
if type(opts) == "string" then
opts = { cwd = opts }
if opts.cwd:sub(1, 4) == "cwd=" then
opts.cwd = opts.cwd:sub(5)
end
end
opts = opts or {}
opts.open = (opts.open ~= nil) and opts.open or true
vim.fn.setqflist({}, " ", { title = "Todo", id = "$", items = content })
if opts.open then
vim.cmd [[copen]]
end
-- local win = vim.fn.getqflist { winid = true }
-- if win.winid ~= 0 then
-- end
end
return M

View File

@ -0,0 +1,71 @@
local has_telescope, telescope = pcall(require, "telescope")
if not has_telescope then
error "This plugins requires nvim-telescope/telescope.nvim"
end
local finders = require "telescope.finders"
local pickers = require "telescope.pickers"
local conf = require("telescope.config").values
local config = require("bookmarks.config").config
local utils = require "telescope.utils"
local function get_text(annotation)
local pref = string.sub(annotation, 1, 2)
local ret = config.keywords[pref]
if ret == nil then
ret = ""
end
return ret .. annotation
end
local function bookmark(opts)
local allmarks = config.cache.data
local marklist = {}
for k, ma in pairs(allmarks) do
for l, v in pairs(ma) do
table.insert(marklist, {
filename = k,
lnum = tonumber(l),
text = v.a ~= "" and get_text(v.a) or v.m,
})
end
end
local display = function(entry)
local displayer = pickers.entry_display.create {
separator = "",
items = {
{ width = 5 },
{ width = 30 },
{ remaining = true },
},
}
local line_info = { entry.lnum, "TelescopeResultsLineNr" }
return displayer {
line_info,
entry.text:gsub(".* | ", ""),
utils.path_smart(entry.filename), -- or path_tail
}
end
pickers.new(opts, {
prompt_title = "bookmarks",
finder = finders.new_table {
results = marklist,
entry_maker = function(entry)
return {
valid = true,
value = entry,
display = display(entry),
ordinal = entry.filename .. entry.text,
filename = entry.filename,
lnum = entry.lnum,
col = 1,
text = entry.text,
}
end,
},
sorter = conf.generic_sorter(opts),
previewer = conf.qflist_previewer(opts),
}):find()
end
return telescope.register_extension { exports = { list = bookmark } }