extract treesitter

This commit is contained in:
Thomas Ruoff
2023-07-13 10:01:01 +02:00
parent ee49d68ad3
commit 3b8633089e
2 changed files with 205 additions and 113 deletions

113
init.lua
View File

@@ -142,14 +142,6 @@ require('lazy').setup({
-- "gc" to comment visual regions/lines -- "gc" to comment visual regions/lines
{ 'numToStr/Comment.nvim', opts = {} }, { 'numToStr/Comment.nvim', opts = {} },
{ -- Highlight, edit, and navigate code
'nvim-treesitter/nvim-treesitter',
dependencies = {
'nvim-treesitter/nvim-treesitter-textobjects',
},
build = ':TSUpdate',
},
-- NOTE: Next Step on Your Neovim Journey: Add/Configure additional "plugins" for kickstart -- NOTE: Next Step on Your Neovim Journey: Add/Configure additional "plugins" for kickstart
-- These are some example plugins that I've included in the kickstart repository. -- These are some example plugins that I've included in the kickstart repository.
-- Uncomment any of the lines below to enable them. -- Uncomment any of the lines below to enable them.
@@ -229,111 +221,6 @@ vim.api.nvim_create_autocmd('TextYankPost', {
pattern = '*', pattern = '*',
}) })
-- [[ Configure Treesitter ]]
-- See `:help nvim-treesitter`
require('nvim-treesitter.configs').setup {
-- Add languages to be installed here that you want installed for treesitter
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',
},
-- Autoinstall languages that are not installed. Defaults to false (but you can change for yourself!)
auto_install = false,
highlight = { enable = true },
indent = { enable = true, disable = { 'python' } },
incremental_selection = {
enable = true,
keymaps = {
init_selection = '<c-space>',
node_incremental = '<c-space>',
scope_incremental = '<c-s>',
node_decremental = '<M-space>',
},
},
textobjects = {
select = {
enable = true,
lookahead = true, -- Automatically jump forward to textobj, similar to targets.vim
keymaps = {
-- You can use the capture groups defined in textobjects.scm
['aa'] = '@parameter.outer',
['ia'] = '@parameter.inner',
['af'] = '@function.outer',
['if'] = '@function.inner',
['ac'] = '@class.outer',
['ic'] = '@class.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',
},
},
swap = {
enable = true,
swap_next = {
['<leader>a'] = '@parameter.inner',
},
swap_previous = {
['<leader>A'] = '@parameter.inner',
},
},
},
}
-- Diagnostic keymaps -- Diagnostic keymaps
vim.keymap.set('n', '[d', vim.diagnostic.goto_prev, { desc = 'previous diagnostic message' }) vim.keymap.set('n', '[d', vim.diagnostic.goto_prev, { desc = 'previous diagnostic message' })
vim.keymap.set('n', ']d', vim.diagnostic.goto_next, { desc = 'next diagnostic message' }) vim.keymap.set('n', ']d', vim.diagnostic.goto_next, { desc = 'next diagnostic message' })

View File

@@ -0,0 +1,205 @@
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,
}