From 4d5085592eb97be99b76e191947b92e0058d067a Mon Sep 17 00:00:00 2001 From: yehuohan Date: Mon, 29 Aug 2022 00:24:37 +0800 Subject: [PATCH] Add excluded_filetypes to avoid invalid windows and buffers --- doc/hop.txt | 6 ++++++ lua/hop/defaults.lua | 2 ++ lua/hop/init.lua | 2 +- lua/hop/jump_target.lua | 4 ++-- lua/hop/window.lua | 10 ++++++---- 5 files changed, 17 insertions(+), 7 deletions(-) diff --git a/doc/hop.txt b/doc/hop.txt index bb18080d..8ce2acd7 100644 --- a/doc/hop.txt +++ b/doc/hop.txt @@ -806,6 +806,12 @@ below. Defaults:~ `multi_windows = false` +`excluded_filetypes` + Set the excluded filetypes to skip hint. + + Defaults:~ + `excluded_filetypes = { }` + ============================================================================== EXTENSION *hop-extension* diff --git a/lua/hop/defaults.lua b/lua/hop/defaults.lua index 32842f05..f8fd7a91 100644 --- a/lua/hop/defaults.lua +++ b/lua/hop/defaults.lua @@ -13,5 +13,7 @@ M.uppercase_labels = false M.multi_windows = false M.hint_position = require'hop.hint'.HintPosition.BEGIN M.hint_offset = 0 +M.excluded_filetypes = { } return M + diff --git a/lua/hop/init.lua b/lua/hop/init.lua index b4a1bda1..acdb1b64 100644 --- a/lua/hop/init.lua +++ b/lua/hop/init.lua @@ -47,7 +47,7 @@ local function create_hint_state(opts) local hint_state = {} -- get all window's context and buffer list - hint_state.all_ctxs = window.get_window_context(opts.multi_windows) + hint_state.all_ctxs = window.get_window_context(opts.multi_windows, opts.excluded_filetypes) hint_state.buf_list = {} for _, bctx in ipairs(hint_state.all_ctxs) do hint_state.buf_list[#hint_state.buf_list + 1] = bctx.hbuf diff --git a/lua/hop/jump_target.lua b/lua/hop/jump_target.lua index 2a6bf3e4..19c3d156 100644 --- a/lua/hop/jump_target.lua +++ b/lua/hop/jump_target.lua @@ -184,7 +184,7 @@ end function M.jump_targets_by_scanning_lines(regex) return function(opts) -- get the window context; this is used to know which part of the visible buffer is to hint - local all_ctxs = window.get_window_context(opts.multi_windows) + local all_ctxs = window.get_window_context(opts.multi_windows, opts.excluded_filetypes) local jump_targets = {} local indirect_jump_targets = {} @@ -289,7 +289,7 @@ end -- Jump target generator for regex applied only on the cursor line. function M.jump_targets_for_current_line(regex) return function(opts) - local context = window.get_window_context(false)[1].contexts[1] + local context = window.get_window_context(false, opts.excluded_filetypes)[1].contexts[1] local line_n = context.cursor_pos[1] local line = vim.api.nvim_buf_get_lines(0, line_n - 1, line_n, false) local jump_targets = {} diff --git a/lua/hop/window.lua b/lua/hop/window.lua index 54c1c222..108c212f 100644 --- a/lua/hop/window.lua +++ b/lua/hop/window.lua @@ -47,7 +47,7 @@ end -- }, -- ... -- } -function M.get_window_context(multi_windows) +function M.get_window_context(multi_windows, excluded_filetypes) local all_ctxs = {} -- Generate contexts of windows @@ -65,11 +65,12 @@ function M.get_window_context(multi_windows) end for _, w in ipairs(vim.api.nvim_tabpage_list_wins(0)) do - local b = vim.api.nvim_win_get_buf(w) - if w ~= cur_hwin then + if w ~= cur_hwin and vim.api.nvim_win_is_valid(w) then + local b = vim.api.nvim_win_get_buf(w) + if not vim.tbl_contains(excluded_filetypes, vim.api.nvim_buf_get_option(b, 'filetype')) then -- check duplicated buffers; the way this is done is by accessing all the already known contexts and checking that - -- the buffer we are accessing is already present in; if it is, we then append the window context to that buffer + -- the buffer we are accessing is already present in; i f it is, we then append the window context to that buffer local bctx = nil for _, buffer_ctx in ipairs(all_ctxs) do if b == buffer_ctx.hbuf then @@ -87,6 +88,7 @@ function M.get_window_context(multi_windows) } end + end end end