From 0a367e1f4a944b34c4ef6b8e2f533dd50f182078 Mon Sep 17 00:00:00 2001 From: Brian Ryall Date: Mon, 22 Nov 2021 10:43:34 -0500 Subject: [PATCH 1/6] initial telescope extension --- lua/telescope/_extensions/harpoon.lua | 11 +++++++ lua/telescope/_extensions/marks.lua | 45 +++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 lua/telescope/_extensions/harpoon.lua create mode 100644 lua/telescope/_extensions/marks.lua diff --git a/lua/telescope/_extensions/harpoon.lua b/lua/telescope/_extensions/harpoon.lua new file mode 100644 index 0000000..6de4b7a --- /dev/null +++ b/lua/telescope/_extensions/harpoon.lua @@ -0,0 +1,11 @@ +local has_telescope, telescope = pcall(require, "telescope") + +if not has_telescope then + error("harpoon.nvim requires nvim-telescope/telescope.nvim") +end + +return telescope.register_extension({ + exports = { + marks = require("telescope._extensions.marks"), + }, +}) diff --git a/lua/telescope/_extensions/marks.lua b/lua/telescope/_extensions/marks.lua new file mode 100644 index 0000000..4afaaa2 --- /dev/null +++ b/lua/telescope/_extensions/marks.lua @@ -0,0 +1,45 @@ +local finders = require("telescope.finders") +local make_entry = require("telescope.make_entry") +local pickers = require("telescope.pickers") +local previewers = require("telescope.previewers") +local sorters = require("telescope.sorters") +local harpoon = require("harpoon") +local entry_display = require("telescope.pickers.entry_display") + +return function(opts) + opts = opts or {} + + pickers.new(opts, { + prompt_title = "harpoon marks", + finder = finders.new_table({ + results = harpoon.get_mark_config().marks, + entry_maker = function(mark) + local displayer = entry_display.create({ + separator = " - ", + items = { + { width = 2 }, + { width = 50 }, + { remaining = true }, + }, + }) + local make_display = function(entry) + return displayer({ + tostring(entry.index), + mark.filename, + }) + end + local line = mark.filename .. ":" .. mark.row .. ":" .. mark.col + return { + value = mark, + ordinal = line, + display = make_display, + lnum = mark.row, + col = mark.col, + filename = mark.filename, + } + end, + }), + sorter = sorters.get_fuzzy_file(), + previewer = previewers.vim_buffer_cat.new({}), + }):find() +end From 7d364f8be7826e2cdd3a09d6c1f9d7a92b1ec5d5 Mon Sep 17 00:00:00 2001 From: Brian Ryall Date: Mon, 22 Nov 2021 12:24:51 -0500 Subject: [PATCH 2/6] add telescope mark delete --- lua/telescope/_extensions/marks.lua | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/lua/telescope/_extensions/marks.lua b/lua/telescope/_extensions/marks.lua index 4afaaa2..d9e1ea8 100644 --- a/lua/telescope/_extensions/marks.lua +++ b/lua/telescope/_extensions/marks.lua @@ -1,10 +1,23 @@ +local action_state = require("telescope.actions.state") +local entry_display = require("telescope.pickers.entry_display") local finders = require("telescope.finders") -local make_entry = require("telescope.make_entry") local pickers = require("telescope.pickers") local previewers = require("telescope.previewers") local sorters = require("telescope.sorters") local harpoon = require("harpoon") -local entry_display = require("telescope.pickers.entry_display") +local harpoon_mark = require("harpoon.mark") + +local delete_harpoon_mark = function(prompt_bufnr) + local confirmation = vim.fn.input(string.format("Delete current mark? [y/n]: ")) + if string.len(confirmation) == 0 or string.sub(string.lower(confirmation), 0, 1) ~= "y" then + print(string.format("Didn't delete mark")) + return + end + local current_picker = action_state.get_current_picker(prompt_bufnr) + current_picker:delete_selection(function(selection) + harpoon_mark.rm_file(selection.filename) + end) +end return function(opts) opts = opts or {} @@ -41,5 +54,10 @@ return function(opts) }), sorter = sorters.get_fuzzy_file(), previewer = previewers.vim_buffer_cat.new({}), + attach_mappings = function(_, map) + map("i", "", delete_harpoon_mark) + map("n", "", delete_harpoon_mark) + return true + end, }):find() end From 0b13c43c3e2f0b616fdb7832b6dcd21dd349c75d Mon Sep 17 00:00:00 2001 From: Brian Ryall Date: Mon, 22 Nov 2021 12:46:06 -0500 Subject: [PATCH 3/6] fix: formatting errors --- lua/harpoon/init.lua | 18 +++--------------- 1 file changed, 3 insertions(+), 15 deletions(-) diff --git a/lua/harpoon/init.lua b/lua/harpoon/init.lua index 86300b2..72c0af0 100644 --- a/lua/harpoon/init.lua +++ b/lua/harpoon/init.lua @@ -59,10 +59,7 @@ local function ensure_correct_config(config) log.trace("_ensure_correct_config()") local projects = config.projects if projects[vim.loop.cwd()] == nil then - log.debug( - "ensure_correct_config(): No config found for:", - vim.loop.cwd() - ) + log.debug("ensure_correct_config(): No config found for:", vim.loop.cwd()) projects[vim.loop.cwd()] = { mark = { marks = {}, @@ -80,10 +77,7 @@ local function ensure_correct_config(config) end if proj.term == nil then - log.debug( - "ensure_correct_config(): No terminal commands found for", - vim.loop.cwd() - ) + log.debug("ensure_correct_config(): No terminal commands found for", vim.loop.cwd()) proj.term = { cmds = {} } end @@ -159,13 +153,7 @@ M.setup = function(config) ["enter_on_sendcmd"] = false, ["excluded_filetypes"] = { "harpoon" }, }, - }, expand_dir( - c_config - ), expand_dir( - u_config - ), expand_dir( - config - )) + }, expand_dir(c_config), expand_dir(u_config), expand_dir(config)) -- There was this issue where the vim.loop.cwd() didn't have marks or term, but had -- an object for vim.loop.cwd() From c50c764c5d6672a58cdef537bef12b44a3b08023 Mon Sep 17 00:00:00 2001 From: Brian Ryall Date: Mon, 22 Nov 2021 15:56:40 -0500 Subject: [PATCH 4/6] initial design of moving marks --- lua/telescope/_extensions/marks.lua | 106 ++++++++++++++++++++-------- 1 file changed, 75 insertions(+), 31 deletions(-) diff --git a/lua/telescope/_extensions/marks.lua b/lua/telescope/_extensions/marks.lua index d9e1ea8..4f0fe28 100644 --- a/lua/telescope/_extensions/marks.lua +++ b/lua/telescope/_extensions/marks.lua @@ -1,3 +1,4 @@ +local action_set = require("telescope.actions.set") local action_state = require("telescope.actions.state") local entry_display = require("telescope.pickers.entry_display") local finders = require("telescope.finders") @@ -7,16 +8,82 @@ local sorters = require("telescope.sorters") local harpoon = require("harpoon") local harpoon_mark = require("harpoon.mark") +local function filter_empty_string(list) + local next = {} + for idx = 1, #list do + if list[idx].filename ~= "" then + table.insert(next, list[idx]) + end + end + + return next +end + +local generate_new_finder = function() + return finders.new_table({ + results = filter_empty_string(harpoon.get_mark_config().marks), + entry_maker = function(entry) + local displayer = entry_display.create({ + separator = " - ", + items = { + { width = 2 }, + { width = 50 }, + { remaining = true }, + }, + }) + local make_display = function(entry) + return displayer({ + tostring(entry.index), + entry.filename, + }) + end + local line = entry.filename .. ":" .. entry.row .. ":" .. entry.col + return { + value = entry, + ordinal = line, + display = make_display, + lnum = entry.row, + col = entry.col, + filename = entry.filename, + } + end, + }) +end + local delete_harpoon_mark = function(prompt_bufnr) local confirmation = vim.fn.input(string.format("Delete current mark? [y/n]: ")) if string.len(confirmation) == 0 or string.sub(string.lower(confirmation), 0, 1) ~= "y" then print(string.format("Didn't delete mark")) return end + local selection = action_state.get_selected_entry() + harpoon_mark.rm_file(selection.filename) local current_picker = action_state.get_current_picker(prompt_bufnr) - current_picker:delete_selection(function(selection) - harpoon_mark.rm_file(selection.filename) - end) + current_picker:refresh(generate_new_finder(), { reset_prompt = true }) +end + +local move_mark_up = function(prompt_bufnr) + local selection = action_state.get_selected_entry() + if harpoon_mark.get_length() == selection.index then + return + end + local mark_list = harpoon.get_mark_config().marks + table.remove(mark_list, selection.index) + table.insert(mark_list, selection.index + 1, selection.value) + local current_picker = action_state.get_current_picker(prompt_bufnr) + current_picker:refresh(generate_new_finder(), { reset_prompt = true }) +end + +local move_mark_down = function(prompt_bufnr) + local selection = action_state.get_selected_entry() + if selection.index == 1 then + return + end + local mark_list = harpoon.get_mark_config().marks + table.remove(mark_list, selection.index) + table.insert(mark_list, selection.index - 1, selection.value) + local current_picker = action_state.get_current_picker(prompt_bufnr) + current_picker:refresh(generate_new_finder(), { reset_prompt = true }) end return function(opts) @@ -24,39 +91,16 @@ return function(opts) pickers.new(opts, { prompt_title = "harpoon marks", - finder = finders.new_table({ - results = harpoon.get_mark_config().marks, - entry_maker = function(mark) - local displayer = entry_display.create({ - separator = " - ", - items = { - { width = 2 }, - { width = 50 }, - { remaining = true }, - }, - }) - local make_display = function(entry) - return displayer({ - tostring(entry.index), - mark.filename, - }) - end - local line = mark.filename .. ":" .. mark.row .. ":" .. mark.col - return { - value = mark, - ordinal = line, - display = make_display, - lnum = mark.row, - col = mark.col, - filename = mark.filename, - } - end, - }), + finder = generate_new_finder(), sorter = sorters.get_fuzzy_file(), previewer = previewers.vim_buffer_cat.new({}), attach_mappings = function(_, map) map("i", "", delete_harpoon_mark) map("n", "", delete_harpoon_mark) + map("i", "", move_mark_up) + map("n", "", move_mark_up) + map("i", "", move_mark_down) + map("n", "", move_mark_down) return true end, }):find() From 61fe69727b2afbfc27bc7d67f21c49aa72c5b273 Mon Sep 17 00:00:00 2001 From: Brian Ryall Date: Mon, 22 Nov 2021 16:13:22 -0500 Subject: [PATCH 5/6] fix: style errors --- lua/harpoon/init.lua | 10 ++++++++-- lua/telescope/_extensions/marks.lua | 9 +++++++-- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/lua/harpoon/init.lua b/lua/harpoon/init.lua index 72c0af0..2acc54a 100644 --- a/lua/harpoon/init.lua +++ b/lua/harpoon/init.lua @@ -59,7 +59,10 @@ local function ensure_correct_config(config) log.trace("_ensure_correct_config()") local projects = config.projects if projects[vim.loop.cwd()] == nil then - log.debug("ensure_correct_config(): No config found for:", vim.loop.cwd()) + log.debug( + "ensure_correct_config(): No config found for:", + vim.loop.cwd() + ) projects[vim.loop.cwd()] = { mark = { marks = {}, @@ -77,7 +80,10 @@ local function ensure_correct_config(config) end if proj.term == nil then - log.debug("ensure_correct_config(): No terminal commands found for", vim.loop.cwd()) + log.debug( + "ensure_correct_config(): No terminal commands found for", + vim.loop.cwd() + ) proj.term = { cmds = {} } end diff --git a/lua/telescope/_extensions/marks.lua b/lua/telescope/_extensions/marks.lua index 4f0fe28..1b7c44a 100644 --- a/lua/telescope/_extensions/marks.lua +++ b/lua/telescope/_extensions/marks.lua @@ -51,8 +51,13 @@ local generate_new_finder = function() end local delete_harpoon_mark = function(prompt_bufnr) - local confirmation = vim.fn.input(string.format("Delete current mark? [y/n]: ")) - if string.len(confirmation) == 0 or string.sub(string.lower(confirmation), 0, 1) ~= "y" then + local confirmation = vim.fn.input( + string.format("Delete current mark? [y/n]: ") + ) + if + string.len(confirmation) == 0 + or string.sub(string.lower(confirmation), 0, 1) ~= "y" + then print(string.format("Didn't delete mark")) return end From 8bfbb471ce90208dfa2a10c0c541bb15939f05bd Mon Sep 17 00:00:00 2001 From: Brian Ryall Date: Tue, 23 Nov 2021 09:16:38 -0500 Subject: [PATCH 6/6] display row and col in finder window --- lua/telescope/_extensions/marks.lua | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lua/telescope/_extensions/marks.lua b/lua/telescope/_extensions/marks.lua index 1b7c44a..f450037 100644 --- a/lua/telescope/_extensions/marks.lua +++ b/lua/telescope/_extensions/marks.lua @@ -23,6 +23,7 @@ local generate_new_finder = function() return finders.new_table({ results = filter_empty_string(harpoon.get_mark_config().marks), entry_maker = function(entry) + local line = entry.filename .. ":" .. entry.row .. ":" .. entry.col local displayer = entry_display.create({ separator = " - ", items = { @@ -34,7 +35,7 @@ local generate_new_finder = function() local make_display = function(entry) return displayer({ tostring(entry.index), - entry.filename, + line, }) end local line = entry.filename .. ":" .. entry.row .. ":" .. entry.col