VisualBasicInvertIfCodeRefactoringProvider.SingleLine.vb 3.8 KB
Newer Older
A
Alireza Habibi 已提交
1 2 3 4 5 6 7
' 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.Composition
Imports Microsoft.CodeAnalysis.CodeRefactorings
Imports Microsoft.CodeAnalysis.Text
Imports Microsoft.CodeAnalysis.VisualBasic.Syntax

C
Cyrus Najmabadi 已提交
8
Namespace Microsoft.CodeAnalysis.VisualBasic.InvertIf
A
Alireza Habibi 已提交
9
    <ExportCodeRefactoringProvider(LanguageNames.VisualBasic, Name:=PredefinedCodeRefactoringProviderNames.InvertIf), [Shared]>
A
Alireza Habibi 已提交
10
    Friend NotInheritable Class VisualBasicInvertSingleLineIfCodeRefactoringProvider
A
Alireza Habibi 已提交
11 12
        Inherits VisualBasicInvertIfCodeRefactoringProvider(Of SingleLineIfStatementSyntax)

13 14 15 16
        <ImportingConstructor>
        Public Sub New()
        End Sub

A
Alireza Habibi 已提交
17
        Protected Overrides Function IsElseless(ifNode As SingleLineIfStatementSyntax) As Boolean
A
Alireza Habibi 已提交
18
            Return ifNode.ElseClause Is Nothing
A
Alireza Habibi 已提交
19 20
        End Function

A
Alireza Habibi 已提交
21
        Protected Overrides Function CanInvert(ifNode As SingleLineIfStatementSyntax) As Boolean
A
Alireza Habibi 已提交
22 23 24
            Return TypeOf ifNode.Parent IsNot SingleLineLambdaExpressionSyntax AndAlso
                Not ifNode.Statements.Any(Function(n) n.IsKind(SyntaxKind.LocalDeclarationStatement)) AndAlso
                Not If(ifNode.ElseClause?.Statements.Any(Function(n) n.IsKind(SyntaxKind.LocalDeclarationStatement)), False)
A
Alireza Habibi 已提交
25
        End Function
A
Alireza Habibi 已提交
26

A
Alireza Habibi 已提交
27
        Protected Overrides Function GetCondition(ifNode As SingleLineIfStatementSyntax) As SyntaxNode
A
Alireza Habibi 已提交
28
            Return ifNode.Condition
A
Alireza Habibi 已提交
29
        End Function
A
Alireza Habibi 已提交
30

31
        Protected Overrides Function GetIfBody(ifNode As SingleLineIfStatementSyntax) As SyntaxList(Of StatementSyntax)
A
Alireza Habibi 已提交
32 33 34
            Return ifNode.Statements
        End Function

35
        Protected Overrides Function GetElseBody(ifNode As SingleLineIfStatementSyntax) As SyntaxList(Of StatementSyntax)
A
Alireza Habibi 已提交
36 37 38
            Return ifNode.ElseClause.Statements
        End Function

39 40 41 42
        Protected Overrides Function UpdateIf(
                sourceText As SourceText,
                ifNode As SingleLineIfStatementSyntax,
                condition As SyntaxNode,
43 44
                trueStatements As SyntaxList(Of StatementSyntax),
                Optional falseStatements As SyntaxList(Of StatementSyntax) = Nothing) As SingleLineIfStatementSyntax
45 46

            Dim isSingleLine = sourceText.AreOnSameLine(ifNode.GetFirstToken(), ifNode.GetLastToken())
47
            If isSingleLine AndAlso falseStatements.Count > 0 Then
48 49 50 51 52
                ' If statement Is on a single line, And we're swapping the true/false parts.
                ' In that case, try to swap the trailing trivia between the true/false parts.
                ' That way the trailing comments/newlines at the end of the 'if' stay there,
                ' And the spaces after the true-part stay where they are.

53 54
                Dim lastTrue = trueStatements.Last()
                Dim lastFalse = falseStatements.Last()
55 56 57 58

                Dim newLastTrue = lastTrue.WithTrailingTrivia(lastFalse.GetTrailingTrivia())
                Dim newLastFalse = lastFalse.WithTrailingTrivia(lastTrue.GetTrailingTrivia())

59 60
                trueStatements = trueStatements.Replace(lastTrue, newLastTrue)
                falseStatements = falseStatements.Replace(lastFalse, newLastFalse)
61 62
            End If

63 64 65
            Dim updatedIf = ifNode _
                .WithCondition(DirectCast(condition, ExpressionSyntax)) _
                .WithStatements(trueStatements)
A
Alireza Habibi 已提交
66

67
            If falseStatements.Count <> 0 Then
68 69
                Dim elseClause =
                    If(updatedIf.ElseClause IsNot Nothing,
70 71
                       updatedIf.ElseClause.WithStatements(falseStatements),
                       SyntaxFactory.SingleLineElseClause(falseStatements))
72 73

                updatedIf = updatedIf.WithElseClause(elseClause)
A
Alireza Habibi 已提交
74 75 76 77
            End If

            Return updatedIf
        End Function
A
Alireza Habibi 已提交
78 79
    End Class
End Namespace
A
Alireza Habibi 已提交
80