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

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

Related

Applying the Code Styling In Visual Studio 2022

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.

Can VS Intellisense be configured to run "to-cursor"?

I use both Delphi and Visual Studio (C#), and I've noticed a difference in how code-completion works which I find really annoying. In Delphi, if you edit existing code so that you change the variable you're using, code completion will give hints based on the code before your cursor. For instance, inserting an "I" into myString to become myIString would give a code hint to show any available variables starting with "myI". Selecting one will then overwrite myString with the new one.
In Visual Studio doing the same thing brings up a code-completion menu with all available variables, and none selected so you see the top of the list. You have to completely delete the variable you're using and start again before you get meaningful code completion.
I seem to make changes like this quite often (maybe I should just get it right in the first place!) so it gets a bit annoying having to remove code, and can be trickier on longer lines with lots of parts to them.
Here's some screenshots which (hopefully) illustrate the difference. I was struggling how best to word this!
Code completion in Delphi
Code completion in Visual Studio
I've tried playing with the Intellisense settings in Visual Studio and there's nothing there which seems to change the behaviour. Is there any way of replicating it in C#?
I've tried playing with the Intellisense settings in Visual Studio and
there's nothing there which seems to change the behaviour. Is there
any way of replicating it in C#?
For now, VS doesn't support for this behavior. The auto completion won't work when we insert a character into existing variable. It will take effect when we type a character in a empty place.
Type character in a new place, the intellisense works.
Insert character,the intellisense not works.
In my opinion your suggestions is really meaningful. I think it could be better if we have a new option to support for auto-completion when insert character, so I suggest you can Go=>Help=>Seed Feedback=>Provide a suggestion to post your idea there.
We who interested in it will vote for you if you share the link here.
As alternative ways:
You can go Tools=>Options=>Text Editor=>C#=>Intellisense=>Enable Show Completion list after a character is deleted option.
Then you can insert 'i', the statement could be myiSring1.GetType(); And delete any or all characters in the String1,it would show the intellisense like below:(e.g:I delete 'r'.) Apparently, this is what you really want, but as I mentioned above, VS itself doesn't support this behavior for now, hope you can use 'delete option' saves some time. Sorry for the inconvenience.
Also, you can check if ReSharper can meet your needs,since it's a third-party tool which seems to charge after xx-days trial, I have no further details for it.
Hope all above helps :)

VS2010 Extensibility: Custom document formatting

Good afternoon,
I've created a visual studio package that registers the Verilog language as a valid content type.
I've got syntax highlighting, outlining, smart indenting, etc all working.
However, I would like to be able to get Visual Studio to automatically format the entire document via Edit->Advanced->Format Document/Selection. Currently these options are invisible, and I expect that I have to let VS2010 know (somehow) that these methods can be called, and provide the correct methods to do this formatting.
I can't seem to find any reference to formatting in the VS2010 SDK and documentation. I was hoping that ISmartIndent would be the solution I was looking for, but it seems that this code only runs on an empty line, or when the enter key is pressed.
Does anyone have any tips or ideas on how I can solve this problem?
Thanks,
Giawa
Edit: I'm using the managed extension framework introduced with VS2010 to accomplish this. I'm writing in C# (and just added the c# tag to my question). Thanks
MEF is not the right way to accomplish the task of creating a language service. Instead, the Managed Package Framework (MPF) should be used to register the language service and perform tasks such as syntax highlighting, outlining, parsing, formatting, parsing, etc.
Since my question was about formatting, I'll cover a little bit of it in my answer. You must override the ReformatSpan method in the custom Source object that you've creating for your language service. There's a good example on the MSDN webpages for VS2005 (applicable to VS2010 as well).
You can also force formatting at any time by calling the ReformatSpan method directly. Here's a working example from my code:
Region region = service.GetRegionContainingLine((line > 0 ? line - 1 : 0));
if (region != null)
{
using (EditArray mgr = new EditArray(this, service.LastActiveTextView, true, "Reformat Brace"))
this.ReformatSpan(mgr, region.ToSpan());
}
Thanks to #James McNellis for pointing me in the correct direction.
Here you go: http://msdn.microsoft.com/en-us/library/ee197665.aspx

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.

Visual Studio Professional 2010: Stop "new {" from autocompleting into "new object {" (C#)

In Visual Studio Professional 2010 whenever I type the following:
new {
It automatically changes to:
new object {
Is there a way to make it not do this? "Object" does not have the properties of the object I want to anonymously create.
https://connect.microsoft.com/VisualStudio/feedback/details/584429/autocomplete-on-new-is-interpreted-as-a-new-function-instead-of-anonymous-class
I'm pretty sure it's a bug, so I went ahead and reported it. Was going to do it sooner or later anyway :)
Hope that's okay with you.
You can disable the IntelliSense completing when you type the bracket.
On the Tools menu select Options. Then, on the right hand side, expand Text Editor then C# then IntelliSense. Remove the { from the textbox under the Committed by typing the following characters:
You may also have to uncheck the Committed by pressing the space bar or get in the habit of writing new{ and relying on the auto formatting when you close the bracket (though I've never done any ASPX stuff so don't know how good the auto formatting is compared to a normal code file.)
I've just gotten in the habit of typing "new{}". Intellisense doesn't kick in then. And since I'm always reformatting the page anyways with crtl-k;crtl-d, it spaces it out correctly later on.

Categories