forked from dotnet/roslynator
/
InlineUsingStaticDirectiveRefactoring.cs
95 lines (78 loc) · 3.67 KB
/
InlineUsingStaticDirectiveRefactoring.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
// Copyright (c) Josef Pihrt and Contributors. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using static Roslynator.CSharp.CSharpFactory;
namespace Roslynator.CSharp.Refactorings
{
internal static class InlineUsingStaticDirectiveRefactoring
{
public static async Task<Document> RefactorAsync(
Document document,
UsingDirectiveSyntax usingDirective,
CancellationToken cancellationToken)
{
SemanticModel semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);
var classSymbol = (INamedTypeSymbol)semanticModel.GetSymbol(usingDirective.Name, cancellationToken);
SyntaxNode parent = usingDirective.Parent;
Debug.Assert(parent.IsKind(SyntaxKind.CompilationUnit, SyntaxKind.NamespaceDeclaration));
int index = SyntaxInfo.UsingDirectiveListInfo(parent).IndexOf(usingDirective);
List<SimpleNameSyntax> names = CollectNames(parent, classSymbol, semanticModel, cancellationToken);
TypeSyntax type = classSymbol.ToTypeSyntax();
SyntaxNode newNode = parent.ReplaceNodes(
names,
(node, _) =>
{
return SimpleMemberAccessExpression(type, node.WithoutTrivia())
.WithTriviaFrom(node)
.WithSimplifierAnnotation();
});
newNode = RemoveUsingDirective(newNode, index);
return await document.ReplaceNodeAsync(parent, newNode, cancellationToken).ConfigureAwait(false);
}
private static List<SimpleNameSyntax> CollectNames(
SyntaxNode node,
INamedTypeSymbol classSymbol,
SemanticModel semanticModel,
CancellationToken cancellationToken)
{
var names = new List<SimpleNameSyntax>();
foreach (SyntaxNode descendant in node.DescendantNodes())
{
if ((!descendant.IsParentKind(SyntaxKind.SimpleMemberAccessExpression)
|| ((MemberAccessExpressionSyntax)descendant.Parent).Name != descendant)
&& (descendant is SimpleNameSyntax name))
{
ISymbol symbol = semanticModel.GetSymbol(name, cancellationToken);
if (symbol.IsKind(SymbolKind.Event, SymbolKind.Field, SymbolKind.Method, SymbolKind.Property)
&& SymbolEqualityComparer.Default.Equals(symbol.ContainingType, classSymbol))
{
names.Add(name);
}
}
}
return names;
}
private static SyntaxNode RemoveUsingDirective(SyntaxNode node, int index)
{
switch (node)
{
case CompilationUnitSyntax compilationUnit:
{
UsingDirectiveSyntax usingDirective = compilationUnit.Usings[index];
return compilationUnit.RemoveNode(usingDirective);
}
case NamespaceDeclarationSyntax namespaceDeclaration:
{
UsingDirectiveSyntax usingDirective = namespaceDeclaration.Usings[index];
return namespaceDeclaration.RemoveNode(usingDirective);
}
}
return node;
}
}
}