Files
vim/lua/custom/plugins/treesitter.lua
2023-07-13 10:01:01 +02:00

206 lines
5.9 KiB
Lua

return {
'nvim-treesitter/nvim-treesitter',
dependencies = {
-- shows treesitter context in end of parenthesis
'haringsrob/nvim_context_vt',
'RRethy/nvim-treesitter-textsubjects',
'nvim-treesitter/nvim-treesitter-textobjects',
},
event = { 'BufReadPost', 'BufNewFile' },
config = function()
local treesitter = require 'nvim-treesitter.configs'
treesitter.setup {
ensure_installed = {
'bash',
'c',
'cpp',
'dockerfile',
'diff',
'git_config',
'git_rebase',
'gitattributes',
'gitcommit',
'gitignore',
'go',
'html',
'http',
'java',
'javascript',
'jq',
'jsdoc',
'json',
'json5',
'latex',
'lua',
'luadoc',
'make',
'markdown',
'markdown_inline',
'mermaid',
'norg',
'norg_meta',
'org',
'python',
'regex',
'rust',
'tsx',
'typescript',
'sql',
'vimdoc',
'vim',
'yaml',
},
highlight = {
enable = true,
},
incremental_selection = {
enable = true,
keymaps = {
init_selection = 'zi',
node_incremental = 'zi',
scope_incremental = 'zo',
node_decremental = 'zd',
},
},
-- textobjects = {
-- select = {
-- enable = true,
-- lookahead = true,
-- keymaps = {
-- ["af"] = "@function.outer",
-- ["if"] = "@function.inner",
-- ["ac"] = "@class.outer",
-- ["ic"] = "@class.inner",
--
-- -- xml attribute
-- ["ax"] = "@attribute.outer",
-- ["ix"] = "@attribute.inner",
--
-- -- json
-- ["ak"] = "@key.outer",
-- ["ik"] = "@key.inner",
-- ["av"] = "@value.outer",
-- ["iv"] = "@value.inner",
-- },
-- },
swap = {
enable = true,
swap_next = {
['<leader>rp'] = '@parameter.inner',
},
swap_previous = {
['<leader>rP'] = '@parameter.inner',
},
},
-- 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",
-- },
-- },
-- },
textsubjects = {
enable = true,
keymaps = {
['.'] = 'textsubjects-smart',
[';'] = 'textsubjects-container-outer',
['i;'] = 'textsubjects-container-inner',
},
},
}
-- local r = require 'utils.remaps'
-- r.which_key('<leader>dt', 'Treesitter')
local which_key_lazy_registers = nil
local function lazy_register_which_key(input, description)
if which_key_lazy_registers == nil then
which_key_lazy_registers = {}
end
which_key_lazy_registers[input] = description
end
local function try_add_to_which_key_by_input(input, description)
local present_which_key, which_key = pcall(require, 'which-key')
local has_leader = string.find(input, '<leader>')
if has_leader then
if present_which_key then
if which_key_lazy_registers ~= nil then
which_key.register(which_key_lazy_registers)
which_key_lazy_registers = nil
end
which_key.register {
[input] = description,
}
else
lazy_register_which_key(input, description)
end
end
end
vim.keymap.set('n', '<leader>dtp', function()
vim.treesitter.inspect_tree { command = 'botright 60vnew' }
end, { desc = 'Treesitter playground' })
vim.keymap.set('n', '<C-e>', function()
local result = vim.treesitter.get_captures_at_cursor(0)
print(vim.inspect(result))
end, { desc = 'show treesitter capture group' })
try_add_to_which_key_by_input('zi', 'Init selection')
try_add_to_which_key_by_input('zi', 'Expand node')
try_add_to_which_key_by_input('zo', 'Expand scope')
try_add_to_which_key_by_input('zd', 'Decrement scope')
-- r.map_virtual("af", "Function outer motion")
-- r.map_virtual("if", "Function inner motion")
-- r.map_virtual("ac", "Class outer motion")
-- r.map_virtual("ic", "Class inner motion")
--
-- r.map_virtual("ax", "Attribute (html, xml) outer motion")
-- r.map_virtual("ix", "Attribute (html, xml) inner motion")
--
-- r.map_virtual("ak", "Json key outer motion")
-- r.map_virtual("ik", "Json key inner motion")
--
-- r.map_virtual("av", "Json value outer motion")
-- r.map_virtual("iv", "Json value inner motion")
--
-- r.which_key("fp", "parameters")
--
try_add_to_which_key_by_input('<leader>rp', 'Swap parameter to next')
try_add_to_which_key_by_input('<leader>rP', 'Swap parameter to previous')
--
-- r.map_virtual("]m", "Go to next function (start)")
-- r.map_virtual("]M", "Go to next function (end)")
--
-- r.map_virtual("]]", "Go to next class (start)")
-- r.map_virtual("][", "Go to next class (end)")
--
-- r.map_virtual("[m", "Go to previous function (start)")
-- r.map_virtual("[M", "Go to previous function (end)")
--
-- r.map_virtual("[[", "Go to previous class (start)")
-- r.map_virtual("[]", "Go to previous class (end)")
end,
build = function()
vim.cmd [[TSUpdate]]
end,
}