/
AddUsingDirectiveRefactoring.cs
96 lines (79 loc) · 3.64 KB
/
AddUsingDirectiveRefactoring.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// Copyright (c) Josef Pihrt. All rights reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
namespace Roslynator.CSharp.Refactorings
{
internal static class AddUsingDirectiveRefactoring
{
public static async Task ComputeRefactoringsAsync(RefactoringContext context, IdentifierNameSyntax identifierName)
{
SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false);
INamespaceSymbol namespaceSymbol = null;
SyntaxNode node = identifierName;
SyntaxNode prevNode = null;
while (node.IsParentKind(
SyntaxKind.QualifiedName,
SyntaxKind.AliasQualifiedName,
SyntaxKind.SimpleMemberAccessExpression))
{
ISymbol symbol = semanticModel.GetSymbol(node, context.CancellationToken);
if (symbol?.Kind == SymbolKind.Namespace)
{
namespaceSymbol = (INamespaceSymbol)symbol;
prevNode = node;
node = node.Parent;
}
else
{
break;
}
}
node = prevNode;
if (node.IsParentKind(SyntaxKind.QualifiedName, SyntaxKind.AliasQualifiedName, SyntaxKind.SimpleMemberAccessExpression)
&& !node.IsDescendantOf(SyntaxKind.UsingDirective)
&& !CSharpUtility.IsNamespaceInScope(node, namespaceSymbol, semanticModel, context.CancellationToken))
{
context.RegisterRefactoring(
$"using {namespaceSymbol};",
cancellationToken => RefactorAsync(context.Document, node, namespaceSymbol, cancellationToken),
RefactoringIdentifiers.AddUsingDirective);
}
}
private static async Task<Document> RefactorAsync(
Document document,
SyntaxNode node,
INamespaceSymbol namespaceSymbol,
CancellationToken cancellationToken = default(CancellationToken))
{
SyntaxNode root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
UsingDirectiveSyntax usingDirective = UsingDirective(ParseName(namespaceSymbol.ToString()));
CompilationUnitSyntax newRoot = ((CompilationUnitSyntax)root)
.ReplaceNode(node.Parent, GetNewNode(node))
.AddUsings(keepSingleLineCommentsOnTop: true, usings: usingDirective);
return document.WithSyntaxRoot(newRoot);
}
private static SyntaxNode GetNewNode(SyntaxNode node)
{
switch (node.Parent.Kind())
{
case SyntaxKind.QualifiedName:
{
var qualifiedName = (QualifiedNameSyntax)node.Parent;
return qualifiedName.Right.WithLeadingTrivia(node.GetLeadingTrivia());
}
case SyntaxKind.SimpleMemberAccessExpression:
{
var memberAccess = (MemberAccessExpressionSyntax)node.Parent;
return memberAccess.Name.WithLeadingTrivia(node.GetLeadingTrivia());
}
}
Debug.Fail(node.Parent?.Kind().ToString());
return node;
}
}
}