From 2d6437f5fbe162dbe5d177854382d12118682ecc Mon Sep 17 00:00:00 2001 From: Thomas Vigouroux Date: Tue, 1 Sep 2020 11:48:15 +0200 Subject: treesitter: use lua-match? instead of match? --- runtime/lua/vim/treesitter/query.lua | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'runtime/lua/vim/treesitter/query.lua') diff --git a/runtime/lua/vim/treesitter/query.lua b/runtime/lua/vim/treesitter/query.lua index 55e69f177f..46b54ad079 100644 --- a/runtime/lua/vim/treesitter/query.lua +++ b/runtime/lua/vim/treesitter/query.lua @@ -60,7 +60,7 @@ local predicate_handlers = { return true end, - ["match?"] = function(match, _, bufnr, predicate) + ["lua-match?"] = function(match, _, bufnr, predicate) local node = match[predicate[2]] local regex = predicate[3] local start_row, _, end_row, _ = node:range() @@ -71,7 +71,7 @@ local predicate_handlers = { return string.find(M.get_node_text(node, bufnr), regex) end, - ["vim-match?"] = (function() + ["match?"] = (function() local magic_prefixes = {['\\v']=true, ['\\m']=true, ['\\M']=true, ['\\V']=true} local function check_magic(str) if string.len(str) < 2 or magic_prefixes[string.sub(str,1,2)] then @@ -114,6 +114,9 @@ local predicate_handlers = { end } +-- As we provide lua-match? also expose vim-match? +predicate_handlers["vim-match?"] = predicate_handlers["match?"] + --- Adds a new predicates to be used in queries -- -- @param name the name of the predicate, without leading # -- cgit From 20c1526552f4f402e4acb24486da9f05d9344741 Mon Sep 17 00:00:00 2001 From: Thomas Vigouroux Date: Tue, 1 Sep 2020 13:15:37 +0200 Subject: treesitter: simplify match_preds --- runtime/lua/vim/treesitter/query.lua | 35 ++++++++++++++++++++++++----------- 1 file changed, 24 insertions(+), 11 deletions(-) (limited to 'runtime/lua/vim/treesitter/query.lua') diff --git a/runtime/lua/vim/treesitter/query.lua b/runtime/lua/vim/treesitter/query.lua index 46b54ad079..95057facc0 100644 --- a/runtime/lua/vim/treesitter/query.lua +++ b/runtime/lua/vim/treesitter/query.lua @@ -135,25 +135,38 @@ function M.list_predicates() return vim.tbl_keys(predicate_handlers) end +local function xor(a, b) + return (a or b) and not (a and b) +end + function Query:match_preds(match, pattern, bufnr) local preds = self.info.patterns[pattern] - if not preds then - return true - end - for _, pred in pairs(preds) do + + for _, pred in pairs(preds or {}) do -- Here we only want to return if a predicate DOES NOT match, and -- continue on the other case. This way unknown predicates will not be considered, -- which allows some testing and easier user extensibility (#12173). -- Also, tree-sitter strips the leading # from predicates for us. + local pred_name + local is_not if string.sub(pred[1], 1, 4) == "not-" then - local pred_name = string.sub(pred[1], 5) - if predicate_handlers[pred_name] and - predicate_handlers[pred_name](match, pattern, bufnr, pred) then - return false - end + pred_name = string.sub(pred[1], 5) + is_not = true + else + pred_name = pred[1] + is_not = false + end + + local handler = predicate_handlers[pred_name] + + if not handler then + a.nvim_err_writeln(string.format("No handler for %s", pred[1])) + return false + end + + local pred_matches = handler(match, pattern, bufnr, pred) - elseif predicate_handlers[pred[1]] and - not predicate_handlers[pred[1]](match, pattern, bufnr, pred) then + if not xor(is_not, pred_matches) then return false end end -- cgit From 9c929e7d23b8eaf3ff57dbbe9346abc4430e7669 Mon Sep 17 00:00:00 2001 From: Björn Linse Date: Sun, 6 Sep 2020 11:25:23 +0200 Subject: lint: just bit twiddlin' --- runtime/lua/vim/treesitter/query.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'runtime/lua/vim/treesitter/query.lua') diff --git a/runtime/lua/vim/treesitter/query.lua b/runtime/lua/vim/treesitter/query.lua index 95057facc0..ca27a50c6a 100644 --- a/runtime/lua/vim/treesitter/query.lua +++ b/runtime/lua/vim/treesitter/query.lua @@ -135,8 +135,8 @@ function M.list_predicates() return vim.tbl_keys(predicate_handlers) end -local function xor(a, b) - return (a or b) and not (a and b) +local function xor(x, y) + return (x or y) and not (x and y) end function Query:match_preds(match, pattern, bufnr) -- cgit