helix: Python file indentation is wrong

Reproduction steps

  1. download latest macOS release 0.4.1
  2. open a python file
  3. move cursor to some line
  4. press o to insert newline
  5. the cursor is at the beginning of line, without any indentation.

Environment

  • Platform: macOS
  • Helix version: 0.4.1

zero config.

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 9
  • Comments: 22 (17 by maintainers)

Commits related to this issue

Most upvoted comments

What you suggest sounds like it would be cleaner.

For python, we ideally want to extend the function node to everything that is currently indented more than the beginning of the function node.

Yea that seems important in python.

Let me know what you think and if I’ve overlooked anything.

Nothing obvious to me at this point. I’m sure there will be some challenges when it actually comes to implementing it.

I’ll have a go if I can find some spare time.

I get autoindentation if I press Enter when the marker is at | in the following:

a = list(|)
->
a = list(
    |
)

but I do not get it with the if statement as hinted should happen in the the file @willparsons refers to above.

if a == 1:|
->
if a == 1:
|

Indent support requires an indents.toml file describing the tree-sitter syntax nodes where it should be automatically inserted, for example see https://github.com/helix-editor/helix/blob/master/runtime/queries/rust/indents.toml. Python doesn’t yet have this file, so contributions are welcome !

If anyone wants to take this up, there’s https://github.com/nvim-treesitter/nvim-treesitter/blob/master/queries/python/indents.scm and the tree-sitter python grammar for reference.

Thanks for looking into this @zyklotomic. What I mean by adjusting the tree-sitter grammar is not to make it parse incorrect code without producing any ERROR nodes. It should still produce errors (after all, the code is incorrect) but ideally they would be a bit more local. For example, an incomplete try_statement shouldn’t suddenly make the surrounding function an error as well. Maybe it could even produce a try_statement node and put the ERROR inside. This way, highlighting and indentation would still work everywhere except for the exact location of the error.

I had a look at the playground and came up with some queries which seem to work pretty well. The PR is #5332, if you want to try it out and report any issues, that would be a great help.

As for retroactive indenting/dedenting, I’m thinking if we can find a general, language-agnostic solution to this. It’d be nice if Helix could automatically rerun indent queries when the syntax tree changes and adjust the indent of existing lines. This will probably take some time though, as it has to be fast, accurate and it needs to detect when the user is trying to override the computed indent (for example when it is incorrect).

The lack of auto-indentation is the only thing that keeps me from fully embracing Helix. For the most part it’s great (especially because most things work out of the box), but auto-indent is really the level of convenience every text-editor should offer. Maybe I’m doing something wrong (I’ve installed Helix via Pacman and pylsp via pip and downloaded the languages.toml file from the repo), but for me not even Indentation after “:” is working, let alone aligning parentheses or operators in multiline clauses.

Is the issue really as severe as @lisael is pointing out? Any hope for a feasible solution in the near to mid future?

Incidentally, what does the “Auto Indent” on the languages page refer to exactly? That it’s part of the auto-formatting?