feat(treesitter): allow capture text to be transformed

Co-authored-by: Lewis Russell <lewis6991@gmail.com>
This commit is contained in:
figsoda
2022-07-21 12:08:37 +01:00
parent 5c4b503d3c
commit bb8845340b
4 changed files with 28 additions and 2 deletions

View File

@@ -452,6 +452,21 @@ local directive_handlers = {
metadata[capture_id].range = range
end
end,
-- Transform the content of the node
-- Example: (#gsub! @_node ".*%.(.*)" "%1")
['gsub!'] = function(match, _, bufnr, pred, metadata)
assert(#pred == 4)
local id = pred[2]
local node = match[id]
local text = M.get_node_text(node, bufnr, { metadata = metadata[id] }) or ''
if not metadata[id] then
metadata[id] = {}
end
metadata[id].text = text:gsub(pred[3], pred[4])
end,
}
--- Adds a new predicate to be used in queries