mirror of
https://github.com/nvim-treesitter/nvim-treesitter.git
synced 2026-07-01 11:06:54 -04:00
chore(modules): remove refactor module
This commit is contained in:
parent
98c12ec23a
commit
82a8b859c6
8 changed files with 8 additions and 518 deletions
83
README.md
83
README.md
|
|
@ -118,7 +118,7 @@ EOF
|
|||
Check [`:h nvim-treesitter-modules`](doc/nvim-treesitter.txt)
|
||||
for a list of available modules and its options.
|
||||
|
||||
# Available Modules
|
||||
# Available modules
|
||||
|
||||
## Highlight
|
||||
|
||||
|
|
@ -158,80 +158,6 @@ require'nvim-treesitter.configs'.setup {
|
|||
EOF
|
||||
```
|
||||
|
||||
## Refactor: highlight definitions
|
||||
|
||||
Highlights definition and usages of the current symbol under the cursor.
|
||||
|
||||
```lua
|
||||
lua <<EOF
|
||||
require'nvim-treesitter.configs'.setup {
|
||||
refactor = {
|
||||
highlight_definitions = { enable = true },
|
||||
},
|
||||
}
|
||||
EOF
|
||||
```
|
||||
|
||||
## Refactor: highlight current scope
|
||||
|
||||
Highlights the block from the current scope where the cursor is.
|
||||
|
||||
```lua
|
||||
lua <<EOF
|
||||
require'nvim-treesitter.configs'.setup {
|
||||
refactor = {
|
||||
highlight_current_scope = { enable = true },
|
||||
},
|
||||
}
|
||||
EOF
|
||||
```
|
||||
|
||||
## Refactor: smart rename
|
||||
|
||||
Renames the symbol under the cursor within the current scope (and current file).
|
||||
|
||||
```lua
|
||||
lua <<EOF
|
||||
require'nvim-treesitter.configs'.setup {
|
||||
refactor = {
|
||||
smart_rename = {
|
||||
enable = true,
|
||||
keymaps = {
|
||||
smart_rename = "grr",
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
EOF
|
||||
```
|
||||
|
||||
## Refactor: navigation
|
||||
|
||||
Provides "go to definition" for the symbol under the cursor,
|
||||
and lists the definitions from the current file. If you use
|
||||
`goto_definition_lsp_fallback` instead of `goto_definition` in the config below
|
||||
`vim.lsp.buf.definition` is used if nvim-treesitter can not resolve the variable.
|
||||
`goto_next_usage`/`goto_previous_usage` go to the next usage of the identifier under the cursor.
|
||||
|
||||
|
||||
```lua
|
||||
lua <<EOF
|
||||
require'nvim-treesitter.configs'.setup {
|
||||
refactor = {
|
||||
navigation = {
|
||||
enable = true,
|
||||
keymaps = {
|
||||
goto_definition = "gnd",
|
||||
list_definitions = "gnD",
|
||||
goto_next_usage = "<a-*>",
|
||||
goto_previous_usage = "<a-#>",
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
EOF
|
||||
```
|
||||
|
||||
## Text objects: select
|
||||
|
||||
Define your own text objects mappings
|
||||
|
|
@ -321,6 +247,13 @@ require'nvim-treesitter.configs'.setup {
|
|||
EOF
|
||||
```
|
||||
|
||||
# External modules
|
||||
|
||||
Other modules can be installed as plugins.
|
||||
|
||||
- [refactor](https://github.com/nvim-treesitter/refactor) - Refactoring and definition modules
|
||||
- [playground](https://github.com/nvim-treesitter/playground) - Treesitter integrated playground
|
||||
|
||||
# Extra features
|
||||
|
||||
## Syntax based code folding
|
||||
|
|
|
|||
|
|
@ -64,11 +64,6 @@ Each module corresponds to an entry in the dictionary passed to the
|
|||
-- Modules and its options go here
|
||||
highlight = { enable = true },
|
||||
incremental_selection = { enable = true },
|
||||
refactor = {
|
||||
highlight_definitions = { enable = true },
|
||||
smart_rename = { enable = true },
|
||||
navigation = { enable = true },
|
||||
},
|
||||
textobjects = { enable = true },
|
||||
}
|
||||
EOF
|
||||
|
|
@ -159,124 +154,6 @@ Supported options:
|
|||
}
|
||||
EOF
|
||||
<
|
||||
|
||||
------------------------------------------------------------------------------
|
||||
REFACTOR *nvim-treesitter-refactor-mod*
|
||||
|
||||
*nvim-treesitter-highlight-definitions-submod*
|
||||
Highlight definitions~
|
||||
|
||||
Highlights definition and usages of the current symbol under the cursor.
|
||||
|
||||
Query files: `locals.scm`.
|
||||
Supported options:
|
||||
- enable: `true` or `false`.
|
||||
- disable: list of languages.
|
||||
|
||||
>
|
||||
lua <<EOF
|
||||
require'nvim-treesitter.configs'.setup {
|
||||
refactor = {
|
||||
highlight_definitions = { enable = true },
|
||||
},
|
||||
}
|
||||
EOF
|
||||
<
|
||||
|
||||
Note: this makes use of the |CursorHold| event, so the highlight is
|
||||
triggered after 'updatetime'.
|
||||
|
||||
*nvim-treesitter-highlight-current-scope-submod*
|
||||
Highlight current scope~
|
||||
|
||||
Highlights the block from the current scope where the cursor is.
|
||||
|
||||
Query files: `locals.scm`.
|
||||
Supported options:
|
||||
- enable: `true` or `false`.
|
||||
- disable: list of languages.
|
||||
|
||||
>
|
||||
lua <<EOF
|
||||
require'nvim-treesitter.configs'.setup {
|
||||
refactor = {
|
||||
highlight_current_scope = { enable = true },
|
||||
},
|
||||
}
|
||||
EOF
|
||||
<
|
||||
|
||||
*nvim-treesitter-smart-rename-submod*
|
||||
Smart rename~
|
||||
|
||||
Renames the symbol under the cursor within the current scope (and current file).
|
||||
|
||||
Query files: `locals.scm`.
|
||||
Supported options:
|
||||
- enable: `true` or `false`.
|
||||
- disable: list of languages.
|
||||
- keymaps:
|
||||
- smart_rename: rename symbol under the cursor.
|
||||
Defaults to `grr`.
|
||||
|
||||
>
|
||||
lua <<EOF
|
||||
require'nvim-treesitter.configs'.setup {
|
||||
refactor = {
|
||||
smart_rename = {
|
||||
enable = true,
|
||||
keymaps = {
|
||||
smart_rename = "grr",
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
EOF
|
||||
<
|
||||
|
||||
*nvim-treesitter-navigation-submod*
|
||||
Navigation~
|
||||
|
||||
Provides "go to definition" for the symbol under the cursor,
|
||||
and lists the definitions from the current file.
|
||||
|
||||
Query files: `locals.scm`.
|
||||
Supported options:
|
||||
- enable: `true` or `false`.
|
||||
- disable: list of languages.
|
||||
- keymaps:
|
||||
- goto_definition: go to the definition of the symbol under the cursor.
|
||||
Defaults to `gnd`.
|
||||
- goto_definition_lsp_fallback: go to the definition of the symbol under
|
||||
the cursor or use vim.lsp.buf.definition if the symbol can not be
|
||||
resolved. You can use your own fallback function if create a mapping for
|
||||
`lua require'nvim-treesitter.refactor.navigation(nil, fallback_function)<cr>` .
|
||||
No default mapping
|
||||
- list_definitions: list all definitions from the current file.
|
||||
Defaults to `gnD`.
|
||||
- goto_next_usage: go to next usage of identifier under the cursor.
|
||||
Defaults to `<a-*>`.
|
||||
- goto_previous_usage: go to previous usage of identifier.
|
||||
Defaults to `<a-#>`.
|
||||
|
||||
>
|
||||
lua <<EOF
|
||||
require'nvim-treesitter.configs'.setup {
|
||||
refactor = {
|
||||
navigation = {
|
||||
enable = true,
|
||||
keymaps = {
|
||||
goto_definition = "gnd",
|
||||
list_definitions = "gnD",
|
||||
goto_next_usage = "<a-*>",
|
||||
goto_previous_usage = "<a-#>",
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
EOF
|
||||
<
|
||||
|
||||
------------------------------------------------------------------------------
|
||||
TEXT OBJECTS *nvim-treesitter-textobjects-mod*
|
||||
|
||||
|
|
@ -744,27 +621,6 @@ Any variable name that does not have another highlight.
|
|||
*hl-TSVariableBuiltin*
|
||||
Variable names that are defined by the languages, like `this` or `self`.
|
||||
|
||||
==============================================================================
|
||||
MODULE-HIGHLIGHTS *nvim-treesitter-module-highlights*
|
||||
|
||||
Apart from the general purpose highlights in |nvim-treesitter-highlights|,
|
||||
some submodules use their own highlight groups to visualize additional
|
||||
information.
|
||||
|
||||
`TSDefinition`
|
||||
*hl-TSDefinition*
|
||||
Used by refactor.highlight_definitions to highlight the definition of the
|
||||
symbol under the cursor.
|
||||
|
||||
`TSDefinitionUsage`
|
||||
*hl-TSDefinitionUsage*
|
||||
Used by refactor.highlight_definitions to highlight usages of the symbol under
|
||||
the cursor.
|
||||
|
||||
`TSCurrentScope`
|
||||
*hl-TSCurrentScope*
|
||||
Used by refactor.highlight_current_scope to highlight the current scope.
|
||||
|
||||
==============================================================================
|
||||
PERFORMANCE *nvim-treesitter-performance*
|
||||
|
||||
|
|
|
|||
|
|
@ -46,41 +46,6 @@ local builtin_modules = {
|
|||
},
|
||||
is_supported = queries.has_locals
|
||||
},
|
||||
refactor = {
|
||||
highlight_definitions = {
|
||||
module_path = 'nvim-treesitter.refactor.highlight_definitions',
|
||||
enable = false,
|
||||
disable = {},
|
||||
is_supported = queries.has_locals
|
||||
},
|
||||
highlight_current_scope = {
|
||||
module_path = 'nvim-treesitter.refactor.highlight_current_scope',
|
||||
enable = false,
|
||||
disable = {},
|
||||
is_supported = queries.has_locals,
|
||||
},
|
||||
smart_rename = {
|
||||
module_path = 'nvim-treesitter.refactor.smart_rename',
|
||||
enable = false,
|
||||
disable = {},
|
||||
is_supported = queries.has_locals,
|
||||
keymaps = {
|
||||
smart_rename = "grr"
|
||||
}
|
||||
},
|
||||
navigation = {
|
||||
module_path = 'nvim-treesitter.refactor.navigation',
|
||||
enable = false,
|
||||
disable = {},
|
||||
is_supported = queries.has_locals,
|
||||
keymaps = {
|
||||
goto_definition = "gnd",
|
||||
list_definitions = "gnD",
|
||||
goto_next_usage = "<a-*>",
|
||||
goto_previous_usage = "<a-#>",
|
||||
}
|
||||
}
|
||||
},
|
||||
textobjects = {
|
||||
select = {
|
||||
module_path = 'nvim-treesitter.textobjects.select',
|
||||
|
|
|
|||
|
|
@ -1,45 +0,0 @@
|
|||
-- This module highlights the current scope of at the cursor position
|
||||
|
||||
local ts_utils = require'nvim-treesitter.ts_utils'
|
||||
local locals = require'nvim-treesitter.locals'
|
||||
local api = vim.api
|
||||
local cmd = api.nvim_command
|
||||
|
||||
local M = {}
|
||||
|
||||
local current_scope_namespace = api.nvim_create_namespace('nvim-treesitter-current-scope')
|
||||
|
||||
function M.highlight_current_scope(bufnr)
|
||||
M.clear_highlights(bufnr)
|
||||
|
||||
local node_at_point = ts_utils.get_node_at_cursor()
|
||||
local current_scope = locals.containing_scope(node_at_point, bufnr)
|
||||
|
||||
local start_line = current_scope:start()
|
||||
|
||||
if current_scope and start_line ~= 0 then
|
||||
ts_utils.highlight_node(current_scope, bufnr, current_scope_namespace, 'TSCurrentScope')
|
||||
end
|
||||
end
|
||||
|
||||
function M.clear_highlights(bufnr)
|
||||
api.nvim_buf_clear_namespace(bufnr, current_scope_namespace, 0, -1)
|
||||
end
|
||||
|
||||
function M.attach(bufnr)
|
||||
cmd(string.format('augroup NvimTreesitterCurrentScope_%d', bufnr))
|
||||
cmd 'au!'
|
||||
-- luacheck: push ignore 631
|
||||
cmd(string.format([[autocmd CursorMoved <buffer=%d> lua require'nvim-treesitter.refactor.highlight_current_scope'.highlight_current_scope(%d)]], bufnr, bufnr))
|
||||
cmd(string.format([[autocmd BufLeave <buffer=%d> lua require'nvim-treesitter.refactor.highlight_current_scope'.clear_highlights(%d)]], bufnr, bufnr))
|
||||
-- luacheck: pop
|
||||
cmd 'augroup END'
|
||||
end
|
||||
|
||||
function M.detach(bufnr)
|
||||
M.clear_highlights(bufnr)
|
||||
cmd(string.format('autocmd! NvimTreesitterCurrentScope_%d CursorMoved', bufnr))
|
||||
cmd(string.format('autocmd! NvimTreesitterCurrentScope_%d BufLeave', bufnr))
|
||||
end
|
||||
|
||||
return M
|
||||
|
|
@ -1,59 +0,0 @@
|
|||
-- This module highlights reference usages and the corresponding
|
||||
-- definition on cursor hold.
|
||||
|
||||
local ts_utils = require'nvim-treesitter.ts_utils'
|
||||
local locals = require'nvim-treesitter.locals'
|
||||
local api = vim.api
|
||||
local cmd = api.nvim_command
|
||||
|
||||
local M = {}
|
||||
|
||||
local usage_namespace = api.nvim_create_namespace('nvim-treesitter-usages')
|
||||
|
||||
function M.highlight_usages(bufnr)
|
||||
M.clear_usage_highlights(bufnr)
|
||||
|
||||
local node_at_point = ts_utils.get_node_at_cursor()
|
||||
local references = locals.get_references(bufnr)
|
||||
|
||||
if not node_at_point or not vim.tbl_contains(references, node_at_point) then
|
||||
return
|
||||
end
|
||||
|
||||
local def_node, scope = locals.find_definition(node_at_point, bufnr)
|
||||
local usages = locals.find_usages(def_node, scope, bufnr)
|
||||
|
||||
for _, usage_node in ipairs(usages) do
|
||||
if usage_node ~= node_at_point then
|
||||
ts_utils.highlight_node(usage_node, bufnr, usage_namespace, 'TSDefinitionUsage')
|
||||
end
|
||||
end
|
||||
|
||||
if def_node ~= node_at_point then
|
||||
ts_utils.highlight_node(def_node, bufnr, usage_namespace, 'TSDefinition')
|
||||
end
|
||||
end
|
||||
|
||||
function M.clear_usage_highlights(bufnr)
|
||||
api.nvim_buf_clear_namespace(bufnr, usage_namespace, 0, -1)
|
||||
end
|
||||
|
||||
function M.attach(bufnr)
|
||||
cmd(string.format('augroup NvimTreesitterUsages_%d', bufnr))
|
||||
cmd 'au!'
|
||||
-- luacheck: push ignore 631
|
||||
cmd(string.format([[autocmd CursorHold <buffer=%d> lua require'nvim-treesitter.refactor.highlight_definitions'.highlight_usages(%d)]], bufnr, bufnr))
|
||||
cmd(string.format([[autocmd CursorMoved <buffer=%d> lua require'nvim-treesitter.refactor.highlight_definitions'.clear_usage_highlights(%d)]], bufnr, bufnr))
|
||||
cmd(string.format([[autocmd InsertEnter <buffer=%d> lua require'nvim-treesitter.refactor.highlight_definitions'.clear_usage_highlights(%d)]], bufnr, bufnr))
|
||||
-- luacheck: pop
|
||||
cmd 'augroup END'
|
||||
end
|
||||
|
||||
function M.detach(bufnr)
|
||||
M.clear_usage_highlights(bufnr)
|
||||
cmd(string.format('autocmd! NvimTreesitterUsages_%d CursorHold', bufnr))
|
||||
cmd(string.format('autocmd! NvimTreesitterUsages_%d CursorMoved', bufnr))
|
||||
cmd(string.format('autocmd! NvimTreesitterUsages_%d InsertEnter', bufnr))
|
||||
end
|
||||
|
||||
return M
|
||||
|
|
@ -1,90 +0,0 @@
|
|||
-- Definition based navigation module
|
||||
|
||||
local ts_utils = require'nvim-treesitter.ts_utils'
|
||||
local utils = require'nvim-treesitter.utils'
|
||||
local locals = require'nvim-treesitter.locals'
|
||||
local configs = require'nvim-treesitter.configs'
|
||||
local api = vim.api
|
||||
|
||||
local M = {}
|
||||
|
||||
function M.goto_definition(bufnr, fallback_function)
|
||||
local bufnr = bufnr or api.nvim_get_current_buf()
|
||||
local node_at_point = ts_utils.get_node_at_cursor()
|
||||
|
||||
if not node_at_point then return end
|
||||
|
||||
local definition = locals.find_definition(node_at_point, bufnr)
|
||||
|
||||
if fallback_function and definition.id == node_at_point.id then
|
||||
fallback_function()
|
||||
else
|
||||
ts_utils.goto_node(definition)
|
||||
end
|
||||
end
|
||||
|
||||
function M.goto_definition_lsp_fallback(bufnr) M.goto_definition(bufnr, vim.lsp.buf.definition) end
|
||||
|
||||
function M.list_definitions(bufnr)
|
||||
local bufnr = bufnr or api.nvim_get_current_buf()
|
||||
local definitions = locals.get_definitions(bufnr)
|
||||
|
||||
if #definitions < 1 then return end
|
||||
|
||||
local qf_list = {}
|
||||
|
||||
for _, def in ipairs(definitions) do
|
||||
locals.recurse_local_nodes(def, function(_, node, _, match)
|
||||
local lnum, col, _ = node:start()
|
||||
|
||||
table.insert(qf_list, {
|
||||
bufnr = bufnr,
|
||||
lnum = lnum + 1,
|
||||
col = col + 1,
|
||||
text = ts_utils.get_node_text(node)[1] or "",
|
||||
kind = match and match:sub(1, 1) or ""
|
||||
})
|
||||
end)
|
||||
end
|
||||
|
||||
vim.fn.setqflist(qf_list, 'r')
|
||||
api.nvim_command('copen')
|
||||
end
|
||||
|
||||
function M.goto_adjacent_usage(bufnr, delta)
|
||||
local bufnr = bufnr or api.nvim_get_current_buf()
|
||||
local node_at_point = ts_utils.get_node_at_cursor()
|
||||
if not node_at_point then return end
|
||||
|
||||
local def_node, scope = locals.find_definition(node_at_point, bufnr)
|
||||
local usages = locals.find_usages(def_node, scope, bufnr)
|
||||
|
||||
local index = utils.index_of(usages, node_at_point)
|
||||
if not index then return end
|
||||
|
||||
local target_index = (index + delta + #usages - 1) % #usages + 1
|
||||
ts_utils.goto_node(usages[target_index])
|
||||
end
|
||||
|
||||
function M.goto_next_usage(bufnr) return M.goto_adjacent_usage(bufnr, 1) end
|
||||
function M.goto_previous_usage(bufnr) return M.goto_adjacent_usage(bufnr, -1) end
|
||||
|
||||
function M.attach(bufnr)
|
||||
local config = configs.get_module('refactor.navigation')
|
||||
|
||||
for fn_name, mapping in pairs(config.keymaps) do
|
||||
local cmd = string.format([[:lua require'nvim-treesitter.refactor.navigation'.%s(%d)<CR>]], fn_name, bufnr)
|
||||
|
||||
api.nvim_buf_set_keymap(bufnr, 'n', mapping, cmd, { silent = true, noremap = true })
|
||||
end
|
||||
end
|
||||
|
||||
function M.detach(bufnr)
|
||||
local config = configs.get_module('refactor.navigation')
|
||||
|
||||
for _, mapping in pairs(config.keymaps) do
|
||||
api.nvim_buf_del_keymap(bufnr, 'n', mapping)
|
||||
end
|
||||
end
|
||||
|
||||
return M
|
||||
|
|
@ -1,65 +0,0 @@
|
|||
-- Binds a keybinding to smart rename definitions and usages.
|
||||
-- Can be used directly using the `smart_rename` function.
|
||||
|
||||
local ts_utils = require'nvim-treesitter.ts_utils'
|
||||
local locals = require'nvim-treesitter.locals'
|
||||
local configs = require'nvim-treesitter.configs'
|
||||
local utils = require'nvim-treesitter.utils'
|
||||
local api = vim.api
|
||||
|
||||
local M = {}
|
||||
|
||||
function M.smart_rename(bufnr)
|
||||
local bufnr = bufnr or api.nvim_get_current_buf()
|
||||
local node_at_point = ts_utils.get_node_at_cursor()
|
||||
|
||||
if not node_at_point then
|
||||
utils.print_warning("No node to rename!")
|
||||
return
|
||||
end
|
||||
|
||||
local node_text = ts_utils.get_node_text(node_at_point)[1]
|
||||
local new_name = vim.fn.input('New name: ', node_text or '')
|
||||
|
||||
-- Empty name cancels the interaction or ESC
|
||||
if not new_name or #new_name < 1 then return end
|
||||
|
||||
local definition, scope = locals.find_definition(node_at_point, bufnr)
|
||||
local nodes_to_rename = locals.find_usages(definition, scope, bufnr)
|
||||
|
||||
if not vim.tbl_contains(nodes_to_rename, node_at_point) then
|
||||
table.insert(nodes_to_rename, node_at_point)
|
||||
end
|
||||
|
||||
if not vim.tbl_contains(nodes_to_rename, definition) then
|
||||
table.insert(nodes_to_rename, definition)
|
||||
end
|
||||
|
||||
local edits = {}
|
||||
|
||||
for _, node in ipairs(nodes_to_rename) do
|
||||
local lsp_range = ts_utils.node_to_lsp_range(node)
|
||||
local text_edit = { range = lsp_range, newText = new_name }
|
||||
table.insert(edits, text_edit)
|
||||
end
|
||||
vim.lsp.util.apply_text_edits(edits, bufnr)
|
||||
end
|
||||
|
||||
function M.attach(bufnr)
|
||||
local config = configs.get_module('refactor.smart_rename')
|
||||
|
||||
for fn_name, mapping in pairs(config.keymaps) do
|
||||
local cmd = string.format([[:lua require'nvim-treesitter.refactor.smart_rename'.%s(%d)<CR>]], fn_name, bufnr)
|
||||
api.nvim_buf_set_keymap(bufnr, 'n', mapping, cmd, { silent = true, noremap = true })
|
||||
end
|
||||
end
|
||||
|
||||
function M.detach(bufnr)
|
||||
local config = configs.get_module('refactor.smart_rename')
|
||||
|
||||
for _, mapping in pairs(config.keymaps) do
|
||||
api.nvim_buf_del_keymap(bufnr, 'n', mapping)
|
||||
end
|
||||
end
|
||||
|
||||
return M
|
||||
|
|
@ -59,11 +59,6 @@ highlight default link TSInclude Include
|
|||
highlight default link TSVariable TSNone
|
||||
highlight default link TSVariableBuiltin Special
|
||||
|
||||
highlight default link TSDefinitionUsage Visual
|
||||
highlight default link TSDefinition Search
|
||||
|
||||
highlight default link TSCurrentScope CursorLine
|
||||
|
||||
highlight default link TSText TSNone
|
||||
highlight default TSStrong term=bold cterm=bold gui=bold
|
||||
highlight default TSEmphasis term=italic cterm=italic gui=italic
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue