From 8593232cc3c56571159870b846fc87e00f359ba3 Mon Sep 17 00:00:00 2001 From: ThePrimeagen Date: Tue, 11 May 2021 11:31:12 -0600 Subject: [PATCH] feat(sort of done): Vimeonteuhonteuhon teu --- README.md | 8 ++++++ lua/harpoon/init.lua | 2 ++ lua/harpoon/mark.lua | 14 ++++++++++ lua/harpoon/ui.lua | 61 ++++++++++++++++++++++++++++++++++---------- plugin/mark.vim | 3 +++ 5 files changed, 75 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 83de38c..a7ee4f8 100644 --- a/README.md +++ b/README.md @@ -135,6 +135,8 @@ require("harpoon").setup({ global_settings = { save_on_toggle = false, save_on_change = true, + nav_last_visited = false, + nav_first_in_list = false, }, ... your other configs ... }) @@ -146,6 +148,12 @@ require("harpoon").setup({ enable this option (on by default) harpoon will not save any changes to your file. It is very unreliable to save your harpoon on exit (at least that is what I have found). +* `nav_last_visited` will open up the last file you visited in harpoon upon + VimEnter. If you are using the git-worktree plugin you could listen to the + tree switch and execute `require("harpoon.ui").nav_on_open()` +* `nav_first_in_list` same as `nav_last_visited` except it will open the first + item in the list of marks upon VimEnter or if you call + `require("harpoon.ui").nav_on_open()` #### Preconfigured Terminal Commands These are project specific commands that you wish to execute on the regular. diff --git a/lua/harpoon/init.lua b/lua/harpoon/init.lua index 69bf98c..16fea7f 100644 --- a/lua/harpoon/init.lua +++ b/lua/harpoon/init.lua @@ -154,6 +154,8 @@ M.setup = function(config) local complete_config = merge_tables({ projects = {}, global_settings = { + ["nav_last_visited"] = false, + ["nav_first_in_list"] = false, ["save_on_toggle"] = false, ["save_on_change"] = true, }, diff --git a/lua/harpoon/mark.lua b/lua/harpoon/mark.lua index fbbb7c5..2f7d8dd 100644 --- a/lua/harpoon/mark.lua +++ b/lua/harpoon/mark.lua @@ -140,6 +140,20 @@ M.get_index_of = function(item) return nil end +M.set_last = function() + log.trace("set_last()") + local mark_idx = M.get_index_of(get_buf_name()) + if not M.valid_index(mark_idx) then + return + end + + harpoon.get_mark_config().last = mark_idx +end + +M.get_last = function() + return harpoon.get_mark_config().last +end + M.status = function() log.trace("status()") local idx = M.get_index_of(get_buf_name()) diff --git a/lua/harpoon/ui.lua b/lua/harpoon/ui.lua index afd0059..02fd17d 100644 --- a/lua/harpoon/ui.lua +++ b/lua/harpoon/ui.lua @@ -72,6 +72,22 @@ local function get_menu_items() return indices end +local function navigate(idx) + local mark = Marked.get_marked_file(idx) + local buf_id = vim.fn.bufnr(mark.filename, true) + local set_row = not vim.api.nvim_buf_is_loaded(buf_id) + + vim.api.nvim_set_current_buf(buf_id) + if set_row and mark.row and mark.col then + vim.cmd(string.format(":call cursor(%d, %d)", mark.row, mark.col)) + log.debug(string.format( + "nav_file(): Setting cursor to row: %d, col: %d", + mark.row, + mark.col + )) + end +end + M.toggle_quick_menu = function() log.trace("toggle_quick_menu()") if Harpoon_win_id ~= nil and vim.api.nvim_win_is_valid(Harpoon_win_id) then @@ -126,6 +142,37 @@ M.on_menu_save = function() Marked.set_mark_list(get_menu_items()) end +M.nav_on_open = function() + local global_config = harpoon.get_global_settings() + if global_config.nav_last_visited then + M.nav_last_visited() + return + end + + if global_config.nav_first_in_list then + M.nav_first_in_list() + return + end +end + +M.nav_first_in_list = function() + local first = Marked.get_index_of(1) + if not first then + return + end + + navigate(first) +end + +M.nav_last_visited = function() + local last_mark = Marked.get_last() + if not last_mark then + return + end + + navigate(last_mark) +end + M.nav_file = function(id) log.trace("nav_file(): Navigating to", id) local idx = Marked.get_index_of(id) @@ -134,19 +181,7 @@ M.nav_file = function(id) return end - local mark = Marked.get_marked_file(idx) - local buf_id = vim.fn.bufnr(mark.filename, true) - local set_row = not vim.api.nvim_buf_is_loaded(buf_id) - - vim.api.nvim_set_current_buf(buf_id) - if set_row and mark.row and mark.col then - vim.cmd(string.format(":call cursor(%d, %d)", mark.row, mark.col)) - log.debug(string.format( - "nav_file(): Setting cursor to row: %d, col: %d", - mark.row, - mark.col - )) - end + navigate(idx) end function M.location_window(options) diff --git a/plugin/mark.vim b/plugin/mark.vim index 23b09a5..a964bb5 100644 --- a/plugin/mark.vim +++ b/plugin/mark.vim @@ -1,4 +1,7 @@ +" Should we do a lua version? +" Challenge they must require harpoon augroup THE_PRIMEAGEN_HARPOON autocmd! autocmd BufLeave,VimLeave * :lua require('harpoon.mark').store_offset() + autocmd VimEnter * :lua require('harpoon.ui').nav_on_open() augroup END