From 65ce5063845f84754c6f74530f1658047100c917 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 | 9 +++++++++ lua/hop/defaults.lua | 2 ++ lua/hop/init.lua | 2 +- lua/hop/jump_target.lua | 4 ++-- lua/hop/window.lua | 40 ++++++++++++++++++++-------------------- 5 files changed, 34 insertions(+), 23 deletions(-) diff --git a/doc/hop.txt b/doc/hop.txt index e82cd3d3..5cde9797 100644 --- a/doc/hop.txt +++ b/doc/hop.txt @@ -806,6 +806,15 @@ below. Defaults:~ `multi_windows = false` +`excluded_filetypes` + Skip hinting windows with the excluded filetypes. Those windows to check + filetypes are collected only when you enable `multi_windows` or execute + `MW`-commands. This option is useful to skip the windows which are only + for displaying something buf not for editing something. + + 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 b82c87cd..319a7679 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 513fc43f..9111a283 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 289b04ad..621a5580 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 @@ -64,28 +64,28 @@ 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 - - -- 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 - local bctx = nil - for _, buffer_ctx in ipairs(all_ctxs) do - if b == buffer_ctx.hbuf then - bctx = buffer_ctx.contexts - break + 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 + local bctx = nil + for _, buffer_ctx in ipairs(all_ctxs) do + if b == buffer_ctx.hbuf then + bctx = buffer_ctx.contexts + break + end end - end - if bctx then - bctx[#bctx + 1] = window_context(w, vim.api.nvim_win_get_cursor(w)) - else - all_ctxs[#all_ctxs + 1] = { - hbuf = b, - contexts = { window_context(w, vim.api.nvim_win_get_cursor(w)) } - } + if bctx then + bctx[#bctx + 1] = window_context(w, vim.api.nvim_win_get_cursor(w)) + else + all_ctxs[#all_ctxs + 1] = { + hbuf = b, + contexts = { window_context(w, vim.api.nvim_win_get_cursor(w)) } + } + end end - end end