35

VSCode seems doesn't auto indent HTML elements in jsx?

enter image description here

Is there any way to fix it.


Update:

In Atom:

When I input <div>, atom will show: enter image description here

After I press the return key, the result is(pay attention to the location of the cursor): enter image description here

While in VSCode:

enter image description here

enter image description here

zhuscat
  • 604
  • 1
  • 6
  • 12

4 Answers4

57

Try changing the language mode to JavaScript React.

  1. Open the commands palette.
  2. Type change language mode
  3. Press Enter
  4. Type javascript react
  5. Press Enter

Once that's done, you'll see the JavaScript React mode in the bottom left corner.

Confirmation of JavaScript React Mode

Once you're in that mode, try again to format the document.

Shaun Luttin
  • 133,272
  • 81
  • 405
  • 467
  • @zhuscat What command are you issuing to format the code? It Works on My Machine (TM). – Shaun Luttin Nov 10 '16 at 16:22
  • @zhuscat Aha. I thought you were talking about the "Format Document" command. Your question update clarifies what you're needing. – Shaun Luttin Nov 12 '16 at 20:47
  • 7
    I just want that when I press the `return` key, there is one more indent compared to the `
    ` in the example of my question.
    – zhuscat Nov 13 '16 at 07:01
  • 2
    And I'm wondering if there is any way to automatically add HTML close tag in `jsx` – zhuscat Nov 13 '16 at 10:35
  • It sounds like what you're looking for are two things @zhuscat. One of them is auto-indent. The other is tag completion. For the latter, have you looked into emmet? – Shaun Luttin Nov 13 '16 at 19:53
  • 1
    I installed a plugin called `auto close tag` to solve the "tag completion" problem. However, the `press return key` problem(when I press the return key, I hope there is one more indent compared to the
    in the example of my question) is still there.
    – zhuscat Nov 14 '16 at 16:19
  • For closing the tag, you can type for example `div` without brackets, and press tab. I'm having the same issues with auto-indenting though. Actually, it goes to the next line tabbed in, but my problem is that the closing tag doesn't tab back out. – Wylliam Judd Sep 11 '17 at 18:02
  • This doesn't work when the closing bracket is on a new line: https://giphy.com/gifs/3ohc0Y4D2kzfD2IVNu/html5 – Vlady Veselinov Jan 02 '18 at 18:39
54

Change the language to Javascript React (see other answers for instructions) then use the following command:

alt + shift + f
kendotwill
  • 1,892
  • 18
  • 17
2

use the extension "Prettier - Code formatter", by Esben Petersen. it will auto format on save, assuming your file is a jsx file.

Mo7
  • 109
  • 9
1

1. Add user settings to

"files.associations": {
    "*.js": "javascriptreact"
},

2. Install plugin

Auto Install Tag

And Reload. It will fix your issue.

BTW, I think there's a bug now. Without component props auto indent work well, but with props, it won't work now.

<Component>Enter

===>

<Component>
    :
</Component> 

But

<Component someProps={10}}Enter

===>

<Component someProps={10}>
:</Component>

Anyone who can fix this please help me :) I am using on mac

ton1
  • 7,238
  • 18
  • 71
  • 126