question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Making auto indentation work

See original GitHub issue

As noted in spike https://github.com/microsoft/vscode-python/issues/8669

There are three approaches to solve this:

1. VSCode Language Configuration

VSCode provides OnEnterRules and indentationRules which dedents/indents the block given the previous line matches the regex. But regexes can’t be used to cover every case, and the current rules are not powerful enough. https://github.com/microsoft/vscode/issues/66235 Also sometimes just knowledge of previous line is not sufficient to make a decision on the indent.

2. On-type indenting

When editor.formatOnType is set to true, one can intervene in between when a user types and indent accordingly. We currently use this in onEnterFormatter.ts and other places. We can use this to cover all the cases one by one. But on-type formatting is not enabled by default nor is very popular (and enables a slew of formatting rules other than cursor alignment), so forcing users to enable on-type formatting as a whole would be good to avoid.

3. Add a key binding for enter

We can add something like

"keybindings": [
    {
        "command": "<Command key>",
        "key": "enter",
        "when": "editorTextFocus && editorLangId == python"
    }
],

(may need more modifying)

and use that to intervene. This way we don’t need to have editor.formatOnType enabled. However we need to keep in mind that in this case we’re responsible for cursor alignment after the user presses enter, otherwise cursor would stay where it is.

As there is no progress on vscode upstream issues related to 1., ~going with 3. makes the most sense at this point.~

https://github.com/kbrose/vsc-python-indent/blob/master/src/indent.ts seems like a wonderful place to take help from. It covers most cases (https://github.com/microsoft/vscode-python/issues/481) we currently have open issues for and is well documented. We can use npm package python-indent-parser similarly to the way @kbrose does.

Also another thing to note here is that having both language configuration and key bindings can override each other sometimes. So we should be moving away from regexes when we solve work items for this.

EDIT: We’ve decided to go with 2.

Work items

Note we also do dedenting when user presses semi-colon : https://github.com/Microsoft/vscode-python/blob/3c7bb774519ebf5aed91f90e10c7b8dd0fc567ec/src/client/typeFormatters/blockFormatProvider.ts#L25 Make sure these does not conflict with what Kbrose’s extension is doing.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:13
  • Comments:7

github_iconTop GitHub Comments

2reactions
karrtikrcommented, Dec 30, 2019

@J-Fields If you browse through the mentioned issues, we used to have indentationRules, and it caused a few bugs, so we decided to remove it. Also I am not sure if we can override their effect with formatOnType, i.e I am not sure what happens when rules conflict.

As indentationRules don’t support every indentation scenario, we can try adding indentationRules after all the other work is done.

1reaction
kbrosecommented, Dec 18, 2019

I have not personally noticed any misbehaving on that front. The only issue I’ve received about unexpected behavior related to overriding Enter was related to the vim plugin, which I resolved here.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Indent the first line of a paragraph - Microsoft Support
To indent the first line of a paragraph, put your cursor at the beginning of the paragraph and press the tab key. When...
Read more >
Controlling Automatic Indenting - Word Ribbon Tips
I think the easiest way to indent is to use the double (upper, lower) left-hand margin indicators in the ruler. Highlight the paragraph,...
Read more >
How does auto-indentation work in the Editor?
How does auto-indentation work in the Editor? Basically you just use the Tab key to indent code, you can indent the current line,...
Read more >
How to make auto-indentation work correctly after macros ...
Otherwise they are easily confused with declarations. These four options control C program indenting: 'cindent' Enables Vim to perform C program ...
Read more >
How do you auto format code in Visual Studio? - Stack Overflow
Its Ctr + K, Ctrl + D in Visual Studio 2013 to format a document. – Kris. Aug 21, 2014 at 14:49. 6....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found