diff options
| author | Jędrzej Boczar <yendreij@gmail.com> | 2021-01-07 23:04:40 +0100 |
|---|---|---|
| committer | Thomas Vigouroux <tomvig38@gmail.com> | 2021-01-14 13:54:43 +0100 |
| commit | 32cc79b7c9dbb9cc8f97d04ff12858070bf20074 (patch) | |
| tree | f5978b4745a0bb20d848e9c7d23f4d0c2610079d | |
| parent | indent: fix wrong line number passed to descendant_for_range (diff) | |
| download | nvim-treesitter-32cc79b7c9dbb9cc8f97d04ff12858070bf20074.tar nvim-treesitter-32cc79b7c9dbb9cc8f97d04ff12858070bf20074.tar.gz nvim-treesitter-32cc79b7c9dbb9cc8f97d04ff12858070bf20074.tar.bz2 nvim-treesitter-32cc79b7c9dbb9cc8f97d04ff12858070bf20074.tar.lz nvim-treesitter-32cc79b7c9dbb9cc8f97d04ff12858070bf20074.tar.xz nvim-treesitter-32cc79b7c9dbb9cc8f97d04ff12858070bf20074.tar.zst nvim-treesitter-32cc79b7c9dbb9cc8f97d04ff12858070bf20074.zip | |
indent: improve behavior on `o`/`<cr>` in languages like Python
| -rw-r--r-- | lua/nvim-treesitter/indent.lua | 21 |
1 files changed, 17 insertions, 4 deletions
diff --git a/lua/nvim-treesitter/indent.lua b/lua/nvim-treesitter/indent.lua index 88406c688..333bdb4eb 100644 --- a/lua/nvim-treesitter/indent.lua +++ b/lua/nvim-treesitter/indent.lua @@ -55,10 +55,23 @@ function M.get_indent(lnum) local indent = 0 local indent_size = get_indent_size() - -- if we are on a new line (for instance by typing `o` or `O`) - -- we should get the node that wraps the line our cursor sits in - -- and if the node is an indent node, we should set the indent level as the indent_size - -- and we set the node as the first child of this wrapper node or the wrapper itself + -- to get corret indetation when we land on an empty line (for instance by typing `o`), we try + -- to use indentation of previous nonblank line, this solves the issue also for languages that + -- do not use @branch after blocks (e.g. Python) + if not node then + local prevnonblank = vim.fn.prevnonblank(lnum) + if prevnonblank ~= lnum then + local prev_node = get_node_at_line(root, prevnonblank-1) + -- we take that node only if ends before lnum, or else we would get incorrect indent + -- on <cr> in positions like e.g. `{|}` in C (| denotes cursor position) + if prev_node and (prev_node:end_() < lnum-1) then + node = prev_node + end + end + end + + -- 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) node = wrapper:child(0) or wrapper |
