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 | |
| parent | a1de79e93a7f190ef08b98bb6aef7e41c47fd2ee (diff) | |
start indent module
| -rw-r--r-- | autoload/nvim_treesitter.vim | 4 | ||||
| -rw-r--r-- | lua/nvim-treesitter/configs.lua | 6 | ||||
| -rw-r--r-- | lua/nvim-treesitter/indent.lua | 69 |
3 files changed, 79 insertions, 0 deletions
diff --git a/autoload/nvim_treesitter.vim b/autoload/nvim_treesitter.vim index a5f1f978..a19a7470 100644 --- a/autoload/nvim_treesitter.vim +++ b/autoload/nvim_treesitter.vim @@ -17,3 +17,7 @@ endfunction function! nvim_treesitter#available_modules(arglead, cmdline, cursorpos) abort return join(luaeval("require'nvim-treesitter.configs'.available_modules()"), "\n") endfunction + +function! nvim_treesitter#indent() abort + return luaeval(printf('require"nvim-treesitter.indent".get_indent(%d)', v:lnum)) +endfunction diff --git a/lua/nvim-treesitter/configs.lua b/lua/nvim-treesitter/configs.lua index 0842f276..2655adc7 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 00000000..bd7d6445 --- /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 |
