From 6b2155daf4f65280d2ae1c3939e39190fff4e2d7 Mon Sep 17 00:00:00 2001 From: Connor Robertson Date: Tue, 24 Oct 2023 10:54:30 -0700 Subject: [PATCH 1/3] add folding based on fold expression avoiding cell markers should only apply to filetypes with a valid cell marker in the config --- README.md | 3 +++ doc/NotebookNavigator.txt | 5 ++++- lua/notebook-navigator/init.lua | 30 ++++++++++++++++++++++++++++++ 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 2b80c19..8f3c605 100644 --- a/README.md +++ b/README.md @@ -120,6 +120,9 @@ Any options that are not specified when calling `setup` will take on their defau -- Current options: "iron" for iron.nvim, "toggleterm" for toggleterm.nvim, -- or "auto" which checks which of the above are installed repl_provider = "auto", + -- If `true`, folding will be based on expression for buffers with a valid + -- cell marker and cells will be folded on entering the buffer + cell_folding = true } ``` diff --git a/doc/NotebookNavigator.txt b/doc/NotebookNavigator.txt index 58518db..ad59633 100644 --- a/doc/NotebookNavigator.txt +++ b/doc/NotebookNavigator.txt @@ -120,6 +120,9 @@ Default values: -- Current options: "iron" for iron.nvim, "toggleterm" for toggleterm.nvim, -- or "auto" which checks which of the above are installed repl_provider = "auto", + -- If `true`, folding will be based on expression for buffers with a valid + -- cell marker and cells will be folded on entering the buffer + cell_folding = true } < @@ -138,4 +141,4 @@ Usage~ any config parameter which you not pass will take on its default value. - vim:tw=78:ts=8:noet:ft=help:norl: \ No newline at end of file + vim:tw=78:ts=8:noet:ft=help:norl: diff --git a/lua/notebook-navigator/init.lua b/lua/notebook-navigator/init.lua index 55b01c5..62baada 100644 --- a/lua/notebook-navigator/init.lua +++ b/lua/notebook-navigator/init.lua @@ -209,6 +209,9 @@ M.config = { -- Current options: "iron" for iron.nvim, "toggleterm" for toggleterm.nvim, -- or "auto" which checks which of the above are installed repl_provider = "auto", + -- If `true`, folding will be based on expression for buffers with a valid + -- cell marker and cells will be folded on entering the buffer + cell_folding = true } --minidoc_afterlines_end @@ -258,6 +261,33 @@ M.setup = function(config) if (M.config.activate_hydra_keys ~= nil) and got_hydra then activate_hydra(M.config) end + + local cell_group = vim.api.nvim_create_augroup("Cells", {clear=true}) + if M.config.cell_folding then + local foldm = vim.o.foldmethod + vim.cmd[[set nofoldenable]] + vim.api.nvim_create_autocmd("BufEnter", { + pattern = {"*"}, + group = cell_group, + callback = function(ev) + local ft = vim.api.nvim_get_option_value("filetype", {buf = ev.buf}) + if M.config.cell_markers[ft] then + local marker = M.config.cell_markers[ft] + local vim_marker = string.gsub(marker, " ", "\\ ") + local expr = "set foldexpr=(getline(v:lnum)=~'^"..vim_marker.."')==0" + vim.cmd[[set foldmethod=expr]] + vim.cmd(expr) + end + end + }) + vim.api.nvim_create_autocmd("BufLeave", { + pattern = {"*"}, + group = cell_group, + callback = function() + vim.cmd("set foldmethod="..foldm) + end + }) + end end return M From a94907d8b82a40a0b43feac39598eaec1c129133 Mon Sep 17 00:00:00 2001 From: Connor Robertson Date: Thu, 26 Oct 2023 09:07:11 -0700 Subject: [PATCH 2/3] only set foldmethod locally on buffer --- lua/notebook-navigator/init.lua | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/lua/notebook-navigator/init.lua b/lua/notebook-navigator/init.lua index 62baada..7e7d2ba 100644 --- a/lua/notebook-navigator/init.lua +++ b/lua/notebook-navigator/init.lua @@ -264,8 +264,6 @@ M.setup = function(config) local cell_group = vim.api.nvim_create_augroup("Cells", {clear=true}) if M.config.cell_folding then - local foldm = vim.o.foldmethod - vim.cmd[[set nofoldenable]] vim.api.nvim_create_autocmd("BufEnter", { pattern = {"*"}, group = cell_group, @@ -274,19 +272,12 @@ M.setup = function(config) if M.config.cell_markers[ft] then local marker = M.config.cell_markers[ft] local vim_marker = string.gsub(marker, " ", "\\ ") - local expr = "set foldexpr=(getline(v:lnum)=~'^"..vim_marker.."')==0" - vim.cmd[[set foldmethod=expr]] + vim.cmd[[setlocal foldmethod=expr]] + local expr = "setlocal foldexpr=(getline(v:lnum)=~'^"..vim_marker.."')==0" vim.cmd(expr) end end }) - vim.api.nvim_create_autocmd("BufLeave", { - pattern = {"*"}, - group = cell_group, - callback = function() - vim.cmd("set foldmethod="..foldm) - end - }) end end From 6e95bed09437a667e60d2c7c52209ddfce0a5d54 Mon Sep 17 00:00:00 2001 From: Connor Robertson Date: Thu, 26 Oct 2023 09:16:41 -0700 Subject: [PATCH 3/3] add warning about replacing foldmethod --- README.md | 1 + doc/NotebookNavigator.txt | 1 + lua/notebook-navigator/init.lua | 1 + 3 files changed, 3 insertions(+) diff --git a/README.md b/README.md index 8f3c605..8dbe90a 100644 --- a/README.md +++ b/README.md @@ -122,6 +122,7 @@ Any options that are not specified when calling `setup` will take on their defau repl_provider = "auto", -- If `true`, folding will be based on expression for buffers with a valid -- cell marker and cells will be folded on entering the buffer + -- * NOTE: This will replace any previously set foldmethod for those buffers cell_folding = true } ``` diff --git a/doc/NotebookNavigator.txt b/doc/NotebookNavigator.txt index ad59633..e1376af 100644 --- a/doc/NotebookNavigator.txt +++ b/doc/NotebookNavigator.txt @@ -122,6 +122,7 @@ Default values: repl_provider = "auto", -- If `true`, folding will be based on expression for buffers with a valid -- cell marker and cells will be folded on entering the buffer + -- * NOTE: This will replace any previously set foldmethod for those buffers cell_folding = true } < diff --git a/lua/notebook-navigator/init.lua b/lua/notebook-navigator/init.lua index 7e7d2ba..85d77f8 100644 --- a/lua/notebook-navigator/init.lua +++ b/lua/notebook-navigator/init.lua @@ -211,6 +211,7 @@ M.config = { repl_provider = "auto", -- If `true`, folding will be based on expression for buffers with a valid -- cell marker and cells will be folded on entering the buffer + -- * NOTE: This will replace any previously set foldmethod for those buffers cell_folding = true } --minidoc_afterlines_end