Update the simplifier to respect and apply the 'use var' user preference. #25865
Update the simplifier to respect and apply the 'use var' user preference. #25865heejaechang merged 105 commits intodotnet:masterfrom
Conversation
|
Tagging @kuhlenh @jinujoseph @dotnet/roslyn-ide |
|
Taggign @heejaechang @mavasani as i used IOperation primarily for this. |
|
would be nice to also recognize if (c) M(1);
else M(2);
M(c ? 1 : 2); |
|
@alrz Not sure how i feel about that one. If it were to be done, it would be a separate feature IMO. |
| { | ||
| internal abstract partial class CSharpTypeStyleDiagnosticAnalyzerBase | ||
| { | ||
| internal class State |
There was a problem hiding this comment.
This type was moved down to the CSharpWorkspace layer and renamed to CSharpTypeStyleContext
|
|
||
| namespace Microsoft.CodeAnalysis.CSharp.Diagnostics.TypeStyle | ||
| { | ||
| internal abstract partial class CSharpTypeStyleHelper |
There was a problem hiding this comment.
This type moved down to CSharpWorkspace
|
|
||
| namespace Microsoft.CodeAnalysis.CSharp.Diagnostics.TypeStyle | ||
| { | ||
| internal sealed class CSharpUseExplicitTypeHelper : CSharpTypeStyleHelper |
There was a problem hiding this comment.
This type moved down to CSharpWorkspace
|
|
||
| namespace Microsoft.CodeAnalysis.CSharp.Diagnostics.TypeStyle | ||
| { | ||
| internal sealed class CSharpUseImplicitTypeHelper : CSharpTypeStyleHelper |
There was a problem hiding this comment.
This type moved down to CSharpWorkspace
| return false; | ||
| } | ||
|
|
||
| // If the simpleName is the type of the Variable Declaration Syntax belonging to LocalDeclaration, For Statement or Using statement |
There was a problem hiding this comment.
all this code was removed. We now defer to CSharpTypeStypeHelper to do the logic. This way we're entirely in sync with the analyzer/refactoring.
|
|
||
| namespace Microsoft.CodeAnalysis.CSharp.Utilities | ||
| { | ||
| internal class CSharpTypeStyleContext |
There was a problem hiding this comment.
This was moved down from CSharpFeatures. Other than renaming it, it was not changed.
|
|
||
| namespace Microsoft.CodeAnalysis.CSharp.Utilities | ||
| { | ||
| internal abstract partial class CSharpTypeStyleHelper |
There was a problem hiding this comment.
This was moved down from CSharpFeatures. Other than renaming it, and making it a singleton, it was not changed.
|
|
||
| namespace Microsoft.CodeAnalysis.CSharp.Utilities | ||
| { | ||
| internal sealed class CSharpUseExplicitTypeHelper : CSharpTypeStyleHelper |
There was a problem hiding this comment.
This was moved down from CSharpFeatures. Other than renaming it, and making it a singleton, it was not changed.
|
|
||
| namespace Microsoft.CodeAnalysis.CSharp.Utilities | ||
| { | ||
| internal sealed class CSharpUseImplicitTypeHelper : CSharpTypeStyleHelper |
There was a problem hiding this comment.
This was moved down from CSharpFeatures. Other than renaming it, and making it a singleton, it was not changed.
?: over explicit if-statement flows.|
Tagging @dotnet/roslyn-ide @jcouv @jmarolf . I've now split this PR out from #26236 This PR is now only the change to move the "use var" logic down to the workspace layer so it can be part of hte simplifier logic. This is highly beneficial and it means that features do not have to always figure out "should i use 'var' or not". Instead, as long as they either generate a type with the Simplifier annotation on it (which the core Workspace code generation helpers do), then the Simplifier will convert to 'var' if that's what the user wants and it's legal to do so. |
|
Also tagging @DustinCampbell |
| { | ||
| void Main() | ||
| { | ||
| {|Simplify:int|} i = 0; |
There was a problem hiding this comment.
these cases were already being tested in this file, but as part of one large test. I kept hte large test, but also broke out individual cases to make things easier to debug when things go wrong.
| } | ||
|
|
||
| /// <summary> | ||
| /// <summary> |
There was a problem hiding this comment.
no idea why the code was bad before. but my VS fixed it.
| get { | ||
| return ResourceManager.GetString("Alias_ambiguous_type_0", resourceCulture); | ||
| } | ||
| } |
There was a problem hiding this comment.
someone manually edited this file in teh past. my VS keeps fixing it, so i'm just going to merge this in since it's a PITA to have to undo this file every time.
| @@ -306,7 +304,7 @@ public static TypeSyntax GetTypeExpression( | |||
| } | |||
There was a problem hiding this comment.
this whole helper can get removed in the future. features should just ask "type.GenerateTypeSyntax()" and they should get the right behavior.
| } | ||
|
|
||
| // If the simpleName is the type of the Variable Declaration Syntax belonging to LocalDeclaration, For Statement or Using statement | ||
| if (simpleName.IsParentKind(SyntaxKind.VariableDeclaration) && |
There was a problem hiding this comment.
we had duplicated a bunch of "can i use var" logic here. It was both broken and incomplete. Now, we just defer to the CSharpUseImplicitTypeHelper which contains all the correct logic and is the place we should always be updating.
| /// </summary> | ||
| public static Option<bool> PreferImplicitTypeInLocalDeclaration { get; } = new Option<bool>(nameof(SimplificationOptions), nameof(PreferImplicitTypeInLocalDeclaration), false, | ||
| public static Option<bool> PreferImplicitTypeInLocalDeclaration { get; } = new Option<bool>(nameof(SimplificationOptions), nameof(PreferImplicitTypeInLocalDeclaration), true, | ||
| storageLocations: new RoamingProfileStorageLocation("TextEditor.%LANGUAGE%.Specific.PreferImplicitTypeInLocalDeclaration")); |
There was a problem hiding this comment.
this is an option that is not actually exposed to the user in any way. I'm not sure why we actually have it.
| /// things quickly, even if it's going against their stated style. | ||
| /// </summary> | ||
| public readonly bool IsStylePreferred; | ||
| public readonly DiagnosticSeverity Severity; |
There was a problem hiding this comment.
This code moved down. I didn't want to change it. I also don't see the value in an auto-prop over a readonly field.
| /// one exception is the refactoring, which is explicitly there to still let people convert | ||
| /// things quickly, even if it's going against their stated style. | ||
| /// </summary> | ||
| public readonly bool IsStylePreferred; |
There was a problem hiding this comment.
The doc comment is very helpful, but the naming IsStylePreferred still seems a bit confusing. I would have assumed that AnalyzeTypeName(typeSyntax).IsStylePreferred would tell me if the current type style of typeSyntax is preferred, but the current implementation of the flag is opposite. Should this be renamed to IsConvertedStylePreferred with current semantics or to IsExistingTypeStylePreferred with negated semantics?
There was a problem hiding this comment.
This code moved down. I didn't want to change it. :) I can change the name if you really want though.
| { | ||
| } | ||
|
|
||
| protected override bool IsStylePreferred( |
There was a problem hiding this comment.
Seems like the result CSharpUseExplicitTypeHelper.IsStylePreferred is the negation of CSharpUseImplicitTypeHelper.IsStylePreferred. Can we share code?
There was a problem hiding this comment.
All this code moved down. I did not want to change it. But i can if you think that's important. let me know.
There was a problem hiding this comment.
Ah, I didn't realize that. In that case, I wouldn't do any rename or functionality changes as part of this PR. We can do it later. Thanks.
|
Can this be merged in? Thanks! |
|
retest windows_debug_vs-integration_prtest please |
|
@heejaechang @mavasani All tests passed. Can this be merged in? Thanks! |
|
Thanks! |
DustinCampbell
left a comment
There was a problem hiding this comment.
Thanks @CyrusNajmabadi!
Note: this PR looks large, but that's primarily due to moving some "use var" functionality down from teh CSharpFeatures layer to the CSharpWorkspace layer. This was done so that the C# simplification service could appropriately simplify code like Dictionary<int, string> d = ... to var d = ... if that matched the user's style preference.
Unfortunately, the classes used for this functionality were not in their own files. They were sibling classes to the "use var/explicit-type' analyzers", and thus show up as huge deletes/adds even though they were barely touched.