Vscode: Explore using formatters for indentation adjustment when formatters are available

Created on 3 Feb 2017  路  8Comments  路  Source: microsoft/vscode

VSCode 1.9 introduced initial support for line indenting as per the release notes, I think it's this commit: https://github.com/Microsoft/vscode/commit/be1b5f75161a30dd63316b832067b41d2caec0b3. 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.
editor-autoindent under-discussion

Most helpful comment

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!

All 8 comments

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

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.

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 @mjbvz ?

Any news on this?

@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

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!

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.

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.

@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: https://github.com/microsoft/vscode/issues/19847#issuecomment-277784969

Was this page helpful?
0 / 5 - 0 ratings