Skip to content

formatter: allow custom switch-case indentation #18682

Closed

Description

TypeScript Version: 2.5.2

Code

Format the following

switch (foo) {
case 'bar':
  break;
}

Expected behavior:

Keep it the same.

Actual behavior:

case is indented.

switch (foo) {
  case 'bar':
    break;
}

This becomes more problematic when used with tslint or javascript code with eslint, because on different systems we can see one winning over the other randomly i.e. sometimes the case is indented, sometimes it's not (eslint rule says do not indent).

Activity

  1. added
    Domain: Formatter
    Suggestion
    Awaiting More Feedback
    on Sep 22, 2017
  2. added
    VS Code Tracked
    on Oct 27, 2017
  3. vincenzovitale commented on Feb 21, 2018

    @vincenzovitale

    Hi guys,
    any news on this?

  4. kenjisiu-ibmhk commented on Mar 11, 2018

    @kenjisiu-ibmhk

    Andrei Neculau (@andreineculau) It has been almost half a year, is there any news on this? I suggest adding a setting (configurable in settings.json) for this, and making the default the new behavior (same indent level for switch and case, one more indent for statement in a case). Thanks!

  5. andreineculau commented on Mar 11, 2018

    @andreineculau
    Author

    I have no news. Running with a disabled tsformatter. The issue is marked with "awaiting more feedback" but not sure from who.

  6. maxcurzi commented on Apr 3, 2018

    @maxcurzi

    I'm programming in C and I'm quite happy with how the autoformat formats the code (it's reasonably close to our coding standard, and MISRA guidelines). However, it is desired in my case that there are indents for switch case statements.

    What I obtain now after pressing ALT+SHIFT+F:
    image

    What I would like:
    image

    And, ideally, the "case" keywords should be the same color as "switch"'s (although, if I remove the brackets after "case" the "case" word does become the expected color.

    Is that possible?
    VSCode 1.21.1

  7. Sanae6 commented on Aug 4, 2018

    @Sanae6

    I'd want a setting made for this specific issue because of some cases of people wanting indent or not.
    I'm one of the people who wants indenting like
    image
    instead of
    image
    since it is in every way less confusing for me to look at. I use Java in another IDE and Node.js with vscode and it'd be nice if I could see my switch case setup look like my Java ones (case is indented)
    using 1.25.1

    edit: oof i didn't actually format the second picture properly and this time i used the keybind for reformatting
    image

  8. dingcang commented on Jan 9, 2019

    @dingcang

    hi guys, how is the progress

  9. ricardolpd commented on Apr 9, 2019

    @ricardolpd

    I guess this has a really low priority, as its annoying but not a big deal. Does anyone from the vscode have an expected release for this?

  10. xmedeko commented on Sep 20, 2019

    @xmedeko

    This is really a severe issue for me using autoformat in VS Code, so I cannot use autoformat due to this bug. And for me, one of the main tasks of IDE is to do code autoformat. IMO it's a major failure of VS Code. All mature IDEs have configurable indentations.

  11. 12 remaining items

  12. added
    Effort: Moderate
    on Oct 15, 2021
  13. andrewbranch commented on Oct 15, 2021

    @andrewbranch
    Member

    I would be happy to look at a PR adding a formatter setting for this. Implementer be warned; our indenter code (“SmartIndenter” 🙄) is not the easiest to work with.

  14. itzmanish commented on Aug 23, 2022

    @itzmanish

    Almost 5 years later, same issue no fix 🙄.

  15. andrewbranch commented on Aug 23, 2022

    @andrewbranch
    Member

    Are you volunteering? All the other “N years” commenters have been pretty quiet since I marked it “Help Wanted.” I thought for sure somebody would have given this a shot by now. 😄

  16. donaldnevermore commented on Apr 7, 2023

    @donaldnevermore
    Contributor

    I'll have a try.

  17. donaldnevermore commented on Apr 11, 2023

    @donaldnevermore
    Contributor

    Andrew Branch (@andrewbranch) Needs help here. The code worked, but I'm not sure if there are more test cases that can be added. I don't really know what is behind the tests folder. It's not like using a testing framework.

  18. a-pav commented on Aug 24, 2024

    @a-pav

    What is the equivalent setting for javascript?

    I have
    "typescript.format.indentSwitchCase"
    but no
    "javascript.format.indentSwitchCase"

  19. xmedeko commented on Dec 3, 2024

    @xmedeko

    a-pav I have created new issue for JavaScript microsoft/vscode#235085 Please, vote for it if it still affects you.

  20. a-pav commented on Oct 25, 2025

    @a-pav

    Ondrej Medek (@xmedeko) Sorry, (Edit: I removed my original comment since it became irrelevant by my later observation that follows.)

    I noticed that auto-completions do not respect the settings I've made in settings.json, Like with these in place:

        "javascript.format.indentSwitchCase": false,
        "javascript.format.semicolons": "remove",
        "javascript.preferences.quoteStyle": "single",
    

    switch case auto-completion is still indented, log still ends with ; and so on.

    I know that we can add custom snippets and make our necessary changes there, but couldn't vscode apply the user configurations to auto completions as well?

  21. xmedeko commented on Oct 27, 2025

    @xmedeko

    a-pav It's strange if none of these settings are applied. Either you do something wrong (e.g. wrong settings file path), or it seems like a different VS Code bug. Mind that there are workspace settings and user settings and the workspace settings overrides the user settings. Check how these settings appears in File > Preferences > Settings.

  22. a-pav commented on Oct 27, 2025

    @a-pav

    Ondrej Medek (@xmedeko) I'm strictly talking about the auto-completions feature.

    The mentioned settings are applied when I use editor.action.formatDocument command (or when I paste a code snippet from another source.) But, the settings are not applied when I use the auto-completion feature to spawn a full blown switch-case for example.

    The aforementioned settings are placed in my user settings.json and this is what I get auto-completing on switch keyword in a .js file inside a brand new workspace:

    switch (key) {
    	case value: // unexpected indentation
    		
    		break; // unexpected semicolon
    
    	default:
    		break;
    }

    Note that if I cut-paste this exact switch block or use the formatDocument command on the file, both the indentations and the semicolons are removed.

    To add another example, auto-completing on log keyword also gives:

    console.log(); // unexpected semicolon
  23. xmedeko commented on Oct 27, 2025

    @xmedeko

    Ah, I see, I do not know much about VS Code IntelliSense settings. It's possible to create own code snippets and use them in IntelliSense. But you are right the default IntelliSense should formatted according to your settings. Fill seaparate bug for it, I think it should be in VSCode repo itself.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Domain: FormatterEffort: ModerateExperience EnhancementSuggestionVS Code Tracked

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions