diff options
| author | Stephan Seitz <stephan.seitz@fau.de> | 2022-01-22 17:07:25 +0100 |
|---|---|---|
| committer | Stephan Seitz <stephan.seitz@fau.de> | 2022-02-05 18:54:55 +0100 |
| commit | b9c38a48cad935e01b96fc11df94763c85c98ee9 (patch) | |
| tree | e39206a6bd64fc73a52a033a8747be5461dc6a03 | |
| parent | 059fbc487c62a5e109143254b720a5dc33201a34 (diff) | |
indents: refactor hanging indent
| -rw-r--r-- | lua/nvim-treesitter/indent.lua | 36 | ||||
| -rw-r--r-- | tests/indent/c/initializer_list.c | 3 | ||||
| -rw-r--r-- | tests/indent/cpp/initializer_list.cpp | 22 |
3 files changed, 25 insertions, 36 deletions
diff --git a/lua/nvim-treesitter/indent.lua b/lua/nvim-treesitter/indent.lua index 55b341f3..df0420fe 100644 --- a/lua/nvim-treesitter/indent.lua +++ b/lua/nvim-treesitter/indent.lua @@ -13,17 +13,15 @@ local function get_last_node_at_line(root, lnum) return root:descendant_for_range(lnum - 1, col, lnum - 1, col) end -local function get_matching_prev_sibling(anchor, start, matcher) - local start_row, start_col = start[1], start[2] - local node = anchor:descendant_for_range(start_row, start_col, start_row, start_col) - local pos = 1 - -- TODO: reconsider this 999 limit or do something differently in future. - -- if anchor has more than 999 children, this would not work. - while pos < 999 and node and not matcher(node) do - node = node:prev_sibling() - pos = pos + 1 +local function find_delimiter(bufnr, node, delimiter) + for child, _ in node:iter_children() do + if child:type() == delimiter then + local linenr = child:start() + local line = vim.api.nvim_buf_get_lines(bufnr, linenr, linenr + 1, false)[1] + local end_char = {child:end_()} + return child, #line == end_char[2] + end end - return node, pos end local M = {} @@ -59,6 +57,7 @@ function M.get_indent(lnum) if not parser or not lnum then return -1 end + local bufnr = vim.api.nvim_get_current_buf() -- get_root_for_position is 0-based. local root, _, lang_tree = tsutils.get_root_for_position(lnum - 1, 0, parser) @@ -126,23 +125,16 @@ function M.get_indent(lnum) if q.aligned_indent[node:id()] and srow ~= erow and (srow ~= lnum - 1) then local metadata = q.aligned_indent[node:id()] local opening_delimiter = metadata.delimiter:sub(1, 1) - local o_delim_node, pos = get_matching_prev_sibling(node, { srow, #vim.fn.getline(srow + 1) - 1 }, function(n) - return n:type() == opening_delimiter - end) + local o_delim_node, is_last_in_line = find_delimiter(bufnr, node, opening_delimiter) if o_delim_node then - if pos == 1 then + if is_last_in_line then -- hanging indent (previous line ended with starting delimiter) indent = indent + indent_size * 1 else - local _, o_scol = o_delim_node:start() - local aligned_indent = math.max(indent, 0) + o_scol - if indent > 0 then - indent = aligned_indent - else - indent = aligned_indent + 1 -- extra space for starting delimiter - end - is_processed = true + local _, o_scol = o_delim_node:end_() + o_scol = o_scol + (metadata.increment or 0) + return math.max(indent, 0) + o_scol end end end diff --git a/tests/indent/c/initializer_list.c b/tests/indent/c/initializer_list.c deleted file mode 100644 index 9bf28d94..00000000 --- a/tests/indent/c/initializer_list.c +++ /dev/null @@ -1,3 +0,0 @@ -int a[] = { - 1, 2, 3, - 4}; diff --git a/tests/indent/cpp/initializer_list.cpp b/tests/indent/cpp/initializer_list.cpp index 5a27d17f..d34ed025 100644 --- a/tests/indent/cpp/initializer_list.cpp +++ b/tests/indent/cpp/initializer_list.cpp @@ -1,17 +1,17 @@ class Foo { - Foo(int a, int b, int c, int d) - : m_a(a) - , m_b(b) - , m_c(c) - , m_d(d) {} + Foo(int a, int b, int c, int d) + : m_a(a) + , m_b(b) + , m_c(c) + , m_d(d) {} - Foo(int a, int b, int c) : - m_a(a), - m_b(b), - m_c(c) - {} + Foo(int a, int b, int c) : + m_a(a), + m_b(b), + m_c(c) + {} - int m_a, m_b, m_c, m_d; + int m_a, m_b, m_c, m_d; }; |
