From 3af2f96b8957bc4b54a7735ec6b9dba9bc4a5712 Mon Sep 17 00:00:00 2001 From: Oliver Davies Date: Thu, 9 Nov 2023 07:23:33 +0000 Subject: [PATCH] chore(nvim): update treesitter configuration --- config/neovim/after/plugin/treesitter.lua | 117 +++++++++++++++++++--- 1 file changed, 103 insertions(+), 14 deletions(-) diff --git a/config/neovim/after/plugin/treesitter.lua b/config/neovim/after/plugin/treesitter.lua index ed61968..59007af 100644 --- a/config/neovim/after/plugin/treesitter.lua +++ b/config/neovim/after/plugin/treesitter.lua @@ -1,48 +1,137 @@ -local has_configs, configs = pcall(require, "nvim-treesitter.configs") -if not has_configs then - return -end +local configs = require "nvim-treesitter.configs" +local context = require "treesitter-context" +local ts_repeat_move = require "nvim-treesitter.textobjects.repeatable_move" + +local map = require("opdavies.keymap").map +local nmap = require("opdavies.keymap").nmap configs.setup { autotag = { enable = true, }, + context_commenting = { enable = true, }, + highlight = { enable = true, }, + indent = { disable = { "yaml" }, enable = true, }, + matchup = { enable = true, }, + textobjects = { select = { enable = true, lookahead = true, keymaps = { - ["af"] = "@function.outer", - ["if"] = "@function.inner", - ["ac"] = "@class.outer", - ["ic"] = "@class.inner", + ["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 = "@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" }, + + ["aa"] = { query = "@parameter.outer", desc = "Select outer part of a parameter/argument" }, + ["ia"] = { query = "@parameter.inner", desc = "Select inner part of a parameter/argument" }, + + ["ac"] = { query = "@class.outer", desc = "Select outer part of a class" }, + ["ic"] = { query = "@class.inner", desc = "Select inner part of a class" }, + + ["af"] = { query = "@call.outer", desc = "Select outer part of a function call" }, + ["if"] = { query = "@call.inner", desc = "Select inner part of a function call" }, + + ["ai"] = { query = "@conditional.outer", desc = "Select outer part of a conditional" }, + ["ii"] = { query = "@conditional.inner", desc = "Select inner part of a conditional" }, + + ["al"] = { query = "@loop.outer", desc = "Select outer part of a loop" }, + ["il"] = { query = "@loop.inner", desc = "Select inner part of a loop" }, + + ["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" }, }, }, }, + + swap = { + enable = true, + + swap_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 = { + ["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" }, + + ["]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 nmap = require("opdavies.keymap").nmap - nmap { "th", "TSHighlightCapturesUnderCursor" } nmap { "tp", "TSPlaygroundToggle" } -local has_context, context = pcall(require, "treesitter-context") -if not has_context then - return -end +-- vim way: ; goes to the direction you were moving. +map {{ "n", "o", "x" }, ";", ts_repeat_move.repeat_last_move} +map {{ "n", "o", "x" }, ",", ts_repeat_move.repeat_last_move_opposite} + +-- Optionally, make builtin f, F, t, T also repeatable with ; and , +map {{ "n", "o", "x" }, "f", ts_repeat_move.builtin_f} +map {{ "n", "o", "x" }, "F", ts_repeat_move.builtin_F} +map {{ "n", "o", "x" }, "t", ts_repeat_move.builtin_t} +map {{ "n", "o", "x" }, "T", ts_repeat_move.builtin_T} context.setup { enable = true }