Applying the Code Styling In Visual Studio 2022 - c#

I need to setup a common code styling pattern which team needs to follow and will give warning or error to the developer while writing/compiling the code or pushing the code on the repository.
I have gone through couple of sites explaining about the EditorConfig file for setting up the styling in Visual Studio.
Also Gone through the link Code Style Option to give the style to the code.
The problem is Code Style Option have predefined styling.
I need to setup some custom styling in code like
A space before and after = operator
A space after comma in function parameter.
A proper indentation will be applied automatically to that file after the build. etc
If the answer lies in the Editorconfig for this Please let me know a guide to data setup in that file

Most of what you asked for is, for the most part, already enabled by default, but you can configure it using an .editorconfig as you mentioned. You won't be able to make it as specific as you requested, however by the sounds of it, I would think you'd want this formatting applied for all cases, and not these specific cases you mention. If that isn't the case, then I don't think there any native way to specify the specific formatting you asked for.
Add an .editorconfig file to your project, and open it. Note that you should be able to create .editorconfigs for each project to specify formatting in each, or you can make a global one.
For adding a space before and after an equal sign, the control for this is "Set spacing for operators". This will apply however to all operators. I don't think you can narrow it down to only equal signs:
For spaces after parameters, the control for this is "Insert space after comma". Again, this will be for all commas used throughout your code, not just for parameters:
Lastly, you can control the indentation settings you wish by looking under the Whitespace → Indentation and spacing section:
For formatting on build you have some options. You can download a format on save extension, but that wouldn't format on "a build" technically. However I think because when a build is performed, a save of all files automatically occurs, and this should also format them.
One other way I've found is using the dotnet format command as a Post-Build event.

Related

C# 2022 IDE - not warning about code styles or variable naming?

New install of Visual Studio 2022 - NET 6 project.
In the past, the variables (in the pic below) would have little squiggles underneath them and when I hover the mouse over them a suggestion would pop up stating something similar to (names are not cased or capitalized correctly). It was like the IDE had a code analyzer (maybe an extension/addon) that constantly monitored coding styles.
After installing VS 2022 I cannot figure out how to make these types of suggestions appear. Can anyone help me with this?
Edit - Additional info
I think, maybe it's rule IDE1006 that I am trying to activate in the IDE
Also, in (Tools)(Options) I have these settings. But not a single notification about variable names that violate IDE1006
I can see all of these notifications too (pic below), so, I know with certainty the code is being analyzed (the analysis is just not pointing out (variable name) violations)
There is a setting for enabling and disabling specific style guidance selections.
You will need to navigate to Tools/Options/Text Editor/C#/Code Style/General
In addition to catching flat out syntax errors, Visual Studio does have code analyzers.
This feature still works. Looking at your image, it appears there are those dotted lines under offet_A and offetBecause. It's hard to know exactly why because I don't have enough context of the few lines we're looking at.
I do know that method names starting with lower-case letter generally trigger this warning, but again, we can't see if you have that going on either.
So my answer is this feature is still active. But it only happens for naming that violates accepted naming conventions.
I edited my .editorconfig file (added the lines below), and now the Style Rules, specifically (IDE1006 naming rule violation), etc are enabled/flagged in my source code
dotnet_naming_rule.locals_should_be_camel_case.severity = suggestion
dotnet_naming_rule.locals_should_be_camel_case.symbols = locals_and_parameters
dotnet_naming_rule.locals_should_be_camel_case.style = camel_case_style
dotnet_naming_symbols.locals_and_parameters.applicable_kinds = parameter, local
dotnet_naming_style.camel_case_style.capitalization = camel_case

Bold or italic in C# or VB documentation comments?

Is there a way to use bold or italic inside documentation comments? Something like:
/// <summary>Cleanup method. This is <b>recommended</b> way of cleanup.</summary>
public void CleanAll();
The list of predefined tags does not contain such a feature, but do you know of some way of achieving emphasis/highlighting? Preferably, if it could be shown also in tooltips when hovering over the code.
We have <c> and <code> there, but they already have their semantics.
This feature is now available in Visual Studio 2019 version 16.3.0 (release notes).
You can use the <i> or <em> tags for italic.
You can use the <b>or <strong> tags for bold.
From the release notes, a variety of html tags seem to be supported, but the official documentation doesn't seem to be updated with this new feature just yet.
It looks like this: .
OP's note: This was the accepted answer before 2019 Visual Studio update after which I accepted the other answer. This one is still useful and valid for users without that update.
Not strictly, no. However, Sandcastle (a documentation generator that generates HTML from the documentation) supports to just use HTML in there, so you can use <em> and <strong> just fine if you build it with Sandcastle.
To put it another way: As Jamiec already notes, XML documentation comments are just XML. So you can put any valid XML in there; the compiler will happily write that into the documentation XML file. It all depends on the software that processes that file. Sandcastle just passes anything it doesn't know on as HTML, since that's its output format anyway.
Visual Studio will simply ignore them when displaying the help tooltip:
ReSharper in its Ctrl+Q view will show HTML tags as text which makes things a bit ugly:
Those are usually only of concern to you if you author a library to be used by others, though. But it also means that within the IDE no one can see your emphasis as intended.
I have found actually little need for emphasis when writing API documentation; oftentimes you can write a sentence differently or restructure to have important nodes in a separate paragraph near the end, to not need emphasis at all. Consistent language and phrasing also helps readers to pick up important notes once they're used to it.
Your code probably just was an example, but I think the summary needs emphasis least of all since it only notes – in a short sentence – what a type is or a method does. If anything, use it in the remarks and even then I'd carefully consider whether you actually need it.
There are other ways of adding emphasis:
- Upper case: some BOLD text // you are shouting, but they WILL read it
- First letter: some Bold text // less emphasis
- Asterisks: some **bold** text // 2 asterisks seem to work best
- Dashes: some --bold-- text // less emphasis
Plain text is old-school, but it can be very effective - and works long after the technology has changed.
An alternate way is to use a wiki markup-like style instead.
/// <summary>Cleanup method. This is *recommended* way of cleanup.</summary>
public void CleanAll();
Edit 1:
AFAIK Visual Studio doesn't understand wiki markup. I was just suggesting to use wiki markup as a convention. Your team would still see the raw (unformatted) wiki markup in the method's intellisense.
To add clarification to the accepted answer above ('This feature is now available in Visual Studio 2019 version 16.3.0' https://stackoverflow.com/a/58227889/17203657)
From the Release Notes (https://learn.microsoft.com/en-us/visualstudio/releases/2019/release-notes-v16.3#net-productivity-163P1) 'There is now Quick Info style support for XML comments.'
Quick Info is when you hover over the method (it is NOT Intellisense), it DOES support Bold, Italic.
IntelliSense is what shows when you're adding arguments to the method, it does NOT support Bold, Italic.
As of VS 16.11.5 I've not found a way to add bolding or italics to the IntelliSense view.
Note: I do not have enough points to add this as a Comment.
I was trying to add a line break into the Intellisense display when I came across <see cref="YourReferenceHere"/> (see here), which inserts (according to the documentation) a clickable reference into the documentation. I have not figured out how to click on Intellisense tooltips, but it does provide a type-formatted/colored display which helps your reference to stand out (note that the reference must be available to Intellisense).
Side Note: I never did figure out how to do a single line break. Closest I can get is a double line break, using the <para/> tag...

How to change formatting options based on solution

My boss set our IDEs up to use non-standard formatting. For example, it adds spaces between the parenthesis and parameters. This is fine for the new stuff that we've started from scratch, but it's super annoying when I go into existing projects and try to format the code with Ctrl-k-d because when I go to compare the file with the latest version before checking in my changes it looks like I've changed almost every single line in the file and it's difficult to pick out the actual changes.
I would like to be able to use his settings in the newer development, but switch to the default settings when working with other projects. Is there a way to accomplish this without having to go into the settings and uncheck/recheck all the appropriate options every time?
EDIT: I understand it might be good to reformat all legacy code to use the new standards. Unfortunately, that's not my decision to make. And even so, I sometimes work from my personal computer which I also use on occasion to do contract work for other companies. I obviously can't reformat their code to fit my bosses standards.
You can easily switch between settings, by creating separate .settings files and using a VSIX package to switch between them by loading them into Visual Studio - I use this for switching between Dual and Single monitor settings via menu items in the Tools menu.
Using an extension is annoyingly complex for such a simple operation, and this was a whole lot easier when Visual Studio supported macros. However I've uploaded my homebrew extension to GitHub in case you want to have a look.
The tricky bit is editing the settings files to only contain the ones you're interested in, which I expect are the ones in Text Editor\C#\Formatting\Spacing.
The Rebracer extension stores code formatting options alongside each solution and when you open a solution, Rebracer will automatically apply that solution's settings.

How do I automatically generate documentation/comments in Monodevelop?

Monodevelop automatically generates verbose documentation for functions and classes if "///" is typed in the appropriate place, upon typing the third '/'.
I want it to go over all of my code, though - I thought there was some button somewhere, and I looked around in the drop-down menus, tried right-clicking file names, etc.
I could not find such an option - does it not exist? I could not find a plugin that does that either.
Try to use Edit-> Document buffer.
It will make /// comments for all your public methods/classes without any comment upon it in current file.
It won't work if you have // comment upon method/class or for protected/private elements.
It is not possible. Your options are:
Manually typing three slashes before each and every function
Using an external tool
Online searches have not come up with any plugins for monodevelop that would do that, and the option does not exist in the vanilla IDE.

How to avoid all code nodes to expand on project opening?

I have a C# project and some code pages have few thousand lines of code. I really like the idea of nodes in the code editor. I use it a lot and create many regions. But every time I open the project, all the nodes are expanded and I have to minimize them manually. It gets really annoying.
I have not found any help about this on internet nor in the options of VS. There must be a setting somewhere.
In the Options dialog onder Text Editor --> C# --> Advanced, there is an option "Enter outlining mode when files open". This should be checked.
In the IDE, from the Tools menu click Options. Alter default Outlining using
TextEditor->C#->Formatting->Advanced
There is a checkbox here you can use to set defaults for Outlining. This is VC# 10 Express but similar in other versions.
If you try the suggested change, you may be disappointed. According to this C# PM, who's responding to a similar complaint:
It is a bit confusing, but the
behavior you're seeing is intended.
The feedback that we received with VS
2003 was that we should persist the
outlining state of source files after
they have been closed and then
reopened. The option in Tools |
Option now effectively means what the
default behavior should be for a file
that you have never opened before. It
has no effect on files that you have
opened previously, since those files
already have a persisted outlining
state.
I don't want be a smart ass here, but often if you have that huge code in one file, you have more than one logical unit and might be able to (ans should) split it. (Single Responsibility Principle).
For my share, I don't like the regions because they are hiding code and I prefer to see all of it.

Categories