Skip to content

Update the simplifier to respect and apply the 'use var' user preference. #25865

Merged
heejaechang merged 105 commits intodotnet:masterfrom
CyrusNajmabadi:useConditionalExpressoin
Apr 19, 2018
Merged

Update the simplifier to respect and apply the 'use var' user preference. #25865
heejaechang merged 105 commits intodotnet:masterfrom
CyrusNajmabadi:useConditionalExpressoin

Conversation

@CyrusNajmabadi
Copy link
Copy Markdown
Contributor

@CyrusNajmabadi CyrusNajmabadi commented Mar 31, 2018

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.

@CyrusNajmabadi CyrusNajmabadi requested a review from a team as a code owner March 31, 2018 23:55
@CyrusNajmabadi
Copy link
Copy Markdown
Contributor Author

Tagging @kuhlenh @jinujoseph @dotnet/roslyn-ide

@CyrusNajmabadi
Copy link
Copy Markdown
Contributor Author

Taggign @heejaechang @mavasani as i used IOperation primarily for this.

@alrz
Copy link
Copy Markdown
Member

alrz commented Apr 1, 2018

would be nice to also recognize

if (c) M(1);
else M(2);

M(c ? 1 : 2);

@CyrusNajmabadi
Copy link
Copy Markdown
Contributor Author

@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
Copy link
Copy Markdown
Contributor Author

@CyrusNajmabadi CyrusNajmabadi Apr 1, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This type was moved down to the CSharpWorkspace layer and renamed to CSharpTypeStyleContext


namespace Microsoft.CodeAnalysis.CSharp.Diagnostics.TypeStyle
{
internal abstract partial class CSharpTypeStyleHelper
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This type moved down to CSharpWorkspace


namespace Microsoft.CodeAnalysis.CSharp.Diagnostics.TypeStyle
{
internal sealed class CSharpUseExplicitTypeHelper : CSharpTypeStyleHelper
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This type moved down to CSharpWorkspace


namespace Microsoft.CodeAnalysis.CSharp.Diagnostics.TypeStyle
{
internal sealed class CSharpUseImplicitTypeHelper : CSharpTypeStyleHelper
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was moved down from CSharpFeatures. Other than renaming it, it was not changed.


namespace Microsoft.CodeAnalysis.CSharp.Utilities
{
internal abstract partial class CSharpTypeStyleHelper
Copy link
Copy Markdown
Contributor Author

@CyrusNajmabadi CyrusNajmabadi Apr 1, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Copy link
Copy Markdown
Contributor Author

@CyrusNajmabadi CyrusNajmabadi Apr 1, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was moved down from CSharpFeatures. Other than renaming it, and making it a singleton, it was not changed.

@CyrusNajmabadi CyrusNajmabadi changed the title Add features to offer using conditional expressions ?: over explicit if-statement flows. Update the simplifier to respect and apply the 'use var' user preference. Apr 18, 2018
@CyrusNajmabadi
Copy link
Copy Markdown
Contributor Author

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.

@CyrusNajmabadi
Copy link
Copy Markdown
Contributor Author

Also tagging @DustinCampbell

{
void Main()
{
{|Simplify:int|} i = 0;
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no idea why the code was bad before. but my VS fixed it.

get {
return ResourceManager.GetString("Alias_ambiguous_type_0", resourceCulture);
}
}
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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(
}
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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) &&
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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"));
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

autoprops?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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(
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like the result CSharpUseExplicitTypeHelper.IsStylePreferred is the negation of CSharpUseImplicitTypeHelper.IsStylePreferred. Can we share code?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All this code moved down. I did not want to change it. But i can if you think that's important. let me know.

Copy link
Copy Markdown
Contributor

@mavasani mavasani Apr 19, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@CyrusNajmabadi
Copy link
Copy Markdown
Contributor Author

Can this be merged in? Thanks!

@heejaechang
Copy link
Copy Markdown
Contributor

retest windows_debug_vs-integration_prtest please

@CyrusNajmabadi
Copy link
Copy Markdown
Contributor Author

@heejaechang @mavasani All tests passed. Can this be merged in? Thanks!

@heejaechang heejaechang merged commit 06393e4 into dotnet:master Apr 19, 2018
@CyrusNajmabadi CyrusNajmabadi deleted the useConditionalExpressoin branch April 19, 2018 23:17
@CyrusNajmabadi
Copy link
Copy Markdown
Contributor Author

Thanks!

Copy link
Copy Markdown
Member

@DustinCampbell DustinCampbell left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @CyrusNajmabadi!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants