diff options
Diffstat (limited to 'lua')
| -rw-r--r-- | lua/nvim-treesitter/indent.lua | 123 |
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"] = {}, |
