mirror of
https://github.com/nvim-treesitter/nvim-treesitter.git
synced 2026-07-13 17:00:09 -04:00
Treesitter indent
also fixes the memoize_by_buf_tick function
This commit is contained in:
parent
36b5f6f075
commit
1735528db5
5 changed files with 80 additions and 56 deletions
|
|
@ -40,7 +40,7 @@ local builtin_modules = {
|
||||||
module_path = 'nvim-treesitter.indent',
|
module_path = 'nvim-treesitter.indent',
|
||||||
enable = false,
|
enable = false,
|
||||||
disable = {},
|
disable = {},
|
||||||
is_supported = queries.has_locals
|
is_supported = queries.has_indents
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,58 +1,64 @@
|
||||||
-- TODO: logic not working properly
|
|
||||||
-- need to find a better way to get the indent from a line
|
|
||||||
-- it almosts works though
|
|
||||||
local api = vim.api
|
|
||||||
local utils = require'nvim-treesitter.ts_utils'
|
|
||||||
local query = require'nvim-treesitter.query'
|
|
||||||
local parsers = require'nvim-treesitter.parsers'
|
local parsers = require'nvim-treesitter.parsers'
|
||||||
|
local queries = require'nvim-treesitter.query'
|
||||||
local utils = require'nvim-treesitter.ts_utils'
|
local utils = require'nvim-treesitter.ts_utils'
|
||||||
local locals = require'nvim-treesitter.locals'
|
|
||||||
|
|
||||||
local M = {}
|
local M = {}
|
||||||
|
|
||||||
local function get_node_at_line(lnum)
|
local function get_node_at_line(root, lnum)
|
||||||
local node = utils.get_node_at_cursor()
|
for node in root:iter_children() do
|
||||||
local srow = node:range()
|
local srow, _, erow = node:range()
|
||||||
|
if srow == lnum then return node end
|
||||||
|
|
||||||
if srow+1 < lnum then
|
if node:child_count() > 0 and srow < lnum and lnum <= erow then
|
||||||
for n in node:iter_children() do
|
return get_node_at_line(node, lnum)
|
||||||
local row = n:range()
|
|
||||||
if row+1 == lnum then
|
|
||||||
node = n
|
|
||||||
break
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
return node
|
local wrapper = root:descendant_for_range(lnum, 0, lnum, -1)
|
||||||
|
local child = wrapper:child(0)
|
||||||
|
return child or wrapper
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local get_indents = utils.memoize_by_buf_tick(function(bufnr)
|
||||||
|
local indents = queries.get_capture_matches(bufnr, '@indent.node', 'indents') or {}
|
||||||
|
local branches = queries.get_capture_matches(bufnr, '@branch.node', 'indents') or {}
|
||||||
|
|
||||||
|
local indents_map = {}
|
||||||
|
for _, node in ipairs(indents) do
|
||||||
|
indents_map[tostring(node)] = true
|
||||||
|
end
|
||||||
|
|
||||||
|
local branches_map = {}
|
||||||
|
for _, node in ipairs(branches) do
|
||||||
|
branches_map[tostring(node)] = true
|
||||||
|
end
|
||||||
|
|
||||||
|
return { indents = indents_map, branches = branches_map }
|
||||||
|
end)
|
||||||
|
|
||||||
function M.get_indent(lnum)
|
function M.get_indent(lnum)
|
||||||
if not parsers.has_parser() or not lnum then return -1 end
|
local parser = parsers.get_parser()
|
||||||
|
if not parser or not lnum then return -1 end
|
||||||
|
|
||||||
local node = get_node_at_line(lnum)
|
local node = get_node_at_line(parser:parse():root(), lnum-1)
|
||||||
local srow, scol, erow, ecol = node:range()
|
local indent_queries = get_indents(vim.api.nvim_get_current_buf())
|
||||||
|
local indents = indent_queries.indents
|
||||||
|
local branches = indent_queries.branches
|
||||||
|
if not indents then return 0 end
|
||||||
|
|
||||||
local parent = locals.containing_scope(node:parent() or node)
|
while node and branches[tostring(node)] do
|
||||||
local parent_row, pscol, perow, pecol = parent:range()
|
node = node:parent()
|
||||||
local parent_indent = vim.fn.indent(parent_row+1)
|
end
|
||||||
|
|
||||||
if (parent_row == srow and pscol == scol) or (perow == erow and pecol == ecol) then
|
local ind = 0
|
||||||
node = parent
|
while node do
|
||||||
srow = node:range()
|
node = node:parent()
|
||||||
node_indent = vim.fn.indent(srow+1)
|
if indents[tostring(node)] then
|
||||||
|
ind = ind + vim.bo.tabstop
|
||||||
parent = locals.containing_scope(parent:parent() or parent)
|
|
||||||
parent_row = parent:range()
|
|
||||||
parent_indent = vim.fn.indent(parent_row+1)
|
|
||||||
|
|
||||||
if node_indent == 0 and parent_indent == 0 then
|
|
||||||
return 0
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local tabstop = vim.bo.tabstop
|
return ind
|
||||||
return parent_indent + tabstop
|
|
||||||
end
|
end
|
||||||
|
|
||||||
local indent_funcs = {}
|
local indent_funcs = {}
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,7 @@ local M = {}
|
||||||
|
|
||||||
local query_cache = caching.create_buffer_cache()
|
local query_cache = caching.create_buffer_cache()
|
||||||
|
|
||||||
M.built_in_query_groups = {'highlights', 'locals', 'textobjects', 'folds'}
|
M.built_in_query_groups = {'highlights', 'locals', 'textobjects', 'folds', 'indents'}
|
||||||
|
|
||||||
-- Creates a function that checks whether a given query exists
|
-- Creates a function that checks whether a given query exists
|
||||||
-- for a specific language.
|
-- for a specific language.
|
||||||
|
|
|
||||||
|
|
@ -201,31 +201,26 @@ end
|
||||||
|
|
||||||
--- Memoizes a function based on the buffer tick of the provided bufnr.
|
--- Memoizes a function based on the buffer tick of the provided bufnr.
|
||||||
-- The cache entry is cleared when the buffer is detached to avoid memory leaks.
|
-- The cache entry is cleared when the buffer is detached to avoid memory leaks.
|
||||||
-- @param fn: the fn to memoize
|
-- @param fn: the fn to memoize, taking the bufnr as first argument
|
||||||
-- @param bufnr_fn: a function that receives all arguments passed to the function
|
|
||||||
-- and returns the bufnr from the arguments
|
|
||||||
-- @returns a memoized function
|
-- @returns a memoized function
|
||||||
function M.memoize_by_buf_tick(fn, bufnr_fn)
|
function M.memoize_by_buf_tick(fn)
|
||||||
local bufnr_fn = bufnr_fn or function(a) return a end
|
|
||||||
local cache = {}
|
local cache = {}
|
||||||
|
|
||||||
return function(...)
|
return function(bufnr)
|
||||||
local bufnr = bufnr_fn(...)
|
|
||||||
local tick = api.nvim_buf_get_changedtick(bufnr)
|
|
||||||
|
|
||||||
if cache[bufnr] then
|
if cache[bufnr] then
|
||||||
if cache[bufnr].last_tick == tick then
|
return cache[bufnr]
|
||||||
return cache[bufnr].result
|
|
||||||
end
|
|
||||||
else
|
else
|
||||||
cache[bufnr] = {}
|
cache[bufnr] = {}
|
||||||
api.nvim_buf_attach(bufnr, false, { on_detach = function() cache[bufnr] = nil end })
|
api.nvim_buf_attach(bufnr, false,
|
||||||
|
{
|
||||||
|
on_changedtick = function() cache[bufnr] = fn(bufnr) end,
|
||||||
|
on_detach = function() cache[bufnr] = nil end
|
||||||
|
}
|
||||||
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
cache[bufnr].last_tick = tick
|
cache[bufnr] = fn(bufnr)
|
||||||
cache[bufnr].result = fn(...)
|
return cache[bufnr]
|
||||||
|
|
||||||
return cache[bufnr].result
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
23
queries/lua/indents.scm
Normal file
23
queries/lua/indents.scm
Normal file
|
|
@ -0,0 +1,23 @@
|
||||||
|
[
|
||||||
|
(function_definition)
|
||||||
|
(local_function)
|
||||||
|
(function)
|
||||||
|
(if_statement)
|
||||||
|
(for_statement)
|
||||||
|
(for_in_statement)
|
||||||
|
(repeat_statement)
|
||||||
|
(while_statement)
|
||||||
|
(table)
|
||||||
|
(arguments)
|
||||||
|
] @indent
|
||||||
|
|
||||||
|
[
|
||||||
|
"end"
|
||||||
|
"{"
|
||||||
|
"}"
|
||||||
|
"("
|
||||||
|
")"
|
||||||
|
"then"
|
||||||
|
(else)
|
||||||
|
(elseif)
|
||||||
|
] @branch
|
||||||
Loading…
Add table
Add a link
Reference in a new issue