Visual Studio New Line Formatting - c#

I'm new to visual studio, so perhaps this is something easy... but I've had no luck finding how to solve this formatting issue.
I would like for VS to auto format this
int x =
5;
like this
int x = 5;
When I type this in and select 'Format Document' nothing changes.
It will also not remove unnecessary newlines such as
MyClass someClass =
new MyClass();
Any help for this would be great!

Visual studio can't fix that. You can do it with the Replace All dialog + regex if you are careful.

jgauffin is right. However you can format other items to speed up the process.
Tip for key combinations for general reformatting
Tip for using a macro to modify all files in your solution

Related

Is there a Possibility to Configure Visual Studio Formatting to ignore whitespaces around variable assignments

I have variables Assigned like this, so all assignment operators are in line.
var filterSets = ...;
var names = ...;
var filter = ...;
when Visual Studio formatted it. (for Example when reapplying the last })
var filterSets = ...;
var names = ...;
var filter = ...;
Whats also applying a change for those 3 lines;
I searched a while and didn't find a way to configure the Formatting to ignore those whitespaces.
I wouldn't really care if it wouldn't show as changes in version Control...
You don't mention which VS version you are using, however, in VS 2017 you can find this in Tools/Options, under Text Editor/C#/Code Style/Formatting/Spacing.
The option is called "Ignore spaces in declaration statements"

How do I search for comments in Visual Studio? (VS 2015)

Is it possible to include comments in a search? Or maybe even exclude code and search for comments only?
Like for example
int a = 1;
//int b = 2;
If I search for int I will only find the int which is not commented. I want to find the commented one.
edit
another thing I just noticed. When I search for things in xaml I cannot find them either. example:
<TextBlock x:Name="veryImportant"/>
cannot be found by searching for for example TextBlock or Name or veryImportant
Could it be possible to find it somehow?
Visual Studio does allow for string searching in comments, although, in your example, it is possible that you have it set to "Match whole word" -- can you confirm that this is not the case?
Using CTRL + f , you can do a simple text find which will include comments.

How can I type "{0}" more quickly in Visual Studio?

In C#, it's common to have to type {0}, {1}, etc. when formatting strings with string.Format() or Console.WriteLine(). Considering its frequency, it's an awkward set of key strokes. But despite my searches, I couldn't find any sort of shorthand or hotkey to automatically insert it in Visual Studio. Has anyone figured out something to expedite the process?
One of the benefits from string interpolation in C#6.0, which I find helps.
Instead of:
string s = String.Format("{0} and {1}", variable1, variable2);
You can do:
string s = $"{variable1} and {variable2}";
See a guide: http://www.informit.com/articles/article.aspx?p=2422807
You can create the following command (C#) with my Visual Commander extension to insert text and then assign a keyboard shortcut to it:
EnvDTE.TextSelection ts = DTE.ActiveDocument.Selection as EnvDTE.TextSelection;
ts.Text = "{0}";

Visual Studio change C# method parameter colouring

In Visual Studio 2013, is there a way to change the syntax colouring of C# method parameters?
e.g. Can I have AAA and BBB colored, but not someInt, Foo, ToString
private int MyMethod(int AAA, int BBB)
{
int someInt = new int();
someInt = AAA + BBB;
string Foo = AAA.ToString();
}
I tried going to Tools -> Options -> Environment -> Fonts and Colours -> Text Editor and changing Identifier, but this changed the coloring of just about everything (variables, methods, parameters).
ReSharper can do this.
First, check this in ReSharper options:
Then choose the color in the VS options:
End result:
I recently found this extention while looking for the same thing for TypeScript, apparently it supports C# and VisualBasic, so it may be helpful for you and anybody else looking:
Visual studio SemanticColorizer
For VS 2017 you can use.
Enhanced Syntax Highlighting by Stanislav Kuzmich
https://marketplace.visualstudio.com/items?itemName=StanislavKuzmichArtStea1th.EnhancedSyntaxHighlighting
Now you can do this in Visual Studio without any extension.
Here is VS 2022
Unfortunately, you will not find a way to colorize parameter variables with the C# language. You do have the option, however, of writing an extension to do just that. Or, you could rewrite everything in C++, where you can get your parameters colorized.

How to find/replace several strings in VS 2012 Solution

I need to replace about 2000 strings with another string, i tried writting a VS Add-In to iterate through a list of strings and to replace one by one in all files, somthing like this:
foreah(item in strings)
{
findWin.FindWhat = item;
findWin.ReplaceWith = "string2";
findWin.Action = vsFindAction.vsFindActionReplaceAll;
findWin.Target = vsFindTarget.vsFindTargetFiles;
findWin.KeepModifiedDocumentsOpen = true;
findWin.WaitForFindToComplete = true;
findWin.Execute();
}
However, it only works if i set the find options to OpenDocuments, otherwise it throws an AccessViolation exception. There are about 8 projects in the solution and a lot files in there.
Is there any clean/better way to achieve that?
Thank you
You can try the DTE.Find.FindReplace method which is better suited when you don't need to interact with the Find/Replace dialog.

Categories