nvim-treesitter icon indicating copy to clipboard operation
nvim-treesitter copied to clipboard

Python: [ in string causes indent on following lines

Open beesdotjson opened this issue 4 years ago • 15 comments

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:

  1. Type "[" (quotes included) on a line, optionally add text around it
  2. 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

beesdotjson avatar Jul 14 '21 05:07 beesdotjson

Can you still reproduce this with the latest python changes about indentation ?

vigoux avatar Aug 02 '21 14:08 vigoux

Yes. Same behavior still happens.

beesdotjson avatar Aug 13 '21 14:08 beesdotjson

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.

gergol avatar Nov 05 '21 15:11 gergol

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.

stsewd avatar Nov 05 '21 18:11 stsewd

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).

tzachar avatar Dec 05 '21 11:12 tzachar

Exactly the same problem. The indentation on the image below is triggered by '[' at line marked '18' or maybe '24'.

изображение

vadim-zyamalov avatar Dec 26 '21 13:12 vadim-zyamalov

Is there any way to switch off indent handling?

    indent = {
        enable = false,
    }

has no effect for me

vadim-zyamalov avatar Dec 26 '21 13:12 vadim-zyamalov

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" },

dvchoudh avatar Dec 30 '21 08:12 dvchoudh

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.

jamesob avatar Mar 04 '22 18:03 jamesob

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.

Xunius avatar Mar 16 '22 01:03 Xunius

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?

psnelgrove-r7 avatar Oct 05 '23 01:10 psnelgrove-r7

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:

axgkl avatar Oct 08 '23 08:10 axgkl

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.

jskladan avatar Oct 26 '23 09:10 jskladan