From 0b2eda02e26b1278c04e4cb752c7b49b5d3acbcd Mon Sep 17 00:00:00 2001 From: Jed Date: Sun, 25 Feb 2024 00:11:47 +0100 Subject: [PATCH 01/11] adds methods to list buf marks and all marks --- lua/marks/mark.lua | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/lua/marks/mark.lua b/lua/marks/mark.lua index 4f65042..39eb4c3 100644 --- a/lua/marks/mark.lua +++ b/lua/marks/mark.lua @@ -262,6 +262,47 @@ function Mark.preview_mark() vim.cmd("normal! zz") end +function Mark:get_buf_list(bufnr) + bufnr = bufnr or a.nvim_get_current_buf() + if not self.buffers[bufnr] then + return + end + + local items = {} + for mark, data in pairs(self.buffers[bufnr].placed_marks) do + local text = a.nvim_buf_get_lines(bufnr, data.line-1, data.line, true)[1] + local bufname = vim.api.nvim_buf_get_name(bufnr) + table.insert(items, { + bufnr = bufnr, + lnum = data.line, + col = data.col + 1, + mark = mark, + line = vim.trim(text), + filename = bufname + }) + end + return items +end + +function Mark:get_all_list() + local items = {} + for bufnr, buffer_state in pairs(self.buffers) do + for mark, data in pairs(buffer_state.placed_marks) do + local text = a.nvim_buf_get_lines(bufnr, data.line-1, data.line, true)[1] + local bufname = vim.api.nvim_buf_get_name(bufnr) + table.insert(items, { + bufnr = bufnr, + lnum = data.line, + col = data.col + 1, + mark = mark, + line = vim.trim(text), + filename = bufname + }) + end + end + return items +end + function Mark:buffer_to_list(list_type, bufnr) list_type = list_type or "loclist" From e7a4f793201d5d4330faf4460052f260b2b86134 Mon Sep 17 00:00:00 2001 From: Jed Date: Sun, 25 Feb 2024 00:12:12 +0100 Subject: [PATCH 02/11] adds telescope extension to list buf marks --- lua/telescope/_extensions/marks.lua | 58 +++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 lua/telescope/_extensions/marks.lua diff --git a/lua/telescope/_extensions/marks.lua b/lua/telescope/_extensions/marks.lua new file mode 100644 index 0000000..a631d23 --- /dev/null +++ b/lua/telescope/_extensions/marks.lua @@ -0,0 +1,58 @@ +local has_telescope, telescope = pcall(require, "telescope") + +if not has_telescope then + error("using marks.nvim telescope extensions requires nvim-telescope/telescope.nvim") +end + +local pickers = require("telescope.pickers") +local finders = require("telescope.finders") +local entry_display = require("telescope.pickers.entry_display") +local conf = require("telescope.config").values +local marks = require("marks") + +local list_buf = function(opts) + opts = opts or {} + local results = marks.mark_state:get_buf_list() or {} + + local displayer = entry_display.create({ + separator = " ", + items = { + { width = 3 }, + { width = 10 }, + {}, + }, + }) + + local make_display = function(entry) + return displayer({ + { entry.mark, "TelescopeResultsIdentifier" }, + { entry.lnum }, + { entry.line, "String" }, + }) + end + + pickers + .new(opts or {}, { + prompt_title = "Buffer Marks", + finder = finders.new_table({ + results = results, + entry_maker = function(entry) + entry.value = entry.mark + entry.ordinal = entry.line + entry.display = make_display + return entry + end, + }), + sorter = conf.generic_sorter(opts), + previewer = conf.grep_previewer(opts), + }) + :find() +end + +return telescope.register_extension({ + exports = { + marks_list_buf = function(opts) + list_buf(opts) + end, + }, +}) From 7ed61925e07b0028e4b31ba58878430673b612a3 Mon Sep 17 00:00:00 2001 From: Jed Date: Mon, 26 Feb 2024 23:54:50 +0100 Subject: [PATCH 03/11] renames telescope extension to avoid conflicts --- .../_extensions/{marks.lua => marks_nvim.lua} | 53 +++++++++++++++---- 1 file changed, 43 insertions(+), 10 deletions(-) rename lua/telescope/_extensions/{marks.lua => marks_nvim.lua} (53%) diff --git a/lua/telescope/_extensions/marks.lua b/lua/telescope/_extensions/marks_nvim.lua similarity index 53% rename from lua/telescope/_extensions/marks.lua rename to lua/telescope/_extensions/marks_nvim.lua index a631d23..30512b9 100644 --- a/lua/telescope/_extensions/marks.lua +++ b/lua/telescope/_extensions/marks_nvim.lua @@ -1,9 +1,4 @@ -local has_telescope, telescope = pcall(require, "telescope") - -if not has_telescope then - error("using marks.nvim telescope extensions requires nvim-telescope/telescope.nvim") -end - +local telescope = require("telescope") local pickers = require("telescope.pickers") local finders = require("telescope.finders") local entry_display = require("telescope.pickers.entry_display") @@ -26,7 +21,7 @@ local list_buf = function(opts) local make_display = function(entry) return displayer({ { entry.mark, "TelescopeResultsIdentifier" }, - { entry.lnum }, + { "l " .. entry.lnum }, { entry.line, "String" }, }) end @@ -49,10 +44,48 @@ local list_buf = function(opts) :find() end +local list_all = function(opts) + opts = opts or {} + local results = marks.mark_state:get_all_list() or {} + + local displayer = entry_display.create({ + separator = " ", + items = { + { width = 3 }, + { width = 10 }, + {}, + }, + }) + + local make_display = function(entry) + return displayer({ + { entry.mark, "TelescopeResultsIdentifier" }, + { "l " .. entry.lnum }, + { entry.filename, "String" }, + }) + end + + pickers + .new(opts or {}, { + prompt_title = "Buffer Marks", + finder = finders.new_table({ + results = results, + entry_maker = function(entry) + entry.value = entry.mark + entry.ordinal = entry.line + entry.display = make_display + return entry + end, + }), + sorter = conf.generic_sorter(opts), + previewer = conf.grep_previewer(opts), + }) + :find() +end + return telescope.register_extension({ exports = { - marks_list_buf = function(opts) - list_buf(opts) - end, + marks_list_buf = list_buf, + marks_list_all = list_all }, }) From 783ab020fd677e9d625d4a8e854bb237e2267a2b Mon Sep 17 00:00:00 2001 From: Jed Date: Tue, 27 Feb 2024 18:33:14 +0100 Subject: [PATCH 04/11] updates readme --- README.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/README.md b/README.md index aa21bcb..3891355 100644 --- a/README.md +++ b/README.md @@ -71,6 +71,28 @@ require'marks'.setup { See `:help marks-setup` for all of the keys that can be passed to the setup function. +## Telescope + +There is a [telescope](https://github.com/nvim-telescope/telescope.nvim) extension allowing to list marks through telescope. + +To activate it you need to load the extension: +```lua +local status_ok, telescope = pcall(require, "telescope") +if not status_ok then + return +end + +telescope.load_extension("marks_nvim") +``` + +You can then use the extension methods to list marks instead of using the native loclist system. +You simply need to call these methods in your mappings. + +```lua +require('telescope').extensions.marks_nvim.marks_list_buf() --[[ List buffer marks ]] +require('telescope').extensions.marks_nvim.marks_list_all() --[[ List all marks ]] +``` + ## Mappings The following default mappings are included: From 6b86b8973d488a6998cb5f10b8b44032e25ab0b7 Mon Sep 17 00:00:00 2001 From: Jed Date: Tue, 27 Feb 2024 18:49:57 +0100 Subject: [PATCH 05/11] renames list all prompt --- lua/telescope/_extensions/marks_nvim.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lua/telescope/_extensions/marks_nvim.lua b/lua/telescope/_extensions/marks_nvim.lua index 30512b9..42f0a1f 100644 --- a/lua/telescope/_extensions/marks_nvim.lua +++ b/lua/telescope/_extensions/marks_nvim.lua @@ -67,7 +67,7 @@ local list_all = function(opts) pickers .new(opts or {}, { - prompt_title = "Buffer Marks", + prompt_title = "All Marks", finder = finders.new_table({ results = results, entry_maker = function(entry) From 0fe9ed85ad0caf8b11219995bd26723fd46af319 Mon Sep 17 00:00:00 2001 From: Jed Date: Sun, 3 Mar 2024 16:54:20 +0100 Subject: [PATCH 06/11] adds telescope support for bookmarks too --- lua/marks/bookmark.lua | 600 +++++++++++++---------- lua/telescope/_extensions/marks_nvim.lua | 242 ++++++--- 2 files changed, 494 insertions(+), 348 deletions(-) diff --git a/lua/marks/bookmark.lua b/lua/marks/bookmark.lua index b39a9ea..1df5cab 100644 --- a/lua/marks/bookmark.lua +++ b/lua/marks/bookmark.lua @@ -1,4 +1,4 @@ -local utils = require'marks.utils' +local utils = require("marks.utils") local a = vim.api local Bookmarks = {} @@ -13,353 +13,413 @@ local Bookmarks = {} -- line, col, sign_id, extmark_id -- local function group_under_cursor(groups, bufnr, pos) - bufnr = bufnr or a.nvim_get_current_buf() - pos = pos or a.nvim_win_get_cursor(0) - - for group_nr, group in pairs(groups) do - if group.marks[bufnr] and group.marks[bufnr][pos[1]] then - return group_nr - end - end - return nil + bufnr = bufnr or a.nvim_get_current_buf() + pos = pos or a.nvim_win_get_cursor(0) + + for group_nr, group in pairs(groups) do + if group.marks[bufnr] and group.marks[bufnr][pos[1]] then + return group_nr + end + end + return nil end local function flatten(marks) - local ret = {} + local ret = {} - for _, buf_marks in pairs(marks) do - for _, mark in pairs(buf_marks) do - table.insert(ret, mark) - end - end + for _, buf_marks in pairs(marks) do + for _, mark in pairs(buf_marks) do + table.insert(ret, mark) + end + end - local function comparator(x, y) - return (x.buf == y.buf and x.line < y.line) or (x.buf < y.buf) - end + local function comparator(x, y) + return (x.buf == y.buf and x.line < y.line) or (x.buf < y.buf) + end - table.sort(ret, comparator) - return ret + table.sort(ret, comparator) + return ret end function Bookmarks:init(group_nr) - local ns = a.nvim_create_namespace("Bookmarks" .. group_nr) - local sign = self.signs[group_nr] - local virt_text = self.virt_text[group_nr] + local ns = a.nvim_create_namespace("Bookmarks" .. group_nr) + local sign = self.signs[group_nr] + local virt_text = self.virt_text[group_nr] - self.groups[group_nr] = { ns = ns, sign = sign, virt_text = virt_text, marks = {} } + self.groups[group_nr] = { ns = ns, sign = sign, virt_text = virt_text, marks = {} } end function Bookmarks:place_mark(group_nr, bufnr) - bufnr = bufnr or a.nvim_get_current_buf() - local group = self.groups[group_nr] + bufnr = bufnr or a.nvim_get_current_buf() + local group = self.groups[group_nr] - if not group then - self:init(group_nr) - group = self.groups[group_nr] - end + if not group then + self:init(group_nr) + group = self.groups[group_nr] + end - local pos = a.nvim_win_get_cursor(0) + local pos = a.nvim_win_get_cursor(0) - if group.marks[bufnr] and group.marks[bufnr][pos[1]] then - -- disallow multiple bookmarks on a single line - return - end + if group.marks[bufnr] and group.marks[bufnr][pos[1]] then + -- disallow multiple bookmarks on a single line + return + end - local data = { buf = bufnr, line = pos[1], col = pos[2], sign_id = -1} + local data = { buf = bufnr, line = pos[1], col = pos[2], sign_id = -1 } - local display_signs = utils.option_nil(self.opt.buf_signs[bufnr], self.opt.signs) - if display_signs and group.sign then - local id = group.sign:byte() * 100 + pos[1] - self:add_sign(bufnr, group.sign, pos[1], id) - data.sign_id = id - end + local display_signs = utils.option_nil(self.opt.buf_signs[bufnr], self.opt.signs) + if display_signs and group.sign then + local id = group.sign:byte() * 100 + pos[1] + self:add_sign(bufnr, group.sign, pos[1], id) + data.sign_id = id + end - local opts = {} - if group.virt_text then - opts.virt_text = {{ group.virt_text, "MarkVirtTextHL" }} - opts.virt_text_pos = "eol" - end + local opts = {} + if group.virt_text then + opts.virt_text = { { group.virt_text, "MarkVirtTextHL" } } + opts.virt_text_pos = "eol" + end - local extmark_id = a.nvim_buf_set_extmark(bufnr, group.ns, pos[1]-1, pos[2], opts) + local extmark_id = a.nvim_buf_set_extmark(bufnr, group.ns, pos[1] - 1, pos[2], opts) - data.extmark_id = extmark_id + data.extmark_id = extmark_id - if not group.marks[bufnr] then - group.marks[bufnr] = {} - end - group.marks[bufnr][pos[1]] = data + if not group.marks[bufnr] then + group.marks[bufnr] = {} + end + group.marks[bufnr][pos[1]] = data - if self.prompt_annotate[group_nr] then - self:annotate(group_nr) - end + if self.prompt_annotate[group_nr] then + self:annotate(group_nr) + end end function Bookmarks:toggle_mark(group_nr, bufnr) - bufnr = bufnr or a.nvim_get_current_buf() - local group = self.groups[group_nr] + bufnr = bufnr or a.nvim_get_current_buf() + local group = self.groups[group_nr] - if not group then - self:init(group_nr) - group = self.groups[group_nr] - end + if not group then + self:init(group_nr) + group = self.groups[group_nr] + end - local pos = a.nvim_win_get_cursor(0) + local pos = a.nvim_win_get_cursor(0) - if group.marks[bufnr] and group.marks[bufnr][pos[1]] then - self:delete_mark(group_nr) - else - self:place_mark(group_nr) - end + if group.marks[bufnr] and group.marks[bufnr][pos[1]] then + self:delete_mark(group_nr) + else + self:place_mark(group_nr) + end end function Bookmarks:delete_mark(group_nr, bufnr, line) - bufnr = bufnr or a.nvim_get_current_buf() - line = line or a.nvim_win_get_cursor(0)[1] - local group = self.groups[group_nr] + bufnr = bufnr or a.nvim_get_current_buf() + line = line or a.nvim_win_get_cursor(0)[1] + local group = self.groups[group_nr] - if not group then - return - end + if not group then + return + end - local mark = group.marks[bufnr][line] + local mark = group.marks[bufnr][line] - if not mark then - return - end + if not mark then + return + end - if mark.sign_id then - utils.remove_sign(bufnr, mark.sign_id, "BookmarkSigns") - end + if mark.sign_id then + utils.remove_sign(bufnr, mark.sign_id, "BookmarkSigns") + end - a.nvim_buf_del_extmark(bufnr, group.ns, mark.extmark_id) - group.marks[bufnr][line] = nil + a.nvim_buf_del_extmark(bufnr, group.ns, mark.extmark_id) + group.marks[bufnr][line] = nil end function Bookmarks:delete_mark_cursor() - local bufnr = a.nvim_get_current_buf() - local pos = a.nvim_win_get_cursor(0) + local bufnr = a.nvim_get_current_buf() + local pos = a.nvim_win_get_cursor(0) - local group_nr = group_under_cursor(self.groups, bufnr, pos) - if not group_nr then - return - end + local group_nr = group_under_cursor(self.groups, bufnr, pos) + if not group_nr then + return + end - self:delete_mark(group_nr, bufnr, pos[1]) + self:delete_mark(group_nr, bufnr, pos[1]) end function Bookmarks:delete_all(group_nr) - local group = self.groups[group_nr] - if not group then - return - end - - for bufnr, buf_marks in pairs(group.marks) do - for _, mark in pairs(buf_marks) do - if mark.sign_id then - utils.remove_sign(bufnr, mark.sign_id, "BookmarkSigns") - end - - a.nvim_buf_del_extmark(bufnr, group.ns, mark.extmark_id) - end - group.marks[bufnr] = nil - end + local group = self.groups[group_nr] + if not group then + return + end + + for bufnr, buf_marks in pairs(group.marks) do + for _, mark in pairs(buf_marks) do + if mark.sign_id then + utils.remove_sign(bufnr, mark.sign_id, "BookmarkSigns") + end + + a.nvim_buf_del_extmark(bufnr, group.ns, mark.extmark_id) + end + group.marks[bufnr] = nil + end end function Bookmarks:next(group_nr) - local bufnr = a.nvim_get_current_buf() - local pos = a.nvim_win_get_cursor(0) - - if not group_nr then - group_nr = group_under_cursor(self.groups, bufnr, pos) - end - - local group = self.groups[group_nr] - if not group then - return - end - - local marks = flatten(group.marks) - - if vim.tbl_isempty(marks) then - return - end - - local function comparator(x, y, _) - if (x.line > y.line and x.buf == y.buf) or (x.buf > y.buf) then - return true - end - - return false - end - - local next = utils.search(marks, {buf = bufnr, line=pos[1]}, - {buf=math.huge, line=math.huge}, comparator, false) - - if not next then - next = marks[1] - end - - if next.buf ~= bufnr then - vim.cmd("silent b" .. next.buf) - end - a.nvim_win_set_cursor(0, { next.line, next.col }) + local bufnr = a.nvim_get_current_buf() + local pos = a.nvim_win_get_cursor(0) + + if not group_nr then + group_nr = group_under_cursor(self.groups, bufnr, pos) + end + + local group = self.groups[group_nr] + if not group then + return + end + + local marks = flatten(group.marks) + + if vim.tbl_isempty(marks) then + return + end + + local function comparator(x, y, _) + if (x.line > y.line and x.buf == y.buf) or (x.buf > y.buf) then + return true + end + + return false + end + + local next = utils.search( + marks, + { buf = bufnr, line = pos[1] }, + { buf = math.huge, line = math.huge }, + comparator, + false + ) + + if not next then + next = marks[1] + end + + if next.buf ~= bufnr then + vim.cmd("silent b" .. next.buf) + end + a.nvim_win_set_cursor(0, { next.line, next.col }) end function Bookmarks:prev(group_nr) - local bufnr = a.nvim_get_current_buf() - local pos = a.nvim_win_get_cursor(0) + local bufnr = a.nvim_get_current_buf() + local pos = a.nvim_win_get_cursor(0) - if not group_nr then - group_nr = group_under_cursor(self.groups, bufnr, pos) - end + if not group_nr then + group_nr = group_under_cursor(self.groups, bufnr, pos) + end - local group = self.groups[group_nr] - if not group then - return - end + local group = self.groups[group_nr] + if not group then + return + end - local marks = flatten(group.marks) + local marks = flatten(group.marks) - if vim.tbl_isempty(marks) then - return - end + if vim.tbl_isempty(marks) then + return + end - local function comparator(x, y, _) - if (x.line < y.line and x.buf == y.buf) or (x.buf < y.buf) then - return true - end + local function comparator(x, y, _) + if (x.line < y.line and x.buf == y.buf) or (x.buf < y.buf) then + return true + end - return false - end + return false + end - local prev = utils.search(marks, {buf = bufnr, line=pos[1]}, - {buf=-1, line=-1}, comparator, false) + local prev = utils.search(marks, { buf = bufnr, line = pos[1] }, { buf = -1, line = -1 }, comparator, false) - if not prev then - prev = marks[#marks] - end + if not prev then + prev = marks[#marks] + end - if prev.buf ~= bufnr then - vim.cmd("silent b" .. prev.buf) - end - a.nvim_win_set_cursor(0, { prev.line, prev.col }) + if prev.buf ~= bufnr then + vim.cmd("silent b" .. prev.buf) + end + a.nvim_win_set_cursor(0, { prev.line, prev.col }) end function Bookmarks:annotate(group_nr) - if vim.fn.has("nvim-0.6") ~= 1 then - error("virtual line annotations requires neovim 0.6 or higher") - end - - local bufnr = a.nvim_get_current_buf() - local pos = a.nvim_win_get_cursor(0) - - group_nr = group_nr or group_under_cursor(self.groups, bufnr, pos) - - if not group_nr then - return - end - - local bookmark = self.groups[group_nr].marks[bufnr][pos[1]] - - if not bookmark then - return - end - - local text = vim.fn.input("annotation: ") - - if text ~= "" then - a.nvim_buf_set_extmark(bufnr, self.groups[group_nr].ns, bookmark.line-1, bookmark.col, { - id = bookmark.extmark_id, virt_lines = {{{text, "MarkVirtTextHL"}}}, - virt_lines_above=true, - }) - else - a.nvim_buf_del_extmark(bufnr, self.groups[group_nr].ns, bookmark.extmark_id) - - local opts = {} - if self.groups[group_nr].virt_text then - opts.virt_text = {{ self.groups[group_nr].virt_text, "MarkVirtTextHL" }} - opts.virt_text_pos = "eol" - end - bookmark.extmark_id = a.nvim_buf_set_extmark(bufnr, self.groups[group_nr].ns, bookmark.line-1, - bookmark.col, opts) - end + if vim.fn.has("nvim-0.6") ~= 1 then + error("virtual line annotations requires neovim 0.6 or higher") + end + + local bufnr = a.nvim_get_current_buf() + local pos = a.nvim_win_get_cursor(0) + + group_nr = group_nr or group_under_cursor(self.groups, bufnr, pos) + + if not group_nr then + return + end + + local bookmark = self.groups[group_nr].marks[bufnr][pos[1]] + + if not bookmark then + return + end + + local text = vim.fn.input("annotation: ") + + if text ~= "" then + a.nvim_buf_set_extmark(bufnr, self.groups[group_nr].ns, bookmark.line - 1, bookmark.col, { + id = bookmark.extmark_id, + virt_lines = { { { text, "MarkVirtTextHL" } } }, + virt_lines_above = true, + }) + else + a.nvim_buf_del_extmark(bufnr, self.groups[group_nr].ns, bookmark.extmark_id) + + local opts = {} + if self.groups[group_nr].virt_text then + opts.virt_text = { { self.groups[group_nr].virt_text, "MarkVirtTextHL" } } + opts.virt_text_pos = "eol" + end + bookmark.extmark_id = + a.nvim_buf_set_extmark(bufnr, self.groups[group_nr].ns, bookmark.line - 1, bookmark.col, opts) + end end function Bookmarks:refresh() - local bufnr = a.nvim_get_current_buf() - - -- if we delete and undo really quickly, the extmark's position will be - -- the same, but the sign will no longer be there. so clear and restore all - -- signs. - - local buf_marks - local display_signs - utils.remove_buf_signs(bufnr, "BookmarkSigns") - for _, group in pairs(self.groups) do - buf_marks = group.marks[bufnr] - if buf_marks then - for _, mark in pairs(vim.tbl_values(buf_marks)) do - local line = a.nvim_buf_get_extmark_by_id(bufnr, group.ns, - mark.extmark_id, {})[1] - - if line + 1 ~= mark.line then - buf_marks[line + 1] = mark - buf_marks[mark.line] = nil - buf_marks[line + 1].line = line + 1 - end - display_signs = utils.option_nil(self.opt.buf_signs[bufnr], self.opt.signs) - if display_signs and group.sign then - self:add_sign(bufnr, group.sign, line + 1, mark.sign_id) - end - end - end - end + local bufnr = a.nvim_get_current_buf() + + -- if we delete and undo really quickly, the extmark's position will be + -- the same, but the sign will no longer be there. so clear and restore all + -- signs. + + local buf_marks + local display_signs + utils.remove_buf_signs(bufnr, "BookmarkSigns") + for _, group in pairs(self.groups) do + buf_marks = group.marks[bufnr] + if buf_marks then + for _, mark in pairs(vim.tbl_values(buf_marks)) do + local line = a.nvim_buf_get_extmark_by_id(bufnr, group.ns, mark.extmark_id, {})[1] + + if line + 1 ~= mark.line then + buf_marks[line + 1] = mark + buf_marks[mark.line] = nil + buf_marks[line + 1].line = line + 1 + end + display_signs = utils.option_nil(self.opt.buf_signs[bufnr], self.opt.signs) + if display_signs and group.sign then + self:add_sign(bufnr, group.sign, line + 1, mark.sign_id) + end + end + end + end +end + +function Bookmarks:get_group_list(group_nr) + local items = {} + if not group_nr or not self.groups[group_nr] then + return items + end + + for bufnr, buffer_marks in pairs(self.groups[group_nr].marks) do + for line, mark in pairs(buffer_marks) do + local text = a.nvim_buf_get_lines(bufnr, line - 1, line, true)[1] + local filename = vim.api.nvim_buf_get_name(bufnr) + table.insert(items, { + bufnr = bufnr, + lnum = line, + col = mark.col + 1, + group = group_nr, + line = vim.trim(text), + filename = filename, + }) + end + end + + return items +end + +function Bookmarks:get_all_list() + local items = {} + for group_nr, group in pairs(self.groups) do + for bufnr, buffer_marks in pairs(group.marks) do + for line, mark in pairs(buffer_marks) do + local text = a.nvim_buf_get_lines(bufnr, line - 1, line, true)[1] + local filename = vim.api.nvim_buf_get_name(bufnr) + + table.insert(items, { + bufnr = bufnr, + lnum = line, + col = mark.col + 1, + group = group_nr, + line = vim.trim(text), + filename = filename, + }) + end + end + end + + return items end function Bookmarks:to_list(list_type, group_nr) - if not group_nr or not self.groups[group_nr] then - return - end - - list_type = list_type or "loclist" - local list_fn = utils.choose_list(list_type) - - local items = {} - for bufnr, buffer_marks in pairs(self.groups[group_nr].marks) do - for line, mark in pairs(buffer_marks) do - local text = a.nvim_buf_get_lines(bufnr, line-1, line, true)[1] - table.insert(items, { bufnr=bufnr, lnum=line, col=mark.col + 1, text=text }) - end - end - - list_fn(items, "r") + if not group_nr or not self.groups[group_nr] then + return + end + + list_type = list_type or "loclist" + local list_fn = utils.choose_list(list_type) + + local items = {} + for bufnr, buffer_marks in pairs(self.groups[group_nr].marks) do + for line, mark in pairs(buffer_marks) do + local text = a.nvim_buf_get_lines(bufnr, line - 1, line, true)[1] + table.insert(items, { bufnr = bufnr, lnum = line, col = mark.col + 1, text = text }) + end + end + + list_fn(items, "r") end function Bookmarks:all_to_list(list_type) - list_type = list_type or "loclist" - local list_fn = utils.choose_list(list_type) - - local items = {} - for group_nr, group in pairs(self.groups) do - for bufnr, buffer_marks in pairs(group.marks) do - for line, mark in pairs(buffer_marks) do - local text = a.nvim_buf_get_lines(bufnr, line-1, line, true)[1] - table.insert(items, { bufnr=bufnr, lnum=line, col=mark.col + 1, - text="bookmark group "..group_nr..": "..text }) - end - end - end - - list_fn(items, "r") + list_type = list_type or "loclist" + local list_fn = utils.choose_list(list_type) + + local items = {} + for group_nr, group in pairs(self.groups) do + for bufnr, buffer_marks in pairs(group.marks) do + for line, mark in pairs(buffer_marks) do + local text = a.nvim_buf_get_lines(bufnr, line - 1, line, true)[1] + table.insert(items, { + bufnr = bufnr, + lnum = line, + col = mark.col + 1, + text = "bookmark group " .. group_nr .. ": " .. text, + }) + end + end + end + + list_fn(items, "r") end function Bookmarks:add_sign(bufnr, text, line, id) - utils.add_sign(bufnr, text, line, id, "BookmarkSigns", self.priority) + utils.add_sign(bufnr, text, line, id, "BookmarkSigns", self.priority) end function Bookmarks.new() - return setmetatable({signs = {"!", "@", "#", "$", "%", "^", "&", "*", "(", [0]=")"}, - virt_text = {}, groups = {}, prompt_annotate = {}, opt = {}}, {__index = Bookmarks}) + return setmetatable({ + signs = { "!", "@", "#", "$", "%", "^", "&", "*", "(", [0] = ")" }, + virt_text = {}, + groups = {}, + prompt_annotate = {}, + opt = {}, + }, { __index = Bookmarks }) end return Bookmarks diff --git a/lua/telescope/_extensions/marks_nvim.lua b/lua/telescope/_extensions/marks_nvim.lua index 42f0a1f..43b8151 100644 --- a/lua/telescope/_extensions/marks_nvim.lua +++ b/lua/telescope/_extensions/marks_nvim.lua @@ -5,87 +5,173 @@ local entry_display = require("telescope.pickers.entry_display") local conf = require("telescope.config").values local marks = require("marks") -local list_buf = function(opts) - opts = opts or {} - local results = marks.mark_state:get_buf_list() or {} - - local displayer = entry_display.create({ - separator = " ", - items = { - { width = 3 }, - { width = 10 }, - {}, - }, - }) - - local make_display = function(entry) - return displayer({ - { entry.mark, "TelescopeResultsIdentifier" }, - { "l " .. entry.lnum }, - { entry.line, "String" }, - }) - end - - pickers - .new(opts or {}, { - prompt_title = "Buffer Marks", - finder = finders.new_table({ - results = results, - entry_maker = function(entry) - entry.value = entry.mark - entry.ordinal = entry.line - entry.display = make_display - return entry - end, - }), - sorter = conf.generic_sorter(opts), - previewer = conf.grep_previewer(opts), - }) - :find() +local list_marks_buf = function(opts) + opts = opts or {} + local results = marks.mark_state:get_buf_list() or {} + + local displayer = entry_display.create({ + separator = " ", + items = { + { width = 3 }, + { width = 10 }, + {}, + }, + }) + + local make_display = function(entry) + return displayer({ + { entry.mark, "TelescopeResultsIdentifier" }, + { "l" .. entry.lnum }, + { entry.line, "String" }, + }) + end + + pickers + .new(opts or {}, { + prompt_title = "Buffer Marks", + finder = finders.new_table({ + results = results, + entry_maker = function(entry) + entry.value = entry.mark + entry.ordinal = entry.line + entry.display = make_display + return entry + end, + }), + sorter = conf.generic_sorter(opts), + previewer = conf.grep_previewer(opts), + }) + :find() +end + +local list_marks_all = function(opts) + opts = opts or {} + local results = marks.mark_state:get_all_list() or {} + + local displayer = entry_display.create({ + separator = " ", + items = { + { width = 1 }, + { width = 7 }, + { width = 15 }, + {}, + }, + }) + + local make_display = function(entry) + return displayer({ + { entry.mark, "TelescopeResultsIdentifier" }, + { "l" .. entry.lnum }, + { entry.line, "String" }, + { vim.fs.normalize(entry.filename) }, + }) + end + + pickers + .new(opts or {}, { + prompt_title = "All Marks", + finder = finders.new_table({ + results = results, + entry_maker = function(entry) + entry.value = entry.mark + entry.ordinal = entry.line + entry.display = make_display + return entry + end, + }), + sorter = conf.generic_sorter(opts), + previewer = conf.grep_previewer(opts), + }) + :find() end -local list_all = function(opts) - opts = opts or {} - local results = marks.mark_state:get_all_list() or {} - - local displayer = entry_display.create({ - separator = " ", - items = { - { width = 3 }, - { width = 10 }, - {}, - }, - }) - - local make_display = function(entry) - return displayer({ - { entry.mark, "TelescopeResultsIdentifier" }, - { "l " .. entry.lnum }, - { entry.filename, "String" }, - }) - end - - pickers - .new(opts or {}, { - prompt_title = "All Marks", - finder = finders.new_table({ - results = results, - entry_maker = function(entry) - entry.value = entry.mark - entry.ordinal = entry.line - entry.display = make_display - return entry - end, - }), - sorter = conf.generic_sorter(opts), - previewer = conf.grep_previewer(opts), - }) - :find() +local list_bookmarks_group = function(group, opts) + opts = opts or {} + local results = marks.bookmark_state:get_group_list(group) or {} + + local displayer = entry_display.create({ + separator = " ", + items = { + { width = 1 }, + { width = 7 }, + { width = 15 }, + {}, + }, + }) + + local make_display = function(entry) + return displayer({ + { entry.group, "TelescopeResultsIdentifier" }, + { "l" .. entry.lnum }, + { entry.line, "String" }, + { vim.fs.normalize(entry.filename) }, + }) + end + + pickers + .new(opts or {}, { + prompt_title = "Bookmark " .. group, + finder = finders.new_table({ + results = results, + entry_maker = function(entry) + entry.value = entry.group + entry.ordinal = entry.line + entry.display = make_display + return entry + end, + }), + sorter = conf.generic_sorter(opts), + previewer = conf.grep_previewer(opts), + }) + :find() +end + +local list_bookmarks_all = function(opts) + opts = opts or {} + local results = marks.bookmark_state:get_all_list() or {} + + local displayer = entry_display.create({ + separator = " ", + items = { + { width = 1 }, + { width = 7 }, + { width = 15 }, + {}, + }, + }) + + local make_display = function(entry) + return displayer({ + { entry.group, "TelescopeResultsIdentifier" }, + { "l" .. entry.lnum }, + { entry.line, "String" }, + { vim.fs.normalize(entry.filename) }, + }) + end + + pickers + .new(opts or {}, { + prompt_title = "All Bookmarks", + finder = finders.new_table({ + results = results, + entry_maker = function(entry) + entry.value = entry.group + entry.ordinal = entry.line + entry.display = make_display + return entry + end, + }), + sorter = conf.generic_sorter(opts), + previewer = conf.grep_previewer(opts), + }) + :find() end return telescope.register_extension({ - exports = { - marks_list_buf = list_buf, - marks_list_all = list_all - }, + exports = { + marks_list_buf = list_marks_buf, + marks_list_all = list_marks_all, + bookmarks_list_group = list_bookmarks_group, + bookmarks_list_all = list_bookmarks_all, + }, }) From c7895951896ffed8f300c469d939b01e76e5de4d Mon Sep 17 00:00:00 2001 From: Jed Date: Mon, 4 Mar 2024 23:42:48 +0100 Subject: [PATCH 07/11] renames filename by path --- lua/marks/bookmark.lua | 4 ++-- lua/marks/mark.lua | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/lua/marks/bookmark.lua b/lua/marks/bookmark.lua index 1df5cab..ca20972 100644 --- a/lua/marks/bookmark.lua +++ b/lua/marks/bookmark.lua @@ -336,7 +336,7 @@ function Bookmarks:get_group_list(group_nr) col = mark.col + 1, group = group_nr, line = vim.trim(text), - filename = filename, + path = filename, }) end end @@ -358,7 +358,7 @@ function Bookmarks:get_all_list() col = mark.col + 1, group = group_nr, line = vim.trim(text), - filename = filename, + path = filename, }) end end diff --git a/lua/marks/mark.lua b/lua/marks/mark.lua index 39eb4c3..e92521c 100644 --- a/lua/marks/mark.lua +++ b/lua/marks/mark.lua @@ -271,14 +271,14 @@ function Mark:get_buf_list(bufnr) local items = {} for mark, data in pairs(self.buffers[bufnr].placed_marks) do local text = a.nvim_buf_get_lines(bufnr, data.line-1, data.line, true)[1] - local bufname = vim.api.nvim_buf_get_name(bufnr) + local path = vim.api.nvim_buf_get_name(bufnr) table.insert(items, { bufnr = bufnr, lnum = data.line, col = data.col + 1, mark = mark, line = vim.trim(text), - filename = bufname + path = path }) end return items @@ -289,14 +289,14 @@ function Mark:get_all_list() for bufnr, buffer_state in pairs(self.buffers) do for mark, data in pairs(buffer_state.placed_marks) do local text = a.nvim_buf_get_lines(bufnr, data.line-1, data.line, true)[1] - local bufname = vim.api.nvim_buf_get_name(bufnr) + local path = vim.api.nvim_buf_get_name(bufnr) table.insert(items, { bufnr = bufnr, lnum = data.line, col = data.col + 1, mark = mark, line = vim.trim(text), - filename = bufname + path = path }) end end From 8bdfc3384895ce9a4006208917532fb7df88c6d3 Mon Sep 17 00:00:00 2001 From: Jed Date: Mon, 4 Mar 2024 23:43:07 +0100 Subject: [PATCH 08/11] uses config to handle path display --- lua/telescope/_extensions/marks_nvim.lua | 38 +++++++++++++----------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/lua/telescope/_extensions/marks_nvim.lua b/lua/telescope/_extensions/marks_nvim.lua index 43b8151..bd45d85 100644 --- a/lua/telescope/_extensions/marks_nvim.lua +++ b/lua/telescope/_extensions/marks_nvim.lua @@ -2,6 +2,7 @@ local telescope = require("telescope") local pickers = require("telescope.pickers") local finders = require("telescope.finders") local entry_display = require("telescope.pickers.entry_display") +local telescope_utils = require("telescope.utils") local conf = require("telescope.config").values local marks = require("marks") @@ -21,13 +22,13 @@ local list_marks_buf = function(opts) local make_display = function(entry) return displayer({ { entry.mark, "TelescopeResultsIdentifier" }, - { "l" .. entry.lnum }, + { entry.lnum }, { entry.line, "String" }, }) end pickers - .new(opts or {}, { + .new(opts, { prompt_title = "Buffer Marks", finder = finders.new_table({ results = results, @@ -46,14 +47,15 @@ end local list_marks_all = function(opts) opts = opts or {} + local conf_path = { path_display = opts.path_display or conf.path_display or {} } local results = marks.mark_state:get_all_list() or {} local displayer = entry_display.create({ separator = " ", items = { { width = 1 }, - { width = 7 }, - { width = 15 }, + { width = 5 }, + { width = 20 }, {}, }, }) @@ -61,14 +63,14 @@ local list_marks_all = function(opts) local make_display = function(entry) return displayer({ { entry.mark, "TelescopeResultsIdentifier" }, - { "l" .. entry.lnum }, + { entry.lnum }, { entry.line, "String" }, - { vim.fs.normalize(entry.filename) }, + { telescope_utils.transform_path(conf_path, entry.path) }, }) end pickers - .new(opts or {}, { + .new(opts, { prompt_title = "All Marks", finder = finders.new_table({ results = results, @@ -87,14 +89,15 @@ end local list_bookmarks_group = function(group, opts) opts = opts or {} + local conf_path = { path_display = opts.path_display or conf.path_display or {} } local results = marks.bookmark_state:get_group_list(group) or {} local displayer = entry_display.create({ separator = " ", items = { { width = 1 }, - { width = 7 }, - { width = 15 }, + { width = 5 }, + { width = 20 }, {}, }, }) @@ -102,14 +105,14 @@ local list_bookmarks_group = function(group, opts) local make_display = function(entry) return displayer({ { entry.group, "TelescopeResultsIdentifier" }, - { "l" .. entry.lnum }, + { entry.lnum }, { entry.line, "String" }, - { vim.fs.normalize(entry.filename) }, + { telescope_utils.transform_path(conf_path, entry.path) }, }) end pickers - .new(opts or {}, { + .new(opts, { prompt_title = "Bookmark " .. group, finder = finders.new_table({ results = results, @@ -128,14 +131,15 @@ end local list_bookmarks_all = function(opts) opts = opts or {} + local conf_path = { path_display = opts.path_display or conf.path_display or {} } local results = marks.bookmark_state:get_all_list() or {} local displayer = entry_display.create({ separator = " ", items = { { width = 1 }, - { width = 7 }, - { width = 15 }, + { width = 5 }, + { width = 20 }, {}, }, }) @@ -143,14 +147,14 @@ local list_bookmarks_all = function(opts) local make_display = function(entry) return displayer({ { entry.group, "TelescopeResultsIdentifier" }, - { "l" .. entry.lnum }, + { entry.lnum }, { entry.line, "String" }, - { vim.fs.normalize(entry.filename) }, + { telescope_utils.transform_path(conf_path, entry.path) }, }) end pickers - .new(opts or {}, { + .new(opts, { prompt_title = "All Bookmarks", finder = finders.new_table({ results = results, From c08af8cd9d5539cb9795fab72af058ef1e63b21f Mon Sep 17 00:00:00 2001 From: Jed Date: Tue, 5 Mar 2024 20:26:38 +0100 Subject: [PATCH 09/11] updates readme --- README.md | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 3891355..5b1f80b 100644 --- a/README.md +++ b/README.md @@ -89,8 +89,16 @@ You can then use the extension methods to list marks instead of using the native You simply need to call these methods in your mappings. ```lua -require('telescope').extensions.marks_nvim.marks_list_buf() --[[ List buffer marks ]] -require('telescope').extensions.marks_nvim.marks_list_all() --[[ List all marks ]] +require('telescope').extensions.marks_nvim.marks_list_buf(opts) --[[ List buffer marks ]] +require('telescope').extensions.marks_nvim.marks_list_all(opts) --[[ List all marks ]] +require('telescope').extensions.marks_nvim.bookmarks_list_group(1, opts) --[[ List a bookmark group marks (takes the group number as argument) ]] +require('telescope').extensions.marks_nvim.bookmarks_list_all(opts) --[[ List all bookmarks marks ]] +``` + +These methods will use your `path_display` telescope configuraiton to display paths. +You can also pass a specific property for one method in the `opts` table. Eg. +```lua +require('telescope').extensions.marks_nvim.marks_list_all({ path_display = 'shorten' }) ``` ## Mappings From e0f3229f3b3298e65c8b7655ea1c5b32270da772 Mon Sep 17 00:00:00 2001 From: Jed Date: Tue, 5 Mar 2024 20:29:40 +0100 Subject: [PATCH 10/11] rollback indent linting --- lua/marks/bookmark.lua | 645 +++++++++++------------ lua/telescope/_extensions/marks_nvim.lua | 318 +++++------ 2 files changed, 481 insertions(+), 482 deletions(-) diff --git a/lua/marks/bookmark.lua b/lua/marks/bookmark.lua index ca20972..d2dfb41 100644 --- a/lua/marks/bookmark.lua +++ b/lua/marks/bookmark.lua @@ -13,413 +13,412 @@ local Bookmarks = {} -- line, col, sign_id, extmark_id -- local function group_under_cursor(groups, bufnr, pos) - bufnr = bufnr or a.nvim_get_current_buf() - pos = pos or a.nvim_win_get_cursor(0) - - for group_nr, group in pairs(groups) do - if group.marks[bufnr] and group.marks[bufnr][pos[1]] then - return group_nr - end - end - return nil + bufnr = bufnr or a.nvim_get_current_buf() + pos = pos or a.nvim_win_get_cursor(0) + + for group_nr, group in pairs(groups) do + if group.marks[bufnr] and group.marks[bufnr][pos[1]] then + return group_nr + end + end + return nil end local function flatten(marks) - local ret = {} + local ret = {} - for _, buf_marks in pairs(marks) do - for _, mark in pairs(buf_marks) do - table.insert(ret, mark) - end - end + for _, buf_marks in pairs(marks) do + for _, mark in pairs(buf_marks) do + table.insert(ret, mark) + end + end - local function comparator(x, y) - return (x.buf == y.buf and x.line < y.line) or (x.buf < y.buf) - end + local function comparator(x, y) + return (x.buf == y.buf and x.line < y.line) or (x.buf < y.buf) + end - table.sort(ret, comparator) - return ret + table.sort(ret, comparator) + return ret end function Bookmarks:init(group_nr) - local ns = a.nvim_create_namespace("Bookmarks" .. group_nr) - local sign = self.signs[group_nr] - local virt_text = self.virt_text[group_nr] + local ns = a.nvim_create_namespace("Bookmarks" .. group_nr) + local sign = self.signs[group_nr] + local virt_text = self.virt_text[group_nr] - self.groups[group_nr] = { ns = ns, sign = sign, virt_text = virt_text, marks = {} } + self.groups[group_nr] = { ns = ns, sign = sign, virt_text = virt_text, marks = {} } end function Bookmarks:place_mark(group_nr, bufnr) - bufnr = bufnr or a.nvim_get_current_buf() - local group = self.groups[group_nr] + bufnr = bufnr or a.nvim_get_current_buf() + local group = self.groups[group_nr] - if not group then - self:init(group_nr) - group = self.groups[group_nr] - end + if not group then + self:init(group_nr) + group = self.groups[group_nr] + end - local pos = a.nvim_win_get_cursor(0) + local pos = a.nvim_win_get_cursor(0) - if group.marks[bufnr] and group.marks[bufnr][pos[1]] then - -- disallow multiple bookmarks on a single line - return - end + if group.marks[bufnr] and group.marks[bufnr][pos[1]] then + -- disallow multiple bookmarks on a single line + return + end - local data = { buf = bufnr, line = pos[1], col = pos[2], sign_id = -1 } + local data = { buf = bufnr, line = pos[1], col = pos[2], sign_id = -1 } - local display_signs = utils.option_nil(self.opt.buf_signs[bufnr], self.opt.signs) - if display_signs and group.sign then - local id = group.sign:byte() * 100 + pos[1] - self:add_sign(bufnr, group.sign, pos[1], id) - data.sign_id = id - end + local display_signs = utils.option_nil(self.opt.buf_signs[bufnr], self.opt.signs) + if display_signs and group.sign then + local id = group.sign:byte() * 100 + pos[1] + self:add_sign(bufnr, group.sign, pos[1], id) + data.sign_id = id + end - local opts = {} - if group.virt_text then - opts.virt_text = { { group.virt_text, "MarkVirtTextHL" } } - opts.virt_text_pos = "eol" - end + local opts = {} + if group.virt_text then + opts.virt_text = { { group.virt_text, "MarkVirtTextHL" } } + opts.virt_text_pos = "eol" + end - local extmark_id = a.nvim_buf_set_extmark(bufnr, group.ns, pos[1] - 1, pos[2], opts) + local extmark_id = a.nvim_buf_set_extmark(bufnr, group.ns, pos[1] - 1, pos[2], opts) - data.extmark_id = extmark_id + data.extmark_id = extmark_id - if not group.marks[bufnr] then - group.marks[bufnr] = {} - end - group.marks[bufnr][pos[1]] = data + if not group.marks[bufnr] then + group.marks[bufnr] = {} + end + group.marks[bufnr][pos[1]] = data - if self.prompt_annotate[group_nr] then - self:annotate(group_nr) - end + if self.prompt_annotate[group_nr] then + self:annotate(group_nr) + end end function Bookmarks:toggle_mark(group_nr, bufnr) - bufnr = bufnr or a.nvim_get_current_buf() - local group = self.groups[group_nr] + bufnr = bufnr or a.nvim_get_current_buf() + local group = self.groups[group_nr] - if not group then - self:init(group_nr) - group = self.groups[group_nr] - end + if not group then + self:init(group_nr) + group = self.groups[group_nr] + end - local pos = a.nvim_win_get_cursor(0) + local pos = a.nvim_win_get_cursor(0) - if group.marks[bufnr] and group.marks[bufnr][pos[1]] then - self:delete_mark(group_nr) - else - self:place_mark(group_nr) - end + if group.marks[bufnr] and group.marks[bufnr][pos[1]] then + self:delete_mark(group_nr) + else + self:place_mark(group_nr) + end end function Bookmarks:delete_mark(group_nr, bufnr, line) - bufnr = bufnr or a.nvim_get_current_buf() - line = line or a.nvim_win_get_cursor(0)[1] - local group = self.groups[group_nr] + bufnr = bufnr or a.nvim_get_current_buf() + line = line or a.nvim_win_get_cursor(0)[1] + local group = self.groups[group_nr] - if not group then - return - end + if not group then + return + end - local mark = group.marks[bufnr][line] + local mark = group.marks[bufnr][line] - if not mark then - return - end + if not mark then + return + end - if mark.sign_id then - utils.remove_sign(bufnr, mark.sign_id, "BookmarkSigns") - end + if mark.sign_id then + utils.remove_sign(bufnr, mark.sign_id, "BookmarkSigns") + end - a.nvim_buf_del_extmark(bufnr, group.ns, mark.extmark_id) - group.marks[bufnr][line] = nil + a.nvim_buf_del_extmark(bufnr, group.ns, mark.extmark_id) + group.marks[bufnr][line] = nil end function Bookmarks:delete_mark_cursor() - local bufnr = a.nvim_get_current_buf() - local pos = a.nvim_win_get_cursor(0) + local bufnr = a.nvim_get_current_buf() + local pos = a.nvim_win_get_cursor(0) - local group_nr = group_under_cursor(self.groups, bufnr, pos) - if not group_nr then - return - end + local group_nr = group_under_cursor(self.groups, bufnr, pos) + if not group_nr then + return + end - self:delete_mark(group_nr, bufnr, pos[1]) + self:delete_mark(group_nr, bufnr, pos[1]) end function Bookmarks:delete_all(group_nr) - local group = self.groups[group_nr] - if not group then - return - end - - for bufnr, buf_marks in pairs(group.marks) do - for _, mark in pairs(buf_marks) do - if mark.sign_id then - utils.remove_sign(bufnr, mark.sign_id, "BookmarkSigns") - end - - a.nvim_buf_del_extmark(bufnr, group.ns, mark.extmark_id) - end - group.marks[bufnr] = nil - end + local group = self.groups[group_nr] + if not group then + return + end + + for bufnr, buf_marks in pairs(group.marks) do + for _, mark in pairs(buf_marks) do + if mark.sign_id then + utils.remove_sign(bufnr, mark.sign_id, "BookmarkSigns") + end + + a.nvim_buf_del_extmark(bufnr, group.ns, mark.extmark_id) + end + group.marks[bufnr] = nil + end end function Bookmarks:next(group_nr) - local bufnr = a.nvim_get_current_buf() - local pos = a.nvim_win_get_cursor(0) - - if not group_nr then - group_nr = group_under_cursor(self.groups, bufnr, pos) - end - - local group = self.groups[group_nr] - if not group then - return - end - - local marks = flatten(group.marks) - - if vim.tbl_isempty(marks) then - return - end - - local function comparator(x, y, _) - if (x.line > y.line and x.buf == y.buf) or (x.buf > y.buf) then - return true - end - - return false - end - - local next = utils.search( - marks, - { buf = bufnr, line = pos[1] }, - { buf = math.huge, line = math.huge }, - comparator, - false - ) - - if not next then - next = marks[1] - end - - if next.buf ~= bufnr then - vim.cmd("silent b" .. next.buf) - end - a.nvim_win_set_cursor(0, { next.line, next.col }) + local bufnr = a.nvim_get_current_buf() + local pos = a.nvim_win_get_cursor(0) + + if not group_nr then + group_nr = group_under_cursor(self.groups, bufnr, pos) + end + + local group = self.groups[group_nr] + if not group then + return + end + + local marks = flatten(group.marks) + + if vim.tbl_isempty(marks) then + return + end + + local function comparator(x, y, _) + if (x.line > y.line and x.buf == y.buf) or (x.buf > y.buf) then + return true + end + + return false + end + + local next = utils.search( + marks, + { buf = bufnr, line = pos[1] }, + { buf = math.huge, line = math.huge }, + comparator, + false + ) + + if not next then + next = marks[1] + end + + if next.buf ~= bufnr then + vim.cmd("silent b" .. next.buf) + end + a.nvim_win_set_cursor(0, { next.line, next.col }) end function Bookmarks:prev(group_nr) - local bufnr = a.nvim_get_current_buf() - local pos = a.nvim_win_get_cursor(0) + local bufnr = a.nvim_get_current_buf() + local pos = a.nvim_win_get_cursor(0) - if not group_nr then - group_nr = group_under_cursor(self.groups, bufnr, pos) - end + if not group_nr then + group_nr = group_under_cursor(self.groups, bufnr, pos) + end - local group = self.groups[group_nr] - if not group then - return - end + local group = self.groups[group_nr] + if not group then + return + end - local marks = flatten(group.marks) + local marks = flatten(group.marks) - if vim.tbl_isempty(marks) then - return - end + if vim.tbl_isempty(marks) then + return + end - local function comparator(x, y, _) - if (x.line < y.line and x.buf == y.buf) or (x.buf < y.buf) then - return true - end + local function comparator(x, y, _) + if (x.line < y.line and x.buf == y.buf) or (x.buf < y.buf) then + return true + end - return false - end + return false + end - local prev = utils.search(marks, { buf = bufnr, line = pos[1] }, { buf = -1, line = -1 }, comparator, false) + local prev = utils.search(marks, { buf = bufnr, line = pos[1] }, { buf = -1, line = -1 }, comparator, false) - if not prev then - prev = marks[#marks] - end + if not prev then + prev = marks[#marks] + end - if prev.buf ~= bufnr then - vim.cmd("silent b" .. prev.buf) - end - a.nvim_win_set_cursor(0, { prev.line, prev.col }) + if prev.buf ~= bufnr then + vim.cmd("silent b" .. prev.buf) + end + a.nvim_win_set_cursor(0, { prev.line, prev.col }) end function Bookmarks:annotate(group_nr) - if vim.fn.has("nvim-0.6") ~= 1 then - error("virtual line annotations requires neovim 0.6 or higher") - end - - local bufnr = a.nvim_get_current_buf() - local pos = a.nvim_win_get_cursor(0) - - group_nr = group_nr or group_under_cursor(self.groups, bufnr, pos) - - if not group_nr then - return - end - - local bookmark = self.groups[group_nr].marks[bufnr][pos[1]] - - if not bookmark then - return - end - - local text = vim.fn.input("annotation: ") - - if text ~= "" then - a.nvim_buf_set_extmark(bufnr, self.groups[group_nr].ns, bookmark.line - 1, bookmark.col, { - id = bookmark.extmark_id, - virt_lines = { { { text, "MarkVirtTextHL" } } }, - virt_lines_above = true, - }) - else - a.nvim_buf_del_extmark(bufnr, self.groups[group_nr].ns, bookmark.extmark_id) - - local opts = {} - if self.groups[group_nr].virt_text then - opts.virt_text = { { self.groups[group_nr].virt_text, "MarkVirtTextHL" } } - opts.virt_text_pos = "eol" - end - bookmark.extmark_id = - a.nvim_buf_set_extmark(bufnr, self.groups[group_nr].ns, bookmark.line - 1, bookmark.col, opts) - end + if vim.fn.has("nvim-0.6") ~= 1 then + error("virtual line annotations requires neovim 0.6 or higher") + end + + local bufnr = a.nvim_get_current_buf() + local pos = a.nvim_win_get_cursor(0) + + group_nr = group_nr or group_under_cursor(self.groups, bufnr, pos) + + if not group_nr then + return + end + + local bookmark = self.groups[group_nr].marks[bufnr][pos[1]] + + if not bookmark then + return + end + + local text = vim.fn.input("annotation: ") + + if text ~= "" then + a.nvim_buf_set_extmark(bufnr, self.groups[group_nr].ns, bookmark.line - 1, bookmark.col, { + id = bookmark.extmark_id, + virt_lines = { { { text, "MarkVirtTextHL" } } }, + virt_lines_above = true, + }) + else + a.nvim_buf_del_extmark(bufnr, self.groups[group_nr].ns, bookmark.extmark_id) + + local opts = {} + if self.groups[group_nr].virt_text then + opts.virt_text = { { self.groups[group_nr].virt_text, "MarkVirtTextHL" } } + opts.virt_text_pos = "eol" + end + bookmark.extmark_id = a.nvim_buf_set_extmark(bufnr, self.groups[group_nr].ns, bookmark.line - 1, bookmark.col, opts) + end end function Bookmarks:refresh() - local bufnr = a.nvim_get_current_buf() - - -- if we delete and undo really quickly, the extmark's position will be - -- the same, but the sign will no longer be there. so clear and restore all - -- signs. - - local buf_marks - local display_signs - utils.remove_buf_signs(bufnr, "BookmarkSigns") - for _, group in pairs(self.groups) do - buf_marks = group.marks[bufnr] - if buf_marks then - for _, mark in pairs(vim.tbl_values(buf_marks)) do - local line = a.nvim_buf_get_extmark_by_id(bufnr, group.ns, mark.extmark_id, {})[1] - - if line + 1 ~= mark.line then - buf_marks[line + 1] = mark - buf_marks[mark.line] = nil - buf_marks[line + 1].line = line + 1 - end - display_signs = utils.option_nil(self.opt.buf_signs[bufnr], self.opt.signs) - if display_signs and group.sign then - self:add_sign(bufnr, group.sign, line + 1, mark.sign_id) - end - end - end - end + local bufnr = a.nvim_get_current_buf() + + -- if we delete and undo really quickly, the extmark's position will be + -- the same, but the sign will no longer be there. so clear and restore all + -- signs. + + local buf_marks + local display_signs + utils.remove_buf_signs(bufnr, "BookmarkSigns") + for _, group in pairs(self.groups) do + buf_marks = group.marks[bufnr] + if buf_marks then + for _, mark in pairs(vim.tbl_values(buf_marks)) do + local line = a.nvim_buf_get_extmark_by_id(bufnr, group.ns, mark.extmark_id, {})[1] + + if line + 1 ~= mark.line then + buf_marks[line + 1] = mark + buf_marks[mark.line] = nil + buf_marks[line + 1].line = line + 1 + end + display_signs = utils.option_nil(self.opt.buf_signs[bufnr], self.opt.signs) + if display_signs and group.sign then + self:add_sign(bufnr, group.sign, line + 1, mark.sign_id) + end + end + end + end end function Bookmarks:get_group_list(group_nr) - local items = {} - if not group_nr or not self.groups[group_nr] then - return items - end - - for bufnr, buffer_marks in pairs(self.groups[group_nr].marks) do - for line, mark in pairs(buffer_marks) do - local text = a.nvim_buf_get_lines(bufnr, line - 1, line, true)[1] - local filename = vim.api.nvim_buf_get_name(bufnr) - table.insert(items, { - bufnr = bufnr, - lnum = line, - col = mark.col + 1, - group = group_nr, - line = vim.trim(text), - path = filename, - }) - end - end - - return items + local items = {} + if not group_nr or not self.groups[group_nr] then + return items + end + + for bufnr, buffer_marks in pairs(self.groups[group_nr].marks) do + for line, mark in pairs(buffer_marks) do + local text = a.nvim_buf_get_lines(bufnr, line - 1, line, true)[1] + local filename = vim.api.nvim_buf_get_name(bufnr) + table.insert(items, { + bufnr = bufnr, + lnum = line, + col = mark.col + 1, + group = group_nr, + line = vim.trim(text), + path = filename, + }) + end + end + + return items end function Bookmarks:get_all_list() - local items = {} - for group_nr, group in pairs(self.groups) do - for bufnr, buffer_marks in pairs(group.marks) do - for line, mark in pairs(buffer_marks) do - local text = a.nvim_buf_get_lines(bufnr, line - 1, line, true)[1] - local filename = vim.api.nvim_buf_get_name(bufnr) - - table.insert(items, { - bufnr = bufnr, - lnum = line, - col = mark.col + 1, - group = group_nr, - line = vim.trim(text), - path = filename, - }) - end - end - end - - return items + local items = {} + for group_nr, group in pairs(self.groups) do + for bufnr, buffer_marks in pairs(group.marks) do + for line, mark in pairs(buffer_marks) do + local text = a.nvim_buf_get_lines(bufnr, line - 1, line, true)[1] + local filename = vim.api.nvim_buf_get_name(bufnr) + + table.insert(items, { + bufnr = bufnr, + lnum = line, + col = mark.col + 1, + group = group_nr, + line = vim.trim(text), + path = filename, + }) + end + end + end + + return items end function Bookmarks:to_list(list_type, group_nr) - if not group_nr or not self.groups[group_nr] then - return - end - - list_type = list_type or "loclist" - local list_fn = utils.choose_list(list_type) - - local items = {} - for bufnr, buffer_marks in pairs(self.groups[group_nr].marks) do - for line, mark in pairs(buffer_marks) do - local text = a.nvim_buf_get_lines(bufnr, line - 1, line, true)[1] - table.insert(items, { bufnr = bufnr, lnum = line, col = mark.col + 1, text = text }) - end - end - - list_fn(items, "r") + if not group_nr or not self.groups[group_nr] then + return + end + + list_type = list_type or "loclist" + local list_fn = utils.choose_list(list_type) + + local items = {} + for bufnr, buffer_marks in pairs(self.groups[group_nr].marks) do + for line, mark in pairs(buffer_marks) do + local text = a.nvim_buf_get_lines(bufnr, line - 1, line, true)[1] + table.insert(items, { bufnr = bufnr, lnum = line, col = mark.col + 1, text = text }) + end + end + + list_fn(items, "r") end function Bookmarks:all_to_list(list_type) - list_type = list_type or "loclist" - local list_fn = utils.choose_list(list_type) - - local items = {} - for group_nr, group in pairs(self.groups) do - for bufnr, buffer_marks in pairs(group.marks) do - for line, mark in pairs(buffer_marks) do - local text = a.nvim_buf_get_lines(bufnr, line - 1, line, true)[1] - table.insert(items, { - bufnr = bufnr, - lnum = line, - col = mark.col + 1, - text = "bookmark group " .. group_nr .. ": " .. text, - }) - end - end - end - - list_fn(items, "r") + list_type = list_type or "loclist" + local list_fn = utils.choose_list(list_type) + + local items = {} + for group_nr, group in pairs(self.groups) do + for bufnr, buffer_marks in pairs(group.marks) do + for line, mark in pairs(buffer_marks) do + local text = a.nvim_buf_get_lines(bufnr, line - 1, line, true)[1] + table.insert(items, { + bufnr = bufnr, + lnum = line, + col = mark.col + 1, + text = "bookmark group " .. group_nr .. ": " .. text, + }) + end + end + end + + list_fn(items, "r") end function Bookmarks:add_sign(bufnr, text, line, id) - utils.add_sign(bufnr, text, line, id, "BookmarkSigns", self.priority) + utils.add_sign(bufnr, text, line, id, "BookmarkSigns", self.priority) end function Bookmarks.new() - return setmetatable({ - signs = { "!", "@", "#", "$", "%", "^", "&", "*", "(", [0] = ")" }, - virt_text = {}, - groups = {}, - prompt_annotate = {}, - opt = {}, - }, { __index = Bookmarks }) + return setmetatable({ + signs = { "!", "@", "#", "$", "%", "^", "&", "*", "(", [0] = ")" }, + virt_text = {}, + groups = {}, + prompt_annotate = {}, + opt = {}, + }, { __index = Bookmarks }) end return Bookmarks diff --git a/lua/telescope/_extensions/marks_nvim.lua b/lua/telescope/_extensions/marks_nvim.lua index bd45d85..402a30a 100644 --- a/lua/telescope/_extensions/marks_nvim.lua +++ b/lua/telescope/_extensions/marks_nvim.lua @@ -7,175 +7,175 @@ local conf = require("telescope.config").values local marks = require("marks") local list_marks_buf = function(opts) - opts = opts or {} - local results = marks.mark_state:get_buf_list() or {} - - local displayer = entry_display.create({ - separator = " ", - items = { - { width = 3 }, - { width = 10 }, - {}, - }, - }) - - local make_display = function(entry) - return displayer({ - { entry.mark, "TelescopeResultsIdentifier" }, - { entry.lnum }, - { entry.line, "String" }, - }) - end - - pickers - .new(opts, { - prompt_title = "Buffer Marks", - finder = finders.new_table({ - results = results, - entry_maker = function(entry) - entry.value = entry.mark - entry.ordinal = entry.line - entry.display = make_display - return entry - end, - }), - sorter = conf.generic_sorter(opts), - previewer = conf.grep_previewer(opts), - }) - :find() + opts = opts or {} + local results = marks.mark_state:get_buf_list() or {} + + local displayer = entry_display.create({ + separator = " ", + items = { + { width = 3 }, + { width = 10 }, + {}, + }, + }) + + local make_display = function(entry) + return displayer({ + { entry.mark, "TelescopeResultsIdentifier" }, + { entry.lnum }, + { entry.line, "String" }, + }) + end + + pickers + .new(opts, { + prompt_title = "Buffer Marks", + finder = finders.new_table({ + results = results, + entry_maker = function(entry) + entry.value = entry.mark + entry.ordinal = entry.line + entry.display = make_display + return entry + end, + }), + sorter = conf.generic_sorter(opts), + previewer = conf.grep_previewer(opts), + }) + :find() end local list_marks_all = function(opts) - opts = opts or {} - local conf_path = { path_display = opts.path_display or conf.path_display or {} } - local results = marks.mark_state:get_all_list() or {} - - local displayer = entry_display.create({ - separator = " ", - items = { - { width = 1 }, - { width = 5 }, - { width = 20 }, - {}, - }, - }) - - local make_display = function(entry) - return displayer({ - { entry.mark, "TelescopeResultsIdentifier" }, - { entry.lnum }, - { entry.line, "String" }, - { telescope_utils.transform_path(conf_path, entry.path) }, - }) - end - - pickers - .new(opts, { - prompt_title = "All Marks", - finder = finders.new_table({ - results = results, - entry_maker = function(entry) - entry.value = entry.mark - entry.ordinal = entry.line - entry.display = make_display - return entry - end, - }), - sorter = conf.generic_sorter(opts), - previewer = conf.grep_previewer(opts), - }) - :find() + opts = opts or {} + local conf_path = { path_display = opts.path_display or conf.path_display or {} } + local results = marks.mark_state:get_all_list() or {} + + local displayer = entry_display.create({ + separator = " ", + items = { + { width = 1 }, + { width = 5 }, + { width = 20 }, + {}, + }, + }) + + local make_display = function(entry) + return displayer({ + { entry.mark, "TelescopeResultsIdentifier" }, + { entry.lnum }, + { entry.line, "String" }, + { telescope_utils.transform_path(conf_path, entry.path) }, + }) + end + + pickers + .new(opts, { + prompt_title = "All Marks", + finder = finders.new_table({ + results = results, + entry_maker = function(entry) + entry.value = entry.mark + entry.ordinal = entry.line + entry.display = make_display + return entry + end, + }), + sorter = conf.generic_sorter(opts), + previewer = conf.grep_previewer(opts), + }) + :find() end local list_bookmarks_group = function(group, opts) - opts = opts or {} - local conf_path = { path_display = opts.path_display or conf.path_display or {} } - local results = marks.bookmark_state:get_group_list(group) or {} - - local displayer = entry_display.create({ - separator = " ", - items = { - { width = 1 }, - { width = 5 }, - { width = 20 }, - {}, - }, - }) - - local make_display = function(entry) - return displayer({ - { entry.group, "TelescopeResultsIdentifier" }, - { entry.lnum }, - { entry.line, "String" }, - { telescope_utils.transform_path(conf_path, entry.path) }, - }) - end - - pickers - .new(opts, { - prompt_title = "Bookmark " .. group, - finder = finders.new_table({ - results = results, - entry_maker = function(entry) - entry.value = entry.group - entry.ordinal = entry.line - entry.display = make_display - return entry - end, - }), - sorter = conf.generic_sorter(opts), - previewer = conf.grep_previewer(opts), - }) - :find() + opts = opts or {} + local conf_path = { path_display = opts.path_display or conf.path_display or {} } + local results = marks.bookmark_state:get_group_list(group) or {} + + local displayer = entry_display.create({ + separator = " ", + items = { + { width = 1 }, + { width = 5 }, + { width = 20 }, + {}, + }, + }) + + local make_display = function(entry) + return displayer({ + { entry.group, "TelescopeResultsIdentifier" }, + { entry.lnum }, + { entry.line, "String" }, + { telescope_utils.transform_path(conf_path, entry.path) }, + }) + end + + pickers + .new(opts, { + prompt_title = "Bookmark " .. group, + finder = finders.new_table({ + results = results, + entry_maker = function(entry) + entry.value = entry.group + entry.ordinal = entry.line + entry.display = make_display + return entry + end, + }), + sorter = conf.generic_sorter(opts), + previewer = conf.grep_previewer(opts), + }) + :find() end local list_bookmarks_all = function(opts) - opts = opts or {} - local conf_path = { path_display = opts.path_display or conf.path_display or {} } - local results = marks.bookmark_state:get_all_list() or {} - - local displayer = entry_display.create({ - separator = " ", - items = { - { width = 1 }, - { width = 5 }, - { width = 20 }, - {}, - }, - }) - - local make_display = function(entry) - return displayer({ - { entry.group, "TelescopeResultsIdentifier" }, - { entry.lnum }, - { entry.line, "String" }, - { telescope_utils.transform_path(conf_path, entry.path) }, - }) - end - - pickers - .new(opts, { - prompt_title = "All Bookmarks", - finder = finders.new_table({ - results = results, - entry_maker = function(entry) - entry.value = entry.group - entry.ordinal = entry.line - entry.display = make_display - return entry - end, - }), - sorter = conf.generic_sorter(opts), - previewer = conf.grep_previewer(opts), - }) - :find() + opts = opts or {} + local conf_path = { path_display = opts.path_display or conf.path_display or {} } + local results = marks.bookmark_state:get_all_list() or {} + + local displayer = entry_display.create({ + separator = " ", + items = { + { width = 1 }, + { width = 5 }, + { width = 20 }, + {}, + }, + }) + + local make_display = function(entry) + return displayer({ + { entry.group, "TelescopeResultsIdentifier" }, + { entry.lnum }, + { entry.line, "String" }, + { telescope_utils.transform_path(conf_path, entry.path) }, + }) + end + + pickers + .new(opts, { + prompt_title = "All Bookmarks", + finder = finders.new_table({ + results = results, + entry_maker = function(entry) + entry.value = entry.group + entry.ordinal = entry.line + entry.display = make_display + return entry + end, + }), + sorter = conf.generic_sorter(opts), + previewer = conf.grep_previewer(opts), + }) + :find() end return telescope.register_extension({ - exports = { - marks_list_buf = list_marks_buf, - marks_list_all = list_marks_all, - bookmarks_list_group = list_bookmarks_group, - bookmarks_list_all = list_bookmarks_all, - }, + exports = { + marks_list_buf = list_marks_buf, + marks_list_all = list_marks_all, + bookmarks_list_group = list_bookmarks_group, + bookmarks_list_all = list_bookmarks_all, + }, }) From 25bee34d47feb5401679c4fef3aa6569711dc0c5 Mon Sep 17 00:00:00 2001 From: Jed Date: Tue, 16 Jul 2024 00:18:38 +0200 Subject: [PATCH 11/11] udpdates path style for file path in list all marks --- README.md | 2 +- lua/telescope/_extensions/marks_nvim.lua | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 5b1f80b..943e72a 100644 --- a/README.md +++ b/README.md @@ -91,7 +91,7 @@ You simply need to call these methods in your mappings. ```lua require('telescope').extensions.marks_nvim.marks_list_buf(opts) --[[ List buffer marks ]] require('telescope').extensions.marks_nvim.marks_list_all(opts) --[[ List all marks ]] -require('telescope').extensions.marks_nvim.bookmarks_list_group(1, opts) --[[ List a bookmark group marks (takes the group number as argument) ]] +require('telescope').extensions.marks_nvim.bookmarks_list_group(1, opts) --[[ List a bookmark group marks (takes the group number as argument) ]] require('telescope').extensions.marks_nvim.bookmarks_list_all(opts) --[[ List all bookmarks marks ]] ``` diff --git a/lua/telescope/_extensions/marks_nvim.lua b/lua/telescope/_extensions/marks_nvim.lua index 402a30a..7165f5a 100644 --- a/lua/telescope/_extensions/marks_nvim.lua +++ b/lua/telescope/_extensions/marks_nvim.lua @@ -61,11 +61,12 @@ local list_marks_all = function(opts) }) local make_display = function(entry) + local file_path = telescope_utils.transform_path(conf_path, entry.path) return displayer({ { entry.mark, "TelescopeResultsIdentifier" }, { entry.lnum }, { entry.line, "String" }, - { telescope_utils.transform_path(conf_path, entry.path) }, + { file_path, "Comment" }, }) end