Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support to switch next or prev entry #48

Merged
merged 2 commits into from
Aug 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lua/LspUI/_meta.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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_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
Expand Down
2 changes: 2 additions & 0 deletions lua/LspUI/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ local default_pos_keybind_config = {
jump_vsplit = "sv",
jump_tab = "t",
toggle_fold = "<Cr>",
next_entry = "J",
prev_entry = "K",
quit = "q",
hide_main = "<leader>h",
fold_all = "w",
Expand Down
92 changes: 92 additions & 0 deletions lua/LspUI/pos_abstract.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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_entry,
"",
{
nowait = true,
noremap = true,
callback = function()
M.action.next_entry()
end,
}
)

api.nvim_buf_set_keymap(
M.secondary_view_buffer(),
"n",
config.options.pos_keybind.secondary.prev_entry,
"",
{
nowait = true,
noremap = true,
callback = function()
M.action.prev_entry()
end,
}
)

api.nvim_buf_set_keymap(
M.secondary_view_buffer(),
"n",
Expand Down Expand Up @@ -1017,6 +1045,64 @@ local action_toggle_fold = function()
M.secondary_view_render()
end

-- next file entry
local action_next_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_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())
Expand Down Expand Up @@ -1124,6 +1210,12 @@ M.action = {
toggle_fold = function()
action_toggle_fold()
end,
next_entry = function()
action_next_entry()
end,
prev_entry = function()
action_prev_entry()
end,
enter_main = function()
action_enter_main()
end,
Expand Down
Loading