From 392f1b786183d89c063dff2091176ac2339c4138 Mon Sep 17 00:00:00 2001 From: Mathias Schneider Date: Wed, 15 Apr 2026 18:28:02 +0200 Subject: [PATCH] fixed folding -- again --- term/.config/nvim/init.lua | 10 +++ term/.config/nvim/lua/basic-settings.lua | 43 ++++++++--- term/.config/nvim/lua/plugins/treesitter.lua | 76 ++++++++++++-------- 3 files changed, 92 insertions(+), 37 deletions(-) diff --git a/term/.config/nvim/init.lua b/term/.config/nvim/init.lua index d232765..6b9eacf 100644 --- a/term/.config/nvim/init.lua +++ b/term/.config/nvim/init.lua @@ -92,3 +92,13 @@ vim.keymap.set("n", "", ":m .+1==", { desc = "Move line down" }) vim.keymap.set("n", "", ":m .-2==", { desc = "Move line up" }) vim.keymap.set("v", "", ":m '>+1gv=gv", { desc = "Move selection down" }) vim.keymap.set("v", "", ":m '<-2gv=gv", { desc = "Move selection up" }) + +-- Folding keymaps +vim.keymap.set("n", "za", "za", { desc = "Toggle fold under cursor" }) +vim.keymap.set("n", "zA", "zA", { desc = "Toggle fold recursively" }) +vim.keymap.set("n", "zR", "zR", { desc = "Open all folds" }) +vim.keymap.set("n", "zM", "zM", { desc = "Close all folds" }) +vim.keymap.set("n", "zo", "zo", { desc = "Open fold under cursor" }) +vim.keymap.set("n", "zc", "zc", { desc = "Close fold under cursor" }) +vim.keymap.set("n", "zO", "zO", { desc = "Open all folds under cursor" }) +vim.keymap.set("n", "zC", "zC", { desc = "Close all folds under cursor" }) diff --git a/term/.config/nvim/lua/basic-settings.lua b/term/.config/nvim/lua/basic-settings.lua index 65df423..ebf708a 100644 --- a/term/.config/nvim/lua/basic-settings.lua +++ b/term/.config/nvim/lua/basic-settings.lua @@ -10,20 +10,45 @@ - vim.go.* for global option shorthand ]] +-- ============================================================================ +-- TREESITTER (Built-in Neovim 0.10+ features) +-- ============================================================================ +-- Enable Treesitter syntax highlighting (native in Neovim 0.10+) +-- This replaces the old nvim-treesitter highlight module +vim.api.nvim_create_autocmd("FileType", { + pattern = "*", + callback = function() + if vim.bo.buftype == "" and vim.bo.filetype ~= "" then + pcall(vim.treesitter.start) + end + end, + desc = "Enable Treesitter highlighting", +}) + -- ============================================================================ -- FOLDING SETTINGS (Code folding) -- ============================================================================ --- Foldmethod determines how code blocks are detected and folded --- "expr" means folds are based on expressions (we use treesitter for this) -vim.opt.foldmethod = "expr" - --- Foldexpr is the expression used to determine fold level --- v:lua.vim.treesitter.foldexpr() uses Treesitter to calculate folds -vim.opt.foldexpr = "v:lua.vim.treesitter.foldexpr()" +-- NOTE: In Neovim 0.10+, Treesitter folding must be set per-buffer via FileType autocmd +-- Setting it globally causes the foldexpr to evaluate before Treesitter attaches -- Foldlevel: Higher number = more folds are open by default --- 99 means almost all folds are open when opening a file -vim.opt.foldlevel = 99 +-- 1-2 means you'll see folds by default; 99 means all folds are open +vim.opt.foldlevel = 1 +vim.opt.foldlevelstart = 1 + +-- Setup Treesitter folding for each buffer after filetype is detected +-- This ensures the parser is loaded before foldexpr evaluates +vim.api.nvim_create_autocmd("FileType", { + pattern = "*", + callback = function() + -- Only set folding for file buffers with Treesitter support + if vim.bo.buftype == "" then + vim.wo.foldmethod = "expr" + vim.wo.foldexpr = "v:lua.vim.treesitter.foldexpr()" + end + end, + desc = "Setup Treesitter folding per filetype", +}) -- ============================================================================ -- LINE NUMBERS diff --git a/term/.config/nvim/lua/plugins/treesitter.lua b/term/.config/nvim/lua/plugins/treesitter.lua index 1d7bafd..11accda 100644 --- a/term/.config/nvim/lua/plugins/treesitter.lua +++ b/term/.config/nvim/lua/plugins/treesitter.lua @@ -3,36 +3,56 @@ return { build = ":TSUpdate", event = { "BufReadPost", "BufNewFile" }, config = function() - -- Ensure nvim-treesitter queries are preferred over runtime queries - -- This fixes the "except*" syntax error with updated parsers - vim.opt.runtimepath:prepend(vim.fn.stdpath("data") .. "/lazy/nvim-treesitter") + -- In the latest nvim-treesitter (post-refactor), the plugin only handles + -- parser installation. Highlighting, folding, and indenting are handled + -- by native Neovim Treesitter integration. - local ok, configs = pcall(require, "nvim-treesitter.configs") + -- CRITICAL: Add nvim-treesitter/runtime to runtimepath so queries are found + local ts_runtime = vim.fn.stdpath("data") .. "/lazy/nvim-treesitter/runtime" + vim.opt.runtimepath:prepend(ts_runtime) + + -- Setup nvim-treesitter (minimal config for parser installation) + require("nvim-treesitter").setup({ + -- Parser installation directory (default: stdpath('data')/site) + install_dir = vim.fn.stdpath("data") .. "/site", + }) + + -- Auto-install parsers for these languages when opening a file + local ensure_installed = { + "python", + "lua", + "json", + "yaml", + "javascript", + "typescript", + "tsx", + "vue", + "html", + "css", + "bash", + "markdown", + "markdown_inline", + } + + -- Install parsers + local ok, install = pcall(require, "nvim-treesitter.install") if ok then - configs.setup({ - auto_install = true, - ensure_installed = { - "python", - "lua", - "json", - "yaml", - "javascript", - "typescript", - "tsx", - "vue", - "html", - "css", - "bash", - "markdown", - "markdown_inline", - }, - highlight = { - enable = true, - -- Disable additional_vim_regex_highlighting to avoid conflicts - additional_vim_regex_highlighting = false, - }, - indent = { enable = true }, - }) + for _, lang in ipairs(ensure_installed) do + vim.schedule(function() + install.install({ lang }) + end) + end end + + -- Enable Treesitter-based indenting (natively supported by Neovim) + vim.api.nvim_create_autocmd("FileType", { + pattern = "*", + callback = function() + if vim.bo.buftype == "" and vim.bo.filetype ~= "" then + vim.bo.indentexpr = "v:lua.vim.treesitter.indentexpr()" + end + end, + desc = "Enable Treesitter indenting", + }) end, }