NulisDefo
NulisDefo

Reputation: 335

VSCode redefining enter behavior in brackets

I've been trying to find how I could modify the behavior of pressing Enter in various brackets () {} []. I've seen someone have this in a video (it was Sublime, but I didn't find how to do it there either).

When pressing enter now, the change is as follows:

{}

to

{
    [marker here]
}

What I would like to do is insert extra empty lines at the beginning and get this:

{

    [marker here]

}

Any suggestions on how to do that?

Upvotes: 1

Views: 395

Answers (1)

Jan Dolejsi
Jan Dolejsi

Reputation: 1528

Here is a related question: How to implement complicated auto-indentation in VScode

I tried to implement a similar OnTypeFormattingEditProvider that modifies the whitespace in a similar style as you want. It is easy to insert the whitespace you want in between the pair of brackets, but I did not figure out how to insert some before the cursor and some after the cursor. So instead of creating the insert edits and letting VS Code apply them, I opted for creating an insertBeforeCursor and insertAfterCursor edits, applying them using workspace.applyEdit(...) and subsequently moving the cursor using the commands.executeCommand('cursorMove', { to: 'left', value: insertAfterCursorText.length } ) and returning [] out of the provideOnTypeFormattingEdits method.

See applyAndMoveCursor method in this on-type formatter implementation. I would be very happy if someone points out how to construct the edits and instruct VS Code whether the edit should/shouldn't move the cursor.

And do not forget to turn on the on-type formatting VS Code editor.formatOnType setting.

Upvotes: 0

Related Questions