Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any way for VS Code to indent to an open bracket?

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.

like image 916
Bri Bri Avatar asked Nov 20 '17 01:11

Bri Bri


People also ask

How do you indent codes in brackets?

brackets-autoindent-extension This extension adds auto-indent function to Adobe Brackets. If you install this extension, you can invoke by "Edit > Auto Indent" menu or "Ctrl+Shift+I" key.


2 Answers

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

like image 121
herrbischoff Avatar answered Nov 14 '22 22:11

herrbischoff


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

like image 36
Edward Avatar answered Nov 14 '22 21:11

Edward