From 3bd96f4eee6cd203036540383bd9a4db926a7d6e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AE=80=20=D0=9A4mi=20=E5=AE=80=20/=20Michel=20TRUONG?= Date: Thu, 20 Apr 2023 22:21:15 +0200 Subject: [PATCH 1/9] Update init.lua folding new options --- lua/nvim-navbuddy/init.lua | 37 +++++++++++++++++++++---------------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/lua/nvim-navbuddy/init.lua b/lua/nvim-navbuddy/init.lua index 6bd5c82..00c1c69 100644 --- a/lua/nvim-navbuddy/init.lua +++ b/lua/nvim-navbuddy/init.lua @@ -64,50 +64,36 @@ local config = { mappings = { [""] = actions.close, ["q"] = actions.close, - ["j"] = actions.next_sibling, ["k"] = actions.previous_sibling, - ["h"] = actions.parent, ["l"] = actions.children, ["0"] = actions.root, - ["v"] = actions.visual_name, ["V"] = actions.visual_scope, - ["y"] = actions.yank_name, ["Y"] = actions.yank_scope, - ["i"] = actions.insert_name, ["I"] = actions.insert_scope, - ["a"] = actions.append_name, ["A"] = actions.append_scope, - ["r"] = actions.rename, - ["d"] = actions.delete, - ["f"] = actions.fold_create, ["F"] = actions.fold_delete, - ["c"] = actions.comment, - [""] = actions.select, ["o"] = actions.select, - ["J"] = actions.move_down, ["K"] = actions.move_up, - ["s"] = actions.toggle_preview, - ["t"] = actions.telescope({ layout_strategy = "horizontal", layout_config = { height = 0.60, width = 0.60, prompt_position = "top", - preview_width = 0.50 + preview_width = 0.50, }, }), }, @@ -119,7 +105,11 @@ local config = { follow_node = true, highlight = true, reorient = "smart", - scrolloff = nil + scrolloff = nil, + }, + folding = { + foldmethod_auto_set_manual = true, + leading_spaces = 2, -- to avoid the monstruosity err msg "at least two spaces before inline comment" }, } @@ -352,6 +342,21 @@ function M.setup(user_config) if user_config.source_buffer ~= nil then config.source_buffer = vim.tbl_deep_extend("keep", user_config.source_buffer, config.source_buffer) end + + if + user_config.folding.foldmethod_auto_set_manual ~= nil + and type(user_config.folding.foldmethod_auto_set_manual) == "boolean" + then + config.folding.foldmethod_auto_set_manual = user_config.folding.foldmethod_auto_set_manual + end + + if + user_config.folding.leading_spaces ~= nil + and type(user_config.folding.leading_spaces) == "number" + and user_config.folding.leading_spaces >= 0 + then + config.folding.leading_spaces = user_config.folding.leading_spaces + end end if config.lsp.auto_attach == true then From 57d48f19e434380217ba6c40e13824530be03255 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AE=80=20=D0=9A4mi=20=E5=AE=80=20/=20Michel=20TRUONG?= Date: Thu, 20 Apr 2023 23:04:36 +0200 Subject: [PATCH 2/9] Update actions.lua MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit • foldmethod auto set to manual • add leading space to avoid ugly error message • clear trailing spaces added by navbuddy when unfolding (i.e. other any trailing spaces stay in place) • avoid duplicate folding --- lua/nvim-navbuddy/actions.lua | 278 +++++++++++++++++++++------------- 1 file changed, 171 insertions(+), 107 deletions(-) diff --git a/lua/nvim-navbuddy/actions.lua b/lua/nvim-navbuddy/actions.lua index 77f6b5a..94dc38c 100644 --- a/lua/nvim-navbuddy/actions.lua +++ b/lua/nvim-navbuddy/actions.lua @@ -1,8 +1,13 @@ +local USER_FOLDMETHOD = vim.o.foldmethod +local USER_FOLDMARKER = vim.o.foldmarker +local USER_FOLDMARKER_O, USER_FOLDMARKER_C = USER_FOLDMARKER:match("([^,]+),([^,]+)") +local api = vim.api + local actions = {} function actions.close(display) display:close() - vim.api.nvim_win_set_cursor(display.for_win, display.start_cursor) + api.nvim_win_set_cursor(display.for_win, display.start_cursor) end function actions.next_sibling(display) @@ -82,9 +87,9 @@ function actions.select(display) display:close() -- to push location to jumplist: -- move display to start_cursor, set mark ', then move to new location - vim.api.nvim_win_set_cursor(display.for_win, display.start_cursor) - vim.api.nvim_command("normal! m'") - vim.api.nvim_win_set_cursor( + api.nvim_win_set_cursor(display.for_win, display.start_cursor) + api.nvim_command("normal! m'") + api.nvim_win_set_cursor( display.for_win, { display.focus_node.name_range["start"].line, display.focus_node.name_range["start"].character } ) @@ -92,61 +97,61 @@ function actions.select(display) if display.config.source_buffer.reorient == "smart" then local total_lines = display.focus_node.scope["end"].line - display.focus_node.scope["start"].line + 1 - if total_lines >= vim.api.nvim_win_get_height(display.for_win) then - vim.api.nvim_command("normal! zt") + if total_lines >= api.nvim_win_get_height(display.for_win) then + api.nvim_command("normal! zt") else local mid_line = bit.rshift(display.focus_node.scope["start"].line + display.focus_node.scope["end"].line, 1) - vim.api.nvim_win_set_cursor(display.for_win, { mid_line, 0 }) - vim.api.nvim_command("normal! zz") - vim.api.nvim_win_set_cursor( + api.nvim_win_set_cursor(display.for_win, { mid_line, 0 }) + api.nvim_command("normal! zz") + api.nvim_win_set_cursor( display.for_win, { display.focus_node.name_range["start"].line, display.focus_node.name_range["start"].character } ) end elseif display.config.source_buffer.reorient == "mid" then - vim.api.nvim_command("normal! zz") + api.nvim_command("normal! zz") elseif display.config.source_buffer.reorient == "top" then - vim.api.nvim_command("normal! zt") + api.nvim_command("normal! zt") end end function actions.yank_name(display) display:close() - vim.api.nvim_win_set_cursor( + api.nvim_win_set_cursor( display.for_win, { display.focus_node.name_range["start"].line, display.focus_node.name_range["start"].character } ) - vim.api.nvim_command("normal! v") - vim.api.nvim_win_set_cursor( + api.nvim_command("normal! v") + api.nvim_win_set_cursor( display.for_win, { display.focus_node.name_range["end"].line, display.focus_node.name_range["end"].character - 1 } ) - vim.api.nvim_command('normal! "+y') + api.nvim_command('normal! "+y') end function actions.yank_scope(display) display:close() - vim.api.nvim_win_set_cursor( + api.nvim_win_set_cursor( display.for_win, { display.focus_node.scope["start"].line, display.focus_node.scope["start"].character } ) - vim.api.nvim_command("normal! v") - vim.api.nvim_win_set_cursor( + api.nvim_command("normal! v") + api.nvim_win_set_cursor( display.for_win, { display.focus_node.scope["end"].line, display.focus_node.scope["end"].character - 1 } ) - vim.api.nvim_command('normal! "+y') + api.nvim_command('normal! "+y') end function actions.visual_name(display) display:close() - vim.api.nvim_win_set_cursor( + api.nvim_win_set_cursor( display.for_win, { display.focus_node.name_range["start"].line, display.focus_node.name_range["start"].character } ) - vim.api.nvim_command("normal! v") - vim.api.nvim_win_set_cursor( + api.nvim_command("normal! v") + api.nvim_win_set_cursor( display.for_win, { display.focus_node.name_range["end"].line, display.focus_node.name_range["end"].character - 1 } ) @@ -154,12 +159,12 @@ end function actions.visual_scope(display) display:close() - vim.api.nvim_win_set_cursor( + api.nvim_win_set_cursor( display.for_win, { display.focus_node.scope["start"].line, display.focus_node.scope["start"].character } ) - vim.api.nvim_command("normal! v") - vim.api.nvim_win_set_cursor( + api.nvim_command("normal! v") + api.nvim_win_set_cursor( display.for_win, { display.focus_node.scope["end"].line, display.focus_node.scope["end"].character - 1 } ) @@ -167,36 +172,36 @@ end function actions.insert_name(display) display:close() - vim.api.nvim_win_set_cursor( + api.nvim_win_set_cursor( display.for_win, { display.focus_node.name_range["start"].line, display.focus_node.name_range["start"].character } ) - vim.api.nvim_feedkeys("i", "n", false) + api.nvim_feedkeys("i", "n", false) end function actions.insert_scope(display) display:close() - vim.api.nvim_win_set_cursor( + api.nvim_win_set_cursor( display.for_win, { display.focus_node.scope["start"].line, display.focus_node.scope["start"].character } ) - vim.api.nvim_feedkeys("i", "n", false) + api.nvim_feedkeys("i", "n", false) end function actions.append_name(display) display:close() - vim.api.nvim_win_set_cursor( + api.nvim_win_set_cursor( display.for_win, { display.focus_node.name_range["end"].line, display.focus_node.name_range["end"].character - 1 } ) - vim.api.nvim_feedkeys("a", "n", false) + api.nvim_feedkeys("a", "n", false) end function actions.append_scope(display) display:close() if string.len( - vim.api.nvim_buf_get_lines( + api.nvim_buf_get_lines( display.for_buf, display.focus_node.scope["end"].line - 1, display.focus_node.scope["end"].line, @@ -204,17 +209,17 @@ function actions.append_scope(display) )[1] ) == display.focus_node.scope["end"].character then - vim.api.nvim_win_set_cursor( + api.nvim_win_set_cursor( display.for_win, { display.focus_node.scope["end"].line, display.focus_node.scope["end"].character } ) else - vim.api.nvim_win_set_cursor( + api.nvim_win_set_cursor( display.for_win, { display.focus_node.scope["end"].line, display.focus_node.scope["end"].character - 1 } ) end - vim.api.nvim_feedkeys("a", "n", false) + api.nvim_feedkeys("a", "n", false) end function actions.rename(display) @@ -224,51 +229,93 @@ end function actions.delete(display) actions.visual_scope(display) - vim.api.nvim_command("normal! d") + api.nvim_command("normal! d") end -function actions.fold_create(display) - if vim.o.foldmethod ~= "manual" then +local is_manual_foldmethod = function(display) + if display.config.folding.foldmethod_auto_set_manual == true then + vim.o.foldmethod = "manual" + return true + end + if USER_FOLDMETHOD ~= "manual" then vim.notify("Fold create action works only when foldmethod is 'manual'", vim.log.levels.ERROR) - return + return false end + return true +end - display.state.leaving_window_for_action = true - vim.api.nvim_set_current_win(display.for_win) - vim.api.nvim_win_set_cursor( - display.for_win, - { display.focus_node.scope["start"].line, display.focus_node.scope["start"].character } - ) - vim.api.nvim_command("normal! v") - vim.api.nvim_win_set_cursor( - display.for_win, - { display.focus_node.scope["end"].line, display.focus_node.scope["end"].character - 1 } - ) - vim.api.nvim_command("normal! zf") - vim.api.nvim_set_current_win(display.mid.winid) +local reinit_foldmethod = function() + vim.o.foldmethod = USER_FOLDMETHOD +end + +local comment_trailing_space = function(display) + api.nvim_set_current_line(api.nvim_get_current_line() .. string.rep(" ", display.config.folding.leading_spaces)) +end + +local clean_trailing_space = function(str, trs) + api.nvim_set_current_line(string.sub(str, 1, string.len(str) - trs)) +end + +local mid_win_id = function(display) + api.nvim_set_current_win(display.mid.winid) display.state.leaving_window_for_action = false end -function actions.fold_delete(display) - if vim.o.foldmethod ~= "manual" then - vim.notify("Fold delete action works only when foldmethod is 'manual'", vim.log.levels.ERROR) +local apply_comment_fold_marker = function(display, cmd) + local start_line = display.focus_node.scope["start"].line + local end_line = display.focus_node.scope["end"].line + local trailing_spaces = 0 + + display.state.leaving_window_for_action = true + api.nvim_set_current_win(display.for_win) + api.nvim_win_set_cursor(display.for_win, { start_line, display.focus_node.scope["start"].character }) + + if + cmd == "normal! zf" + and (api.nvim_get_current_line():find(USER_FOLDMARKER_O) or api.nvim_get_current_line():find(USER_FOLDMARKER_C)) + then + mid_win_id(display) return end - display.state.leaving_window_for_action = true - vim.api.nvim_set_current_win(display.for_win) - vim.api.nvim_win_set_cursor( - display.for_win, - { display.focus_node.scope["start"].line, display.focus_node.scope["start"].character } - ) - vim.api.nvim_command("normal! v") - vim.api.nvim_win_set_cursor( - display.for_win, - { display.focus_node.scope["end"].line, display.focus_node.scope["end"].character - 1 } - ) - pcall(vim.api.nvim_command, "normal! zd") - vim.api.nvim_set_current_win(display.mid.winid) - display.state.leaving_window_for_action = false + comment_trailing_space(display) + api.nvim_command("normal! v") + + api.nvim_win_set_cursor(display.for_win, { end_line, display.focus_node.scope["end"].character - 1 }) + comment_trailing_space(display) + if not pcall(function() + api.nvim_command(cmd) + end) then + trailing_spaces = 2 + else + trailing_spaces = display.config.folding.leading_spaces + 2 + end + + if cmd == "normal! zd" then + api.nvim_set_current_win(display.for_win) + api.nvim_win_set_cursor(display.for_win, { start_line, 0 }) + clean_trailing_space(api.nvim_get_current_line(), trailing_spaces) + api.nvim_win_set_cursor(display.for_win, { end_line, 0 }) + clean_trailing_space(api.nvim_get_current_line(), trailing_spaces) + end + + mid_win_id(display) +end + +function actions.fold_create(display) + if is_manual_foldmethod(display) == false then + return + end + apply_comment_fold_marker(display, "normal! zf") + reinit_foldmethod() +end + +function actions.fold_delete(display) + if is_manual_foldmethod(display) == false then + return + end + start_line, end_line = apply_comment_fold_marker(display, "normal! zd") + reinit_foldmethod() end function actions.comment(display) @@ -279,15 +326,15 @@ function actions.comment(display) end display.state.leaving_window_for_action = true - vim.api.nvim_set_current_win(display.for_win) - vim.api.nvim_buf_set_mark( + api.nvim_set_current_win(display.for_win) + api.nvim_buf_set_mark( display.for_buf, "<", display.focus_node.scope["start"].line, display.focus_node.scope["start"].character, {} ) - vim.api.nvim_buf_set_mark( + api.nvim_buf_set_mark( display.for_buf, ">", display.focus_node.scope["end"].line, @@ -295,7 +342,7 @@ function actions.comment(display) {} ) comment.locked("toggle.linewise")("v") - vim.api.nvim_set_current_win(display.mid.winid) + api.nvim_set_current_win(display.mid.winid) display.state.leaving_window_for_action = false end @@ -311,11 +358,14 @@ local function swap_nodes(for_buf, nodeA, nodeB) return end - local nodeA_text = vim.api.nvim_buf_get_lines(for_buf, nodeA.scope["start"].line-1, nodeA.scope["end"].line-1+1, false) - local mid_text = vim.api.nvim_buf_get_lines(for_buf, nodeA.scope["end"].line-1+1, nodeB.scope["start"].line-1, false) - local nodeB_text = vim.api.nvim_buf_get_lines(for_buf, nodeB.scope["start"].line-1, nodeB.scope["end"].line-1+1, false) + local nodeA_text = + api.nvim_buf_get_lines(for_buf, nodeA.scope["start"].line - 1, nodeA.scope["end"].line - 1 + 1, false) + local mid_text = + api.nvim_buf_get_lines(for_buf, nodeA.scope["end"].line - 1 + 1, nodeB.scope["start"].line - 1, false) + local nodeB_text = + api.nvim_buf_get_lines(for_buf, nodeB.scope["start"].line - 1, nodeB.scope["end"].line - 1 + 1, false) - local start_line = nodeA.scope["start"].line-1 + local start_line = nodeA.scope["start"].line - 1 local nodeA_line_cnt = nodeA.scope["end"].line + 1 - nodeA.scope["start"].line local mid_line_cnt = nodeB.scope["start"].line - nodeA.scope["end"].line - 1 local nodeB_line_cnt = nodeB.scope["end"].line + 1 - nodeB.scope["start"].line @@ -349,9 +399,21 @@ local function swap_nodes(for_buf, nodeA, nodeB) nodeB.name_range["end"].line = nodeB.name_range["end"].line - nodeA_line_cnt - mid_line_cnt -- Set lines - vim.api.nvim_buf_set_lines(for_buf, start_line, start_line + nodeB_line_cnt, false, nodeB_text) - vim.api.nvim_buf_set_lines(for_buf, start_line + nodeB_line_cnt, start_line + nodeB_line_cnt + mid_line_cnt, false, mid_text) - vim.api.nvim_buf_set_lines(for_buf, start_line + nodeB_line_cnt + mid_line_cnt, start_line + nodeB_line_cnt + mid_line_cnt + nodeA_line_cnt, false, nodeA_text) + api.nvim_buf_set_lines(for_buf, start_line, start_line + nodeB_line_cnt, false, nodeB_text) + api.nvim_buf_set_lines( + for_buf, + start_line + nodeB_line_cnt, + start_line + nodeB_line_cnt + mid_line_cnt, + false, + mid_text + ) + api.nvim_buf_set_lines( + for_buf, + start_line + nodeB_line_cnt + mid_line_cnt, + start_line + nodeB_line_cnt + mid_line_cnt + nodeA_line_cnt, + false, + nodeA_text + ) end function actions.move_down(display) @@ -375,7 +437,7 @@ function actions.move_up(display) end function actions.toggle_preview(display) - if vim.api.nvim_win_get_buf(display.right.winid) == display.right.bufnr then + if api.nvim_win_get_buf(display.right.winid) == display.right.bufnr then display:show_preview() else display:hide_preview() @@ -409,11 +471,11 @@ function actions.telescope(opts) local function make_display(entry) local node = entry.value local kind = navic.adapt_lsp_num_to_str(node.kind) - local kind_hl = "Navbuddy"..kind + local kind_hl = "Navbuddy" .. kind local name_hl = "NavbuddyNormalFloat" local columns = { { string.lower(kind), kind_hl }, - { node.name, name_hl}, + { node.name, name_hl }, } return displayer(columns) end @@ -423,37 +485,39 @@ function actions.telescope(opts) value = node, display = make_display, name = node.name, - ordinal = string.lower(navic.adapt_lsp_num_to_str(node.kind)).." "..node.name, + ordinal = string.lower(navic.adapt_lsp_num_to_str(node.kind)) .. " " .. node.name, lnum = node.name_range["start"].line, col = node.name_range["start"].character, bufnr = display.for_buf, - filename = vim.api.nvim_buf_get_name(display.for_buf), + filename = api.nvim_buf_get_name(display.for_buf), } end display:close() - pickers.new(opts, { - prompt_title = "Fuzzy Search", - finder = finders.new_table({ - results = display.focus_node.parent.children, - entry_maker = make_entry - }), - sorter = conf.generic_sorter(opts), - previewer = conf.qflist_previewer(opts), - attach_mappings = function(prompt_bufnr, _) - t_actions.select_default:replace(function() - local selection = action_state.get_selected_entry() - display.focus_node = selection.value - t_actions.close(prompt_bufnr) - end) - t_actions.close:enhance({ - post = function() - display = require("nvim-navbuddy.display"):new(display) - end - }) - return true - end, - }):find() + pickers + .new(opts, { + prompt_title = "Fuzzy Search", + finder = finders.new_table({ + results = display.focus_node.parent.children, + entry_maker = make_entry, + }), + sorter = conf.generic_sorter(opts), + previewer = conf.qflist_previewer(opts), + attach_mappings = function(prompt_bufnr, _) + t_actions.select_default:replace(function() + local selection = action_state.get_selected_entry() + display.focus_node = selection.value + t_actions.close(prompt_bufnr) + end) + t_actions.close:enhance({ + post = function() + display = require("nvim-navbuddy.display"):new(display) + end, + }) + return true + end, + }) + :find() end end From e170e40a0cdeda703819a2cb2b95f175e2ff1968 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AE=80=20=D0=9A4mi=20=E5=AE=80=20/=20Michel=20TRUONG?= Date: Thu, 20 Apr 2023 23:06:45 +0200 Subject: [PATCH 3/9] Update init.lua new folding options --- lua/nvim-navbuddy/init.lua | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/lua/nvim-navbuddy/init.lua b/lua/nvim-navbuddy/init.lua index 00c1c69..d0f5fae 100644 --- a/lua/nvim-navbuddy/init.lua +++ b/lua/nvim-navbuddy/init.lua @@ -343,19 +343,21 @@ function M.setup(user_config) config.source_buffer = vim.tbl_deep_extend("keep", user_config.source_buffer, config.source_buffer) end - if - user_config.folding.foldmethod_auto_set_manual ~= nil - and type(user_config.folding.foldmethod_auto_set_manual) == "boolean" - then - config.folding.foldmethod_auto_set_manual = user_config.folding.foldmethod_auto_set_manual - end + if user_config.folding ~= nil then + if + user_config.folding.foldmethod_auto_set_manual ~= nil + and type(user_config.folding.foldmethod_auto_set_manual) == "boolean" + then + config.folding.foldmethod_auto_set_manual = user_config.folding.foldmethod_auto_set_manual + end - if - user_config.folding.leading_spaces ~= nil - and type(user_config.folding.leading_spaces) == "number" - and user_config.folding.leading_spaces >= 0 - then - config.folding.leading_spaces = user_config.folding.leading_spaces + if + user_config.folding.leading_spaces ~= nil + and type(user_config.folding.leading_spaces) == "number" + and user_config.folding.leading_spaces >= 0 + then + config.folding.leading_spaces = user_config.folding.leading_spaces + end end end From cc4ce58b80498cbea4bdce97b1035ef4ffeef1ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AE=80=20=D0=9A4mi=20=E5=AE=80=20/=20Michel=20TRUONG?= Date: Thu, 20 Apr 2023 23:23:16 +0200 Subject: [PATCH 4/9] Update README.md --- README.md | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 55d1345..fa98fc5 100644 --- a/README.md +++ b/README.md @@ -98,6 +98,9 @@ Use `setup` to override any of the default options * `follow_node` : Keep the current node in focus on the source buffer * `highlight` : Highlight the currently focused node * reorient: Reorient buffer after changing nodes. options are "smart", "top", "mid" or "none" +* `folding` : + * `foldmethod_auto_set_manual` : Auto switch to manual mode + * `leading_spaces` : Add leading space to the foldmarker comment string ```lua local navbuddy = require("nvim-navbuddy") @@ -226,7 +229,11 @@ navbuddy.setup { highlight = true, -- Highlight the currently focused node reorient = "smart", -- "smart", "top", "mid" or "none" scrolloff = nil -- scrolloff value when navbuddy is open - } + }, + folding = { + foldmethod_auto_set_manual = true, -- Auto switch to manual mode + leading_spaces = 2, -- Add leading space to the foldmarker comment string + }, } ``` From 47e509820a02e8f4f6cbf05f283cc8de64c0f7fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AE=80=20=D0=9A4mi=20=E5=AE=80=20/=20Michel=20TRUONG?= Date: Thu, 20 Apr 2023 23:23:50 +0200 Subject: [PATCH 5/9] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index fa98fc5..57aa9cd 100644 --- a/README.md +++ b/README.md @@ -233,7 +233,7 @@ navbuddy.setup { folding = { foldmethod_auto_set_manual = true, -- Auto switch to manual mode leading_spaces = 2, -- Add leading space to the foldmarker comment string - }, + }, } ``` From 68ccedd91803ad2b4e77266353423062f288ea9e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AE=80=20=D0=9A4mi=20=E5=AE=80=20/=20Michel=20TRUONG?= Date: Thu, 20 Apr 2023 23:29:20 +0200 Subject: [PATCH 6/9] Update navbuddy.txt --- doc/navbuddy.txt | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/doc/navbuddy.txt b/doc/navbuddy.txt index 3a4bbd8..af3f9ee 100644 --- a/doc/navbuddy.txt +++ b/doc/navbuddy.txt @@ -91,7 +91,7 @@ Use |navbuddy.setup| to override any of the default options the preference list is { "clangd", "pyright" }. Then clangd will be prefered. - source_buffer: + source_buffer: table follow_node: boolean Move the source buffer such that focused node is visible. highlight: boolean @@ -100,6 +100,12 @@ Use |navbuddy.setup| to override any of the default options Reorient buffer after changing nodes. options are "smart", "top", "mid" or "none" + folding: table + foldmethod_auto_set_manual: bool + Auto switch to manual mode + leading_spaces: number + Add leading space to the foldmarker comment string + Defaults > local navbuddy = require("nvim-navbuddy") @@ -229,7 +235,11 @@ Defaults > highlight = true, -- Highlight the currently focused node reorient = "smart", -- "smart", "top", "mid" or "none" scrolloff = nil -- scrolloff value when navbuddy is open - } + }, + folding = { + foldmethod_auto_set_manual = true, -- Auto switch to manual mode + leading_spaces = 2, -- Add leading space to the foldmarker comment string + }, } < ============================================================================= From 8b4cce887c4a7de73715e4f453abe2ac8178b790 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AE=80=20=D0=9A4mi=20=E5=AE=80=20/=20Michel=20TRUONG?= Date: Fri, 21 Apr 2023 13:02:00 +0200 Subject: [PATCH 7/9] Update actions.lua code refactoring --- lua/nvim-navbuddy/actions.lua | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/lua/nvim-navbuddy/actions.lua b/lua/nvim-navbuddy/actions.lua index 94dc38c..5ebea76 100644 --- a/lua/nvim-navbuddy/actions.lua +++ b/lua/nvim-navbuddy/actions.lua @@ -1,6 +1,8 @@ local USER_FOLDMETHOD = vim.o.foldmethod local USER_FOLDMARKER = vim.o.foldmarker local USER_FOLDMARKER_O, USER_FOLDMARKER_C = USER_FOLDMARKER:match("([^,]+),([^,]+)") +local CMD_FOLD_N_ZF = "normal! zf" +local CMD_FOLD_N_ZD = "normal! zd" local api = vim.api local actions = {} @@ -271,7 +273,7 @@ local apply_comment_fold_marker = function(display, cmd) api.nvim_win_set_cursor(display.for_win, { start_line, display.focus_node.scope["start"].character }) if - cmd == "normal! zf" + cmd == CMD_FOLD_N_ZF and (api.nvim_get_current_line():find(USER_FOLDMARKER_O) or api.nvim_get_current_line():find(USER_FOLDMARKER_C)) then mid_win_id(display) @@ -291,7 +293,7 @@ local apply_comment_fold_marker = function(display, cmd) trailing_spaces = display.config.folding.leading_spaces + 2 end - if cmd == "normal! zd" then + if cmd == CMD_FOLD_N_ZD then api.nvim_set_current_win(display.for_win) api.nvim_win_set_cursor(display.for_win, { start_line, 0 }) clean_trailing_space(api.nvim_get_current_line(), trailing_spaces) @@ -306,7 +308,7 @@ function actions.fold_create(display) if is_manual_foldmethod(display) == false then return end - apply_comment_fold_marker(display, "normal! zf") + apply_comment_fold_marker(display, CMD_FOLD_N_ZF) reinit_foldmethod() end @@ -314,7 +316,7 @@ function actions.fold_delete(display) if is_manual_foldmethod(display) == false then return end - start_line, end_line = apply_comment_fold_marker(display, "normal! zd") + apply_comment_fold_marker(display, CMD_FOLD_N_ZD) reinit_foldmethod() end From 071fdde1e271ba1f942dab4e4df8756f7ec65e34 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AE=80=20=D0=9A4mi=20=E5=AE=80=20/=20Michel=20TRUONG?= Date: Sat, 22 Apr 2023 19:49:47 +0200 Subject: [PATCH 8/9] Update actions.lua --- lua/nvim-navbuddy/actions.lua | 264 ++++++++++++++++------------------ 1 file changed, 122 insertions(+), 142 deletions(-) diff --git a/lua/nvim-navbuddy/actions.lua b/lua/nvim-navbuddy/actions.lua index 5ebea76..ca3a098 100644 --- a/lua/nvim-navbuddy/actions.lua +++ b/lua/nvim-navbuddy/actions.lua @@ -1,15 +1,10 @@ -local USER_FOLDMETHOD = vim.o.foldmethod -local USER_FOLDMARKER = vim.o.foldmarker -local USER_FOLDMARKER_O, USER_FOLDMARKER_C = USER_FOLDMARKER:match("([^,]+),([^,]+)") -local CMD_FOLD_N_ZF = "normal! zf" -local CMD_FOLD_N_ZD = "normal! zd" -local api = vim.api +local USER_FOLDMETHOD = vim.o.foldmethod -- get user foldmethod preference to restore it local actions = {} function actions.close(display) display:close() - api.nvim_win_set_cursor(display.for_win, display.start_cursor) + vim.api.nvim_win_set_cursor(display.for_win, display.start_cursor) end function actions.next_sibling(display) @@ -89,9 +84,9 @@ function actions.select(display) display:close() -- to push location to jumplist: -- move display to start_cursor, set mark ', then move to new location - api.nvim_win_set_cursor(display.for_win, display.start_cursor) - api.nvim_command("normal! m'") - api.nvim_win_set_cursor( + vim.api.nvim_win_set_cursor(display.for_win, display.start_cursor) + vim.api.nvim_command("normal! m'") + vim.api.nvim_win_set_cursor( display.for_win, { display.focus_node.name_range["start"].line, display.focus_node.name_range["start"].character } ) @@ -99,61 +94,61 @@ function actions.select(display) if display.config.source_buffer.reorient == "smart" then local total_lines = display.focus_node.scope["end"].line - display.focus_node.scope["start"].line + 1 - if total_lines >= api.nvim_win_get_height(display.for_win) then - api.nvim_command("normal! zt") + if total_lines >= vim.api.nvim_win_get_height(display.for_win) then + vim.api.nvim_command("normal! zt") else local mid_line = bit.rshift(display.focus_node.scope["start"].line + display.focus_node.scope["end"].line, 1) - api.nvim_win_set_cursor(display.for_win, { mid_line, 0 }) - api.nvim_command("normal! zz") - api.nvim_win_set_cursor( + vim.api.nvim_win_set_cursor(display.for_win, { mid_line, 0 }) + vim.api.nvim_command("normal! zz") + vim.api.nvim_win_set_cursor( display.for_win, { display.focus_node.name_range["start"].line, display.focus_node.name_range["start"].character } ) end elseif display.config.source_buffer.reorient == "mid" then - api.nvim_command("normal! zz") + vim.api.nvim_command("normal! zz") elseif display.config.source_buffer.reorient == "top" then - api.nvim_command("normal! zt") + vim.api.nvim_command("normal! zt") end end function actions.yank_name(display) display:close() - api.nvim_win_set_cursor( + vim.api.nvim_win_set_cursor( display.for_win, { display.focus_node.name_range["start"].line, display.focus_node.name_range["start"].character } ) - api.nvim_command("normal! v") - api.nvim_win_set_cursor( + vim.api.nvim_command("normal! v") + vim.api.nvim_win_set_cursor( display.for_win, { display.focus_node.name_range["end"].line, display.focus_node.name_range["end"].character - 1 } ) - api.nvim_command('normal! "+y') + vim.api.nvim_command('normal! "+y') end function actions.yank_scope(display) display:close() - api.nvim_win_set_cursor( + vim.api.nvim_win_set_cursor( display.for_win, { display.focus_node.scope["start"].line, display.focus_node.scope["start"].character } ) - api.nvim_command("normal! v") - api.nvim_win_set_cursor( + vim.api.nvim_command("normal! v") + vim.api.nvim_win_set_cursor( display.for_win, { display.focus_node.scope["end"].line, display.focus_node.scope["end"].character - 1 } ) - api.nvim_command('normal! "+y') + vim.api.nvim_command('normal! "+y') end function actions.visual_name(display) display:close() - api.nvim_win_set_cursor( + vim.api.nvim_win_set_cursor( display.for_win, { display.focus_node.name_range["start"].line, display.focus_node.name_range["start"].character } ) - api.nvim_command("normal! v") - api.nvim_win_set_cursor( + vim.api.nvim_command("normal! v") + vim.api.nvim_win_set_cursor( display.for_win, { display.focus_node.name_range["end"].line, display.focus_node.name_range["end"].character - 1 } ) @@ -161,12 +156,12 @@ end function actions.visual_scope(display) display:close() - api.nvim_win_set_cursor( + vim.api.nvim_win_set_cursor( display.for_win, { display.focus_node.scope["start"].line, display.focus_node.scope["start"].character } ) - api.nvim_command("normal! v") - api.nvim_win_set_cursor( + vim.api.nvim_command("normal! v") + vim.api.nvim_win_set_cursor( display.for_win, { display.focus_node.scope["end"].line, display.focus_node.scope["end"].character - 1 } ) @@ -174,36 +169,36 @@ end function actions.insert_name(display) display:close() - api.nvim_win_set_cursor( + vim.api.nvim_win_set_cursor( display.for_win, { display.focus_node.name_range["start"].line, display.focus_node.name_range["start"].character } ) - api.nvim_feedkeys("i", "n", false) + vim.api.nvim_feedkeys("i", "n", false) end function actions.insert_scope(display) display:close() - api.nvim_win_set_cursor( + vim.api.nvim_win_set_cursor( display.for_win, { display.focus_node.scope["start"].line, display.focus_node.scope["start"].character } ) - api.nvim_feedkeys("i", "n", false) + vim.api.nvim_feedkeys("i", "n", false) end function actions.append_name(display) display:close() - api.nvim_win_set_cursor( + vim.api.nvim_win_set_cursor( display.for_win, { display.focus_node.name_range["end"].line, display.focus_node.name_range["end"].character - 1 } ) - api.nvim_feedkeys("a", "n", false) + vim.api.nvim_feedkeys("a", "n", false) end function actions.append_scope(display) display:close() if string.len( - api.nvim_buf_get_lines( + vim.api.nvim_buf_get_lines( display.for_buf, display.focus_node.scope["end"].line - 1, display.focus_node.scope["end"].line, @@ -211,17 +206,17 @@ function actions.append_scope(display) )[1] ) == display.focus_node.scope["end"].character then - api.nvim_win_set_cursor( + vim.api.nvim_win_set_cursor( display.for_win, { display.focus_node.scope["end"].line, display.focus_node.scope["end"].character } ) else - api.nvim_win_set_cursor( + vim.api.nvim_win_set_cursor( display.for_win, { display.focus_node.scope["end"].line, display.focus_node.scope["end"].character - 1 } ) end - api.nvim_feedkeys("a", "n", false) + vim.api.nvim_feedkeys("a", "n", false) end function actions.rename(display) @@ -231,7 +226,7 @@ end function actions.delete(display) actions.visual_scope(display) - api.nvim_command("normal! d") + vim.api.nvim_command("normal! d") end local is_manual_foldmethod = function(display) @@ -251,72 +246,74 @@ local reinit_foldmethod = function() end local comment_trailing_space = function(display) - api.nvim_set_current_line(api.nvim_get_current_line() .. string.rep(" ", display.config.folding.leading_spaces)) + vim.api.nvim_set_current_line(vim.api.nvim_get_current_line() .. string.rep(" ", display.config.folding.leading_spaces)) end -local clean_trailing_space = function(str, trs) - api.nvim_set_current_line(string.sub(str, 1, string.len(str) - trs)) +local clean_trailing_space = function() + vim.api.nvim_set_current_line(""..vim.api.nvim_get_current_line():gsub("%s+$", "")) end -local mid_win_id = function(display) - api.nvim_set_current_win(display.mid.winid) - display.state.leaving_window_for_action = false -end +function actions.fold_create(display) + local foldmarker_o, foldmarker_c = vim.o.foldmarker:match("([^,]+),([^,]+)") -local apply_comment_fold_marker = function(display, cmd) - local start_line = display.focus_node.scope["start"].line - local end_line = display.focus_node.scope["end"].line - local trailing_spaces = 0 + if is_manual_foldmethod(display) == false then + return + end display.state.leaving_window_for_action = true - api.nvim_set_current_win(display.for_win) - api.nvim_win_set_cursor(display.for_win, { start_line, display.focus_node.scope["start"].character }) + vim.api.nvim_set_current_win(display.for_win) + vim.api.nvim_win_set_cursor( + display.for_win, + { display.focus_node.scope["start"].line, display.focus_node.scope["start"].character } + ) - if - cmd == CMD_FOLD_N_ZF - and (api.nvim_get_current_line():find(USER_FOLDMARKER_O) or api.nvim_get_current_line():find(USER_FOLDMARKER_C)) - then - mid_win_id(display) + -- avoid duplicate fold marker comment string + if vim.api.nvim_get_current_line():find(foldmarker_o) or vim.api.nvim_get_current_line():find(foldmarker_c) then + vim.api.nvim_set_current_win(display.mid.winid) + display.state.leaving_window_for_action = false return end comment_trailing_space(display) - api.nvim_command("normal! v") - - api.nvim_win_set_cursor(display.for_win, { end_line, display.focus_node.scope["end"].character - 1 }) + vim.api.nvim_command("normal! v") + vim.api.nvim_win_set_cursor( + display.for_win, + { display.focus_node.scope["end"].line, display.focus_node.scope["end"].character - 1 } + ) comment_trailing_space(display) - if not pcall(function() - api.nvim_command(cmd) - end) then - trailing_spaces = 2 - else - trailing_spaces = display.config.folding.leading_spaces + 2 - end - - if cmd == CMD_FOLD_N_ZD then - api.nvim_set_current_win(display.for_win) - api.nvim_win_set_cursor(display.for_win, { start_line, 0 }) - clean_trailing_space(api.nvim_get_current_line(), trailing_spaces) - api.nvim_win_set_cursor(display.for_win, { end_line, 0 }) - clean_trailing_space(api.nvim_get_current_line(), trailing_spaces) - end - - mid_win_id(display) -end + vim.api.nvim_command("normal! zf") + vim.api.nvim_set_current_win(display.mid.winid) + display.state.leaving_window_for_action = false -function actions.fold_create(display) - if is_manual_foldmethod(display) == false then - return - end - apply_comment_fold_marker(display, CMD_FOLD_N_ZF) reinit_foldmethod() end function actions.fold_delete(display) + local start_line = display.focus_node.scope["start"].line + local end_line = display.focus_node.scope["end"].line + if is_manual_foldmethod(display) == false then return end - apply_comment_fold_marker(display, CMD_FOLD_N_ZD) + + display.state.leaving_window_for_action = true + vim.api.nvim_set_current_win(display.for_win) + vim.api.nvim_win_set_cursor( + display.for_win, + { start_line, display.focus_node.scope["start"].character } + ) + vim.api.nvim_win_set_cursor( + display.for_win, + { end_line, display.focus_node.scope["end"].character - 1 } + ) + pcall(vim.api.nvim_command, "normal! zd") + vim.api.nvim_win_set_cursor(display.for_win, { start_line, 0 }) + clean_trailing_space() + vim.api.nvim_win_set_cursor(display.for_win, { end_line, 0 }) + clean_trailing_space() + vim.api.nvim_set_current_win(display.mid.winid) + display.state.leaving_window_for_action = false + reinit_foldmethod() end @@ -328,15 +325,15 @@ function actions.comment(display) end display.state.leaving_window_for_action = true - api.nvim_set_current_win(display.for_win) - api.nvim_buf_set_mark( + vim.api.nvim_set_current_win(display.for_win) + vim.api.nvim_buf_set_mark( display.for_buf, "<", display.focus_node.scope["start"].line, display.focus_node.scope["start"].character, {} ) - api.nvim_buf_set_mark( + vim.api.nvim_buf_set_mark( display.for_buf, ">", display.focus_node.scope["end"].line, @@ -344,7 +341,7 @@ function actions.comment(display) {} ) comment.locked("toggle.linewise")("v") - api.nvim_set_current_win(display.mid.winid) + vim.api.nvim_set_current_win(display.mid.winid) display.state.leaving_window_for_action = false end @@ -360,14 +357,11 @@ local function swap_nodes(for_buf, nodeA, nodeB) return end - local nodeA_text = - api.nvim_buf_get_lines(for_buf, nodeA.scope["start"].line - 1, nodeA.scope["end"].line - 1 + 1, false) - local mid_text = - api.nvim_buf_get_lines(for_buf, nodeA.scope["end"].line - 1 + 1, nodeB.scope["start"].line - 1, false) - local nodeB_text = - api.nvim_buf_get_lines(for_buf, nodeB.scope["start"].line - 1, nodeB.scope["end"].line - 1 + 1, false) + local nodeA_text = vim.api.nvim_buf_get_lines(for_buf, nodeA.scope["start"].line-1, nodeA.scope["end"].line-1+1, false) + local mid_text = vim.api.nvim_buf_get_lines(for_buf, nodeA.scope["end"].line-1+1, nodeB.scope["start"].line-1, false) + local nodeB_text = vim.api.nvim_buf_get_lines(for_buf, nodeB.scope["start"].line-1, nodeB.scope["end"].line-1+1, false) - local start_line = nodeA.scope["start"].line - 1 + local start_line = nodeA.scope["start"].line-1 local nodeA_line_cnt = nodeA.scope["end"].line + 1 - nodeA.scope["start"].line local mid_line_cnt = nodeB.scope["start"].line - nodeA.scope["end"].line - 1 local nodeB_line_cnt = nodeB.scope["end"].line + 1 - nodeB.scope["start"].line @@ -401,21 +395,9 @@ local function swap_nodes(for_buf, nodeA, nodeB) nodeB.name_range["end"].line = nodeB.name_range["end"].line - nodeA_line_cnt - mid_line_cnt -- Set lines - api.nvim_buf_set_lines(for_buf, start_line, start_line + nodeB_line_cnt, false, nodeB_text) - api.nvim_buf_set_lines( - for_buf, - start_line + nodeB_line_cnt, - start_line + nodeB_line_cnt + mid_line_cnt, - false, - mid_text - ) - api.nvim_buf_set_lines( - for_buf, - start_line + nodeB_line_cnt + mid_line_cnt, - start_line + nodeB_line_cnt + mid_line_cnt + nodeA_line_cnt, - false, - nodeA_text - ) + vim.api.nvim_buf_set_lines(for_buf, start_line, start_line + nodeB_line_cnt, false, nodeB_text) + vim.api.nvim_buf_set_lines(for_buf, start_line + nodeB_line_cnt, start_line + nodeB_line_cnt + mid_line_cnt, false, mid_text) + vim.api.nvim_buf_set_lines(for_buf, start_line + nodeB_line_cnt + mid_line_cnt, start_line + nodeB_line_cnt + mid_line_cnt + nodeA_line_cnt, false, nodeA_text) end function actions.move_down(display) @@ -439,7 +421,7 @@ function actions.move_up(display) end function actions.toggle_preview(display) - if api.nvim_win_get_buf(display.right.winid) == display.right.bufnr then + if vim.api.nvim_win_get_buf(display.right.winid) == display.right.bufnr then display:show_preview() else display:hide_preview() @@ -473,11 +455,11 @@ function actions.telescope(opts) local function make_display(entry) local node = entry.value local kind = navic.adapt_lsp_num_to_str(node.kind) - local kind_hl = "Navbuddy" .. kind + local kind_hl = "Navbuddy"..kind local name_hl = "NavbuddyNormalFloat" local columns = { { string.lower(kind), kind_hl }, - { node.name, name_hl }, + { node.name, name_hl}, } return displayer(columns) end @@ -487,39 +469,37 @@ function actions.telescope(opts) value = node, display = make_display, name = node.name, - ordinal = string.lower(navic.adapt_lsp_num_to_str(node.kind)) .. " " .. node.name, + ordinal = string.lower(navic.adapt_lsp_num_to_str(node.kind)).." "..node.name, lnum = node.name_range["start"].line, col = node.name_range["start"].character, bufnr = display.for_buf, - filename = api.nvim_buf_get_name(display.for_buf), + filename = vim.api.nvim_buf_get_name(display.for_buf), } end display:close() - pickers - .new(opts, { - prompt_title = "Fuzzy Search", - finder = finders.new_table({ - results = display.focus_node.parent.children, - entry_maker = make_entry, - }), - sorter = conf.generic_sorter(opts), - previewer = conf.qflist_previewer(opts), - attach_mappings = function(prompt_bufnr, _) - t_actions.select_default:replace(function() - local selection = action_state.get_selected_entry() - display.focus_node = selection.value - t_actions.close(prompt_bufnr) - end) - t_actions.close:enhance({ - post = function() - display = require("nvim-navbuddy.display"):new(display) - end, - }) - return true - end, - }) - :find() + pickers.new(opts, { + prompt_title = "Fuzzy Search", + finder = finders.new_table({ + results = display.focus_node.parent.children, + entry_maker = make_entry + }), + sorter = conf.generic_sorter(opts), + previewer = conf.qflist_previewer(opts), + attach_mappings = function(prompt_bufnr, _) + t_actions.select_default:replace(function() + local selection = action_state.get_selected_entry() + display.focus_node = selection.value + t_actions.close(prompt_bufnr) + end) + t_actions.close:enhance({ + post = function() + display = require("nvim-navbuddy.display"):new(display) + end + }) + return true + end, + }):find() end end From 09c43a8805db4d084c3067d1887dab9444f99fc2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AE=80=20=D0=9A4mi=20=E5=AE=80=20/=20Michel=20TRUONG?= Date: Sat, 22 Apr 2023 19:50:13 +0200 Subject: [PATCH 9/9] Update init.lua --- lua/nvim-navbuddy/init.lua | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/lua/nvim-navbuddy/init.lua b/lua/nvim-navbuddy/init.lua index d0f5fae..3e673df 100644 --- a/lua/nvim-navbuddy/init.lua +++ b/lua/nvim-navbuddy/init.lua @@ -64,36 +64,50 @@ local config = { mappings = { [""] = actions.close, ["q"] = actions.close, + ["j"] = actions.next_sibling, ["k"] = actions.previous_sibling, + ["h"] = actions.parent, ["l"] = actions.children, ["0"] = actions.root, + ["v"] = actions.visual_name, ["V"] = actions.visual_scope, + ["y"] = actions.yank_name, ["Y"] = actions.yank_scope, + ["i"] = actions.insert_name, ["I"] = actions.insert_scope, + ["a"] = actions.append_name, ["A"] = actions.append_scope, + ["r"] = actions.rename, + ["d"] = actions.delete, + ["f"] = actions.fold_create, ["F"] = actions.fold_delete, + ["c"] = actions.comment, + [""] = actions.select, ["o"] = actions.select, + ["J"] = actions.move_down, ["K"] = actions.move_up, + ["s"] = actions.toggle_preview, + ["t"] = actions.telescope({ layout_strategy = "horizontal", layout_config = { height = 0.60, width = 0.60, prompt_position = "top", - preview_width = 0.50, + preview_width = 0.50 }, }), }, @@ -105,11 +119,11 @@ local config = { follow_node = true, highlight = true, reorient = "smart", - scrolloff = nil, + scrolloff = nil }, folding = { foldmethod_auto_set_manual = true, - leading_spaces = 2, -- to avoid the monstruosity err msg "at least two spaces before inline comment" + leading_spaces = 2, }, }