From 323027715f2a2799534e363bb64b5c111bb0f62e Mon Sep 17 00:00:00 2001 From: Rishabh Dwivedi Date: Mon, 30 Aug 2021 02:28:47 +0530 Subject: [PATCH 1/5] harpoon dynamic commands for projects --- lua/harpoon/cmd-ui.lua | 146 +++++++++++++++++++++++++++ lua/harpoon/term.lua | 58 +++++++++++ lua/harpoon/test/manage_cmd_spec.lua | 141 ++++++++++++++++++++++++++ 3 files changed, 345 insertions(+) create mode 100644 lua/harpoon/cmd-ui.lua create mode 100644 lua/harpoon/test/manage_cmd_spec.lua diff --git a/lua/harpoon/cmd-ui.lua b/lua/harpoon/cmd-ui.lua new file mode 100644 index 0000000..d650cfe --- /dev/null +++ b/lua/harpoon/cmd-ui.lua @@ -0,0 +1,146 @@ +local harpoon = require("harpoon") +local popup = require("popup") +local log = require("harpoon.dev").log +local term = require("harpoon.term") + +local M = {} + +Harpoon_cmd_win_id = nil +Harpoon_cmd_bufh = nil + +local function close_menu(force_save) + force_save = force_save or false + local global_config = harpoon.get_global_settings() + + if global_config.save_on_toggle or force_save then + require("harpoon.cmd-ui").on_menu_save() + end + + vim.api.nvim_win_close(Harpoon_cmd_win_id, true) + + Harpoon_cmd_win_id = nil + Harpoon_cmd_bufh = nil +end + + +local function create_window() + log.trace("_create_window()") + local config = harpoon.get_menu_config() + local width = config.width or 60 + local height = config.height or 10 + local borderchars = config.borderchars + or { "─", "│", "─", "│", "╭", "╮", "╯", "╰" } + local bufnr = vim.api.nvim_create_buf(false, false) + + local Harpoon_cmd_win_id, win = popup.create(bufnr, { + title = "Harpoon Commands", + highlight = "HarpoonWindow", + line = math.floor(((vim.o.lines - height) / 2) - 1), + col = math.floor((vim.o.columns - width) / 2), + minwidth = width, + minheight = height, + borderchars = borderchars, + }) + + vim.api.nvim_win_set_option( + win.border.win_id, + "winhl", + "Normal:HarpoonBorder" + ) + + return { + bufnr = bufnr, + win_id = Harpoon_cmd_win_id, + } +end + +local function is_white_space(str) + local white_space_chars = { + [" "] = true, + ["\t"] = true, + ["\r"] = true, + ["\f"] = true, + ["\v"] = true + } + for i = 1, string.len(str) do + if white_space_chars[string.sub(str, i, i)] == nil then + return false + end + end + return true +end + +local function get_menu_items() + log.trace("_get_menu_items()") + local lines = vim.api.nvim_buf_get_lines(Harpoon_cmd_bufh, 0, -1, true) + local indices = {} + + for _, line in pairs(lines) do + if not is_white_space(line) then + table.insert(indices, line) + end + end + + return indices +end + +M.toggle_quick_menu = function() + log.trace("toggle_quick_menu()") + if Harpoon_cmd_win_id ~= nil and vim.api.nvim_win_is_valid(Harpoon_cmd_win_id) then + close_menu() + return + end + + local win_info = create_window() + local contents = {} + local global_config = harpoon.get_global_settings() + + Harpoon_cmd_win_id = win_info.win_id + Harpoon_cmd_bufh = win_info.bufnr + + for idx, cmd in pairs(harpoon.get_term_config().cmds) do + contents[idx] = cmd + end + + vim.api.nvim_win_set_option(Harpoon_cmd_win_id, "number", true) + vim.api.nvim_buf_set_name(Harpoon_cmd_bufh, "harpoon-cmd-menu") + vim.api.nvim_buf_set_lines(Harpoon_cmd_bufh, 0, #contents, false, contents) + vim.api.nvim_buf_set_option(Harpoon_cmd_bufh, "filetype", "harpoon") + vim.api.nvim_buf_set_option(Harpoon_cmd_bufh, "buftype", "acwrite") + vim.api.nvim_buf_set_option(Harpoon_cmd_bufh, "bufhidden", "delete") + -- TODO: maybe vim.fn.input() can be used to implement some select_menu_item + -- vim.api.nvim_buf_set_keymap( + -- Harpoon_cmd_bufh, + -- "n", + -- "", + -- ":lua require('harpoon.cmd-ui').select_menu_item()", + -- {} + -- ) + vim.cmd( + string.format( + "autocmd BufWriteCmd :lua require('harpoon.cmd-ui').on_menu_save()", + Harpoon_cmd_bufh + ) + ) + if global_config.save_on_change then + vim.cmd( + string.format( + "autocmd TextChanged,TextChangedI :lua require('harpoon.cmd-ui').on_menu_save()", + Harpoon_cmd_bufh + ) + ) + end + vim.cmd( + string.format( + "autocmd BufModifiedSet set nomodified", + Harpoon_cmd_bufh + ) + ) +end + +M.on_menu_save = function() + log.trace("on_menu_save()") + term.set_cmd_list(get_menu_items()) +end + +return M diff --git a/lua/harpoon/term.lua b/lua/harpoon/term.lua index 0c63699..d34c20c 100644 --- a/lua/harpoon/term.lua +++ b/lua/harpoon/term.lua @@ -51,6 +51,17 @@ local function find_terminal(args) return term_handle end +local function get_first_empty_slot() + log.trace("_get_first_empty_slot()") + for idx, cmd in pairs(harpoon.get_term_config().cmds) do + if cmd == "" then + return idx + end + end + return M.get_length() + 1; +end + + M.gotoTerminal = function(idx) log.trace("gotoTerminal(): Terminal:", idx) local term_handle = find_terminal(idx) @@ -80,4 +91,51 @@ M.clear_all = function() terminals = {} end +M.get_length = function() + log.trace("_get_length()") + return table.maxn(harpoon.get_term_config().cmds) +end + +M.valid_index = function(idx) + if idx == nil or idx > M.get_length() or idx <= 0 then + return false + end + return true +end + +M.emit_changed = function() + log.trace("_emit_changed()") + if harpoon.get_global_settings().save_on_change then + harpoon.save() + end +end + +M.add_cmd = function(cmd) + log.trace("add_cmd()") + local found_idx = get_first_empty_slot(); + harpoon.get_term_config().cmds[found_idx] = cmd + M.emit_changed() +end + +M.rm_cmd = function(idx) + log.trace("rm_cmd()") + if not M.valid_index(idx) then + log.debug("rm_cmd(): no cmd exists for index", idx) + return + end + table.remove(harpoon.get_term_config().cmds, idx) + M.emit_changed() +end + +M.set_cmd_list = function(new_list) + log.trace("set_cmd_list(): New list:", new_list) + for k in pairs(harpoon.get_term_config().cmds) do + harpoon.get_term_config().cmds[k] = nil + end + for k, v in pairs(new_list) do + harpoon.get_term_config().cmds[k] = v + end + M.emit_changed() +end + return M diff --git a/lua/harpoon/test/manage_cmd_spec.lua b/lua/harpoon/test/manage_cmd_spec.lua new file mode 100644 index 0000000..f0ab1ff --- /dev/null +++ b/lua/harpoon/test/manage_cmd_spec.lua @@ -0,0 +1,141 @@ +local harpoon = require("harpoon") +local term = require("harpoon.term") + +local function assert_table_equals(tbl1, tbl2) + if #tbl1 ~= #tbl2 then + assert(false, ""..#tbl1.." != "..#tbl2) + end + for i = 1, #tbl1 do + if tbl1[i] ~= tbl2[i] then + assert.equals(tbl1[i], tbl2[i]) + end + end +end + +describe("basic functionalities", function() + local emitted + local cmds + + before_each(function() + emitted = false + cmds = {} + harpoon.get_term_config = function() + return { + cmds = cmds + } + end + term.emit_changed = function() + emitted = true + end + end) + + it("add_cmd for empty", function() + term.add_cmd("cmake ..") + local expected_result = { + "cmake .." + } + assert_table_equals(harpoon.get_term_config().cmds, expected_result) + assert.equals(emitted, true) + end) + + it("add_cmd for non_empty", function() + term.add_cmd("cmake ..") + term.add_cmd("make") + term.add_cmd("ninja") + local expected_result = { + "cmake ..", + "make", + "ninja", + } + assert_table_equals(harpoon.get_term_config().cmds, expected_result) + assert.equals(emitted, true) + end) + + it("rm_cmd: removing a valid element", function() + term.add_cmd("cmake ..") + term.add_cmd("make") + term.add_cmd("ninja") + term.rm_cmd(2) + local expected_result = { + "cmake ..", + "ninja", + } + assert_table_equals(harpoon.get_term_config().cmds, expected_result) + assert.equals(emitted, true) + end) + + it("rm_cmd: remove first element", function() + term.add_cmd("cmake ..") + term.add_cmd("make") + term.add_cmd("ninja") + term.rm_cmd(1) + local expected_result = { + "make", + "ninja", + } + assert_table_equals(harpoon.get_term_config().cmds, expected_result) + assert.equals(emitted, true) + end) + + it("rm_cmd: remove last element", function() + term.add_cmd("cmake ..") + term.add_cmd("make") + term.add_cmd("ninja") + term.rm_cmd(3) + local expected_result = { + "cmake ..", + "make", + } + assert_table_equals(harpoon.get_term_config().cmds, expected_result) + assert.equals(emitted, true) + end) + + it("rm_cmd: trying to remove invalid element", function() + term.add_cmd("cmake ..") + term.add_cmd("make") + term.add_cmd("ninja") + term.rm_cmd(5) + local expected_result = { + "cmake ..", + "make", + "ninja", + } + assert_table_equals(harpoon.get_term_config().cmds, expected_result) + assert.equals(emitted, true) + term.rm_cmd(0) + assert_table_equals(harpoon.get_term_config().cmds, expected_result) + term.rm_cmd(-1) + assert_table_equals(harpoon.get_term_config().cmds, expected_result) + end) + + it("get_length", function() + term.add_cmd("cmake ..") + term.add_cmd("make") + term.add_cmd("ninja") + assert.equals(term.get_length(), 3) + end) + + it("valid_index", function() + term.add_cmd("cmake ..") + term.add_cmd("make") + term.add_cmd("ninja") + assert(term.valid_index(1)) + assert(term.valid_index(2)) + assert(term.valid_index(3)) + assert(not term.valid_index(0)) + assert(not term.valid_index(-1)) + assert(not term.valid_index(4)) + end) + + it("set_cmd_list", function() + term.add_cmd("cmake ..") + term.add_cmd("make") + term.add_cmd("ninja") + term.set_cmd_list({"make uninstall", "make install",}) + local expected_result = { + "make uninstall", + "make install", + } + assert_table_equals(expected_result, harpoon.get_term_config().cmds) + end) +end) From 730dc0c0d22496ded668a823f85f514651fb8bc5 Mon Sep 17 00:00:00 2001 From: Rishabh Dwivedi Date: Mon, 30 Aug 2021 04:01:03 +0530 Subject: [PATCH 2/5] updated formatting --- lua/harpoon/cmd-ui.lua | 36 +++++++++++++++------------- lua/harpoon/term.lua | 21 ++++++++-------- lua/harpoon/test/manage_cmd_spec.lua | 12 +++++----- 3 files changed, 35 insertions(+), 34 deletions(-) diff --git a/lua/harpoon/cmd-ui.lua b/lua/harpoon/cmd-ui.lua index d650cfe..d2497a5 100644 --- a/lua/harpoon/cmd-ui.lua +++ b/lua/harpoon/cmd-ui.lua @@ -22,7 +22,6 @@ local function close_menu(force_save) Harpoon_cmd_bufh = nil end - local function create_window() log.trace("_create_window()") local config = harpoon.get_menu_config() @@ -55,19 +54,19 @@ local function create_window() end local function is_white_space(str) - local white_space_chars = { - [" "] = true, - ["\t"] = true, - ["\r"] = true, - ["\f"] = true, - ["\v"] = true - } - for i = 1, string.len(str) do - if white_space_chars[string.sub(str, i, i)] == nil then - return false + local white_space_chars = { + [" "] = true, + ["\t"] = true, + ["\r"] = true, + ["\f"] = true, + ["\v"] = true, + } + for i = 1, string.len(str) do + if white_space_chars[string.sub(str, i, i)] == nil then + return false + end end - end - return true + return true end local function get_menu_items() @@ -76,9 +75,9 @@ local function get_menu_items() local indices = {} for _, line in pairs(lines) do - if not is_white_space(line) then - table.insert(indices, line) - end + if not is_white_space(line) then + table.insert(indices, line) + end end return indices @@ -86,7 +85,10 @@ end M.toggle_quick_menu = function() log.trace("toggle_quick_menu()") - if Harpoon_cmd_win_id ~= nil and vim.api.nvim_win_is_valid(Harpoon_cmd_win_id) then + if + Harpoon_cmd_win_id ~= nil + and vim.api.nvim_win_is_valid(Harpoon_cmd_win_id) + then close_menu() return end diff --git a/lua/harpoon/term.lua b/lua/harpoon/term.lua index d34c20c..768991c 100644 --- a/lua/harpoon/term.lua +++ b/lua/harpoon/term.lua @@ -58,10 +58,9 @@ local function get_first_empty_slot() return idx end end - return M.get_length() + 1; + return M.get_length() + 1 end - M.gotoTerminal = function(idx) log.trace("gotoTerminal(): Terminal:", idx) local term_handle = find_terminal(idx) @@ -112,7 +111,7 @@ end M.add_cmd = function(cmd) log.trace("add_cmd()") - local found_idx = get_first_empty_slot(); + local found_idx = get_first_empty_slot() harpoon.get_term_config().cmds[found_idx] = cmd M.emit_changed() end @@ -128,14 +127,14 @@ M.rm_cmd = function(idx) end M.set_cmd_list = function(new_list) - log.trace("set_cmd_list(): New list:", new_list) - for k in pairs(harpoon.get_term_config().cmds) do - harpoon.get_term_config().cmds[k] = nil - end - for k, v in pairs(new_list) do - harpoon.get_term_config().cmds[k] = v - end - M.emit_changed() + log.trace("set_cmd_list(): New list:", new_list) + for k in pairs(harpoon.get_term_config().cmds) do + harpoon.get_term_config().cmds[k] = nil + end + for k, v in pairs(new_list) do + harpoon.get_term_config().cmds[k] = v + end + M.emit_changed() end return M diff --git a/lua/harpoon/test/manage_cmd_spec.lua b/lua/harpoon/test/manage_cmd_spec.lua index f0ab1ff..f63ad51 100644 --- a/lua/harpoon/test/manage_cmd_spec.lua +++ b/lua/harpoon/test/manage_cmd_spec.lua @@ -3,7 +3,7 @@ local term = require("harpoon.term") local function assert_table_equals(tbl1, tbl2) if #tbl1 ~= #tbl2 then - assert(false, ""..#tbl1.." != "..#tbl2) + assert(false, "" .. #tbl1 .. " != " .. #tbl2) end for i = 1, #tbl1 do if tbl1[i] ~= tbl2[i] then @@ -21,7 +21,7 @@ describe("basic functionalities", function() cmds = {} harpoon.get_term_config = function() return { - cmds = cmds + cmds = cmds, } end term.emit_changed = function() @@ -32,7 +32,7 @@ describe("basic functionalities", function() it("add_cmd for empty", function() term.add_cmd("cmake ..") local expected_result = { - "cmake .." + "cmake ..", } assert_table_equals(harpoon.get_term_config().cmds, expected_result) assert.equals(emitted, true) @@ -131,10 +131,10 @@ describe("basic functionalities", function() term.add_cmd("cmake ..") term.add_cmd("make") term.add_cmd("ninja") - term.set_cmd_list({"make uninstall", "make install",}) + term.set_cmd_list({ "make uninstall", "make install" }) local expected_result = { - "make uninstall", - "make install", + "make uninstall", + "make install", } assert_table_equals(expected_result, harpoon.get_term_config().cmds) end) From bfdeafc913cbf46d49b99d79d0e1f5ad566adab4 Mon Sep 17 00:00:00 2001 From: Rishabh Dwivedi Date: Thu, 2 Sep 2021 00:01:26 +0530 Subject: [PATCH 3/5] simplified whitespace and breadbutter for logging --- lua/harpoon/cmd-ui.lua | 18 +++--------------- 1 file changed, 3 insertions(+), 15 deletions(-) diff --git a/lua/harpoon/cmd-ui.lua b/lua/harpoon/cmd-ui.lua index d2497a5..f2252db 100644 --- a/lua/harpoon/cmd-ui.lua +++ b/lua/harpoon/cmd-ui.lua @@ -54,19 +54,7 @@ local function create_window() end local function is_white_space(str) - local white_space_chars = { - [" "] = true, - ["\t"] = true, - ["\r"] = true, - ["\f"] = true, - ["\v"] = true, - } - for i = 1, string.len(str) do - if white_space_chars[string.sub(str, i, i)] == nil then - return false - end - end - return true + return str:gsub("%s", "") == "" end local function get_menu_items() @@ -84,7 +72,7 @@ local function get_menu_items() end M.toggle_quick_menu = function() - log.trace("toggle_quick_menu()") + log.trace("cmd-ui#toggle_quick_menu()") if Harpoon_cmd_win_id ~= nil and vim.api.nvim_win_is_valid(Harpoon_cmd_win_id) @@ -141,7 +129,7 @@ M.toggle_quick_menu = function() end M.on_menu_save = function() - log.trace("on_menu_save()") + log.trace("cmd-ui#on_menu_save()") term.set_cmd_list(get_menu_items()) end From 732be7f4be8d41bec337140bb7cad28d96cc3abb Mon Sep 17 00:00:00 2001 From: Rishabh Dwivedi Date: Sat, 4 Sep 2021 20:51:58 +0530 Subject: [PATCH 4/5] updated luacheckrc and formatting --- .luacheckrc | 2 + lua/harpoon/cmd-ui.lua | 104 ++++++++++++++++++++--------------------- 2 files changed, 54 insertions(+), 52 deletions(-) diff --git a/.luacheckrc b/.luacheckrc index 4de34b6..b87a48f 100644 --- a/.luacheckrc +++ b/.luacheckrc @@ -6,5 +6,7 @@ globals = { "HarpoonConfig", "Harpoon_bufh", "Harpoon_win_id", + "Harpoon_cmd_win_id", + "Harpoon_cmd_bufh", } read_globals = { "vim" } diff --git a/lua/harpoon/cmd-ui.lua b/lua/harpoon/cmd-ui.lua index f2252db..9ac816a 100644 --- a/lua/harpoon/cmd-ui.lua +++ b/lua/harpoon/cmd-ui.lua @@ -28,7 +28,7 @@ local function create_window() local width = config.width or 60 local height = config.height or 10 local borderchars = config.borderchars - or { "─", "│", "─", "│", "╭", "╮", "╯", "╰" } + or { "─", "│", "─", "│", "╭", "╮", "╯", "╰" } local bufnr = vim.api.nvim_create_buf(false, false) local Harpoon_cmd_win_id, win = popup.create(bufnr, { @@ -42,9 +42,9 @@ local function create_window() }) vim.api.nvim_win_set_option( - win.border.win_id, - "winhl", - "Normal:HarpoonBorder" + win.border.win_id, + "winhl", + "Normal:HarpoonBorder" ) return { @@ -54,7 +54,7 @@ local function create_window() end local function is_white_space(str) - return str:gsub("%s", "") == "" + return str:gsub("%s", "") == "" end local function get_menu_items() @@ -76,61 +76,61 @@ M.toggle_quick_menu = function() if Harpoon_cmd_win_id ~= nil and vim.api.nvim_win_is_valid(Harpoon_cmd_win_id) - then - close_menu() - return - end + then + close_menu() + return + end - local win_info = create_window() - local contents = {} - local global_config = harpoon.get_global_settings() + local win_info = create_window() + local contents = {} + local global_config = harpoon.get_global_settings() - Harpoon_cmd_win_id = win_info.win_id - Harpoon_cmd_bufh = win_info.bufnr + Harpoon_cmd_win_id = win_info.win_id + Harpoon_cmd_bufh = win_info.bufnr - for idx, cmd in pairs(harpoon.get_term_config().cmds) do - contents[idx] = cmd - end + for idx, cmd in pairs(harpoon.get_term_config().cmds) do + contents[idx] = cmd + end - vim.api.nvim_win_set_option(Harpoon_cmd_win_id, "number", true) - vim.api.nvim_buf_set_name(Harpoon_cmd_bufh, "harpoon-cmd-menu") - vim.api.nvim_buf_set_lines(Harpoon_cmd_bufh, 0, #contents, false, contents) - vim.api.nvim_buf_set_option(Harpoon_cmd_bufh, "filetype", "harpoon") - vim.api.nvim_buf_set_option(Harpoon_cmd_bufh, "buftype", "acwrite") - vim.api.nvim_buf_set_option(Harpoon_cmd_bufh, "bufhidden", "delete") - -- TODO: maybe vim.fn.input() can be used to implement some select_menu_item - -- vim.api.nvim_buf_set_keymap( - -- Harpoon_cmd_bufh, - -- "n", - -- "", - -- ":lua require('harpoon.cmd-ui').select_menu_item()", - -- {} - -- ) - vim.cmd( - string.format( - "autocmd BufWriteCmd :lua require('harpoon.cmd-ui').on_menu_save()", - Harpoon_cmd_bufh - ) - ) - if global_config.save_on_change then + vim.api.nvim_win_set_option(Harpoon_cmd_win_id, "number", true) + vim.api.nvim_buf_set_name(Harpoon_cmd_bufh, "harpoon-cmd-menu") + vim.api.nvim_buf_set_lines(Harpoon_cmd_bufh, 0, #contents, false, contents) + vim.api.nvim_buf_set_option(Harpoon_cmd_bufh, "filetype", "harpoon") + vim.api.nvim_buf_set_option(Harpoon_cmd_bufh, "buftype", "acwrite") + vim.api.nvim_buf_set_option(Harpoon_cmd_bufh, "bufhidden", "delete") + -- TODO: maybe vim.fn.input() can be used to implement some select_menu_item + -- vim.api.nvim_buf_set_keymap( + -- Harpoon_cmd_bufh, + -- "n", + -- "", + -- ":lua require('harpoon.cmd-ui').select_menu_item()", + -- {} + -- ) vim.cmd( + string.format( + "autocmd BufWriteCmd :lua require('harpoon.cmd-ui').on_menu_save()", + Harpoon_cmd_bufh + ) + ) + if global_config.save_on_change then + vim.cmd( string.format( - "autocmd TextChanged,TextChangedI :lua require('harpoon.cmd-ui').on_menu_save()", - Harpoon_cmd_bufh + "autocmd TextChanged,TextChangedI :lua require('harpoon.cmd-ui').on_menu_save()", + Harpoon_cmd_bufh ) + ) + end + vim.cmd( + string.format( + "autocmd BufModifiedSet set nomodified", + Harpoon_cmd_bufh + ) ) end - vim.cmd( - string.format( - "autocmd BufModifiedSet set nomodified", - Harpoon_cmd_bufh - ) - ) -end -M.on_menu_save = function() - log.trace("cmd-ui#on_menu_save()") - term.set_cmd_list(get_menu_items()) -end + M.on_menu_save = function() + log.trace("cmd-ui#on_menu_save()") + term.set_cmd_list(get_menu_items()) + end -return M + return M From 3123335be9ac40a8088ed04d77959f8c0cece24a Mon Sep 17 00:00:00 2001 From: Rishabh Dwivedi Date: Sun, 5 Sep 2021 22:18:49 +0530 Subject: [PATCH 5/5] cmd-ui: fixed formatting for stylua --- lua/harpoon/cmd-ui.lua | 102 ++++++++++++++++++++--------------------- 1 file changed, 51 insertions(+), 51 deletions(-) diff --git a/lua/harpoon/cmd-ui.lua b/lua/harpoon/cmd-ui.lua index 9ac816a..7a03cd0 100644 --- a/lua/harpoon/cmd-ui.lua +++ b/lua/harpoon/cmd-ui.lua @@ -28,7 +28,7 @@ local function create_window() local width = config.width or 60 local height = config.height or 10 local borderchars = config.borderchars - or { "─", "│", "─", "│", "╭", "╮", "╯", "╰" } + or { "─", "│", "─", "│", "╭", "╮", "╯", "╰" } local bufnr = vim.api.nvim_create_buf(false, false) local Harpoon_cmd_win_id, win = popup.create(bufnr, { @@ -42,9 +42,9 @@ local function create_window() }) vim.api.nvim_win_set_option( - win.border.win_id, - "winhl", - "Normal:HarpoonBorder" + win.border.win_id, + "winhl", + "Normal:HarpoonBorder" ) return { @@ -76,61 +76,61 @@ M.toggle_quick_menu = function() if Harpoon_cmd_win_id ~= nil and vim.api.nvim_win_is_valid(Harpoon_cmd_win_id) - then - close_menu() - return - end + then + close_menu() + return + end - local win_info = create_window() - local contents = {} - local global_config = harpoon.get_global_settings() + local win_info = create_window() + local contents = {} + local global_config = harpoon.get_global_settings() - Harpoon_cmd_win_id = win_info.win_id - Harpoon_cmd_bufh = win_info.bufnr + Harpoon_cmd_win_id = win_info.win_id + Harpoon_cmd_bufh = win_info.bufnr - for idx, cmd in pairs(harpoon.get_term_config().cmds) do - contents[idx] = cmd - end + for idx, cmd in pairs(harpoon.get_term_config().cmds) do + contents[idx] = cmd + end - vim.api.nvim_win_set_option(Harpoon_cmd_win_id, "number", true) - vim.api.nvim_buf_set_name(Harpoon_cmd_bufh, "harpoon-cmd-menu") - vim.api.nvim_buf_set_lines(Harpoon_cmd_bufh, 0, #contents, false, contents) - vim.api.nvim_buf_set_option(Harpoon_cmd_bufh, "filetype", "harpoon") - vim.api.nvim_buf_set_option(Harpoon_cmd_bufh, "buftype", "acwrite") - vim.api.nvim_buf_set_option(Harpoon_cmd_bufh, "bufhidden", "delete") - -- TODO: maybe vim.fn.input() can be used to implement some select_menu_item - -- vim.api.nvim_buf_set_keymap( - -- Harpoon_cmd_bufh, - -- "n", - -- "", - -- ":lua require('harpoon.cmd-ui').select_menu_item()", - -- {} - -- ) - vim.cmd( + vim.api.nvim_win_set_option(Harpoon_cmd_win_id, "number", true) + vim.api.nvim_buf_set_name(Harpoon_cmd_bufh, "harpoon-cmd-menu") + vim.api.nvim_buf_set_lines(Harpoon_cmd_bufh, 0, #contents, false, contents) + vim.api.nvim_buf_set_option(Harpoon_cmd_bufh, "filetype", "harpoon") + vim.api.nvim_buf_set_option(Harpoon_cmd_bufh, "buftype", "acwrite") + vim.api.nvim_buf_set_option(Harpoon_cmd_bufh, "bufhidden", "delete") + -- TODO: maybe vim.fn.input() can be used to implement some select_menu_item + -- vim.api.nvim_buf_set_keymap( + -- Harpoon_cmd_bufh, + -- "n", + -- "", + -- ":lua require('harpoon.cmd-ui').select_menu_item()", + -- {} + -- ) + vim.cmd( string.format( - "autocmd BufWriteCmd :lua require('harpoon.cmd-ui').on_menu_save()", - Harpoon_cmd_bufh - ) - ) - if global_config.save_on_change then - vim.cmd( - string.format( - "autocmd TextChanged,TextChangedI :lua require('harpoon.cmd-ui').on_menu_save()", + "autocmd BufWriteCmd :lua require('harpoon.cmd-ui').on_menu_save()", Harpoon_cmd_bufh - ) - ) - end + ) + ) + if global_config.save_on_change then vim.cmd( + string.format( + "autocmd TextChanged,TextChangedI :lua require('harpoon.cmd-ui').on_menu_save()", + Harpoon_cmd_bufh + ) + ) + end + vim.cmd( string.format( - "autocmd BufModifiedSet set nomodified", - Harpoon_cmd_bufh + "autocmd BufModifiedSet set nomodified", + Harpoon_cmd_bufh ) - ) - end + ) +end - M.on_menu_save = function() - log.trace("cmd-ui#on_menu_save()") - term.set_cmd_list(get_menu_items()) - end +M.on_menu_save = function() + log.trace("cmd-ui#on_menu_save()") + term.set_cmd_list(get_menu_items()) +end - return M +return M