diff options
| author | kiyan42 <yazdani.kiyan@protonmail.com> | 2020-10-13 01:02:30 +0200 |
|---|---|---|
| committer | Kiyan Yazdani <yazdani.kiyan@protonmail.com> | 2020-10-19 21:08:15 +0200 |
| commit | 36b5f6f075114b5566d0ab225f97466ff36de41f (patch) | |
| tree | 19cb1662c93352b73059973e7ef9d037674b6bc9 /lua | |
| parent | docs(highlights): document LanguageTree. (diff) | |
| download | nvim-treesitter-36b5f6f075114b5566d0ab225f97466ff36de41f.tar nvim-treesitter-36b5f6f075114b5566d0ab225f97466ff36de41f.tar.gz nvim-treesitter-36b5f6f075114b5566d0ab225f97466ff36de41f.tar.bz2 nvim-treesitter-36b5f6f075114b5566d0ab225f97466ff36de41f.tar.lz nvim-treesitter-36b5f6f075114b5566d0ab225f97466ff36de41f.tar.xz nvim-treesitter-36b5f6f075114b5566d0ab225f97466ff36de41f.tar.zst nvim-treesitter-36b5f6f075114b5566d0ab225f97466ff36de41f.zip | |
start indent module
Diffstat (limited to 'lua')
| -rw-r--r-- | lua/nvim-treesitter/configs.lua | 6 | ||||
| -rw-r--r-- | lua/nvim-treesitter/indent.lua | 69 |
2 files changed, 75 insertions, 0 deletions
diff --git a/lua/nvim-treesitter/configs.lua b/lua/nvim-treesitter/configs.lua index 0842f2769..2655adc79 100644 --- a/lua/nvim-treesitter/configs.lua +++ b/lua/nvim-treesitter/configs.lua @@ -36,6 +36,12 @@ local builtin_modules = { }, is_supported = queries.has_locals }, + indent = { + module_path = 'nvim-treesitter.indent', + enable = false, + disable = {}, + is_supported = queries.has_locals + } } local attached_buffers_by_module = caching.create_buffer_cache() diff --git a/lua/nvim-treesitter/indent.lua b/lua/nvim-treesitter/indent.lua new file mode 100644 index 000000000..bd7d64454 --- /dev/null +++ b/lua/nvim-treesitter/indent.lua @@ -0,0 +1,69 @@ +-- 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 utils = require'nvim-treesitter.ts_utils' +local locals = require'nvim-treesitter.locals' + +local M = {} + +local function get_node_at_line(lnum) + local node = utils.get_node_at_cursor() + local srow = node:range() + + if srow+1 < lnum then + for n in node:iter_children() do + local row = n:range() + if row+1 == lnum then + node = n + break + end + end + end + + return node +end + +function M.get_indent(lnum) + if not parsers.has_parser() or not lnum then return -1 end + + local node = get_node_at_line(lnum) + local srow, scol, erow, ecol = node:range() + + local parent = locals.containing_scope(node:parent() or node) + local parent_row, pscol, perow, pecol = parent:range() + local parent_indent = vim.fn.indent(parent_row+1) + + if (parent_row == srow and pscol == scol) or (perow == erow and pecol == ecol) then + node = parent + srow = node:range() + node_indent = vim.fn.indent(srow+1) + + 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 + + local tabstop = vim.bo.tabstop + return parent_indent + tabstop +end + +local indent_funcs = {} + +function M.attach(bufnr) + indent_funcs[bufnr] = vim.bo.indentexpr + vim.bo.indentexpr = 'nvim_treesitter#indent()' +end + +function M.detach(bufnr) + vim.bo.indentexpr = indent_funcs[bufnr] +end + +return M |
