Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closing Brace auto-indent doesn't work properly #40235

Closed
akshita31 opened this issue Dec 14, 2017 · 25 comments
Closed

Closing Brace auto-indent doesn't work properly #40235

akshita31 opened this issue Dec 14, 2017 · 25 comments
Assignees
Labels
bug Issue identified by VS Code Team member as probable bug editor-autoindent Editor auto indentation issues javascript JavaScript support issues *out-of-scope Posted issue is not in scope of VS Code typescript Typescript support issues

Comments

@akshita31
Copy link
Contributor

  • VSCode Version: Code 1.18.1 (929bacb, 2017-11-16T18:32:36.023Z)
  • OS Version: Windows_NT x64 10.0.16299
  • Extensions: Extensions are disabled

Steps to Reproduce:

  1. Add spaces between opening and closing brace of a method
  2. Move the caret to a position in between the spaces and hit enter

Expected
The closing brace is indented properly

Actual Behavior
The spaces preceding the closing brace are retained

ezgif-4-4fb6926784

Reproduces without extensions: Yes

@akshita31
Copy link
Contributor Author

Any updates on the issue ?

@colombod
Copy link
Contributor

colombod commented Mar 6, 2018

Is there any action or update on this item?

@colombod
Copy link
Contributor

colombod commented Mar 6, 2018

@rebornix do you have any ETA? we have a PR 1906 that is blocked on this item. How can I assist?

@vcipriani
Copy link

Can this be prioritized? Its preventing omnisharp from implementing standard C# bracket formatting. See dotnet/vscode-csharp#1906 and dotnet/vscode-csharp#1591). This makes using VS Code for C# a non-starter in my org.

@pengshao
Copy link

Yes, this is particularly frustrating that being a flagship language made my Microsoft and a flagship editor made by the same company, they cannot make such a simple thing work out of box, for years.

@sr229
Copy link

sr229 commented Jun 29, 2020

Keep in mind this is not the same as Visual Studio (they're probably not the same team as well), so this kind of feature may take a while as it may break existing functionality @pengshao82

@sr229
Copy link

sr229 commented Aug 9, 2020

Do we have any updates regarding this? The issue that this has linked has been open since 2017.

@pengshao
Copy link

pengshao commented Sep 2, 2020

@sr229 I know, that is why I was referring vscode as flagship "editor" instead of IDE. VS on the other hand handles this just fine as I use it in my daily work. But for performance reason we are thinking of encouraging people to switch to VS code and this bug was pretty much a joy killer that took us by big surprise...

@swaidi
Copy link

swaidi commented Sep 23, 2020

the problem i'm facing now
even if i correct it manually, when i press save it goes to the wrong position
any help will be appreciated

@mjbvz
Copy link
Collaborator

mjbvz commented Oct 3, 2020

Closing this since after 3 years it has one upvote (plus a handful of comments that are essentially upvotes)

While this is a bug, it does not break the code and is fixed by running format (protip: try enabling format on save!)

@mjbvz mjbvz closed this as completed Oct 3, 2020
@mjbvz mjbvz added the *out-of-scope Posted issue is not in scope of VS Code label Oct 3, 2020
@nicholasoxford
Copy link

I think saying this has one upvote is short sighted. There are multiple issues linked to this and someone complained 9 days before you made your message. I am still having this problem as well

@dawesi
Copy link

dawesi commented Jan 11, 2021

there are several other editors that have preferences panels for this, it's about time vscode was less prescriptive considering how many developer communities it supports. The original team dismissed this and said there was only two ways to do code bracing and indenting 'ever', and I've argued this config panel since day 1 of vscode. It would solve a lot of developer issues just to give control to the DEVELOPER on how they format code so that plugins for languages could also default to what THEY use, not what core team think in theory what you should use.

The limited support for bracing, indentation and brackets has made this editor frustrating (and laughable) for years. Why not just look at other editors and see what they have done, then do similar... tried and TESTED solutions. #sigh instead of this elitist 'we know better' about how you should code 'theory'. Pretty much EVERY other editor has implmented this, why? because it makes sense.

#intentingispersonal

https://mondrian.pentaho.com/images/code_indentation.png
https://www.nikialeksey.com/assets/imgs/code-wrapping-settings.jpg

@MJakeShapiro
Copy link

I truly hope y'all reconsider this issue. Yes it might not break anything but the simple things that make life easier are what make people stay.

@ironcutter24
Copy link

Still having this issue. Most annoying thing in VS Code for me.

@gabrieldechichi
Copy link

gabrieldechichi commented May 23, 2021

Would also like to voice my frustration on this. It prevents me from using VSCode with C# for years now. And I don't think there are only a handful of users who are frustated by this, is just that the git issues are hard to find on google (i.e searching for "vscode C# place brace on new line" on Google does not point to any of the relevant issues)

@gabrieldechichi
Copy link

gabrieldechichi commented May 23, 2021

For anyone seeing this in the future. I've found a workaround to automatically place braces on a new line by using keybindgs and snippets. Adding the following to your keybinding.json config should do the trick:

{
    "key": "Shift+[",
    "command": "editor.action.insertSnippet",
    "when": "(editorLangId == csharp || editorLangId == cpp || editorLangId == c) && editorTextFocus",
    "args": {
        "snippet": "\n{\t$0\n}"
    }
},

This is not a perfect solution, as it jumps the line when you type { instead of waiting for you to hit enter, but it's been working quite nicely for me since the combo { + enter does exactly what you would expect in other IDEs like VS (you'll have an open brace in new line, and your cursor in the middle of both braces)

@ironcutter24
Copy link

I got @gabrieldechichi's solution to work when enter is pressed.
First download this extension to add macros.

Add to settings.json:

// See keybindings.json for use information
"macros": {
    "newLineAndFormat": [
        {"command": "type", "args": {"text": "\n"}},
        "editor.action.formatDocument",
    ],
}

Add to keybindings.json:

// C# only
{
    // Format on enter pressed
    "key": "enter",
    "command": "macros.newLineAndFormat",
    "when": "editorLangId == csharp && editorTextFocus"
}

This keybind is active while editing C# documents only.
The language can be customized by replacing the language id with one from this list.

This solves pretty much all of my formatting issues.

@Olyhrias
Copy link

It's something that should be as simple as a line in settings.json. I don't understand why they keep ignoring it.

@gabrieldechichi
Copy link

// C# only
{
// Format on enter pressed
"key": "enter",
"command": "macros.newLineAndFormat",
"when": "editorLangId == csharp && editorTextFocus"
}

@taglia24 Does your format action takes the cursor to the beginning of the line, instead of respecting current indentation?

image

@ironcutter24
Copy link

ironcutter24 commented Jun 14, 2021

It seemed to work fine after disabling some extensions, but it's back @gabrieldechichi.
For me it happens everytime the code is formatted.

I was trying this (really cheap) trick using comments as placeholders:

"macros": {
      "newLineAndFormat": [
          {"command": "type", "args": {"text": "\n"}},
          {"command": "editor.action.commentLine"},
          "editor.action.formatDocument",
          {"command": "editor.action.removeCommentLine"}
      ],
  },

Anyway formatDocument seems to work asynchronously, so some some sort of onCompletion event is needed to time the placeholder removal.

If anyone is willing to mess around a bit I leave a shortcut to the full list of commands.

I miss Visual Studio a bit more for every minute I spend on this.

@sarkh200
Copy link

sarkh200 commented Aug 2, 2021

A workaround is to add this to settings.json while having the macros extension added
"macros": { "curl": [ {"command": "type","args": {"text": "\n{\n\n}"}}, "cursorUp", "tab", ], },
and then adding a keybinding to keybindings.json
// C# only { "key": "shift+enter", "command": "macros.curl", "when": "editorLangId == csharp && editorTextFocus" }

@D-B-Daugh
Copy link

D-B-Daugh commented Sep 24, 2021

Since somehow none of the above worked for me, I went ahead and made a (probably) needlessly long macro to do what I wanted.

For this as above you will need the macro extension.

Put this in settings.json:

"macros": { "curlForm": [ "cursorLeft", {"command": "type","args": {"text": "\n"}}, "cursorRight", 
        {"command": "type","args": {"text": "\n"}}, 
        {"command": "type","args": {"text": "\n"}}, 
        "cursorUp", "tab" ], },

Put this is keybindings.json:

// C#
    { "key": "shift+enter", "command": "macros.curlForm", "when": "editorLangId == csharp && editorTextFocus" }

You will have to use Shift+Enter when wanting to do this. Make a curly, once inside the automatic 2 hold shift and press enter.

@HoveringGoat
Copy link

Ironcutter that worked for me! I spent wayyyy too long trying to solve this. But i'll be damned if i have confusing bracket placement.

@ironcutter24
Copy link

ironcutter24 commented Nov 26, 2021

I wrote a really minimal vscode extension (C# Curly Formatter) to solve curly brackets indentation problems.
I'm also going to make it keep tab spacing in new lines, like in regular Visual Studio, as soon as I can.
Hope this helps, let me know if you encounter any issues with it.

@HunterLintz
Copy link

I wrote a really minimal vscode extension (C# Curly Formatter) to solve curly brackets indentation problems. I'm also going to make it keep tab spacing in new lines, like in regular Visual Studio, as soon as I can. Hope this helps, let me know if you encounter any issues with it.

You ABSOLUTE Legend

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Issue identified by VS Code Team member as probable bug editor-autoindent Editor auto indentation issues javascript JavaScript support issues *out-of-scope Posted issue is not in scope of VS Code typescript Typescript support issues
Projects
None yet
Development

No branches or pull requests