提交 06fb47c9 编写于 作者: Š Šimon Koníček

Adding helpers to share language specific code

上级 e2d58a81
...@@ -18,16 +18,7 @@ internal sealed class CSharpSplitIntoConsecutiveIfStatementsCodeRefactoringProvi ...@@ -18,16 +18,7 @@ internal sealed class CSharpSplitIntoConsecutiveIfStatementsCodeRefactoringProvi
protected override int LogicalOrSyntaxKind => (int)SyntaxKind.LogicalOrExpression; protected override int LogicalOrSyntaxKind => (int)SyntaxKind.LogicalOrExpression;
protected override bool IsConditionOfIfStatement(SyntaxNode expression, out SyntaxNode ifStatementNode) protected override bool IsConditionOfIfStatement(SyntaxNode expression, out SyntaxNode ifStatementNode)
{ => Helpers.IsConditionOfIfStatement(expression, out ifStatementNode);
if (expression.Parent is IfStatementSyntax ifStatement && ifStatement.Condition == expression)
{
ifStatementNode = ifStatement;
return true;
}
ifStatementNode = null;
return false;
}
protected override bool HasElseClauses(SyntaxNode ifStatementNode) protected override bool HasElseClauses(SyntaxNode ifStatementNode)
{ {
......
...@@ -17,16 +17,7 @@ internal sealed class CSharpSplitIntoNestedIfStatementsCodeRefactoringProvider ...@@ -17,16 +17,7 @@ internal sealed class CSharpSplitIntoNestedIfStatementsCodeRefactoringProvider
protected override int LogicalAndSyntaxKind => (int)SyntaxKind.LogicalAndExpression; protected override int LogicalAndSyntaxKind => (int)SyntaxKind.LogicalAndExpression;
protected override bool IsConditionOfIfStatement(SyntaxNode expression, out SyntaxNode ifStatementNode) protected override bool IsConditionOfIfStatement(SyntaxNode expression, out SyntaxNode ifStatementNode)
{ => Helpers.IsConditionOfIfStatement(expression, out ifStatementNode);
if (expression.Parent is IfStatementSyntax ifStatement && ifStatement.Condition == expression)
{
ifStatementNode = ifStatement;
return true;
}
ifStatementNode = null;
return false;
}
protected override SyntaxNode SplitIfStatement( protected override SyntaxNode SplitIfStatement(
SyntaxNode ifStatementNode, ExpressionSyntax condition1, ExpressionSyntax condition2) SyntaxNode ifStatementNode, ExpressionSyntax condition1, ExpressionSyntax condition2)
......
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using Microsoft.CodeAnalysis.CSharp.Syntax;
namespace Microsoft.CodeAnalysis.CSharp.SplitOrMergeIfStatements
{
internal static class Helpers
{
public static bool IsConditionOfIfStatement(SyntaxNode expression, out SyntaxNode ifStatementNode)
{
if (expression.Parent is IfStatementSyntax ifStatement && ifStatement.Condition == expression)
{
ifStatementNode = ifStatement;
return true;
}
ifStatementNode = null;
return false;
}
}
}
...@@ -104,7 +104,8 @@ private bool CanBeMergedWithParent(ISyntaxFactsService syntaxFacts, SyntaxNode i ...@@ -104,7 +104,8 @@ private bool CanBeMergedWithParent(ISyntaxFactsService syntaxFacts, SyntaxNode i
private static SyntaxNode GetPreviousStatement(ISyntaxFactsService syntaxFacts, SyntaxNode statement) private static SyntaxNode GetPreviousStatement(ISyntaxFactsService syntaxFacts, SyntaxNode statement)
{ {
if (!syntaxFacts.IsExecutableBlock(statement.Parent)) if (!syntaxFacts.IsStatement(statement) ||
!syntaxFacts.IsExecutableBlock(statement.Parent))
{ {
return null; return null;
} }
......
...@@ -65,7 +65,8 @@ await CanBeSeparateStatementsAsync(document, syntaxFacts, currentIfStatement, ca ...@@ -65,7 +65,8 @@ await CanBeSeparateStatementsAsync(document, syntaxFacts, currentIfStatement, ca
return false; return false;
} }
if (!syntaxFacts.IsExecutableBlock(ifStatement.Parent)) if (!syntaxFacts.IsStatement(ifStatement) ||
!syntaxFacts.IsExecutableBlock(ifStatement.Parent))
{ {
return false; return false;
} }
......
' Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
Imports System.Collections.Immutable
Imports Microsoft.CodeAnalysis.VisualBasic.Syntax
Namespace Microsoft.CodeAnalysis.VisualBasic.SplitOrMergeIfStatements
Friend Module Helpers
Public Function IsConditionOfIfStatement(expression As SyntaxNode, ByRef ifStatementNode As SyntaxNode) As Boolean
If TypeOf expression.Parent Is IfStatementSyntax AndAlso
DirectCast(expression.Parent, IfStatementSyntax).Condition Is expression AndAlso
TypeOf expression.Parent.Parent Is MultiLineIfBlockSyntax Then
ifStatementNode = expression.Parent.Parent
Return True
End If
If TypeOf expression.Parent Is ElseIfStatementSyntax AndAlso
DirectCast(expression.Parent, ElseIfStatementSyntax).Condition Is expression AndAlso
TypeOf expression.Parent.Parent Is ElseIfBlockSyntax Then
ifStatementNode = expression.Parent.Parent
Return True
End If
ifStatementNode = Nothing
Return False
End Function
Public Function GetElseClauses(ifStatementNode As SyntaxNode) As SyntaxList(Of SyntaxNode)
If TypeOf ifStatementNode Is MultiLineIfBlockSyntax Then
Dim ifBlock = DirectCast(ifStatementNode, MultiLineIfBlockSyntax)
Return AddIfNotNull(ifBlock.ElseIfBlocks, ifBlock.ElseBlock)
ElseIf TypeOf ifStatementNode Is ElseIfBlockSyntax Then
Dim elseIfBlock = DirectCast(ifStatementNode, ElseIfBlockSyntax)
Dim ifBlock = DirectCast(elseIfBlock.Parent, MultiLineIfBlockSyntax)
Dim nextElseIfBlocks = ifBlock.ElseIfBlocks.RemoveRange(0, ifBlock.ElseIfBlocks.IndexOf(elseIfBlock) + 1)
Return AddIfNotNull(nextElseIfBlocks, ifBlock.ElseBlock)
End If
Throw ExceptionUtilities.UnexpectedValue(ifStatementNode)
End Function
Private Function AddIfNotNull(list As SyntaxList(Of SyntaxNode), node As SyntaxNode) As SyntaxList(Of SyntaxNode)
Return If(node IsNot Nothing, list.Add(node), list)
End Function
End Module
End Namespace
...@@ -79,12 +79,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.SplitOrMergeIfStatements ...@@ -79,12 +79,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.SplitOrMergeIfStatements
End Function End Function
Protected Overrides Function HasElseClauses(ifStatementNode As SyntaxNode) As Boolean Protected Overrides Function HasElseClauses(ifStatementNode As SyntaxNode) As Boolean
If TypeOf ifStatementNode Is MultiLineIfBlockSyntax Then Return Helpers.GetElseClauses(ifStatementNode).Count > 0
Dim ifBlock = DirectCast(ifStatementNode, MultiLineIfBlockSyntax)
Return ifBlock.ElseIfBlocks.Count > 0 OrElse ifBlock.ElseBlock IsNot Nothing
End If
Return True
End Function End Function
Protected Overrides Function MergeIfStatements(firstIfStatementNode As SyntaxNode, Protected Overrides Function MergeIfStatements(firstIfStatementNode As SyntaxNode,
......
...@@ -47,16 +47,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.SplitOrMergeIfStatements ...@@ -47,16 +47,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.SplitOrMergeIfStatements
End Function End Function
Protected Overrides Function GetElseClauses(ifStatementNode As SyntaxNode) As ImmutableArray(Of SyntaxNode) Protected Overrides Function GetElseClauses(ifStatementNode As SyntaxNode) As ImmutableArray(Of SyntaxNode)
If TypeOf ifStatementNode Is MultiLineIfBlockSyntax Then Return Helpers.GetElseClauses(ifStatementNode).ToImmutableArray()
Dim ifBlock = DirectCast(ifStatementNode, MultiLineIfBlockSyntax)
Return ImmutableArray.ToImmutableArray(Of SyntaxNode)(ifBlock.ElseIfBlocks).Add(ifBlock.ElseBlock)
ElseIf TypeOf ifStatementNode Is ElseIfBlockSyntax Then
Dim elseIfBlock = DirectCast(ifStatementNode, ElseIfBlockSyntax)
Dim ifBlock = DirectCast(elseIfBlock.Parent, MultiLineIfBlockSyntax)
Dim nextElseIfBlocks = ifBlock.ElseIfBlocks.RemoveRange(0, ifBlock.ElseIfBlocks.IndexOf(elseIfBlock) + 1)
Return ImmutableArray.ToImmutableArray(Of SyntaxNode)(nextElseIfBlocks).Add(ifBlock.ElseBlock)
End If
Throw ExceptionUtilities.UnexpectedValue(ifStatementNode)
End Function End Function
Protected Overrides Function MergeIfStatements(outerIfStatementNode As SyntaxNode, Protected Overrides Function MergeIfStatements(outerIfStatementNode As SyntaxNode,
......
...@@ -16,31 +16,11 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.SplitOrMergeIfStatements ...@@ -16,31 +16,11 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.SplitOrMergeIfStatements
Protected Overrides ReadOnly Property LogicalOrSyntaxKind As Integer = SyntaxKind.OrElseExpression Protected Overrides ReadOnly Property LogicalOrSyntaxKind As Integer = SyntaxKind.OrElseExpression
Protected Overrides Function IsConditionOfIfStatement(expression As SyntaxNode, ByRef ifStatementNode As SyntaxNode) As Boolean Protected Overrides Function IsConditionOfIfStatement(expression As SyntaxNode, ByRef ifStatementNode As SyntaxNode) As Boolean
If TypeOf expression.Parent Is IfStatementSyntax AndAlso Return Helpers.IsConditionOfIfStatement(expression, ifStatementNode)
DirectCast(expression.Parent, IfStatementSyntax).Condition Is expression AndAlso
TypeOf expression.Parent.Parent Is MultiLineIfBlockSyntax Then
ifStatementNode = expression.Parent.Parent
Return True
End If
If TypeOf expression.Parent Is ElseIfStatementSyntax AndAlso
DirectCast(expression.Parent, ElseIfStatementSyntax).Condition Is expression AndAlso
TypeOf expression.Parent.Parent Is ElseIfBlockSyntax Then
ifStatementNode = expression.Parent.Parent
Return True
End If
ifStatementNode = Nothing
Return False
End Function End Function
Protected Overrides Function HasElseClauses(ifStatementNode As SyntaxNode) As Boolean Protected Overrides Function HasElseClauses(ifStatementNode As SyntaxNode) As Boolean
If TypeOf ifStatementNode Is MultiLineIfBlockSyntax Then Return Helpers.GetElseClauses(ifStatementNode).Count > 0
Dim ifBlock = DirectCast(ifStatementNode, MultiLineIfBlockSyntax)
Return ifBlock.ElseIfBlocks.Count > 0 OrElse ifBlock.ElseBlock IsNot Nothing
End If
Return True
End Function End Function
Protected Overrides Function SplitIfStatementIntoElseClause(ifStatementNode As SyntaxNode, Protected Overrides Function SplitIfStatementIntoElseClause(ifStatementNode As SyntaxNode,
......
...@@ -16,22 +16,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.SplitOrMergeIfStatements ...@@ -16,22 +16,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.SplitOrMergeIfStatements
Protected Overrides ReadOnly Property LogicalAndSyntaxKind As Integer = SyntaxKind.AndAlsoExpression Protected Overrides ReadOnly Property LogicalAndSyntaxKind As Integer = SyntaxKind.AndAlsoExpression
Protected Overrides Function IsConditionOfIfStatement(expression As SyntaxNode, ByRef ifStatementNode As SyntaxNode) As Boolean Protected Overrides Function IsConditionOfIfStatement(expression As SyntaxNode, ByRef ifStatementNode As SyntaxNode) As Boolean
If TypeOf expression.Parent Is IfStatementSyntax AndAlso Return Helpers.IsConditionOfIfStatement(expression, ifStatementNode)
DirectCast(expression.Parent, IfStatementSyntax).Condition Is expression AndAlso
TypeOf expression.Parent.Parent Is MultiLineIfBlockSyntax Then
ifStatementNode = expression.Parent.Parent
Return True
End If
If TypeOf expression.Parent Is ElseIfStatementSyntax AndAlso
DirectCast(expression.Parent, ElseIfStatementSyntax).Condition Is expression AndAlso
TypeOf expression.Parent.Parent Is ElseIfBlockSyntax Then
ifStatementNode = expression.Parent.Parent
Return True
End If
ifStatementNode = Nothing
Return False
End Function End Function
Protected Overrides Function SplitIfStatement(ifStatementNode As SyntaxNode, Protected Overrides Function SplitIfStatement(ifStatementNode As SyntaxNode,
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册