summaryrefslogtreecommitdiff
path: root/lua/nvim-treesitter/indent.lua
diff options
context:
space:
mode:
Diffstat (limited to 'lua/nvim-treesitter/indent.lua')
-rw-r--r--lua/nvim-treesitter/indent.lua54
1 files changed, 31 insertions, 23 deletions
diff --git a/lua/nvim-treesitter/indent.lua b/lua/nvim-treesitter/indent.lua
index e66edf1d..af08bcd6 100644
--- a/lua/nvim-treesitter/indent.lua
+++ b/lua/nvim-treesitter/indent.lua
@@ -1,6 +1,6 @@
-local parsers = require'nvim-treesitter.parsers'
-local queries = require'nvim-treesitter.query'
-local tsutils = require'nvim-treesitter.ts_utils'
+local parsers = require "nvim-treesitter.parsers"
+local queries = require "nvim-treesitter.query"
+local tsutils = require "nvim-treesitter.ts_utils"
local M = {}
@@ -8,7 +8,9 @@ local M = {}
local function get_node_at_line(root, lnum)
for node in root:iter_children() do
local srow, _, erow = node:range()
- if srow == lnum then return node end
+ if srow == lnum then
+ return node
+ end
if node:child_count() > 0 and srow < lnum and lnum <= erow then
return get_node_at_line(node, lnum)
@@ -17,13 +19,15 @@ local function get_node_at_line(root, lnum)
end
local function node_fmt(node)
- if not node then return nil end
+ if not node then
+ return nil
+ end
return tostring(node)
end
local get_indents = tsutils.memoize_by_buf_tick(function(bufnr, root, lang)
local get_map = function(capture)
- local matches = queries.get_capture_matches(bufnr, capture, 'indents', root, lang) or {}
+ local matches = queries.get_capture_matches(bufnr, capture, "indents", root, lang) or {}
local map = {}
for _, node in ipairs(matches) do
map[tostring(node)] = true
@@ -32,29 +36,33 @@ local get_indents = tsutils.memoize_by_buf_tick(function(bufnr, root, lang)
end
return {
- indents = get_map('@indent.node'),
- branches = get_map('@branch.node'),
- returns = get_map('@return.node'),
- ignores = get_map('@ignore.node'),
+ indents = get_map "@indent.node",
+ branches = get_map "@branch.node",
+ returns = get_map "@return.node",
+ ignores = get_map "@ignore.node",
}
end, {
-- Memoize by bufnr and lang together.
key = function(bufnr, _, lang)
- return tostring(bufnr) .. '_' .. lang
- end
+ return tostring(bufnr) .. "_" .. lang
+ end,
})
function M.get_indent(lnum)
local parser = parsers.get_parser()
- if not parser or not lnum then return -1 end
+ if not parser or not lnum then
+ return -1
+ end
local root, _, lang_tree = tsutils.get_root_for_position(lnum, 0, parser)
-- Not likely, but just in case...
- if not root then return 0 end
+ if not root then
+ return 0
+ end
local q = get_indents(vim.api.nvim_get_current_buf(), root, lang_tree:lang())
- local node = get_node_at_line(root, lnum-1)
+ local node = get_node_at_line(root, lnum - 1)
local indent = 0
local indent_size = vim.fn.shiftwidth()
@@ -65,11 +73,11 @@ function M.get_indent(lnum)
if not node then
local prevnonblank = vim.fn.prevnonblank(lnum)
if prevnonblank ~= lnum then
- local prev_node = get_node_at_line(root, prevnonblank-1)
+ local prev_node = get_node_at_line(root, prevnonblank - 1)
-- get previous node in any case to avoid erroring
- while not prev_node and prevnonblank-1 > 0 do
- prevnonblank = vim.fn.prevnonblank(prevnonblank-1)
- prev_node = get_node_at_line(root, prevnonblank-1)
+ while not prev_node and prevnonblank - 1 > 0 do
+ prevnonblank = vim.fn.prevnonblank(prevnonblank - 1)
+ prev_node = get_node_at_line(root, prevnonblank - 1)
end
-- nodes can be marked @return to prevent using them
@@ -91,7 +99,7 @@ function M.get_indent(lnum)
-- if the prevnonblank fails (prev_node wraps our line) we need to fall back to taking
-- the first child of the node that wraps the current line, or the wrapper itself
if not node then
- local wrapper = root:descendant_for_range(lnum-1, 0, lnum-1, -1)
+ local wrapper = root:descendant_for_range(lnum - 1, 0, lnum - 1, -1)
node = wrapper:child(0) or wrapper
if q.indents[node_fmt(wrapper)] ~= nil and wrapper ~= root then
indent = indent_size
@@ -107,7 +115,7 @@ function M.get_indent(lnum)
while node do
-- do not indent if we are inside an @ignore block
- if q.ignores[node_fmt(node)] and node:start() < lnum-1 and node:end_() > lnum-1 then
+ if q.ignores[node_fmt(node)] and node:start() < lnum - 1 and node:end_() > lnum - 1 then
return -1
end
@@ -129,8 +137,8 @@ local indent_funcs = {}
function M.attach(bufnr)
indent_funcs[bufnr] = vim.bo.indentexpr
- vim.bo.indentexpr = 'nvim_treesitter#indent()'
- vim.api.nvim_command("au Filetype "..vim.bo.filetype.." setlocal indentexpr=nvim_treesitter#indent()")
+ vim.bo.indentexpr = "nvim_treesitter#indent()"
+ vim.api.nvim_command("au Filetype " .. vim.bo.filetype .. " setlocal indentexpr=nvim_treesitter#indent()")
end
function M.detach(bufnr)