mirror of
https://github.com/nvim-treesitter/nvim-treesitter.git
synced 2026-07-11 16:00:02 -04:00
feat(textobjects): Add start to include preceding things like documentation
This commit is contained in:
parent
31d1f068fd
commit
b480d25155
4 changed files with 39 additions and 6 deletions
|
|
@ -126,7 +126,7 @@ function M.get_capture_matches(bufnr, capture_string, query_kind)
|
||||||
|
|
||||||
local matches = {}
|
local matches = {}
|
||||||
for _, match in pairs(M.get_locals(bufnr, query_kind)) do
|
for _, match in pairs(M.get_locals(bufnr, query_kind)) do
|
||||||
local insert = utils.get_at_path(match, capture_string..'.node')
|
local insert = utils.get_at_path(match, capture_string)
|
||||||
|
|
||||||
if insert then
|
if insert then
|
||||||
table.insert(matches, insert)
|
table.insert(matches, insert)
|
||||||
|
|
|
||||||
|
|
@ -31,7 +31,7 @@ function M.select_textobject(query_string)
|
||||||
for m in queries.iter_prepared_matches(query, root, bufnr, start_row, end_row) do
|
for m in queries.iter_prepared_matches(query, root, bufnr, start_row, end_row) do
|
||||||
for _, n in pairs(m) do
|
for _, n in pairs(m) do
|
||||||
if n.node then
|
if n.node then
|
||||||
table.insert(matches, n.node)
|
table.insert(matches, n)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
@ -39,19 +39,38 @@ function M.select_textobject(query_string)
|
||||||
|
|
||||||
local match_length
|
local match_length
|
||||||
local smallest_range
|
local smallest_range
|
||||||
|
local earliest_start
|
||||||
|
|
||||||
for _, m in pairs(matches) do
|
for _, m in pairs(matches) do
|
||||||
if ts_utils.is_in_node_range(m, row, col) then
|
if ts_utils.is_in_node_range(m.node, row, col) then
|
||||||
local length = ts_utils.node_length(m)
|
local length = ts_utils.node_length(m.node)
|
||||||
if not match_length or length < match_length then
|
if not match_length or length < match_length then
|
||||||
smallest_range = m
|
smallest_range = m
|
||||||
match_length = length
|
match_length = length
|
||||||
end
|
end
|
||||||
|
-- for nodes with same length take the one with earliest start
|
||||||
|
if match_length and length == smallest_range then
|
||||||
|
local start = m.start
|
||||||
|
if start then
|
||||||
|
local _, _, start_byte = m.start.node:start()
|
||||||
|
if not earliest_start or start_byte < earliest_start then
|
||||||
|
smallest_range = m
|
||||||
|
match_length = length
|
||||||
|
earliest_start = start_byte
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
if smallest_range then
|
if smallest_range then
|
||||||
ts_utils.update_selection(bufnr, smallest_range)
|
if smallest_range.start then
|
||||||
|
local start_range = {smallest_range.start.node:range()}
|
||||||
|
local node_range = {smallest_range.node:range()}
|
||||||
|
ts_utils.update_selection(bufnr, {start_range[1], start_range[2], node_range[3], node_range[4]})
|
||||||
|
else
|
||||||
|
ts_utils.update_selection(bufnr, smallest_range.node)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -325,7 +325,12 @@ end
|
||||||
|
|
||||||
-- Set visual selection to node
|
-- Set visual selection to node
|
||||||
function M.update_selection(buf, node)
|
function M.update_selection(buf, node)
|
||||||
local start_row, start_col, end_row, end_col = node:range()
|
local start_row, start_col, end_row, end_col
|
||||||
|
if type(node) == 'table' then
|
||||||
|
start_row, start_col, end_row, end_col = unpack(node)
|
||||||
|
else
|
||||||
|
start_row, start_col, end_row, end_col = node:range()
|
||||||
|
end
|
||||||
|
|
||||||
if end_row == vim.fn.line('$') then
|
if end_row == vim.fn.line('$') then
|
||||||
end_col = #vim.fn.getline('$')
|
end_col = #vim.fn.getline('$')
|
||||||
|
|
|
||||||
|
|
@ -4,3 +4,12 @@
|
||||||
|
|
||||||
(for_range_loop
|
(for_range_loop
|
||||||
(_)? @loop.inner) @loop.outer
|
(_)? @loop.inner) @loop.outer
|
||||||
|
|
||||||
|
(template_declaration
|
||||||
|
(function_definition) @function.outer) @function.outer.start
|
||||||
|
|
||||||
|
(template_declaration
|
||||||
|
(struct_specifier) @class.outer) @class.outer.start
|
||||||
|
|
||||||
|
(template_declaration
|
||||||
|
(class_specifier) @class.outer) @class.outer.start
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue