10

I'm looking to modify VS Code's indentation behavior so that if I press enter after typing a line like this:

variable = function(param1,

It'll indent to the level of the open parenthesis so that I can easily format code like this:

variable = function(param1,
                    param2)

I'd like it to work for open square brackets and curly brackets as well:

variable = function([1, 2, 3, 4
                     5, 6, 7, 8],
                    param2,
                    {'a': 1, 'b': 2,
                     'c': 3, 'd': 4},
                    param4)

I'd prefer it to have this behavior for pretty much every language I work with, though the curly braces behavior isn't necessary (and may even be undesirable) when working in C++ or C#.

This is very similar to Sublime Text's indent_to_bracket setting.

Is there any way to accomplish this? If there's no setting, I'm willing to tinker with whatever is necessary. I'm also open to an extension that can do this, or even writing an extension if it's necessary and makes sense to do so.

Bri Bri
  • 2,169
  • 3
  • 19
  • 44

3 Answers3

10

There is a closed issue on GitHub for this very feature. A recent comment from the development team reads as follows:

This feature request will not be considered in the next 6-12 months roadmap and as such will be closed to keep the number of issues we have to maintain actionable. Thanks for understanding and happy coding!

Therefore, it will not be included in the foreseeable future.

The only option right now would be to attempt to create an extension that does exactly this or even hack on the main editor source code. I suggest you start here: https://code.visualstudio.com/docs/extensions/overview

herrbischoff
  • 3,294
  • 1
  • 29
  • 50
  • 1
    This seems like the only road I can go on to get what I want... too bad I have to write an extension from scratch, but maybe at some point I'll get around to it! – Bri Bri Dec 05 '17 at 17:28
  • 2
    Absurd that it's more than a year later and this still hasn't been implemented. – Chockomonkey Sep 14 '18 at 19:46
6

There is an extension available since 2019, called Python Indent. The way you mentioned is called "between bracket pairs" there. This is the example from that extension:

data = {'a': 0,
        | # <- pressing enter should put your cursor at the "|"
| # <- This is where default VS Code puts your cursor.

In PEP 8, it is called "aligned with opening delimiter".

Edward
  • 554
  • 8
  • 15
  • 1
    Simple, straight-forward solution. I've been scouring the internet for an hour for this. Thanks! – HarryS May 20 '21 at 09:00
1

It looks like ESLint will do this for you.

Examples of correct code for this rule with the 2, { "FunctionExpression": {"parameters": "first"} } option:

/*eslint indent: ["error", 2, {"FunctionExpression": {"parameters": "first"}}]*/

var foo = function(bar, baz,
                   qux, boop) {
  qux();
}

From indent rule : Function Expressions.

Install the extension vscode-eslint integration and then

If you haven't installed ESLint either locally or globally do so by running npm install eslint in the workspace folder for a local install or npm install -g eslint for a global install.

Then a rule like

"indent": ["error", 2, { "FunctionExpression": {"body": 1, "parameters": "first"} }],

in your .eslintrc.json file will do what you want - at least on save if you have

"eslint.autoFixOnSave": true

in your settings. false is the default. I don't believe you can make it "fix" the problem as you type - but only on save. You can also modify this setting

// An array of language ids which should be validated by ESLint

"eslint.validate": [ "javascript", "javascriptreact" ],

Mark
  • 143,421
  • 24
  • 428
  • 436
  • This is a step in the right direction, but a) I'm not writing any Javascript right now, so ESLint is of little use to me, and b) it's fairly important to me that it happen as a proper indentation behavior when I create a newline rather than having to save file first. – Bri Bri Nov 20 '17 at 15:44
  • @GuyGizmo What language are you writing in VSCode mainly? I am also using Tslint for writing Typescript and it offers exactly the feature you are looking for. – kentor Dec 04 '17 at 16:08
  • Currently C#, but I also frequently write in C++ and Python. And occasionally any number of other languages too. Basically I need a solution that works in any language. – Bri Bri Dec 04 '17 at 16:12