From 12f9859a29e485752b9eb7b206990de1bece1030 Mon Sep 17 00:00:00 2001 From: Alireza Habibi Date: Mon, 14 Oct 2019 20:31:52 +0330 Subject: [PATCH] Attach the simplifier --- .../ConvertIfToSwitchTests.cs | 24 ++++++------ ...SwitchCodeRefactoringProvider.Rewriting.cs | 38 +++++++++---------- 2 files changed, 30 insertions(+), 32 deletions(-) diff --git a/src/EditorFeatures/CSharpTest/CodeActions/ConvertIfToSwitch/ConvertIfToSwitchTests.cs b/src/EditorFeatures/CSharpTest/CodeActions/ConvertIfToSwitch/ConvertIfToSwitchTests.cs index 1ca116dbe2b..4a2825c2365 100644 --- a/src/EditorFeatures/CSharpTest/CodeActions/ConvertIfToSwitch/ConvertIfToSwitchTests.cs +++ b/src/EditorFeatures/CSharpTest/CodeActions/ConvertIfToSwitch/ConvertIfToSwitchTests.cs @@ -341,7 +341,7 @@ void M(object o) { switch (o) { - case string s when (s.Length > 5 && s.Length < 10): + case string s when s.Length > 5 && s.Length < 10: return; case int i: return; @@ -985,7 +985,7 @@ void M(int i) { switch (i) { - case 1 when i == 2 && (i == 3): + case 1 when i == 2 && i == 3: return; case 10: return; @@ -1015,7 +1015,7 @@ void M(int i) { switch (i) { - case 1 when (i == 2) && i == 3: + case 1 when i == 2 && i == 3: return; case 10: return; @@ -1045,7 +1045,7 @@ void M(int i) { switch (i) { - case 1 when (i == 2) && (i == 3): + case 1 when i == 2 && i == 3: return; case 10: return; @@ -1105,7 +1105,7 @@ void M(int i) { switch (i) { - case 1 when i == 2 && (i == 3): + case 1 when i == 2 && i == 3: return; case 10: return; @@ -1135,7 +1135,7 @@ void M(int i) { switch (i) { - case 1 when (i == 2) && i == 3: + case 1 when i == 2 && i == 3: return; case 10: return; @@ -1165,7 +1165,7 @@ void M(int i) { switch (i) { - case 1 when (i == 2) && (i == 3): + case 1 when i == 2 && i == 3: return; case 10: return; @@ -1195,7 +1195,7 @@ void M(int i) { switch (i) { - case 1 when (i == 2 && i == 3): + case 1 when i == 2 && i == 3: return; case 10: return; @@ -1285,7 +1285,7 @@ void M(int i) { switch (i) { - case 1 when (i == 2) && i == 3: + case 1 when i == 2 && i == 3: return; case 10: return; @@ -1315,7 +1315,7 @@ void M(int i) { switch (i) { - case 1 when (i == 2) && (i == 3): + case 1 when i == 2 && i == 3: return; case 10: return; @@ -1345,7 +1345,7 @@ void M(int i) { switch (i) { - case 1 when ((i == 2) && i == 3): + case 1 when i == 2 && i == 3: return; case 10: return; @@ -1375,7 +1375,7 @@ void M(int i) { switch (i) { - case 1 when (i == 2 && (i == 3)): + case 1 when i == 2 && i == 3: return; case 10: return; diff --git a/src/Features/Core/Portable/ConvertIfToSwitch/AbstractConvertIfToSwitchCodeRefactoringProvider.Rewriting.cs b/src/Features/Core/Portable/ConvertIfToSwitch/AbstractConvertIfToSwitchCodeRefactoringProvider.Rewriting.cs index a91a4953ce6..0c2125ef6ea 100644 --- a/src/Features/Core/Portable/ConvertIfToSwitch/AbstractConvertIfToSwitchCodeRefactoringProvider.Rewriting.cs +++ b/src/Features/Core/Portable/ConvertIfToSwitch/AbstractConvertIfToSwitchCodeRefactoringProvider.Rewriting.cs @@ -9,6 +9,7 @@ using System.Threading.Tasks; using Microsoft.CodeAnalysis.Editing; using Microsoft.CodeAnalysis.Formatting; +using Microsoft.CodeAnalysis.Simplification; using Roslyn.Utilities; namespace Microsoft.CodeAnalysis.ConvertIfToSwitch @@ -33,32 +34,29 @@ internal abstract partial class AbstractConvertIfToSwitchCodeRefactoringProvider var generator = SyntaxGenerator.GetGenerator(document); var ifSpan = ifStatement.Span; - SyntaxNode @switch; - if (convertToSwitchExpression) - { - @switch = CreateSwitchExpressionStatement(target, sections); - } - else - { - var lastNode = sections.LastOrDefault()?.SyntaxToRemove ?? ifStatement; - @switch = CreateSwitchStatement(ifStatement, target, sections.Select(AsSwitchSectionSyntax)) - .WithLeadingTrivia(ifStatement.GetLeadingTrivia()) - .WithTrailingTrivia(lastNode.GetTrailingTrivia()) - .WithAdditionalAnnotations(Formatter.Annotation); - } + var @switch = convertToSwitchExpression + ? CreateSwitchExpressionStatement(target, sections) + : CreateSwitchStatement(ifStatement, target, sections.Select(section => AsSwitchSectionSyntax(section, generator))); + + var lastNode = sections.Last().SyntaxToRemove; + @switch = @switch + .WithLeadingTrivia(ifStatement.GetLeadingTrivia()) + .WithTrailingTrivia(lastNode.GetTrailingTrivia()) + .WithAdditionalAnnotations(Formatter.Annotation) + .WithAdditionalAnnotations(Simplifier.Annotation); var nodesToRemove = sections.Skip(1).Select(s => s.SyntaxToRemove).Where(s => s.Parent == ifStatement.Parent); root = root.RemoveNodes(nodesToRemove, SyntaxRemoveOptions.KeepNoTrivia); root = root.ReplaceNode(root.FindNode(ifSpan), @switch); return document.WithSyntaxRoot(root); + } - SyntaxNode AsSwitchSectionSyntax(AnalyzedSwitchSection section) - { - var statements = AsSwitchSectionStatements(section.Body); - return section.Labels.IsDefault - ? generator.DefaultSwitchSection(statements) - : generator.SwitchSectionFromLabels(section.Labels.Select(AsSwitchLabelSyntax), statements); - } + private SyntaxNode AsSwitchSectionSyntax(AnalyzedSwitchSection section, SyntaxGenerator generator) + { + var statements = AsSwitchSectionStatements(section.Body); + return section.Labels.IsDefault + ? generator.DefaultSwitchSection(statements) + : generator.SwitchSectionFromLabels(section.Labels.Select(AsSwitchLabelSyntax), statements); } } } -- GitLab