mirror of
https://github.com/neovim/neovim.git
synced 2025-02-25 18:55:25 -06:00
feat(treesitter): allow capture text to be transformed
Co-authored-by: Lewis Russell <lewis6991@gmail.com>
This commit is contained in:
@@ -354,6 +354,14 @@ local function get_range_from_metadata(node, id, metadata)
|
||||
return { node:range() }
|
||||
end
|
||||
|
||||
---@private
|
||||
local function get_node_text(node, id, metadata, source)
|
||||
if metadata[id] and metadata[id].text then
|
||||
return metadata[id].text
|
||||
end
|
||||
return query.get_node_text(node, source)
|
||||
end
|
||||
|
||||
--- Gets language injection points by language.
|
||||
---
|
||||
--- This is where most of the injection processing occurs.
|
||||
@@ -408,7 +416,7 @@ function LanguageTree:_get_injections()
|
||||
|
||||
-- Lang should override any other language tag
|
||||
if name == 'language' and not lang then
|
||||
lang = query.get_node_text(node, self._source) --[[@as string]]
|
||||
lang = get_node_text(node, id, metadata, self._source) --[[@as string]]
|
||||
elseif name == 'combined' then
|
||||
combined = true
|
||||
elseif name == 'content' and #ranges == 0 then
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user