nvim-treesitter
nvim-treesitter copied to clipboard
Python: [ in string causes indent on following lines
Describe the bug
"[" anywhere on a line is treated the same as an opening [ and upon pressing enter, the next line is indented.
To Reproduce
Steps to reproduce the behavior:
- Type
"["(quotes included) on a line, optionally add text around it - Press enter
Expected behavior
The next line should not be indented.
Output of :checkhealth nvim_treesitter
health#nvim_treesitter#check
Installation
- OK:
tree-sitter found 0.20.0 (parser generator, only needed for :TSInstallFromGrammar)
- OK:
node found v16.4.1 (only needed for :TSInstallFromGrammar)
- OK:
git executable found.
- OK:
cc executable found. Selected from { vim.NIL, "cc", "gcc", "clang", "cl" }
- OK: Neovim was compiled with tree-sitter runtime ABI version 13 (required >=13). Parsers must be compatible with runtime ABI.
Parser/Features H L F I J
- bibtex ✓ . ✓ ✓ .
- python ✓ ✓ ✓ ✓ ✓
- lua ✓ ✓ ✓ ✓ ✓
- json ✓ ✓ ✓ ✓ .
- cpp ✓ ✓ ✓ ✓ ✓
- latex ✓ . ✓ . ✓
- css ✓ . ✓ ✓ ✓
- regex ✓ . . . .
- c ✓ ✓ ✓ ✓ ✓
- bash ✓ ✓ ✓ . ✓
- yaml ✓ ✓ ✓ ✓ ✓
- cmake ✓ . ✓ . .
- rust ✓ ✓ ✓ ✓ ✓
- toml ✓ ✓ ✓ ✓ ✓
- javascript ✓ ✓ ✓ ✓ ✓
Legend: H[ighlight], L[ocals], F[olds], I[ndents], In[j]ections
+) multiple parsers found, only one will be used
x) errors found in the query, try to run :TSUpdate {lang}
Output of nvim --version
NVIM v0.5.0
Build type: Release
LuaJIT 2.0.5
Compilation: /usr/bin/cc -D_FORTIFY_SOURCE=2 -march=x86-64 -mtune=generic -O2 -pipe -fno-plt -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=1 -DNVIM_TS_HAS_SET_MATCH_LIMIT -O2 -DNDEBUG -Wall -Wextra -pedantic -Wno-unused-parameter -Wstrict-prototypes -std=gnu99 -Wshadow -Wconversion -Wmissing-prototypes -Wimplicit-fallthrough -Wvla -fstack-protector-strong -fno-common -fdiagnostics-color=always -DINCLUDE_GENERATED_DECLARATIONS -D_GNU_SOURCE -DNVIM_MSGPACK_HAS_FLOAT32 -DNVIM_UNIBI_HAS_VAR_FROM -DMIN_LOG_LEVEL=3 -I/build/neovim/src/neovim-0.5.0/build/config -I/build/neovim/src/neovim-0.5.0/src -I/usr/include -I/build/neovim/src/neovim-0.5.0/build/src/nvim/auto -I/build/neovim/src/neovim-0.5.0/build/include
Compiled by builduser
Features: +acl +iconv +tui
See ":help feature-compile"
system vimrc file: "$VIM/sysinit.vim"
fall-back for $VIM: "/usr/share/nvim"
Run :checkhealth for more info
Can you still reproduce this with the latest python changes about indentation ?
Yes. Same behavior still happens.
Same here, it makes plugin more or less unusable within these python files. Interestingly, if you add the matching closing bracket ] in a comment at the end of the line, the indentation works again. Also the same bug seems to be caused by other kinds of brackets, not only in strings but also in comments.
Same here, it makes plugin more or less unusable within these python files. Interestingly, if you add the matching closing bracket
]in a comment at the end of the line, the indentation works again. Also the same bug seems to be caused by other kinds of brackets, not only in strings but also in comments.
indentation is still experimental. The complicated part is that when a sentence is incomplete, treesitter will generate an error node, and then the indentation code doesn't know what node it could be.
I am having the same issue. I think it is more than the incomplete behaviour, as this also happens when you add 'indent triggering' characters in comment, e.g.,
# this comment will trigger indent on the next line:
Notice the : at the end of the comment.
For what its worth, this does not happen in other languages (tried cpp and c).
Exactly the same problem. The indentation on the image below is triggered by '[' at line marked '18' or maybe '24'.

Is there any way to switch off indent handling?
indent = {
enable = false,
}
has no effect for me
Is there any way to switch off indent handling?
indent = { enable = false, }has no effect for me
Adding this in your indent field for your treesitter config should do it:
disable = true,
If you want to disable treesitter indenting for python files only, you can add this:
disable = { "python" },
Can confirm I'm still seeing this as of 50cf310; makes the plugin nearly unusable for Python.
If you want to disable treesitter indenting for python files only, you can add this:
disable = { "python" },
This isn't working for me.
Can confirm that this is happening to me as well (neovim version v0.6.1, treesitter: 0.20.4). I'm only testing python.
It seems that it is not only [, these opening brackets all trigger the problem: [, (, {, but NOT <.
It happens within comments as well, and can also be fixed by matching those brackets in comments.
NOTE that this is with the indent module disabled already. I have to disable highlight as well to make the issue go.
Ran into this today when writing a range query for a Redis sorted set.
self.conn.zrangebyscore(key, "-inf", f"({upper_bound}")
neovim: v0.9.2, treesitter: 0.20.8
I'm guessing if I were to go look for the implementation details this would be here and not at the parser level?
from .const import SB, RB, CB # brackets, to not confuse treesitter
I love treesitter too much to switch it off completely - but questions from colleagues getting nasty about this :blush:
Thank you @Xunius for getting me to the right path! This can be worked around (at least on my setup :D) by this:
highlight = {
enable = true,
additional_vim_regex_highlighting = { "python" },
},
Not ideal, probably, but solves the problem, at least temporarily.