SyntaxEditor.SetModifiers may not properly format the result when multiple modifiers are added at once #27835
Labels
Area-IDE
Concept-API
This issue involves adding, removing, clarification, or modification of an API.
help wanted
The issue is "up for grabs" - add a comment if you are interested in working on it
IDE-Formatter
Code formatter and/or smart indent
Milestone
(I apologize in advance for not using the issue template; but selecting the code line and using "Open Issue" didn't suggest it. Also, I'm not sure if it is actually tied to any particular Visual Studio version since it happens in my own Analyzer using either
Microsoft.CodeAnalysis.CSharp.Workspaces
2.4.0 or 2.8.2. In case it does matter, this is on Visual Studio Enterprise 2017 15.7.0)I tried writing a CodeFix to make certain fields
static
andreadonly
; so I borrowed the following snippet:roslyn/src/Features/Core/Portable/MakeFieldReadonly/AbstractMakeFieldReadonlyCodeFixProvider.cs
Line 64 in ef41d39
Whenever this code snippet is run (at least in my CodeFix), the resulting code is incorrectly formatted. I don't know if and when this happens during the built-in "Make read only" CodeFix, but when I copied that snippet into one of my own analyzers, the space being between
readonly
and the type name is missing (only in Visual Studio though when trying it on my target project; it works correctly in the Unit Test which is even more interresting).Hence I'm assuming that the built-in CodeFix would fail in that case, just like my own does.
My CodeFix doesn't have those fancy base classes that do the work for me, so the method looks a little more involved (but should basically do just the same):
When invoked on a static field
instead of the expected
the result is the less compily (is that a word?) with a missing space
If I use the
else
block (modified for single result) in my own CodeFix, the space is applied correctly:The result is always correct if I run the Unit Test that is created with the "Analyzer with CodeFix" template, but it isn't when run inside Visual Studio on "real" code.
I'm not sure what the proper fix for this would be (or if this is actually a real bug in roslyn code vs. me missing something while copying the code over to my CodeFix); the main apparent difference is that the loop applies
.WithAdditionalAnnotations(Formatter.Annotation)
to the newly generated nodes (other than the fact the whole expression is rebuilt from scratch instead of updated).My CodeFix works just fine when the target field has no modifiers at all and both are added at the same time. Adding
static
also works (probably because my Code Style settings/.editorconfig
specify thatstatic
should come beforereadonly
), but adding justreadonly
does not.Please let me know if theres any additional infomation I (sh|c)ould provide (other than a sample solution; which will probably have to wait until the weekend or so).
The text was updated successfully, but these errors were encountered: