From 039a4ef4c568d2a030939e3828983440b72c6b25 Mon Sep 17 00:00:00 2001 From: jinzhongjia Date: Sat, 10 Aug 2024 21:52:04 +0800 Subject: [PATCH 1/2] feat: support to switch next or prev file entry --- lua/LspUI/_meta.lua | 2 +- lua/LspUI/config.lua | 2 + lua/LspUI/pos_abstract.lua | 92 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 95 insertions(+), 1 deletion(-) diff --git a/lua/LspUI/_meta.lua b/lua/LspUI/_meta.lua index 53cbedf..8aed282 100644 --- a/lua/LspUI/_meta.lua +++ b/lua/LspUI/_meta.lua @@ -35,7 +35,7 @@ --- @field filter { whitelist: string[]?, blacklist:string[]? }? the filter of blacklist and whitelist, should be filetype list -- this is just for some keybind like definition, type definition, declaration, reference, implementation ---- @alias LspUI_pos_keybind_config { secondary: { jump: string?, jump_tab: string?, jump_split: string?, jump_vsplit: string?, toggle_fold:string?, quit:string?, hide_main:string?, fold_all:string?, expand_all:string?, enter: string? }?, main: { back: string?, hide_secondary: string? }? , transparency: number? } +--- @alias LspUI_pos_keybind_config { secondary: { jump: string?, jump_tab: string?, jump_split: string?, jump_vsplit: string?, toggle_fold: string?, next_file_entry: string?, prev_file_entry: string?, quit:string?, hide_main:string?, fold_all:string?, expand_all:string?, enter: string? }?, main: { back: string?, hide_secondary: string? }? , transparency: number? } -- TODO: change this -- TODO: replace above LspUI_pos_keybind_config with LspUI_pos_config diff --git a/lua/LspUI/config.lua b/lua/LspUI/config.lua index a75157e..d72bd36 100644 --- a/lua/LspUI/config.lua +++ b/lua/LspUI/config.lua @@ -108,6 +108,8 @@ local default_pos_keybind_config = { jump_vsplit = "sv", jump_tab = "t", toggle_fold = "", + next_file_entry = "J", + prev_file_entry = "K", quit = "q", hide_main = "h", fold_all = "w", diff --git a/lua/LspUI/pos_abstract.lua b/lua/LspUI/pos_abstract.lua index caf9efd..67d3636 100644 --- a/lua/LspUI/pos_abstract.lua +++ b/lua/LspUI/pos_abstract.lua @@ -336,6 +336,34 @@ local secondary_view_keybind = function() } ) + api.nvim_buf_set_keymap( + M.secondary_view_buffer(), + "n", + config.options.pos_keybind.secondary.next_file_entry, + "", + { + nowait = true, + noremap = true, + callback = function() + M.action.next_file_entry() + end, + } + ) + + api.nvim_buf_set_keymap( + M.secondary_view_buffer(), + "n", + config.options.pos_keybind.secondary.prev_file_entry, + "", + { + nowait = true, + noremap = true, + callback = function() + M.action.prev_file_entry() + end, + } + ) + api.nvim_buf_set_keymap( M.secondary_view_buffer(), "n", @@ -1017,6 +1045,64 @@ local action_toggle_fold = function() M.secondary_view_render() end +-- next file entry +local action_next_file_entry = function() + -- when current_item not exist, just return + if not current_item.uri then + return + end + local current_uri = current_item.uri + local line = 1 + local has_seen_current = false + for uri, val in pairs(M.datas()) do + if has_seen_current then + api.nvim_win_set_cursor(M.secondary_view_window(), { line, 0 }) + break + end + line = line + 1 + if not val.fold then + line = line + #val.range + end + + if uri == current_uri then + has_seen_current = true + end + end +end + +-- prev file entry +local action_prev_file_entry = function() + -- when current_item not exist, just return + if not current_item.uri then + return + end + local current_uri = current_item.uri + local line = 1 + local prev_range_nums = 0 + for uri, val in pairs(M.datas()) do + if uri == current_uri then + line = line - prev_range_nums + if line > 1 then + line = line - 1 + end + api.nvim_win_set_cursor(M.secondary_view_window(), { line, 0 }) + break + end + + line = line + 1 + if not val.fold then + prev_range_nums = #val.range + line = line + prev_range_nums + else + prev_range_nums = 0 + end + end + + print(string.format("prev file is %d", line)) + + -- api.nvim_win_set_cursor(M.secondary_view_window(), { line, 0 }) +end + local action_enter_main = function() if not M.main_view_hide() then api.nvim_set_current_win(M.main_view_window()) @@ -1124,6 +1210,12 @@ M.action = { toggle_fold = function() action_toggle_fold() end, + next_file_entry = function() + action_next_file_entry() + end, + prev_file_entry = function() + action_prev_file_entry() + end, enter_main = function() action_enter_main() end, From 97d38c14e78d56aa68f61b6e5d44286f361a6719 Mon Sep 17 00:00:00 2001 From: jinzhongjia Date: Sat, 10 Aug 2024 21:58:50 +0800 Subject: [PATCH 2/2] rename method --- lua/LspUI/_meta.lua | 2 +- lua/LspUI/config.lua | 4 ++-- lua/LspUI/pos_abstract.lua | 20 ++++++++++---------- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/lua/LspUI/_meta.lua b/lua/LspUI/_meta.lua index 8aed282..3f918a2 100644 --- a/lua/LspUI/_meta.lua +++ b/lua/LspUI/_meta.lua @@ -35,7 +35,7 @@ --- @field filter { whitelist: string[]?, blacklist:string[]? }? the filter of blacklist and whitelist, should be filetype list -- this is just for some keybind like definition, type definition, declaration, reference, implementation ---- @alias LspUI_pos_keybind_config { secondary: { jump: string?, jump_tab: string?, jump_split: string?, jump_vsplit: string?, toggle_fold: string?, next_file_entry: string?, prev_file_entry: string?, quit:string?, hide_main:string?, fold_all:string?, expand_all:string?, enter: string? }?, main: { back: string?, hide_secondary: string? }? , transparency: number? } +--- @alias LspUI_pos_keybind_config { secondary: { jump: string?, jump_tab: string?, jump_split: string?, jump_vsplit: string?, toggle_fold: string?, next_entry: string?, prev_entry: string?, quit:string?, hide_main:string?, fold_all:string?, expand_all:string?, enter: string? }?, main: { back: string?, hide_secondary: string? }? , transparency: number? } -- TODO: change this -- TODO: replace above LspUI_pos_keybind_config with LspUI_pos_config diff --git a/lua/LspUI/config.lua b/lua/LspUI/config.lua index d72bd36..90cd641 100644 --- a/lua/LspUI/config.lua +++ b/lua/LspUI/config.lua @@ -108,8 +108,8 @@ local default_pos_keybind_config = { jump_vsplit = "sv", jump_tab = "t", toggle_fold = "", - next_file_entry = "J", - prev_file_entry = "K", + next_entry = "J", + prev_entry = "K", quit = "q", hide_main = "h", fold_all = "w", diff --git a/lua/LspUI/pos_abstract.lua b/lua/LspUI/pos_abstract.lua index 67d3636..a89ca30 100644 --- a/lua/LspUI/pos_abstract.lua +++ b/lua/LspUI/pos_abstract.lua @@ -339,13 +339,13 @@ local secondary_view_keybind = function() api.nvim_buf_set_keymap( M.secondary_view_buffer(), "n", - config.options.pos_keybind.secondary.next_file_entry, + config.options.pos_keybind.secondary.next_entry, "", { nowait = true, noremap = true, callback = function() - M.action.next_file_entry() + M.action.next_entry() end, } ) @@ -353,13 +353,13 @@ local secondary_view_keybind = function() api.nvim_buf_set_keymap( M.secondary_view_buffer(), "n", - config.options.pos_keybind.secondary.prev_file_entry, + config.options.pos_keybind.secondary.prev_entry, "", { nowait = true, noremap = true, callback = function() - M.action.prev_file_entry() + M.action.prev_entry() end, } ) @@ -1046,7 +1046,7 @@ local action_toggle_fold = function() end -- next file entry -local action_next_file_entry = function() +local action_next_entry = function() -- when current_item not exist, just return if not current_item.uri then return @@ -1071,7 +1071,7 @@ local action_next_file_entry = function() end -- prev file entry -local action_prev_file_entry = function() +local action_prev_entry = function() -- when current_item not exist, just return if not current_item.uri then return @@ -1210,11 +1210,11 @@ M.action = { toggle_fold = function() action_toggle_fold() end, - next_file_entry = function() - action_next_file_entry() + next_entry = function() + action_next_entry() end, - prev_file_entry = function() - action_prev_file_entry() + prev_entry = function() + action_prev_entry() end, enter_main = function() action_enter_main()