Merge pull request #291 from vigoux/post-upstream-work

fix: refactor after upstream refactor
This commit is contained in:
Steven Sojka 2020-08-15 08:22:14 -05:00 committed by GitHub
commit 994baf4539
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 20 additions and 106 deletions

View file

@ -9,7 +9,7 @@ local M = {
highlighters = {} highlighters = {}
} }
local hlmap = vim.treesitter.TSHighlighter.hl_map local hlmap = vim.treesitter.highlighter.hl_map
-- Misc -- Misc
hlmap.error = "TSError" hlmap.error = "TSError"
@ -79,7 +79,7 @@ function M.attach(bufnr, lang)
local query = queries.get_query(lang, "highlights") local query = queries.get_query(lang, "highlights")
if not query then return end if not query then return end
M.highlighters[bufnr] = ts.TSHighlighter.new(query, bufnr, lang) M.highlighters[bufnr] = ts.highlighter.new(query, bufnr, lang)
end end
function M.detach(bufnr) function M.detach(bufnr)

View file

@ -2,7 +2,6 @@ local api = vim.api
local ts = vim.treesitter local ts = vim.treesitter
local utils = require'nvim-treesitter.utils' local utils = require'nvim-treesitter.utils'
local parsers = require'nvim-treesitter.parsers' local parsers = require'nvim-treesitter.parsers'
local predicates = require'nvim-treesitter.query_predicates'
local M = {} local M = {}
@ -181,14 +180,6 @@ function M.iter_prepared_matches(query, qnode, bufnr, start_row, end_row)
if pred[1] == "set!" and type(pred[2]) == "string" then if pred[1] == "set!" and type(pred[2]) == "string" then
insert_to_path(prepared_match, split(pred[2]), pred[3]) insert_to_path(prepared_match, split(pred[2]), pred[3])
end end
-- predicates
if type(pred[1]) == 'string' then
if not predicates.check_predicate(query, prepared_match, pred) or
not predicates.check_negated_predicate(query, prepared_match, pred) then
return iterator()
end
end
end end
end end

View file

@ -1,100 +1,28 @@
local utils = require'nvim-treesitter.utils' local query = require"vim.treesitter.query"
local ts_utils = require'nvim-treesitter.ts_utils'
local M = {} local function error(str)
vim.api.nvim_err_writeln(str)
end
local function get_nth_child(node, n) query.add_predicate("nth?", function(match, pattern, bufnr, pred)
if node:named_child_count() > n then if #pred ~= 3 then
return node:named_child(n) error("nth? must hav exactly two arguments")
return
end end
end
local function get_node(query, match, pred_item) local node = match[pred[2]]
return utils.get_at_path(match, query.captures[pred_item]..'.node') local n = pred[3] - 1
end if node and node:parent() and node:named_child_count() > n then
return node:named_child(n) == node
local function create_adjacent_predicate(match_successive_nodes)
return function(query, match, pred)
if #pred < 3 then error("adjacent? must have at least two arguments!") end
local node = get_node(query, match, pred[2])
if not node then return true end
local adjacent_types = {unpack(pred, 3)}
local adjacent_node = ts_utils.get_next_node(node)
if match_successive_nodes then
-- Move to the last node in a series that doesn't match the node type
-- and use that node to compare with.
while adjacent_node and adjacent_node:type() == node:type() do
node = adjacent_node
adjacent_node = ts_utils.get_next_node(node)
end
end
if not adjacent_node then return false end
for _, adjacent_type in ipairs(adjacent_types) do
if type(adjacent_type) == "number" then
if get_node(query, match, adjacent_type) == adjacent_node then
return true
end
elseif type(adjacent_type) == "string" then
if adjacent_node:type() == adjacent_type then
return true
end
end
end
return false
end end
end
function M.check_predicate(query, match, pred) return false
local check_function = M[pred[1]] end)
if check_function then
return check_function(query, match, pred)
else
return true
end
end
function M.check_negated_predicate(query, match, pred) query.add_predicate('has-ancestor?', function(match, pattern, bufnr, pred)
local check_function = M[string.sub(pred[1], #"not-" + 1)] if #pred ~= 3 then error("has-ancestor? must have exactly two arguments!") return end
if check_function then
return not check_function(query, match, pred)
else
return true
end
end
M['first?'] = function (query, match, pred) local node = match[pred[2]]
if #pred ~= 2 then error("first? must have exactly one argument!") end
local node = get_node(query, match, pred[2])
if node and node:parent() then
return get_nth_child(node:parent(), 0) == node
end
end
M['last?'] = function (query, match, pred)
if #pred ~= 2 then error("first? must have exactly one argument!") end
local node = get_node(query, match, pred[2])
if node and node:parent() then
local num_children = node:parent():named_child_count()
return get_nth_child(node:parent(), num_children - 1) == node
end
end
M['nth?'] = function(query, match, pred)
if #pred ~= 3 then error("nth? must have exactly two arguments!") end
local node = get_node(query, match, pred[2])
if node and node:parent() then
return get_nth_child(node:parent(), pred[3] - 1) == node
end
end
M['has-ancestor?'] = function(query, match, pred)
if #pred ~= 3 then error("has-ancestor? must have exactly two arguments!") end
local node = get_node(query, match, pred[2])
local ancestor_type = pred[3] local ancestor_type = pred[3]
if not node then return true end if not node then return true end
@ -106,9 +34,4 @@ M['has-ancestor?'] = function(query, match, pred)
node = node:parent() node = node:parent()
end end
return false return false
end end)
M['adjacent?'] = create_adjacent_predicate(false)
M['adjacent-block?'] = create_adjacent_predicate(true)
return M