Skip to content

Commit

Permalink
feat: support quantified captures (#612)
Browse files Browse the repository at this point in the history
This commit also removes all uses of `#make-range!` as it is no longer
necessary.
  • Loading branch information
ribru17 authored Sep 26, 2024
1 parent a34d063 commit aad2c8e
Show file tree
Hide file tree
Showing 44 changed files with 1,119 additions and 2,192 deletions.
42 changes: 12 additions & 30 deletions lua/nvim-treesitter-textobjects/shared.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
local ts = vim.treesitter
local add_bytes = require("vim.treesitter._range").add_bytes

-- luacheck: push ignore 631
---@alias TSTextObjects.Metadata {range: {[1]: number, [2]: number, [3]: number, [4]: number, [5]: number, [6]: number, [7]: string}}
Expand Down Expand Up @@ -82,29 +83,19 @@ local get_query_matches = memoize(function(bufnr, query_group, root, root_lang)
-- Extract capture names from each match
for id, nodes in pairs(match) do
local query_name = query.captures[id] -- name of the capture in the query
if type(nodes) ~= "table" then
nodes = { nodes }
end
if query_name ~= nil then
local path = vim.split(query_name, "%.")
for _, node in ipairs(nodes) do
local range = { node:range(true) }
---@cast range Range6

-- Range could be changed by directives
local node_data = metadata[id]
if node_data and node_data.range then
range = {
node_data.range[1],
node_data.range[2],
range[3],
node_data.range[3],
node_data.range[4],
range[6],
}
if metadata[id] and metadata[id].range then
insert_to_path(prepared_match, path, add_bytes(bufnr, metadata[id].range))
else
local srow, scol, sbyte, erow, ecol, ebyte = nodes[1]:range(true)
if #nodes > 1 then
local _, _, _, e_erow, e_ecol, e_ebyte = nodes[#nodes]:range(true)
erow = e_erow
ecol = e_ecol
ebyte = e_ebyte
end

insert_to_path(prepared_match, path, range)
insert_to_path(prepared_match, path, { srow, scol, sbyte, erow, ecol, ebyte })
end
end
end
Expand Down Expand Up @@ -436,16 +427,7 @@ M.available_textobjects = memoize(function(lang, query_group)
return {}
end

local found_textobjects = parsed_queries.captures or {}
for _, pattern in pairs(parsed_queries.info.patterns) do
for _, q in ipairs(pattern) do
local query, arg1 = unpack(q) --[=[@as string, string[]]=]
if query == "make-range!" and not vim.tbl_contains(found_textobjects, arg1) then
table.insert(found_textobjects, arg1)
end
end
end
return found_textobjects
return parsed_queries.captures or {}
end, function(lang, query_group)
return string.format("%s-%s", lang, query_group)
end)
Expand Down
45 changes: 0 additions & 45 deletions plugin/query_predicates.lua

This file was deleted.

44 changes: 14 additions & 30 deletions queries/apex/textobjects.scm
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,17 @@
body: (block
.
"{"
.
(_) @_start
(_)? @_end
.
"}"
(#make-range! "function.inner" @_start @_end)))
_+ @function.inner
"}"))

(constructor_declaration) @function.outer

(constructor_declaration
body: (constructor_body
.
"{"
.
(_) @_start
(_)? @_end
.
"}"
(#make-range! "function.inner" @_start @_end)))
_+ @function.inner
"}"))

(for_statement
body: (_)? @loop.inner) @loop.outer
Expand Down Expand Up @@ -59,39 +51,31 @@
arguments: (argument_list
.
"("
.
(_) @_start
(_)? @_end
.
")"
(#make-range! "call.inner" @_start @_end)))
_+ @call.inner
")"))

; parameters
(formal_parameters
"," @_start
"," @parameter.outer
.
(formal_parameter) @parameter.inner
(#make-range! "parameter.outer" @_start @parameter.inner))
(formal_parameter) @parameter.inner @parameter.outer)

(formal_parameters
.
(formal_parameter) @parameter.inner
(formal_parameter) @parameter.inner @parameter.outer
.
","? @_end
(#make-range! "parameter.outer" @parameter.inner @_end))
","? @parameter.outer)

(argument_list
"," @_start
"," @parameter.outer
.
(_) @parameter.inner
(#make-range! "parameter.outer" @_start @parameter.inner))
(_) @parameter.inner @parameter.outer)

(argument_list
.
(_) @parameter.inner
(_) @parameter.inner @parameter.outer
.
","? @_end
(#make-range! "parameter.outer" @parameter.inner @_end))
","? @parameter.outer)

[
(line_comment)
Expand Down
8 changes: 2 additions & 6 deletions queries/bash/textobjects.scm
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,8 @@
body: (compound_statement
.
"{"
.
(_) @_start
(_)? @_end
.
"}"
(#make-range! "function.inner" @_start @_end)))
_+ @function.inner
"}"))

(case_statement) @conditional.outer

Expand Down
89 changes: 26 additions & 63 deletions queries/c/textobjects.scm
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
; TODO: supported by official Tree-sitter if (_)* is more than one node
; Neovim: will only match if (_) is exactly one node
;(function_definition
;body: (compound_statement
;("{" (_)* @function.inner "}"))?) @function.outer
(declaration
declarator: (function_declarator)) @function.outer

Expand All @@ -13,12 +8,8 @@
body: (compound_statement
.
"{"
.
(_) @_start
(_)? @_end
.
"}"
(#make-range! "function.inner" @_start @_end)))
_+ @function.inner
"}"))

(struct_specifier
body: (_) @class.inner) @class.outer
Expand All @@ -31,24 +22,16 @@
consequence: (compound_statement
.
"{"
.
(_) @_start
(_)? @_end
.
"}"
(#make-range! "conditional.inner" @_start @_end))) @conditional.outer
_+ @conditional.inner
"}")) @conditional.outer

(if_statement
alternative: (else_clause
(compound_statement
.
"{"
.
(_) @_start
(_)? @_end
.
"}"
(#make-range! "conditional.inner" @_start @_end)))) @conditional.outer
_+ @conditional.inner
"}"))) @conditional.outer

(if_statement) @conditional.outer

Expand All @@ -74,38 +57,26 @@
body: (compound_statement
.
"{"
.
(_) @_start
(_)? @_end
.
"}"
(#make-range! "loop.inner" @_start @_end))) @loop.outer
_+ @loop.inner
"}")) @loop.outer

(for_statement) @loop.outer

(for_statement
body: (compound_statement
.
"{"
.
(_) @_start
(_)? @_end
.
"}"
(#make-range! "loop.inner" @_start @_end))) @loop.outer
_+ @loop.inner
"}")) @loop.outer

(do_statement) @loop.outer

(do_statement
body: (compound_statement
.
"{"
.
(_) @_start
(_)? @_end
.
"}"
(#make-range! "loop.inner" @_start @_end))) @loop.outer
_+ @loop.inner
"}")) @loop.outer

(compound_statement) @block.outer

Expand All @@ -117,12 +88,8 @@
arguments: (argument_list
.
"("
.
(_) @_start
(_)? @_end
.
")"
(#make-range! "call.inner" @_start @_end)))
_+ @call.inner
")"))

(return_statement
(_)? @return.inner) @return.outer
Expand All @@ -145,31 +112,27 @@
(preproc_else
(_) @statement.outer)

((parameter_list
"," @_start
(parameter_list
"," @parameter.outer
.
(parameter_declaration) @parameter.inner)
(#make-range! "parameter.outer" @_start @parameter.inner))
(parameter_declaration) @parameter.inner @parameter.outer)

((parameter_list
(parameter_list
.
(parameter_declaration) @parameter.inner
(parameter_declaration) @parameter.inner @parameter.outer
.
","? @_end)
(#make-range! "parameter.outer" @parameter.inner @_end))
","? @parameter.outer)

((argument_list
"," @_start
(argument_list
"," @parameter.outer
.
(_) @parameter.inner)
(#make-range! "parameter.outer" @_start @parameter.inner))
(_) @parameter.inner @parameter.outer)

((argument_list
(argument_list
.
(_) @parameter.inner
(_) @parameter.inner @parameter.outer
.
","? @_end)
(#make-range! "parameter.outer" @parameter.inner @_end))
","? @parameter.outer)

(number_literal) @number.inner

Expand Down
Loading

0 comments on commit aad2c8e

Please sign in to comment.