aboutsummaryrefslogtreecommitdiffstats
path: root/lua
diff options
context:
space:
mode:
authorChristian Clason <c.clason@uni-graz.at>2023-05-17 13:27:49 +0200
committerChristian Clason <c.clason@uni-graz.at>2023-05-17 15:25:35 +0200
commitfa23ff4bd7d77370613ecb37e49edb3ec3e7a931 (patch)
tree7de6bfa94893a483e5d32dcfd7a339b8b832a21c /lua
parentUpdate parsers: c (diff)
downloadnvim-treesitter-fa23ff4bd7d77370613ecb37e49edb3ec3e7a931.tar
nvim-treesitter-fa23ff4bd7d77370613ecb37e49edb3ec3e7a931.tar.gz
nvim-treesitter-fa23ff4bd7d77370613ecb37e49edb3ec3e7a931.tar.bz2
nvim-treesitter-fa23ff4bd7d77370613ecb37e49edb3ec3e7a931.tar.lz
nvim-treesitter-fa23ff4bd7d77370613ecb37e49edb3ec3e7a931.tar.xz
nvim-treesitter-fa23ff4bd7d77370613ecb37e49edb3ec3e7a931.tar.zst
nvim-treesitter-fa23ff4bd7d77370613ecb37e49edb3ec3e7a931.zip
refactor(indent): backport improvements from main
* replace ts_utils.memoize_by_buftick by simpler implementation * simplify construction of hash map * prefer Neovim APIs
Diffstat (limited to 'lua')
-rw-r--r--lua/nvim-treesitter/indent.lua123
1 files changed, 71 insertions, 52 deletions
diff --git a/lua/nvim-treesitter/indent.lua b/lua/nvim-treesitter/indent.lua
index fc11b4063..f453127bc 100644
--- a/lua/nvim-treesitter/indent.lua
+++ b/lua/nvim-treesitter/indent.lua
@@ -1,7 +1,5 @@
-local parsers = require "nvim-treesitter.parsers"
-local queries = require "nvim-treesitter.query"
-local tsutils = require "nvim-treesitter.ts_utils"
local ts = vim.treesitter
+local parsers = require "lua.nvim-treesitter.parsers"
local M = {}
@@ -15,6 +13,10 @@ M.comment_parsers = {
phpdoc = true,
}
+local function getline(lnum)
+ return vim.api.nvim_buf_get_lines(0, lnum - 1, lnum, false)[1] or ""
+end
+
---@param root TSNode
---@param lnum integer
---@param col? integer
@@ -29,10 +31,18 @@ end
---@param col? integer
---@return TSNode
local function get_last_node_at_line(root, lnum, col)
- col = col or (#vim.fn.getline(lnum) - 1)
+ col = col or (#getline(lnum) - 1)
return root:descendant_for_range(lnum - 1, col, lnum - 1, col)
end
+---@param node TSNode
+---@return number
+local function node_length(node)
+ local _, _, start_byte = node:start()
+ local _, _, end_byte = node:end_()
+ return end_byte - start_byte
+end
+
---@param bufnr integer
---@param node TSNode
---@param delimiter string
@@ -52,42 +62,50 @@ local function find_delimiter(bufnr, node, delimiter)
end
end
-local get_indents = tsutils.memoize_by_buf_tick(function(bufnr, root, lang)
- local map = {
- indent = {
- auto = {},
- begin = {},
- ["end"] = {},
- dedent = {},
- branch = {},
- ignore = {},
- align = {},
- zero = {},
- },
- }
+---Memoize a function using hash_fn to hash the arguments.
+---@generic F: function
+---@param fn F
+---@param hash_fn fun(...): any
+---@return F
+local function memoize(fn, hash_fn)
+ local cache = setmetatable({}, { __mode = "kv" }) ---@type table<any,any>
- local function split(to_split)
- local t = {}
- for str in string.gmatch(to_split, "([^.]+)") do
- table.insert(t, str)
+ return function(...)
+ local key = hash_fn(...)
+ if cache[key] == nil then
+ local v = fn(...) ---@type any
+ cache[key] = v ~= nil and v or vim.NIL
end
- return t
+
+ local v = cache[key]
+ return v ~= vim.NIL and v or nil
end
+end
+
+local get_indents = memoize(function(bufnr, root, lang)
+ local map = {
+ ["indent.auto"] = {},
+ ["indent.begin"] = {},
+ ["indent.end"] = {},
+ ["indent.dedent"] = {},
+ ["indent.branch"] = {},
+ ["indent.ignore"] = {},
+ ["indent.align"] = {},
+ ["indent.zero"] = {},
+ }
- for name, node, metadata in queries.iter_captures(bufnr, "indents", root, lang) do
- local path = split(name)
- -- node may contain a period so append directly.
- table.insert(path, node:id())
- queries.insert_to_path(map, path, metadata or {})
+ --TODO(clason): remove when dropping Nvim 0.8 compat
+ local query = (ts.query.get or ts.get_query)(lang, "indents")
+ for id, node, metadata in query:iter_captures(root, bufnr) do
+ if query.captures[id]:sub(1, 1) ~= "_" then
+ map[query.captures[id]][node:id()] = metadata or {}
+ end
end
return map
-end, {
- -- Memoize by bufnr and lang together.
- key = function(bufnr, root, lang)
- return tostring(bufnr) .. root:id() .. "_" .. lang
- end,
-})
+end, function(bufnr, root, lang)
+ return tostring(bufnr) .. root:id() .. "_" .. lang
+end)
---@param lnum number (1-indexed)
function M.get_indent(lnum)
@@ -97,6 +115,7 @@ function M.get_indent(lnum)
return -1
end