Typescript: auto align colons in object literals, variables, and equals like WebStorm

Created on 26 Oct 2016  ·  15Comments  ·  Source: microsoft/TypeScript

_From @kinergy on October 17, 2016 2:56_

  • VSCode Version: 1.6.1
  • OS Version: macOS 10.12

Using Format Code on a js file:

var abcde = 'abcde',
    def   = {
      a : 'aaa',
      bb: 'b'
    };

turns into:

var abcde = 'abcde',
  def = {
    a: 'aaa',
    bb: 'b'
  };

It would be really nice to have a setting to align the beginning of var names, align the equals, and align the colons in object literals like WebStorm. I tried looking for an extension, but wasn't able to identify one.

_Copied from original issue: Microsoft/vscode#13841_

Awaiting More Feedback Suggestion VS Code Tracked

Most helpful comment

Really need this!

All 15 comments

@kinergy thank you for opening this issue.

@jrieken is this issue related to the formatting stab you are doing?

_From @jrieken on October 25, 2016 7:12_

Nah, the actual formatting smarts must come from an extension - TypeScript/JavaScript in this case. What we do it just calling into those formatters and apply their results

This issue is now awaiting more feedback. This means we'd like to hear from more people who would be helped by this feature, understand their use cases, and possibly contrast with other proposals that might solve the problem in a simpler way (or solve many other problems at once).

If this feature looks like a good fit for you, please use the :+1: reaction on the original post. Comments outlining different scenarios that would be benefited from the feature are also welcomed.

recommend making compatible with http://eslint.org/docs/rules/indent

Good feature. I also find it easier to read code when as much is aligned as possible from one line to the next (colons, equals, etc).

+1

Another +1. We are really struggling to keep a good formatted code in our project, and we need to do it by hand in VSCode, as opposed to WebStorm :'(.

By the way, you can get a temporal fix with this extension:
https://marketplace.visualstudio.com/items?itemName=annsk.alignment

Although the behaviour is different, and it uses the maximum indent "width" for the whole document.

Most of the time, it's needed to format some part of code manually by developers to align the code vertically.

For example: in the following code that is formatted by typescript formatter,

constructor (public http: Http, public chd: ChangeDetectorRef) {
    this.ctx.customer_exp = { sel: { "new0.customer": "id" } };
    this.ctx.perso_exp = { sel: { "new0.perso": "id" } };
    this.ctx.car_exp = { sel: { "new0.car": "id" } };
}

I changed format of some part to improve readability of the formatted code, After changing the code is like this

constructor (public http: Http, public chd: ChangeDetectorRef) {
    this.ctx.customer_exp = {sel: { "new0.customer": "id" }};
    this.ctx.perso_exp    = {sel: { "new0.perso"   : "id" }};
    this.ctx.car_exp      = {sel: { "new0.car"     : "id" }};
}

It would be nice, typescript formatter remember code that manually formatted part and protect them.

According to my opinion if this is possible, no longer needed to align colons automatically.

In connection with this property, following value can be added to settings file.

typescript.format.rememberManuallyFormattedCodePart = true;

Otherwise to find a common solution that will be a solution for everyone is very difficult.

Copied from issue: TypeScript/issues/14601

Really need this!

+1 On this as well. I tested VSCode to see if we could use it as a replacement to Intellij/Webstorm and found this as a pretty key issue. Most of our codebase uses colon/equal alignment as the formatting preference and I gave up on VSCode when I found this.

I would really love to use VSCode in development once this is fixed.

I have to agree with johnsba1. The code formatting is one of the key issues that makes it really hard to switch over from WebStorm. I know VSCode lets people write all these third party plugins that supposedly help with formatting, but I haven't been able to find one that's particularly good.

Would you please name other editors which is doing it on type script files?

Hi, are there any changes in issue? It would be nice, if we can configure document format according to ESLint indend rules.

Would you please name other editors which is doing it on type script files?

WebStorm, PHPStorm, ...

Any yes, no maybe on this one?

Missing a parallel feature set to eslint indent is the only thing keeping my TS code an ugly zit in other wise really clean code.

Was this page helpful?
0 / 5 - 0 ratings