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

Foldmethod auto set manual #49

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
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
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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
},
}
```

Expand Down
14 changes: 12 additions & 2 deletions doc/navbuddy.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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")
Expand Down Expand Up @@ -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
},
}
<
=============================================================================
Expand Down
60 changes: 53 additions & 7 deletions lua/nvim-navbuddy/actions.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
local USER_FOLDMETHOD = vim.o.foldmethod -- get user foldmethod preference to restore it

local actions = {}

function actions.close(display)
Expand Down Expand Up @@ -227,9 +229,34 @@ function actions.delete(display)
vim.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 false
end
return true
end

local reinit_foldmethod = function()
vim.o.foldmethod = USER_FOLDMETHOD
end

local comment_trailing_space = function(display)
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()
vim.api.nvim_set_current_line(""..vim.api.nvim_get_current_line():gsub("%s+$", ""))
end

function actions.fold_create(display)
local foldmarker_o, foldmarker_c = vim.o.foldmarker:match("([^,]+),([^,]+)")

if is_manual_foldmethod(display) == false then
return
end

Expand All @@ -239,36 +266,55 @@ function actions.fold_create(display)
display.for_win,
{ display.focus_node.scope["start"].line, display.focus_node.scope["start"].character }
)

-- 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)
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)
vim.api.nvim_command("normal! zf")
vim.api.nvim_set_current_win(display.mid.winid)
display.state.leaving_window_for_action = false

reinit_foldmethod()
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 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

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 }
{ 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 }
{ 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

function actions.comment(display)
Expand Down
21 changes: 21 additions & 0 deletions lua/nvim-navbuddy/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ local config = {
reorient = "smart",
scrolloff = nil
},
folding = {
foldmethod_auto_set_manual = true,
leading_spaces = 2,
},
}

setmetatable(config.icons, {
Expand Down Expand Up @@ -352,6 +356,23 @@ 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 ~= 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
end
end
end

if config.lsp.auto_attach == true then
Expand Down