robust treesitter setup for upcoming changes

This commit is contained in:
Daniel Winkler 2026-01-31 15:24:16 +11:00
commit aa39a3c207
6 changed files with 186 additions and 90 deletions

8
flake.lock generated
View file

@ -22,16 +22,16 @@
},
"nixpkgs": {
"locked": {
"lastModified": 1769598131,
"narHash": "sha256-e7VO/kGLgRMbWtpBqdWl0uFg8Y2XWFMdz0uUJvlML8o=",
"lastModified": 1769461804,
"narHash": "sha256-msG8SU5WsBUfVVa/9RPLaymvi5bI8edTavbIq3vRlhI=",
"owner": "nixos",
"repo": "nixpkgs",
"rev": "fa83fd837f3098e3e678e6cf017b2b36102c7211",
"rev": "bfc1b8a4574108ceef22f02bafcf6611380c100d",
"type": "github"
},
"original": {
"owner": "nixos",
"ref": "nixos-25.11",
"ref": "nixos-unstable",
"repo": "nixpkgs",
"type": "github"
}

View file

@ -4,7 +4,8 @@
description = "Daniel's NixCats";
inputs = {
nixpkgs.url = "github:nixos/nixpkgs/nixos-25.11";
nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
#nixpkgs.url = "github:nixos/nixpkgs/nixos-25.11";
wrappers = {
url = "github:BirdeeHub/nix-wrapper-modules";
inputs.nixpkgs.follows = "nixpkgs";
@ -44,7 +45,7 @@
clickhouse = def false;
gitPlugins = def true;
julia = def false;
lua = def false;
lua = def true;
markdown = def false;
nix = def true;
optional = def false;

View file

@ -38,7 +38,7 @@
general = lib.mkDefault true;
gitPlugins = lib.mkDefault true;
julia = lib.mkDefault false;
lua = lib.mkDefault false;
lua = lib.mkDefault true;
markdown = lib.mkDefault false;
nix = lib.mkDefault true;
optional = lib.mkDefault false;

View file

@ -190,18 +190,37 @@ end)
-- Treesitter
now_if_args(function()
vim.treesitter.language.register("markdown", { "markdown", "codecompanion" })
vim.treesitter.language.register("markdown", { "markdown", "codecompanion", "rmd", "quarto" })
-- Base configuration
require 'treesitter-context'.setup {
enable = true,
multiwindow = false, -- Enable multiwindow support.
max_lines = 30, -- How many lines the window should span. Values <= 0 mean no limit.
min_window_height = 70, -- Minimum editor window height to enable context. Values <= 0 mean no limit.
line_numbers = true,
multiline_threshold = 10, -- Maximum number of lines to show for a single context
trim_scope = 'outer', -- Which context lines to discard if `max_lines` is exceeded. Choices: 'inner', 'outer'
mode = 'cursor', -- Line used to calculate context. Choices: 'cursor', 'topline'
-- Separator between context and content. Should be a single character string, like '-'.
-- When separator is set, the context will only show up when there are at least 2 lines above cursorline.
separator = '-',
zindex = 20, -- The Z-index of the context window
on_attach = nil, -- (fun(buf: integer): boolean) return false to disable attaching
}
local ok_configs, configs = pcall(require, "nvim-treesitter.configs")
if ok_configs and configs.setup then
local opts = {
highlight = { enable = true },
indent = { enable = false },
textobjects = {
move = {
enable = true,
set_jumps = true, -- whether to set jumps in the jumplist
set_jumps = true,
goto_next_start = {
["]a"] = "@paramter.inner",
["]a"] = "@parameter.inner", -- fixed typo
["]f"] = "@function.outer",
["]o"] = "@loop.*",
["]s"] = { query = "@local.scope", desc = "Next scope" },
@ -227,7 +246,7 @@ now_if_args(function()
},
goto_previous = {
["[e"] = "@conditional.outer",
}
},
},
swap = {
enable = true,
@ -240,7 +259,7 @@ now_if_args(function()
},
lsp_interop = {
enable = true,
border = 'none',
border = "none",
floating_preview_opts = {},
peek_definition_code = {
["<leader>lm"] = "@function.outer",
@ -250,14 +269,6 @@ now_if_args(function()
},
}
-- Environment-specific Overrides
if not Config.isNixCats then
opts.auto_install = true
opts.ensure_installed = Config.treesitter_helpers.default_parsers
else
opts.auto_install = false
-- Nix handles installation, so ensure_installed is skipped/empty
end
-- Manual parser check for non-Nix users
if not Config.isNixCats then
@ -269,25 +280,108 @@ now_if_args(function()
require("nvim-treesitter").install(to_install)
end
end
-- Environment-specific Overrides
if not Config.isNixCats then
opts.auto_install = true
opts.ensure_installed = Config.treesitter_helpers.default_parsers
else
opts.auto_install = false
-- Nix handles installation, so ensure_installed is skipped/empty
end
local configs = require("nvim-treesitter.configs")
configs.setup(opts)
require 'treesitter-context'.setup {
enable = true,
multiwindow = false, -- Enable multiwindow support.
max_lines = 30, -- How many lines the window should span. Values <= 0 mean no limit.
min_window_height = 70, -- Minimum editor window height to enable context. Values <= 0 mean no limit.
line_numbers = true,
multiline_threshold = 10, -- Maximum number of lines to show for a single context
trim_scope = 'outer', -- Which context lines to discard if `max_lines` is exceeded. Choices: 'inner', 'outer'
mode = 'cursor', -- Line used to calculate context. Choices: 'cursor', 'topline'
-- Separator between context and content. Should be a single character string, like '-'.
-- When separator is set, the context will only show up when there are at least 2 lines above cursorline.
separator = '-',
zindex = 20, -- The Z-index of the context window
on_attach = nil, -- (fun(buf: integer): boolean) return false to disable attaching
return
end
vim.api.nvim_create_autocmd("FileType", {
pattern = "*",
callback = function(args)
-- Use explicit buffer + filetype to avoid any ambiguity
local ok = pcall(vim.treesitter.start, args.buf, args.match)
vim.bo.syntax = 'on'
end,
})
-- Textobjects: require plugin and bail out quietly if missing
local ok_nto, nto = pcall(require, "nvim-treesitter-textobjects")
if not ok_nto then
return
end
nto.setup({
move = {
set_jumps = true,
},
})
local move = require("nvim-treesitter-textobjects.move")
local swap = require("nvim-treesitter-textobjects.swap")
-- Map motion function names to actual functions
local move_fns = {
goto_next_start = move.goto_next_start,
goto_next_end = move.goto_next_end,
goto_previous_start = move.goto_previous_start,
goto_previous_end = move.goto_previous_end,
goto_next = move.goto_next,
goto_previous = move.goto_previous,
}
-- All motions defined in one place
-- spec = { query_or_list, query_group, desc }
local move_maps = {
goto_next_start = {
["]a"] = { "@parameter.inner", "textobjects", "Next parameter" },
["]f"] = { "@function.outer", "textobjects", "Next function start" },
["]o"] = { { "@loop.inner", "@loop.outer" }, "textobjects", "Next loop" },
["]s"] = { "@local.scope", "locals", "Next scope" },
["]z"] = { "@fold", "folds", "Next fold" },
},
goto_next_end = {
["]M"] = { "@function.outer", "textobjects", "Next function end" },
["]["] = { "@class.outer", "textobjects", "Next class end" },
},
goto_previous_start = {
["[a"] = { "@parameter.inner", "textobjects", "Previous parameter" },
["[f"] = { "@function.outer", "textobjects", "Previous function start" },
["[o"] = { { "@loop.inner", "@loop.outer" }, "textobjects", "Previous loop" },
["[s"] = { "@local.scope", "locals", "Previous scope" },
["[z"] = { "@fold", "folds", "Previous fold" },
},
goto_previous_end = {
["[M"] = { "@function.outer", "textobjects", "Previous function end" },
["[]"] = { "@class.outer", "textobjects", "Previous class end" },
},
goto_next = {
["]e"] = { "@conditional.outer", "textobjects", "Next conditional" },
},
goto_previous = {
["[e"] = { "@conditional.outer", "textobjects", "Previous conditional" },
},
}
-- Generate motion keymaps
for fn_name, maps in pairs(move_maps) do
local fn = move_fns[fn_name]
if fn then
for lhs, spec in pairs(maps) do
local query_or_list, group, desc = spec[1], spec[2], spec[3]
vim.keymap.set({ "n", "x", "o" }, lhs, function()
fn(query_or_list, group)
end, { desc = desc })
end
end
end
-- Swap keymaps (unchanged, but minimal)
vim.keymap.set("n", "<leader>x", function()
swap.swap_next("@parameter.inner")
end, { desc = "Swap with next parameter" })
vim.keymap.set("n", "<leader>X", function()
swap.swap_previous("@parameter.inner")
end, { desc = "Swap with previous parameter" })
end)
-- zk

View file

@ -17,7 +17,7 @@ later(function()
formatters_by_ft = {
javascript = { "prettier" },
json = { "prettier" },
python = { "black" },
python = { "ruff_format", "ruff_organize_imports" },
nix = { "alejandra" },
-- r = { "my_styler" },
rmd = { "injected" },

View file

@ -50,6 +50,7 @@ now_if_args(function()
},
diagnostics = {
disable = { "trailing-space" },
globals = { "vim" },
},
workspace = {
checkThirdParty = false,