Skip to content

improve indentation rules #17868

@rebornix

Description

@rebornix

Indentation

Intro

Indent Style is a convention to convey the structure of source code to human readers in programming languages. It's not a requirement in most programming languages but usually language authors adopt a style for their sample code and encourage users to use that. In some particular programming languages, Python for example, indentations are even meaningful to compilers/interpreters, wrong indentations may lead to compiling or runtime errors.

VS Code helps users to write correct indentations with ease. To make this happen, we followed TextMate's style and implemented three regexp patterns you can set to tell VS Code when to increase or decrease indentations.

  • increaseIndentPattern
  • decreaseIndentPattern
  • indentNextLinePattern
  • unIndentedLinePattern

However, these regexp patterns only decide whether we should modify the indentaion of a line if it matches one pattern, they don't tell us when is a good time to apply the modification. There are two ways to implement this feature, an active one and a passive one.

  • Active: Whenever the model changes(user input, code format, etc), run indentaion regex rules against current line.
  • Passive: Whenever users press enter key, run indentaion regex rules against current line.

Active or Passive

The active way looks really good as the indentation can be adjusted immediately when the content of current line matches the indentation rule. Take Ruby def/end block as an example:

ruby-indentation

Ruby programmers or anyone running into similar situation may only be 100% satisfied if we adjust the indentation correctly once they press d as part of the closing keyword end. But the catch is users might keep typing after we modify the indentation, the content of current line may not match any regex rule any more, should we keep an eye on that and undo previous indentation change? To make things worse, if any indentation regex rule contains $, which matches the ending position of a line, how can we tell whether users finish typing in current line or not?

Taking above into consideration, passive way might be more reasonable. When a user press enter, he or she is sure that the content in current line is satisfactory for the moment and wants to work on following lines. We can calcualte the final indentation for current line and do the updates if it is different from the existing one. Besides, we can calculate the potential correct indentation level for the following line.

In VS Code we chose the passive way.

Are we in good shape now?

We implemented the logic described above (there are several other components that contribute to the indentation, we'll discuss that later) but it's not precisely as expected. Currently we got somewhat wrong with the semantics of decreaseIndentPattern and there might be other cases that might be related or similar:

TODO

  • enrich support for decreaseIndentPattern
  • auto indent selection
  • move lines should honor indentation

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions