提交 12f9859a 编写于 作者: A Alireza Habibi

Attach the simplifier

上级 cdf4fd3e
......@@ -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;
......
......@@ -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);
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册