Skip to content

Explore using formatters for indentation adjustment when formatters are available #19847

Open

Description

@borekb

VSCode 1.9 introduced initial support for line indenting as per the release notes, I think it's this commit: be1b5f7. I'm very interested in this feature, big 👍 for bringing it into VSCode.

Here is some initial feedback:

  • I tried for a couple of file formats, it only worked for TypeScript and not e.g., for JSON or PHP.
  • Formatting (Alt+Shift+F) can already do indentation better than the new implementation, maybe it could be used as a fallback if the language doesn't have proper indentation rules.
  • If I read the code correctly, it re-indents the whole file. It should respect my selection. For example, if I have only 10 lines selected, it should re-indent those. If I don't have any selection, it should do the whole file.
  • The command is currently editor.action.reindentlines, proper casing would be editor.action.reindentLines.

Activity

  1. rebornix commented on Feb 3, 2017

    @rebornix
    Contributor

    Borek Bernard (@borekb) thanks for your detailed feedback!

    The command is currently editor.action.reindentlines, proper casing would be editor.action.reindentLines

    Yes it should be improved

    If I read the code correctly, it re-indents the whole file. It should respect my selection. For example, if I have only 10 lines selected, it should re-indent those. If I don't have any selection, it should do the whole file.

    The reason that right now we don't support Reindent in ranges is that we have some issues with boundaries in embedded-language files. Some of them are just TextMate Grammar issues but I'm sure if that can fix all. Once that issue is addressed, I think it's easy to support reindent in ranges/selection. And then Reindent when paste/moveLines/etc.

    I tried for a couple of file formats, it only worked for TypeScript and not e.g., for JSON or PHP.

    It's on the plan, adding Indentation Rules to most languages we support

    Formatting (Alt+Shift+F) can already do indentation better than the new implementation, maybe it could be used as a fallback if the language doesn't have proper indentation rules.

    Nice idea! I'm thinking about a similar solution like running formatting on indentation characters and see what's the proper edits a formatter would provide. That means we run format on leading whitespaces instead of whole lines but there might be performance hit while handling large files.


    Really appreciate your feedback! Feedback and PRz are always welcome.

  2. borekb commented on Feb 3, 2017

    @borekb
    Author

    That means we run format on leading whitespaces instead of whole lines

    Exactly, that's how I think about "reindenting" as a user. Technically, the two commands (formatting vs. indenting) should probably share the same logic.

  3. rebornix commented on Feb 6, 2017

    @rebornix
    Contributor

    Did some basic investigation on this and I found that leveraging formatter to do indent is kind of ideal as formatters' behaviors are not predictable. Take below code as example

    foo() {
    var a;
      }
    

    I obverse following:

    • Format selection on leading whitepsaces doesn't work
    • Format } works
    • Format v on the second line doesn't work
    • Format var a; on the second line works

    You can see that TypeScript formatter has no idea about adjusting the indentation if don't select any thing, even though it supports Format Selection.

    We may need other ways to handle it as we can't force formatters to take care of indentation always. But it might be a good idea to allow formatters to provide a separate function about indentation adjustment. Any thoughts Matt Bierner (@mjbvz) ?

  4. added
    under-discussion
    on Apr 19, 2017
  5. borekb commented on May 12, 2017

    @borekb
    Author

    Any news on this?

  6. rebornix commented on May 12, 2017

    @rebornix
    Contributor

    Borek Bernard (@borekb) sorry I don't have time working on this issue this iteration but it's on my radar, I received similar issues or feedback on this every other day

  7. dobriai commented on May 14, 2017

    @dobriai

    FWIW: This feature is a life-saver for me. As much as I appreciate js-beautify, it seems that it cannot be told not to mess with my white-space. Trying to format something like this:

    let aha          = 7;
    let badahabadaha = 9;
    

    mangles it, by trimming the white space in front of '= 7;' and thus looses the '=' alignment! Big deal and I just don't see a way to turn this behavior off.

    Reindent Lines seems to do whatever most other editors do, so this is the cure for me. But it is very important to have a version that works on the current selection only!

  8. added
    editor-autoindent
    on Jun 7, 2017
  9. changed the title [-]Reindent lines in 1.9 – feedback[/-] [+]Explore using formatters for indentation adjustment when formatters are available[/+] on Nov 3, 2020
  10. rebornix commented on Nov 3, 2020

    @rebornix
    Contributor

    The title is modified to one good idea proposed above: when there is a good formatter registered for a language, we should probably use the formatter other than regex based indentation rules.

  11. added this to the Backlog milestone on Nov 3, 2020
  12. andycraig commented on Nov 3, 2020

    @andycraig

    The title is modified to one good idea proposed above: when there is a good formatter registered for a language, we should probably use the formatter other than regex based indentation rules.

    Peng Lyu (@rebornix) A user of the vscode-R extension pointed out that the R formatter relies on the code being parseable. This means that Format Selection won't fix indentation if the selection only contains part of a for loop, for example. My understanding is that Reindent Selected Lines should still work in this case, because it uses simpler regex-based rules. So perhaps Reindent Selected Lines should use regex-based indentation rules, even if Reindent Lines uses the registered formatter? Or perhaps indentation should have its own LSP command, which the language server can implement or not?

    (Reindent Selected Lines isn't working at all at the moment for R, but that's a separate issue.)

    EDIT: I read this thread more carefully and noticed that I've basically just repeated what you observed in an earlier comment: #19847 (comment)

  13. added
    feature-request
    and removed
    under-discussion
    on Dec 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

editor-autoindentfeature-request

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions