From a086657031c9da61fc6744d94d2cfdce38180ff1 Mon Sep 17 00:00:00 2001 From: Chris Date: Wed, 10 Dec 2025 14:19:07 +0000 Subject: [PATCH] Treesitter update --- lua/autocmd.lua | 31 +++- lua/keymap.lua | 15 +- lua/plugins/treesitter-textobjects.lua | 233 ++++++++++++++++--------- lua/plugins/treesitter.lua | 26 ++- 4 files changed, 198 insertions(+), 107 deletions(-) diff --git a/lua/autocmd.lua b/lua/autocmd.lua index 1133bb3..5299a5f 100644 --- a/lua/autocmd.lua +++ b/lua/autocmd.lua @@ -30,7 +30,7 @@ vim.api.nvim_create_autocmd('SwapExists', { desc = 'Always choose to delete the swap file, files are saved automatically', group = vim.api.nvim_create_augroup('NoSwaps', { clear = true }), callback = function(args) - vim.cmd("let v:swapchoice = 'd'") + vim.cmd "let v:swapchoice = 'd'" end, }) @@ -116,4 +116,33 @@ vim.api.nvim_create_autocmd('BufWritePost', { end, }) +vim.api.nvim_create_autocmd('FileType', { + pattern = { + 'bash', + 'c', + 'diff', + 'html', + 'lua', + 'luadoc', + 'markdown', + 'markdown_inline', + 'query', + 'vim', + 'vimdoc', + 'php', + 'javascript', + 'typescript', + 'json', + }, + callback = function() + -- syntax highlighting, provided by Neovim + vim.treesitter.start() + -- folds, provided by Neovim + vim.wo.foldexpr = 'v:lua.vim.treesitter.foldexpr()' + vim.wo.foldmethod = 'expr' + -- indentation, provided by nvim-treesitter + vim.bo.indentexpr = "v:lua.require'nvim-treesitter'.indentexpr()" + end, +}) + require('helpers').edit_cf('a', '/lua/autocmd.lua') diff --git a/lua/keymap.lua b/lua/keymap.lua index 91f361d..e816c79 100644 --- a/lua/keymap.lua +++ b/lua/keymap.lua @@ -30,13 +30,13 @@ vim.keymap.set('n', 'c', function() vim.treesitter.inspect_tree() end, { desc = 'Treesitter' }) -vim.keymap.set('n', '', function() - local node = vim.treesitter.get_node {} - local range = { vim.treesitter.get_node_range(node) } - vim.api.nvim_win_set_cursor(0, { range[3] + 1, range[4] - 1 }) - vim.fn.setpos("'x", { 0, range[1] + 1, range[2] + 1, 0 }) - vim.cmd.normal 'v`x' -end, { desc = 'Select surrounding treesitter node' }) +-- vim.keymap.set('n', '', function() +-- local node = vim.treesitter.get_node {} +-- local range = { vim.treesitter.get_node_range(node) } +-- vim.api.nvim_win_set_cursor(0, { range[3] + 1, range[4] - 1 }) +-- vim.fn.setpos("'x", { 0, range[1] + 1, range[2] + 1, 0 }) +-- vim.cmd.normal 'v`x' +-- end, { desc = 'Select surrounding treesitter node' }) vim.keymap.set('v', '', function() local start = vim.api.nvim_win_get_cursor(0) @@ -258,7 +258,6 @@ vim.keymap.set('n', ']', 'cnext', { desc = 'Next item in quickf vim.keymap.set('n', '[', 'cprevious', { desc = 'Previous item in quickfix list' }) vim.keymap.set('n', 'gd', 'Telescope lsp_definitions', { desc = 'Go to definition' }) - local function open_test() require('neotest').summary.open() require('neotest').output_panel.open() diff --git a/lua/plugins/treesitter-textobjects.lua b/lua/plugins/treesitter-textobjects.lua index 6e58f32..26b0a6d 100644 --- a/lua/plugins/treesitter-textobjects.lua +++ b/lua/plugins/treesitter-textobjects.lua @@ -1,86 +1,153 @@ +-- Highlight, edit, and navigate code return { - 'nvim-treesitter/nvim-treesitter-textobjects', - dependencies = { - 'nvim-treesitter/nvim-treesitter', - }, - lazy = false, - enabled = true, - config = function() - require('nvim-treesitter.configs').setup { - incremental_selection = { - enable = true, - keymaps = { - -- mappings for incremental selection (visual mappings) - init_selection = 'gnn', -- maps in normal mode to init the node/scope selection - node_incremental = 'grn', -- increment to the upper named parent - scope_incremental = 'grc', -- increment to the upper scope (as defined in locals.scm) - node_decremental = 'grm', -- decrement to the previous node - }, - }, + -- 'nvim-treesitter/nvim-treesitter-textobjects', + -- lazy = true, + -- branch = 'main', + -- dependencies = { + -- 'nvim-treesitter/nvim-treesitter', + -- }, + -- config = function() + -- -- configuration + -- require('nvim-treesitter-textobjects').setup { + -- select = { + -- lookahead = true, + -- selection_modes = { + -- ['@parameter.outer'] = 'v', -- charwise + -- ['@function.outer'] = 'V', -- linewise + -- ['@class.outer'] = 'V', -- blockwise + -- }, + -- include_surrounding_whitespace = false, + -- }, + -- move = { + -- set_jumps = true, + -- }, + -- } - textobjects = { - -- syntax-aware textobjects - enable = true, - keymaps = { - ['iL'] = { - -- you can define your own textobjects directly here - go = '(function_definition) @function', - }, - -- or you use the queries from supported languages with textobjects.scm - ['af'] = '@function.outer', - ['if'] = '@function.inner', - ['aC'] = '@class.outer', - ['iC'] = '@class.inner', - ['ay'] = '@conditional.outer', - ['iy'] = '@conditional.inner', - ['aj'] = '@loop.outer', - ['ij'] = '@loop.inner', - ['is'] = '@statement.inner', - ['as'] = '@statement.outer', - ['ac'] = '@comment.outer', - ['ic'] = '@comment.inner', - ['ap'] = '@comment.parameter', - ['ip'] = '@comment.parameter', - }, - move = { - enable = true, - set_jumps = true, -- whether to set jumps in the jumplist - goto_next_start = { - [']m'] = '@function.outer', - [']]'] = '@class.outer', - }, - goto_next_end = { - [']M'] = '@function.outer', - [']['] = '@class.outer', - }, - goto_previous_start = { - ['[m'] = '@function.outer', - ['[['] = '@class.outer', - }, - goto_previous_end = { - ['[M'] = '@function.outer', - ['[]'] = '@class.outer', - }, - }, - select = { - enable = true, - keymaps = { - -- You can use the capture groups defined in textobjects.scm - ['af'] = '@function.outer', - ['if'] = '@function.inner', - ['ac'] = '@class.outer', - ['ic'] = '@class.inner', - -- Or you can define your own textobjects like this - ['iF'] = { - python = '(function_definition) @function', - cpp = '(function_definition) @function', - c = '(function_definition) @function', - java = '(method_declaration) @function', - go = '(method_declaration) @function', - }, - }, - }, - }, - } - end, + -- for keys, query in + -- { + -- -- or you use the queries from supported languages with textobjects.scm + -- ['af'] = '@function.outer', + -- ['if'] = '@function.inner', + -- ['aC'] = '@class.outer', + -- ['iC'] = '@class.inner', + -- ['ay'] = '@conditional.outer', + -- ['iy'] = '@conditional.inner', + -- ['aj'] = '@loop.outer', + -- ['ij'] = '@loop.inner', + -- ['is'] = '@statement.inner', + -- ['as'] = '@statement.outer', + -- ['ac'] = '@comment.outer', + -- ['ic'] = '@comment.inner', + -- ['ap'] = '@comment.parameter', + -- ['ip'] = '@comment.parameter', + -- ['an'] = '@local.scope', + -- } + -- do + -- vim.keymap.set({ 'n', 'x', 'o' }, keys, function() + -- require('nvim-treesitter-textobjects.select').select_textobject(query, 'textobjects') + -- end) + -- end + + -- for keys, query in + -- { + -- -- or you use the queries from supported languages with textobjects.scm + -- [']'] = '@function.outer', + -- ['o'] = { '@loop.inner', '@loop.outer' }, + -- ['n'] = '@local.scope', + -- ['y'] = '@conditional.outer', + -- ['e'] = '@statement.outer', + -- ['/'] = '@comment.outer', + -- } + -- do + -- vim.keymap.set({ 'n', 'x', 'o' }, ']' .. keys, function() + -- require('nvim-treesitter-textobjects.move').goto_next_start(query, 'textobjects') + -- end) + -- vim.keymap.set({ 'n', 'x', 'o' }, '[' .. keys, function() + -- require('nvim-treesitter-textobjects.move').goto_previous_start(query, 'textobjects') + -- end) + -- end + -- -- require('nvim-treesitter.configs').setup { + -- -- incremental_selection = { + -- -- enable = true, + -- -- keymaps = { + -- -- -- mappings for incremental selection (visual mappings) + -- -- init_selection = '', -- maps in normal mode to init the node/scope selection + -- -- node_incremental = '', -- increment to the upper named parent + -- -- scope_incremental = '', -- increment to the upper scope (as defined in locals.scm) + -- -- node_decremental = '', -- decrement to the previous node + -- -- }, + -- -- }, + + -- -- textobjects = { + -- -- -- syntax-aware textobjects + -- -- enable = true, + -- -- keymaps = { + -- -- ['iL'] = { + -- -- -- you can define your own textobjects directly here + -- -- go = '(function_definition) @function', + -- -- }, + -- -- -- or you use the queries from supported languages with textobjects.scm + -- -- ['af'] = '@function.outer', + -- -- ['if'] = '@function.inner', + -- -- ['aC'] = '@class.outer', + -- -- ['iC'] = '@class.inner', + -- -- ['ay'] = '@conditional.outer', + -- -- ['iy'] = '@conditional.inner', + -- -- ['aj'] = '@loop.outer', + -- -- ['ij'] = '@loop.inner', + -- -- ['is'] = '@statement.inner', + -- -- ['as'] = '@statement.outer', + -- -- ['ac'] = '@comment.outer', + -- -- ['ic'] = '@comment.inner', + -- -- ['ap'] = '@comment.parameter', + -- -- ['ip'] = '@comment.parameter', + -- -- }, + -- -- move = { + -- -- enable = true, + -- -- set_jumps = true, -- whether to set jumps in the jumplist + -- -- goto_next_start = { + -- -- [']m'] = '@function.outer', + -- -- [']]'] = '@class.outer', + -- -- }, + -- -- goto_next_end = { + -- -- [']M'] = '@function.outer', + -- -- [']['] = '@class.outer', + -- -- }, + -- -- goto_previous_start = { + -- -- ['[m'] = '@function.outer', + -- -- ['[['] = '@class.outer', + -- -- }, + -- -- goto_previous_end = { + -- -- ['[M'] = '@function.outer', + -- -- ['[]'] = '@class.outer', + -- -- }, + -- -- }, + -- -- select = { + -- -- enable = true, + -- -- keymaps = { + -- -- -- You can use the capture groups defined in textobjects.scm + -- -- ['af'] = '@function.outer', + -- -- ['if'] = '@function.inner', + -- -- ['ac'] = '@class.outer', + -- -- ['ic'] = '@class.inner', + -- -- -- Or you can define your own textobjects like this + -- -- ['iF'] = { + -- -- python = '(function_definition) @function', + -- -- cpp = '(function_definition) @function', + -- -- c = '(function_definition) @function', + -- -- java = '(method_declaration) @function', + -- -- go = '(method_declaration) @function', + -- -- }, + -- -- }, + -- -- }, + -- -- }, + -- -- } + -- end, + -- opts = {}, + -- -- There are additional nvim-treesitter modules that you can use to interact + -- -- with nvim-treesitter. You should go explore a few and see what interests you: + -- -- + -- -- - Incremental selection: Included, see `:help nvim-treesitter-incremental-selection-mod` + -- -- - Show your current context: https://github.com/nvim-treesitter/nvim-treesitter-context + -- -- - Treesitter + textobjects: https://github.com/nvim-treesitter/nvim-treesitter-textobjects } diff --git a/lua/plugins/treesitter.lua b/lua/plugins/treesitter.lua index 66edc49..9a373b0 100644 --- a/lua/plugins/treesitter.lua +++ b/lua/plugins/treesitter.lua @@ -2,10 +2,11 @@ return { 'nvim-treesitter/nvim-treesitter', build = ':TSUpdate', - main = 'nvim-treesitter.configs', -- Sets main module to use for opts + branch = 'main', -- [[ Configure Treesitter ]] See `:help nvim-treesitter` - opts = { - ensure_installed = { + config = function() + require('nvim-treesitter').setup {} + require('nvim-treesitter').install { 'bash', 'c', 'diff', @@ -17,18 +18,13 @@ return { 'query', 'vim', 'vimdoc', - }, - -- Autoinstall languages that are not installed - auto_install = true, - highlight = { - enable = true, - -- Some languages depend on vim's regex highlighting system (such as Ruby) for indent rules. - -- If you are experiencing weird indenting issues, add the language to - -- the list of additional_vim_regex_highlighting and disabled languages for indent. - additional_vim_regex_highlighting = { 'ruby' }, - }, - indent = { enable = true, disable = { 'ruby' } }, - }, + 'php', + 'javascript', + 'typescript', + 'json', + } + end, + opts = {}, -- There are additional nvim-treesitter modules that you can use to interact -- with nvim-treesitter. You should go explore a few and see what interests you: --