diff --git a/.config/nvim/after/queries/ecma/textobjects.scm b/.config/nvim/after/queries/ecma/textobjects.scm new file mode 100644 index 0000000..baac449 --- /dev/null +++ b/.config/nvim/after/queries/ecma/textobjects.scm @@ -0,0 +1,5 @@ +; extends +(object + (pair + key: (_) @property.lhs + value: (_) @property.inner @property.rhs) @property.outer) diff --git a/.config/nvim/lua/josean/plugins/nvim-treesitter-text-objects.lua b/.config/nvim/lua/josean/plugins/nvim-treesitter-text-objects.lua index ea67935..68d5164 100644 --- a/.config/nvim/lua/josean/plugins/nvim-treesitter-text-objects.lua +++ b/.config/nvim/lua/josean/plugins/nvim-treesitter-text-objects.lua @@ -1,9 +1,6 @@ return { "nvim-treesitter/nvim-treesitter-textobjects", - event = { "BufReadPost", "BufNewFile" }, - dependencies = { - "nvim-treesitter/nvim-treesitter", - }, + lazy = true, config = function() require("nvim-treesitter.configs").setup({ textobjects = { @@ -15,39 +12,99 @@ return { keymaps = { -- You can use the capture groups defined in textobjects.scm - ["a="] = { query = "@assignment.outer", desc = "Select outer part of an assignment region" }, - ["i="] = { query = "@assignment.inner", desc = "Select inner part of an assignment region" }, + ["a="] = { query = "@assignment.outer", desc = "Select outer part of an assignment" }, + ["i="] = { query = "@assignment.inner", desc = "Select inner part of an assignment" }, + ["l="] = { query = "@assignment.lhs", desc = "Select left hand side of an assignment" }, + ["r="] = { query = "@assignment.rhs", desc = "Select right hand side of an assignment" }, - ["a:"] = { query = "@parameter.outer", desc = "Select outer part of a parameter/field region" }, - ["i:"] = { query = "@parameter.inner", desc = "Select inner part of a parameter/field region" }, + -- works for javascript/typescript files (custom capture I created in after/queries/ecma/textobjects.scm) + ["a:"] = { query = "@property.outer", desc = "Select outer part of an object property" }, + ["i:"] = { query = "@property.inner", desc = "Select inner part of an object property" }, + ["l:"] = { query = "@property.lhs", desc = "Select left part of an object property" }, + ["r:"] = { query = "@property.rhs", desc = "Select right part of an object property" }, - ["ai"] = { query = "@conditional.outer", desc = "Select outer part of a conditional region" }, - ["ii"] = { query = "@conditional.inner", desc = "Select inner part of a conditional region" }, + ["aa"] = { query = "@parameter.outer", desc = "Select outer part of a parameter/argument" }, + ["ia"] = { query = "@parameter.inner", desc = "Select inner part of a parameter/argument" }, - ["al"] = { query = "@loop.outer", desc = "Select outer part of a loop region" }, - ["il"] = { query = "@loop.inner", desc = "Select inner part of a loop region" }, + ["ai"] = { query = "@conditional.outer", desc = "Select outer part of a conditional" }, + ["ii"] = { query = "@conditional.inner", desc = "Select inner part of a conditional" }, - ["ab"] = { query = "@block.outer", desc = "Select outer part of a block region" }, -- overrides default text object block of parenthesis to parenthesis - ["ib"] = { query = "@block.inner", desc = "Select inner part of a block region" }, -- overrides default text object block of parenthesis to parenthesis + ["al"] = { query = "@loop.outer", desc = "Select outer part of a loop" }, + ["il"] = { query = "@loop.inner", desc = "Select inner part of a loop" }, - ["af"] = { query = "@function.outer", desc = "Select outer part of a function region" }, - ["if"] = { query = "@function.inner", desc = "Select inner part of a function region" }, + ["af"] = { query = "@call.outer", desc = "Select outer part of a function call" }, + ["if"] = { query = "@call.inner", desc = "Select inner part of a function call" }, - ["ac"] = { query = "@class.outer", desc = "Select outer part of a class region" }, - ["ic"] = { query = "@class.inner", desc = "Select inner part of a class region" }, + ["am"] = { query = "@function.outer", desc = "Select outer part of a method/function definition" }, + ["im"] = { query = "@function.inner", desc = "Select inner part of a method/function definition" }, + + ["ac"] = { query = "@class.outer", desc = "Select outer part of a class" }, + ["ic"] = { query = "@class.inner", desc = "Select inner part of a class" }, }, - include_surrounding_whitespace = true, }, swap = { enable = true, swap_next = { - ["on"] = "@parameter.inner", -- swap object under cursor with next + ["na"] = "@parameter.inner", -- swap parameters/argument with next + ["n:"] = "@property.outer", -- swap object property with next + ["nm"] = "@function.outer", -- swap function with next }, swap_previous = { - ["op"] = "@parameter.inner", -- swap object under cursor with previous + ["pa"] = "@parameter.inner", -- swap parameters/argument with prev + ["p:"] = "@property.outer", -- swap object property with prev + ["pm"] = "@function.outer", -- swap function with previous + }, + }, + move = { + enable = true, + set_jumps = true, -- whether to set jumps in the jumplist + goto_next_start = { + ["]f"] = { query = "@call.outer", desc = "Next function call start" }, + ["]m"] = { query = "@function.outer", desc = "Next method/function def start" }, + ["]c"] = { query = "@class.outer", desc = "Next class start" }, + ["]i"] = { query = "@conditional.outer", desc = "Next conditional start" }, + ["]l"] = { query = "@loop.outer", desc = "Next loop start" }, + + -- You can pass a query group to use query from `queries//.scm file in your runtime path. + -- Below example nvim-treesitter's `locals.scm` and `folds.scm`. They also provide highlights.scm and indent.scm. + ["]s"] = { query = "@scope", query_group = "locals", desc = "Next scope" }, + ["]z"] = { query = "@fold", query_group = "folds", desc = "Next fold" }, + }, + goto_next_end = { + ["]F"] = { query = "@call.outer", desc = "Next function call end" }, + ["]M"] = { query = "@function.outer", desc = "Next method/function def end" }, + ["]C"] = { query = "@class.outer", desc = "Next class end" }, + ["]I"] = { query = "@conditional.outer", desc = "Next conditional end" }, + ["]L"] = { query = "@loop.outer", desc = "Next loop end" }, + }, + goto_previous_start = { + ["[f"] = { query = "@call.outer", desc = "Prev function call start" }, + ["[m"] = { query = "@function.outer", desc = "Prev method/function def start" }, + ["[c"] = { query = "@class.outer", desc = "Prev class start" }, + ["[i"] = { query = "@conditional.outer", desc = "Prev conditional start" }, + ["[l"] = { query = "@loop.outer", desc = "Prev loop start" }, + }, + goto_previous_end = { + ["[F"] = { query = "@call.outer", desc = "Prev function call end" }, + ["[M"] = { query = "@function.outer", desc = "Prev method/function def end" }, + ["[C"] = { query = "@class.outer", desc = "Prev class end" }, + ["[I"] = { query = "@conditional.outer", desc = "Prev conditional end" }, + ["[L"] = { query = "@loop.outer", desc = "Prev loop end" }, }, }, }, }) + + local ts_repeat_move = require("nvim-treesitter.textobjects.repeatable_move") + + -- vim way: ; goes to the direction you were moving. + vim.keymap.set({ "n", "x", "o" }, ";", ts_repeat_move.repeat_last_move) + vim.keymap.set({ "n", "x", "o" }, ",", ts_repeat_move.repeat_last_move_opposite) + + -- Optionally, make builtin f, F, t, T also repeatable with ; and , + vim.keymap.set({ "n", "x", "o" }, "f", ts_repeat_move.builtin_f) + vim.keymap.set({ "n", "x", "o" }, "F", ts_repeat_move.builtin_F) + vim.keymap.set({ "n", "x", "o" }, "t", ts_repeat_move.builtin_t) + vim.keymap.set({ "n", "x", "o" }, "T", ts_repeat_move.builtin_T) end, } diff --git a/.config/nvim/lua/josean/plugins/nvim-treesitter.lua b/.config/nvim/lua/josean/plugins/nvim-treesitter.lua index 97a5663..69596e3 100644 --- a/.config/nvim/lua/josean/plugins/nvim-treesitter.lua +++ b/.config/nvim/lua/josean/plugins/nvim-treesitter.lua @@ -4,6 +4,7 @@ return { event = { "BufReadPre", "BufNewFile" }, build = ":TSUpdate", dependencies = { + "nvim-treesitter/nvim-treesitter-textobjects", "windwp/nvim-ts-autotag", }, config = function() @@ -41,13 +42,20 @@ return { "dockerfile", "gitignore", }, + incremental_selection = { + enable = true, + keymaps = { + init_selection = "", + node_incremental = "", + scope_incremental = false, + node_decremental = "", + }, + }, -- enable nvim-ts-context-commentstring plugin for commenting tsx and jsx context_commentstring = { enable = true, enable_autocmd = false, }, - -- auto install above language parsers - auto_install = true, }) end, },