Export Resharper code style settings into Editor.Config - c#

I am trying to create a single place for code-style and formatting rules for our solution.
We are currently using Resharper, which stores this info in the {slnName}.dotSettings file. However, some developers don't want to use Resharper due to its performance issues. So they don't get these rules and warnings.
EditorConfig seemed like a suitable replacement, but I wanted to translate the ReSharper rules (where possible) into editor config rules and also include all the ReSharper specific rules so that those who do use ReSharper get the extra rules too, and they are all stored in one place (One source of truth!).
Anyway, trying to grasp what the .dotSettings file is doing has been unfruitful, and I know that Resharper will read in editor config files, so I'd like it to export it too

With Resharper 2019.1 you can now 'Export settings to .editorconfig'. The option lives in Resharper -> Tools.
This will export the standard editor config options, the Visual Studio specific config options and the resharper specific configs. Very useful!

In Reshaper 2019.3, this option is found in "Resharper Options | Code Editing | General Formatter Style"
As described here -> https://blog.jetbrains.com/dotnet/2019/03/28/export-editorconfig-code-style-optimize-references-sdk-projects-resharper-2019-1-eap-updates/

Related

VS 2022 - Convert to file-scoped namespace in all files

I'm converting my project to .NET 6 and I want to use filescoped namespaces everywhere. But the conversion tool exists only in the editor.
Has anyone found out if there's a way to run this editor function across all files in solution at once? (Looks like Rider has that function)
Adding a rule to use file scoped namespaces in .editorconfig worked for me:
create an .editorconfig file in the solution directory
add following line/content below (docs, code - IDE0161)
Example .editorconfig file content:
[*.cs]
csharp_style_namespace_declarations = file_scoped:warning
After that the preview changes dialog had an option to apply the fix to the whole project/solution:
I always have problems finding files that are supposed to be updated (.editorconfig in this case). I don't even know if I should search for it in the project's, Visual Studio installation's or any folder on the PC. So I like the answer in the link below because it says where in the interface to change the setting.
Best answer in my opinion is here:
https://www.ilkayilknur.com/how-to-convert-block-scoped-namespacees-to-file-scoped-namespaces
It says that you can change the code-style preference (and enable the display of the option to apply this preference in a document / project / solution) by going to Tools => Options => Text Editor => C#=> Code Style and then changing the related preference.
EditorConfig syntax
csharp_style_namespace_declarations = file_scoped:error
dotnet_diagnostic.IDE0161.severity = error
Note
Syntax option = rule:severity will be deprecated, sooner or later.
I strongly recommend to read this article before you start build .editorconfig for your project.
After you have configured the .editorconfig, you can configure a 'Code Cleanup' setting to automatically convert all files to use file-scoped namespace. Go to Tools -> Options -> Text Editor -> Code Cleanup -> Configure Code Cleanup. Then add the 'Apply namespace preferences'. Then go to Analyze -> Code Cleanup (or just search for 'Code cleanup') and run the Code Cleanup to automatically change the namespaces to file-scoped.

Antlr4 C# targets and output path of generated files

I have a C# solution with an Antlr3 grammar file, and I'm trying to upgrade to Anltr4. It turns out the grammar was the easy part (it became better, and one third the size!). Generating the parser turned out to be the tricky part.
In the old solution I merely ran AntlrWorks to update the lexer and parser .cs files when the grammar file changed. The lexer and parser were included directly in the same project as the grammar so the framework around the parser could make use of them directly.
With the Antlr4 targets for C# I noticed that (at least by default) the output path of the generated Parser C# classes is in the intermediate directory, e.g. obj\Debug. This means I can't use the parser directly in the same project. Am I supposed to change the output path if I want the generated source usable in my sln? Don't I want it in my sln?
I tried making a separate project for the parser generation, that is, a project containing only the grammar file. When this project is built using the Antlr4 targets, the resulting assembly can be referenced from projects needing the parser classes. However, if this project is included in the solution I still don't get any intellisense in visual studio since it bases its intellisense on source files for loaded projects, so it is still not quite usable. The only way I could get it working properly was to build then unload the parser project, and have other projects reference it as an assembly file rather than a project.
EDIT:
After looking on the antlr-interest list I found this thread indicating that the problem with intellisense may be Resharpers fault. If this is the case, then my question is rather how to keep using both Antlr4 and Resharper in my solution?
https://groups.google.com/forum/#!topic/antlr-discussion/QSuJXphaBDg
I found an extension for Resharper: ReSharper.ExternalCode.
Tip for Antlr: add obj\Debug in Resharper -> Code Inspection -> External Code and reload solution.
It's works for me.
There is an easy way to get the best of both Antlr 4 and Visual Studio. You are on the right track in having a separate grammar project and implementation project, but you are missing a couple steps.
Follow these steps to get the Intellisense working
In Visual Studio, go to Tools -> Extensions and Updates and search the Online section for "ANTLR Language Support" by Sam Harwell.
This does a few things:
Adds Templates for the combined grammars.
Adds Syntax Highlighting
Adds an MSBuild target for the grammar to generate the parser.
In your solution, set up your project structure like this:
Solution
Grammar Project
ANTLR 4 Combined Grammar
Implementation Project
Folder for Links to Generated files
Listener Implementation Class
Write and Compile your grammar.
In your folder for the Links to Generated Files, Right-Click the folder and click Add -> Existing Item
Browse to Grammar Project\obj\Debug and select all the generated parser files.
This next step is important. On the Add button there is a little drop-down arrow. Click the drop-down arrow and click "Add As Link".
This will add the generated files to the implementation project using a symbolic link instead of a direct copy.
This gives the added benefit of not having to remove and re-add the parser files if you have to change your grammar later.
Intellisense should work now for your generated parser classes, and you don't
even have to disable ReSharper.
You are encountering a known bug with ReSharper. The ANTLR 3 and ANTLR 4 C# build integration is reliable, and uses long-established patterns that Microsoft uses with other languages and code generation tasks. Your options are:
Stop using ReSharper
Get the ReSharper authors to address the lack of support for this feature, or
Modify the ANTLR 4 target to work around the limitation in ReSharper, which will introduce other problems into the build which may or may not be acceptable for your use.
Option 1 is certainly the least expensive, and most likely to provide reliable long-term support for your projects.

Disable stylecop analysis for specific projects within solution

Is there a way to easily stop StyleCop warnings from being displayed within specific projects in a solution. Or, more pointedly, a way to select which projects StyleCop analysis should be performed on by Visual Studio?
We have a solution with 9 projects in it. There are only 2 projects that I actually want to see StyleCop warnings for, so I've created StyleCop.Settings files within those project root directories. This means that, for the rest of the projects, the default rule set is applied and I get a screen full of warnings every time I open a class.
The only way I can think to remove these warnings is to add another StyleCop.Settings file a folder level above with all the rules switched off and set merge options on the specific Settings files I am interested in to not merge with this parent file. This just feels wrong though. Is there a cleaner option or is this my only one?
UPDATE: I'm specifically looking for a way to stop the warnings from appearing within Visual Studio. I've added a Settings.StyleCop file to the solution folder and disabled all the rules. I run StyleCop analysis across one of my test projects and there are no errors reported. However, opening a test class reveals a raft of StyleCop warnings, which I want to suppress. Could this be the StyleCop for ReSharper plugin? I have a code cleanup profile created and have disabled certain rules within there but that doesn't appear to make any difference within my test classes.
Please have a look at File Lists configuration - they allow to disable rules by default per project:
Enabled Or Disabled By Default
In addition, a new setting allows you to determine whether rules
should be enabled or disabled by default. This can be set either at
the project level or at the SourceFileList level. For example, here’s
how you would set up a project with all rules disabled by default, and
only two rules explicitly enabled:
<StyleCopSettings Version="4.3">
<GlobalSettings>
<BooleanProperty Name="RulesEnabledByDefault">False</BooleanProperty>
</GlobalSettings>

Is there a way to sync settings from Settings.StyleCop to ReSharper?

My question is quite simple: Is there a way to sync the changes you make in the Settings.StyleCop file into the ReSharper settings?
As it is now I have to first make the changes in Settings.StyleCop (so that StyleCop can validate my code using the rules I want) and then I have to make the same changes in ReSharper to get syntax highlighting and warnings in code as i type.
I found this question but it doesn't seem to apply anymore. ReSharper doesn't read StyleCop.Settings.
With the StyleCop (4.7.44.0) plugin for R#:
Under the R# options, Tools, StyleCop section:

How do I setup per-assembly resharper settings

I work in a codebase where for historic reasons not all projects have identical coding conventions. In fact, within a single .sln I have projects requiring different naming conventions.
Is there a way to have resharper use different settings based on which project the file I'm editing lives in?
Since ReSharper 6.1 you can finally have per project settings!
Basically, all you need to do is to create a file named <project file name>.DotSettings, e.g. MyUnitTest.csproj.DotSettings.
See this blog post for more info.
Check out the Resharper Settings Manager tool
You'll need to first install the ReSharper Power Toys to use it.
Resharper now supports applying different settings to different Solutions from Resharper 6.1. See this blog post for further details http://blogs.jetbrains.com/dotnet/2011/11/resharper-settings-in-61/
However, from the looks of it, you cannot yet apply different settings to different projects within a solution. This is a feature I would very much welcome too!
Additionally (to the other answers) when using SVN the "*.sln.DotSettings" files are changed every time a different machine opens the solution. To combat this set the read-only attribute on the DotSettings files, after checking out from the repository.

Categories

Resources