How to make Visual Studio format dictionary initialization? - c#

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.

Related

Fixing identation after and if statement Visual Studio 2019 programming in C#

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”.

Visual Studio IDE0059 C# Unnecessary assignment of a value bug?

I have the following C# Code (I reduced it to the bare minimum to simplify it). Visual Studio 2019, .NET Framework 4.7.2.
public void Demo()
{
ReportStart();
var success = false;
try
{
int no = 1;
switch (no)
{
case 1:
default:
break;
}
DoSomething();
success = true;
}
finally
{
ReportEnd(success);
}
}
From my understanding, there is nothing wrong about it. The function may fail (I don't want to catch it) but before leaving, it will report successful execution to another method. When debugging, it does exactly what it should.
Interestingly, Visual Studio 2019 will report the following:
When I follow the suggestion by choosing "Remove redundant assignment", it will remove the line success = true;, effectively changing the outcome!
Now what is the switch/case for, you'd ask? When removing it, the recommendation disappears:
Is there any reason for that, or is it a bug in Visual Studio?
It seems to be a known issue with Roslyn and Visual Studio 2019 16.4, please refer to the GitHub issues #39755 and #39344.
The milestone is set to the version 16.5 Preview 2, so it was already fixed and you can try the preview 2 of 16.5 version or wait for stable one (personally, I'm not using a Preview versions)

How can I make a VSCode theme recognize C# interfaces?

I am trying to get a theme for Visual Studio Code working to what I want. Currently, I'm trying to use Obsidian working with C# rules, but I'm not sure which key word to use to override color customizations. VSCode does not seem to recognize interfaces as they're language specific.
"editor.tokenColorCustomizations": {
"functions" :{
"foreground": "#F1F2F3"
},
"interface": { //not valid
"foreground": "#B48C8C"
}
}
How can I get VSCode color customizations to recognize c# specific syntaxes?
editor.tokenColorCustomizations can use a number of values: comments, functions, keywords, numbers, strings, types and variables. If none of those work for you textMateRules is available as well. So you can do something like:
"editor.tokenColorCustomizations": {
"textMateRules": [{
"scope": "yourScopeHere",
"settings": {
"fontStyle": "italic",
"foreground": "#C69650"
}
}]
},
So you just have to figure out what scope you need for "interface".
For that, try CTRL-Shift-P and type scope: choose
Developer: Inspect Editor Tokens and Scopes
and for whichever keyword is selected, like interface you will get a listing of its textmate scope. That should be inserted as the scope value above. [In my experience, it is more accurate to open the "Inspect TM Scopes" panel and then click a couple of items and then the one, like interface, that you want - the scope panel will remain open.] You can copy from the scopes panel.
You may need only the main scope listed, but if need to narrow its scope you can include the others listed in a comma-separated list in the scopes: ..., ..., ...
Based on Davi's answer:
Edit "C:\Program Files\Microsoft VS Code\resources\app\extensions\csharp\syntaxes\csharp.tmLanguage.json":
Find:
{"name":"storage.type.cs","match":"#?[[:alpha:]][[:alnum:]]*"}
Replace with:
{"name":"storage.type.interface.cs","match":"#?[I][[:upper:]][[:alpha:]][[:alnum:]]"},{"name":"storage.type.cs","match":"#?[_[:alpha:]][_[:alnum:]]"}
Add to settings.json:
"editor.tokenColorCustomizations": {
"[Default Dark+]": { // remove scope to apply to all themes
"textMateRules": [
{
"scope": "entity.name.type.interface.cs",
"settings": {
"foreground": "#b8d7a3"
}
},
{
"scope": "storage.type.interface.cs",
"settings": {
"foreground": "#b8d7a3"
}
}
]
}
},
VSCode 1.63.2:
Ctrl + Shift + P > Open Settings (JSON)
Paste this:
"editor.tokenColorCustomizations": {
"[Default Dark+]": {
"textMateRules": [
{
"scope": "entity.name.type.interface",
"settings": {
"foreground": "#a4ddaf"
}
}
]
}
},
I believe it can be partially done by editing "Program Files\Microsoft VS Code\resources\app\extensions\csharp\syntaxes" by adding support for "storage.type.interface.cs" with a regular expression that matches the convention.
Something like [I]([A-Z][a-z][A-Za-z]*)
You could also exclude possible mismatches like
IISManager, IPhoneDevice
https://code.visualstudio.com/api/language-extensions/syntax-highlight-guide
https://www.apeth.com/nonblog/stories/textmatebundle.html
good luck with that and please let me know if you got it done

Put class open brace on new line as you type in Visual Studio Code [C#]

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.

How do I stop Auto Outline Expansion in Visual Studio 2010?

How to do I stop Visual Studio 2010 from expanding outlines/regions automatically?
Right now whenever I create certain syntax errors (usually while editing, sometimes just from starting to type new code), it will auto expand every outline/region below that line.
I've been through the formating settings, but I can't find anything. Maybe I don't know the proper terminology that applies to the setting.
Example code that causes this behavior(one of many):
public string myMethod1(string myStr)
{
try //<-SELECT AND DELETE ME
{ //<-AND ME TOO, AT THE SAME TIME
return myStr + "success";
}
catch
{
return myStr + "failed";
}
}
//ALL OF THESE BELOW WERE COLLAPSED,
//BUT WILL EXPAND WHEN "TRY {" IS DELETED
#region HIDING
public string myMethod2(string myStr) { /*...*/ }
public string myMethod3(string myStr) { /*...*/ }
public string myMethod4(string myStr) { /*...*/ }
public string myMethod5(string myStr) { /*...*/ }
#endregion HIDING
Thanks for any help!
EDIT:
I can force to happen by removing any starting bracket {, anywhere in my code.
This is not normal behavior. It is possible that an add-on you have installed is incorrectly doing this. You can reset your environment to the default settings by going to
Tools -> Import and Export Settings ...
choose Reset all settings
click next
save your current settings if you want to
then choose the default setting (I prefer General Development Settings because I work on lots of different things)
Click finish
Saving your current settings allows you to keep your old stuff in case you do not like what defaults you have chosen. So there is really no chance for loss of your environment setup when you do this.

Categories

Resources