After I type if (view=="") { and press Enter, VS formats and indents the curly braces to this:
if (view == "")
{
}
How can I change the settings in Visual Studio 2019 to just have it like this
if (view == "")
{
}
I've been looking and trying different combinations and can't seem to find the setting.
If you want to get the format you need, you can refer to the parameters of this option:
Tools>Options>Text Editor>C#>Code Style>Formatting>Indentation, uncheck the option ”Indent open and close braces”.
Related
I have enabled the latest C# extension in my Visual Studio Code editor. Instead of formatting the code while saving or by applying the key combination Ctrl + K, Ctrl + F or Alt + Shift + F, I need to format the current line of code while hitting the Enter key. This feature is already available in Visual Studio, but not found in Visual Studio Code by default.
This is the sample code output I need to achieve:
I have found an option which makes it easier to format code while typing.
I applied the below settings in workspace settings:
{
"editor.formatOnSave": true,
"editor.formatOnType": true
}
This works fine for me.
Go to menu File → Preference → Settings.
Search for format
Select the options you would like:
Format on Paste
Format on Save
Format on Type
Close the Settings window.
You can also see it in your settings.json file:
Go to menu File → Preferences → Keyboard Shortcut (Ctrl + K, Ctrl + S)
Click on the keybindings.json link:
Enter the below binding for the Enter key. This binding will overwrite the defaults for current user.
{
"key": "enter",
"command": "editor.action.formatDocument",
"when": "editorHasSelection"
}
Another alternative solution is to use macros extension - a custom macros support for Visual Studio Code, so you will be able to do more than one command in one key binding.
Add macros to User Settings:
"macros": {
"formatWithEnter": [
"editor.action.insertLineAfter",
"editor.action.formatDocument"
]
}
And the below key binding to keybindings.json:
{
"key": "enter",
"command": "macros.formatWithEnter"
}
Code formatters available on Visual Studio default as
On Windows: Shift + Alt + F
On Mac: Shift + Option + F
If you again wish to do it when pressing Enter you need to set up your workspace preferences and then configure the key bindings:
{ "key": "enter", "command": "editor.action.format" }
It now formats the whole document if nothing is selected, and else it formats the selection.
Also there is the beautify.onSave, editor.formatOnSave option. Please try that too to make the code pretty.
Edit: This doesn't actually work because this will suppress the regular Enter key behavior
To get this to work for me I had to install two extensions
C# (powered by OmniSharp)
C# FixFormat
Without the second extension I was getting the error visual studio code there is no formatter for 'csharp'-files installed
I also made sure my Visual Studio Code was up to date (menu* → Help → Restart and Update or Check for Updates)
Then I added a custom key binding. menu File → Preferences → Keyboard Shortcuts → "For advanced customizations open and edit keybindings.json":
[{
"key": "enter",
"command": "editor.action.formatDocument",
"when": "editorTextFocus"
}
]
I had to type in the word enter because the dialog to capture keys doesn't acknowledge it.
In Visual Studio Code (MacOS) I've already spend hours finding how to put open brace to the new line when you type. I type this:
class Foo{
...and press Enter. I get the closing brace automatically:
class Foo{
}
But I want this (like VS 2017 did):
class Foo
{
}
I can do that with format command (Shift + Alt + F), but can I have it automatically?
I've tried different extensions (my lines from in User Settings).
ryannaddy.vscode-format: "format.newLine": { "brace": true }
Leopotam.csharpfixformat: "csharpfixformat.style.braces.onSameLine": false
"editor.formatOnType": true
Omnisharp: { "FormattingOptions": { "NewLinesForBracesIn...": true, } }
All without any success, none of these setting did this formatting on typing.
There should be an option in the VS Text Editor settings under
Tools -> Options -> Text Editor -> C# -> Formatting
Try to check them and see if that works.
My visual studio 2017 auto formats following c# code:
while (source[--start] != '\"');
into:
while (source[--start] != '\"')
;
I haven't found any related settings which would be related to this rule.
Why does it add new line and tab before semicolon in this case?
Thank you.
I am trying to write C# code in MonoDevelop for Ubuntu Linux, but the editor will not format the braces. It used to format them nicely, like so:
void Method()
{
if (condition)
{
//...
}
}
Then one time as I reopened MonoDevelop, the editor started leaving the left brace hanging:
void Method() {
//cursor left here after pressing Enter, no right brace
After I went to Edit > Preferences > Text Editor > Behavior > Checked "Insert matching brace", the editor started appending a } to the end of the block. However, the problem is that now my code is formatted Eclipse/Java-style, which I definitely don't want. See below:
void Method() {
if (condition) {
//...
}
}
For some reason, going to Edit > Preferences > Source Code > Code Formatting > C# > Setting policy to "Microsoft Visual Studio" doesn't help with this. "Enable on the fly code formatting" is checked off, too. Edit > Format > Document works perfectly though, so I don't know what's wrong.
Edit > Preferences > Source Code > Code Formatting are IDE settings which are used when creating new solutions/projects. You should go into your solution settings(double click on solution in SolutionPad) and change settings under "Source Code->Code Formatting->C# source code".
MonoDevelop supports per project(or solution if project has "inherit" policy set).
All Visual Studios (2012 too) do not format the following:
_messageProcessor = new Dictionary<ServerDataTypes, MessageProcessor>()
{
{ServerDataTypes.FrameData, ProcessFrameData } ,
{ ServerDataTypes.ServerStatusResult,ProcessServerStatusResult },
{ ServerDataTypes.PlayerMessage, ProcessPlayerMessage},
....
};
How can I make my Visual Studio 2010 (or 2012) to auto-format that? I need the following result:
_messageProcessor = new Dictionary<ServerDataTypes, MessageProcessor>()
{
{ ServerDataTypes.FrameData, ProcessFrameData },
{ ServerDataTypes.ServerStatusResult, ProcessServerStatusResult },
{ ServerDataTypes.PlayerMessage, ProcessPlayerMessage },
...
};
It's like in the auto-properties for the newly created objects. The format is working for that. But not for this. So, how to fix it?
Short answer: you can't with VS out of the box. Resharper comes close, but it's reformatting doesn't quite do this style either. I've actually submitted a request for it do do this.
You might look for some other extension or perhaps develop a macro of some sort.