We have a big legacy solution (170+ projects) and want to:
Perform initial code cleanup automatically for all our codebase with rules defined by us.
Have plugin available to integrate with ReSharper, so that we could see warnings/hints/suggestions in code and apply quick fixes with Ctrl + Enter.
Have config in source control, so it can be shared between all team members and coding style checked by TeamCity after every commit.
The best what we found for this is to use R# + StyleCop for this, but I recently found out that there are many more rules for code quality used by FxCop and Visual Studio - Microsoft Code Analisys Rule set:
http://msdn.microsoft.com/en-us/library/dd264925.aspx
StyleCop rule set is not compatible with it. We would prefer following Microsoft rule set, not StyleCop one.
Question: is there a way to integrate Microsoft rule set (customized) into ReSharper and have all three above - cleanup, quick fixes, TeamCity integration with Microsoft rules? After a day of googling I did not find how to do it. Can anyone suggest?
I believe this should be achievable in resharper now.
ReSharper allows you to configure an extensive set of formatting rules and enforce them in your code. These rules are applied when ReSharper produces new code with code completion and code generation features, code templates and refactorings. The formatting rules can be also applied to the existing code in the current selection, current file, or in a larger scope up to the entire solution.
Source: Jetbrains Resharper Help
Related
I and my team spent the last few months defining rules about C# code formatting and style for our project, so we ended up with an .editorconfig file.
Moreover, we all installed the extension Productivity Power Tools, in order to format the code when saving the file (CTRL+S) rather than having to use the VS built-in feature Code Clean Up. We took this decision when we realized that the latter requires a special shortcut (CTRL+K, CTRL+E) and that every member of the team needs to set this setting locally, since there is no way to put it in the source control.
This way of working went great with VS 2019.
But, in the last month most members of the team upgraded to VS 2022 and the extension Productivity Power Tools 22 removed - apparently without any reason - the Format On Save and Remove and Sort Usings on save features.
We apparently solved this issue with the extension VSE-FormatDocumentOnSave - by setting Edit.FormatDocument Edit.RemoveAndSort in the Commands option. It seems to work great but our concern is the fact that this is not a Microsoft tool and who knows what will happen in the future.
So, since the code style and formatting rules are a concern for every team out there, I would like to know if anyone knows a better way to manage this, considering that we are a team of 20+ developers.
Starting from VS2022-Preview2, there is no need to additional extension to "format on-save", you just run a customized code cleanup that contains "Format document" action on-save:
Customize your cleanup profile to perform "Format document" action (if not already there), in Analyze > Code Cleanup > Configure Code Cleanup
Enable "Run code cleanup profile on save" in VS configuration Tools > Options > Text Editor > Code Cleanup
Bringing Code Cleanup on Save To Visual Studio 2022 17.1 Preview 2
Unfortunately code cleanup setting is not included to .editorconfig file neither to exported config file .vssettings that could be shared across a team, maybe it will be included in the future.
Some feature requests related to this:
visual studio 2019 allow import and export Configure Code Cleanup profile
Provide a way to share Code Cleanup profiles as related to EditorConfig
Code Cleanup and EditorConfig
"Format Document On Save" worked for me.
Configuration:
Why not try the Code Cleanup On Save extension by Mads Kristensen (who works for Microsoft). I have not used it myself but all his other VS extensions are awesome.
Edit: The 'clean on save' feature has been added to Visual Studio 17.1 which is in preview at the moment. So no extensions required.
I'm toying with getting our team and projects ready for VS 2019. Right away, trying to set up Code Analysis for a new project, I find this:
So, if this is deprecated (and apparently can't even be used, so I'm thinking "deprecated" really means "gone"), where are we supposed to set up our Rule Sets? Is there some other location, or perhaps an altogether new solution to the problem of style and code quality?
Going forward, static analysis will be provided by Roslyn analyzers: https://github.com/dotnet/roslyn-analyzers
Migrate from FxCop code analysis to .NET compiler platform analyzers
Microsoft IntelliCode looks great!
I've been running into an issue with Visual Studio 2017 where a T4 template that is generating a bunch of C# classes from metadata is auto formatting the files based on the settings in Visual Studio's C# formatting options. I have changed these options and verified that the changes are reflected in the generated code. Basically what is happening is the T4 code generation completes and then Visual Studio begins to open every single file that was just generated and format it.
I've turned off formatting for the C# language in Tools and Options and disabled .editorconfig files just in case it was picking up one of those, but the template generation seems to ignore this. The problem is that these T4 templates generate ~2000 files which are then formatted which causes TFS to check out the file and eventually crashes visual studio about halfway through the formatting. Is there an option somewhere I'm missing to turn off formatting for generated code? This was not an issue prior to our recent upgrade to VS2017.
The default setting for Visual Studio 2017 is that it doesn't do anything to template files on the onset, so in order for this to happen it should be something you've added to your VS environment. Few things to check...
Any Visual Studio extensions or features. T4 is not well supported so a lot of the extensions and features out there have bugs in them. It maybe that the software provider hasn't thought of your problem yet and you should raise a bug with them. If your using an extension or feature to assist in your software development of T4 then try a different one. Additionally extensions or features not related to T4 also format files. An examples are Productivity Power Tools or Resharper
Under the properties of your template files check the Custom Tool value. It may not seem like the obvious choice, but occasionally if this is not set to TextTemplatingFileGenerator or TextTemplatingFilePreprocessor, you need know why it's not set that way. There might be a perfectly good reason for why its set differently, just so long as you know what that reason is
Normally templates use the *.tt extension, however there are many different acceptable extensions that suit different occasions. Make sure the extension isn't named the same as an existing extension, that formats files in its own way
If the features for C# are off, you might want to consider turning off all formatting features one by one incase the templates files are being considered as part of a different language or under a different option. Start with Tools > Options > Text Editor > Basic > Advanced > Pretty listing as an example. That may give you a clue as to where the setting is being affected.
HTH
Is there a StyleCop+ version which works for Visual Studio 2017?
StyleCop+ offers more custom rules for making your style tougher.
Most of them have flexible settings, configured through user-friendly UI.
And a small example illustrates what this rule is about.
I'm using StyleCop Analyzers and, while I don't know how easy StyleCop+ is to use, the Analyzers are pretty straight-forward. There are some useful rules missing (that are in StyleCop+) but I'm using Menees Analyzers (https://www.nuget.org/packages/Menees.Analyzers.2017/) to help with some of those. Again, changing the settings and so on is probably not as easy as StyleCop+, but it's not too tricky.
I am currently doing research to complete my org's migration to VS 2015, including implementing StyleCop.Analyzers, getting ReSharper settings updated and checked into source control, etc.
One topic I keep coming across is "compatibility" between StyleCop and ReSharper. A Jet Brains dev has even forked classic StyleCop in order to make it "compatible" with ReSharper 9.
What does "compatible" mean here? Is it as simple as making all of ReSharper's default code analysis settings match those in StyleCop, or something more involved?
Our upgrades/changes include:
Visual Studio 2010 to 2015
ReSharper 8 to 10
StyleCop classic to StyleCop.Analyzers
... so I just want to make sure I have my bases covered.
Is it as simple as making all of ReSharper's default code analysis
settings match those in StyleCop, or something more involved?
Yes, pretty much. It's all about keeping the rules in sync so that one doesn't flag an error the other is ignoring.
One thing to watch for on this migration is the version of the R# plugin. I haven't had much joy with ReSharper.StyleCop, but
StyleCop by JetBrains 4.8 works fine, plus it is designed to "play nice" with
StyleCop.Analyzers.