From ec9b84ac3505373877cbb68f794c3409ab05a4dc Mon Sep 17 00:00:00 2001 From: Jonas Hahn Date: Fri, 29 Aug 2025 17:38:23 +0200 Subject: [PATCH] Implemented folding and some improvements. Moved functions to the utils module --- lua/config/autocmds.lua | 18 ++++++++++++++++-- lua/config/keymaps.lua | 14 ++++++++++++++ lua/config/options.lua | 10 ++++++++++ lua/plugins/misc.lua | 4 +++- lua/plugins/nvimtree.lua | 12 +++++------- lua/plugins/searching.lua | 2 +- lua/plugins/treesitter.lua | 1 + lua/plugins/ui.lua | 33 ++++++++------------------------- lua/utils/after.lua | 0 lua/utils/functions.lua | 12 ++++++++++++ 10 files changed, 70 insertions(+), 36 deletions(-) create mode 100644 lua/utils/after.lua diff --git a/lua/config/autocmds.lua b/lua/config/autocmds.lua index 1577c75..ff1101f 100644 --- a/lua/config/autocmds.lua +++ b/lua/config/autocmds.lua @@ -1,9 +1,8 @@ -- Save the last file on exit vim.api.nvim_create_autocmd("VimLeave", { callback = function() - -- Save the last file path to a file local last_file = vim.fn.expand('%:p') -- Get the absolute path of the current file - if last_file ~= "" then + if last_file ~= "" then -- The operator means not equal in lua local file = io.open(vim.fn.stdpath('data') .. "/lastfile.txt", "w") if file then file:write(last_file) @@ -12,3 +11,18 @@ vim.api.nvim_create_autocmd("VimLeave", { end end, }) + +vim.api.nvim_create_augroup("RememberFolds", { + clear = true +}) +vim.api.nvim_create_autocmd({"BufWinLeave"}, { + group = "RememberFolds", + pattern = "*", + command = "silent! mkview", +}) +vim.api.nvim_create_autocmd({"BufWinEnter"}, { + group = "RememberFolds", + pattern = "*", + command = "silent! loadview", +}) + diff --git a/lua/config/keymaps.lua b/lua/config/keymaps.lua index 979e896..d836f36 100644 --- a/lua/config/keymaps.lua +++ b/lua/config/keymaps.lua @@ -33,6 +33,20 @@ vim.keymap.set('n', 'ya', 'ggVG"+y') vim.keymap.set('n', 'ss', ':wa') vim.keymap.set("n", "nn", ":e ~/synced/brainstore/zettelkasten/quick", { silent = true }) +-- Folding +local opts = { noremap = true, silent = true } +local is_all_folded = false +local function toggle_fold() + if is_all_folded then + vim.opt.foldlevel = 99 + else + vim.opt.foldlevel = 0 + end + is_all_folded = not is_all_folded +end +vim.api.nvim_set_keymap("n", "ft", "za", opts) -- toggle fold under cursor +vim.keymap.set("n", "fs", toggle_fold, opts) -- close all folds + -- Quickly open some buffers vim.keymap.set('n', 'occ', ':e ~/.config/nvim/init.lua`.zz') vim.keymap.set('n', 'oct', ':e ~/synced/vault/contacts/contacts.txt`.zz') diff --git a/lua/config/options.lua b/lua/config/options.lua index 7ff7529..9876cc8 100644 --- a/lua/config/options.lua +++ b/lua/config/options.lua @@ -16,3 +16,13 @@ vim.g.loaded_netrw = 1 vim.g.loaded_netrwPlugin = 1 vim.opt.signcolumn = "yes" + +-- Enable Treesitter-based folding +vim.opt.foldmethod = "expr" +vim.opt.foldexpr = "nvim_treesitter#foldexpr()" +vim.opt.foldlevelstart = 99 -- open all folds by default +vim.opt.fillchars = "fold:╌" + +-- 3. Persist folds using mkview/loadview + + diff --git a/lua/plugins/misc.lua b/lua/plugins/misc.lua index c344a40..0c82c33 100644 --- a/lua/plugins/misc.lua +++ b/lua/plugins/misc.lua @@ -1,4 +1,3 @@ -local base_zet = "~/synced/brainstore/zettelkasten" return { { @@ -43,6 +42,8 @@ return { 'Ascyii/telekasten.nvim', dev = true, config = function() + local base_zet = "~/synced/brainstore/zettelkasten" + -- Again can only use opts when not using config require("telekasten").setup({ home = vim.fn.expand(base_zet), @@ -61,6 +62,7 @@ return { uuid_separator = "-", }) + -- Telekasten occupies the z namespace vim.keymap.set("n", "z", "Telekasten panel") vim.keymap.set("n", "zf", "Telekasten find_notes") vim.keymap.set("n", "zg", "Telekasten search_notes") diff --git a/lua/plugins/nvimtree.lua b/lua/plugins/nvimtree.lua index dd55bbd..b9a0225 100644 --- a/lua/plugins/nvimtree.lua +++ b/lua/plugins/nvimtree.lua @@ -2,13 +2,16 @@ return { "nvim-tree/nvim-tree.lua", keys = { { "e", function() - require("nvim-tree.api").tree.toggle({ find_file = true, update_root = true, focus = true, }) + require("nvim-tree.api").tree.toggle({ + find_file = true, + update_root = true, + focus = true, + }) end } }, opts = { sort_by = "case_sensitive", - view = { width = 35, side = "right", @@ -19,7 +22,6 @@ return { update_focused_file = { enable = false, }, - renderer = { group_empty = true, highlight_git = true, @@ -39,25 +41,21 @@ return { }, }, }, - filters = { dotfiles = false, git_clean = false, no_buffer = false, custom = { ".git" }, }, - git = { enable = true, ignore = false, }, - actions = { open_file = { quit_on_open = false, resize_window = true, }, }, - } } diff --git a/lua/plugins/searching.lua b/lua/plugins/searching.lua index d248c92..ebb887a 100644 --- a/lua/plugins/searching.lua +++ b/lua/plugins/searching.lua @@ -57,7 +57,7 @@ return { no_ignore = true, -- Also show files in gitignore follow = true, disable_devicons = true, - prompt_title = "Find Files - custom", + prompt_title = "Find Files", find_command = { "rg", "--files", "--glob", "!**/.git/*", diff --git a/lua/plugins/treesitter.lua b/lua/plugins/treesitter.lua index 5cf93a9..28d2928 100644 --- a/lua/plugins/treesitter.lua +++ b/lua/plugins/treesitter.lua @@ -10,6 +10,7 @@ return { ensure_installed = { "typst", "go", "bash", "python", "nix", "lua" }, highlight = { enable = true }, indent = { enable = true }, + fold = { enable = true }, }) end }, diff --git a/lua/plugins/ui.lua b/lua/plugins/ui.lua index 507b7bd..020c36f 100644 --- a/lua/plugins/ui.lua +++ b/lua/plugins/ui.lua @@ -1,14 +1,4 @@ ---- @return string -local function get_cwd() - local cwd = vim.fn.getcwd() - local home = os.getenv("HOME") - - if cwd:sub(1, #home) == home then - return "~" .. cwd:sub(#home + 1) - else - return cwd - end -end +local utils = require("utils.functions") return { { @@ -21,15 +11,17 @@ return { }, { 'nvim-lualine/lualine.nvim', - dependencies = { 'nvim-tree/nvim-web-devicons' }, + dependencies = { + 'nvim-tree/nvim-web-devicons' + }, opts = { options = { - icons_enabled = false, + icons_enabled = true, theme = 'gruvbox', component_separators = { left = '', right = '' }, section_separators = { left = '', right = '' }, always_divide_middle = true, - always_show_tabline = true, + always_show_tabline = false, globalstatus = false, refresh = { statusline = 300, @@ -40,21 +32,12 @@ return { sections = { lualine_a = { 'mode' }, lualine_b = { 'branch', 'diff', 'diagnostics' }, - lualine_c = { get_cwd, 'filename' }, + lualine_c = { utils.get_cwd(), 'filename' }, lualine_x = { 'encoding', 'fileformat', 'filetype' }, lualine_y = { 'progress' }, lualine_z = { 'location' } }, - inactive_sections = { - lualine_a = {}, - lualine_b = {}, - lualine_c = { 'filename' }, - lualine_x = { 'location' }, - lualine_y = {}, - lualine_z = {} - }, - extensions = { "nvim-tree" } - + extensions = { "nvim-tree" } -- show another line on the tree } }, { diff --git a/lua/utils/after.lua b/lua/utils/after.lua new file mode 100644 index 0000000..e69de29 diff --git a/lua/utils/functions.lua b/lua/utils/functions.lua index b335d2e..341f554 100644 --- a/lua/utils/functions.lua +++ b/lua/utils/functions.lua @@ -5,6 +5,18 @@ function M.sleep(n) os.execute("sleep " .. tonumber(n)) end +--- @return string +function M.get_cwd() + local cwd = vim.fn.getcwd() + local home = os.getenv("HOME") + + if cwd:sub(1, #home) == home then + return "~" .. cwd:sub(#home + 1) + else + return cwd + end +end + --- @return {} function M.get_lsp_servers() local servers = { "lua_ls" }