Neovim auto-indent produces confusing results in some cases

Many times we want to enter multiple parameters, so we have the following format:

func(
    x = 1,
    y = 2
)

When pressing enter after line 4), line 5 should be indented to the front, not after 4 spaces:

func(
    x = 1,
    y = 2
)
| expected
    | actual

Below is my configuration, what should I do to make it work as expected?

vim.o.filetype = "plugin"

python.lua:

vim.bo.expandtab = true
vim.bo.shiftwidth = 4
vim.bo.tabstop = 4
vim.bo.softtabstop = 4

I’m pretty sure you have treesitter indenting enabled, I would suggest disabling it entirely or for python files only

Yes, this works.

In addition to this, I want to implement another function, do you know how it should do it.

for example:

def func(x, y):
    print(x, y) <CR>
| <BS> <CR>
| expected
    | actual

I can describe it more intuitively with a dynamic graph:

Peek 2022-02-18 14-16

I want the indentation to always keep the indent amount of the previous line, if the previous line doesn’t have any indentation, the next line shouldn’t be indented either, shouldn’t it?

I can’t seem to reproduce this, I get your expected result. Are you sure you’ve disabled treesitter indentation for python files?

Yes, but I disabled the indentation of the treesitter I now want it to work like the diagram, here is my treeitter config:

require("nvim-treesitter.configs").setup(
    {
        ensure_installed = "maintained",
        sync_install = false,
        highlight = {
            enable = true,
            additional_vim_regex_highlighting = false
        },
        incremental_selection = {
            enable = true,
            keymaps = {
                init_selection = "<CR>",
                node_incremental = "<CR>",
                node_decremental = "<BS>",
                scope_incremental = "<TAB>"
            }
        },
        indent = {
            enable = false
        },
        rainbow = {
            enable = true,
            extended_mode = true
            -- colors = {}, -- table of hex strings
            -- termcolors = {} -- table of colour name strings
        },
        context_commentstring = {
            enable = true
        }
    }
)

enable = false is invalid, you need to use this instead:

  • Disable treesitter indentation entirely:
disable = true,
  • Disable treesitter indenting for python files only:
disable = { "python" },
1 Like

I believe that enable = false disables the entire indent module. This is documented in the README and code is here: nvim-treesitter/configs.lua at master · nvim-treesitter/nvim-treesitter · GitHub

I don’t believe that disable = true is supported but disable = { <language>, ...} is supported as you have written. The disable handling is here: nvim-treesitter/configs.lua at master · nvim-treesitter/nvim-treesitter · GitHub

@askfiy already has enable = false in his treesitter indentation config. It doesn’t seem to disable it for him, neither does it for me. disable = true is supported, and that’s how you disable treesitter indentation entirely or independent languages. (Atleast that’s what works for me).

For me either disable = true or enable = false works equally well, but disable = {‘python’} doesn’t.

Also, the phenomenon in my second picture can’t be solved by this, I don’t know what to do,lol …

I really don’t think treesitter indentation is getting disabled for you. Can you try uninstalling the python parser entirely using :TSUninstall python and see if the behavior you expect happens?