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 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
|
// Copyright (c) 2013 Daniel Grunwald
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using System;
using System.Collections.Generic;
using System.Linq;
using ICSharpCode.NRefactory.CSharp.Resolver;
using ICSharpCode.NRefactory.Semantics;
using ICSharpCode.NRefactory.TypeSystem;
namespace ICSharpCode.NRefactory.CSharp.Refactoring
{
/// <summary>
/// 1) When a type cannot be resolved, offers to add a using declaration
/// or to replace it with the fully qualified type name.
/// 2) When an extension method cannot be resolved, offers to add a using declaration.
/// 3) When the caret is on a namespace name, offers to add a using declaration
/// and simplify the type references to use the new shorter option.
/// </summary>
[ContextAction ("Add using", Description = "Add missing using declaration.")]
public class AddUsingAction : ICodeActionProvider
{
public IEnumerable<CodeAction> GetActions(RefactoringContext context)
{
AstNode node = context.GetNode();
if (node is Identifier)
node = node.Parent;
if (node is SimpleType || node is IdentifierExpression) {
return GetActionsForType(context, node)
.Concat(GetActionsForAddNamespaceUsing(context, node));
} else if (node is MemberReferenceExpression && node.Parent is InvocationExpression) {
return GetActionsForExtensionMethodInvocation(context, (InvocationExpression)node.Parent);
} else if (node is MemberReferenceExpression) {
return GetActionsForAddNamespaceUsing(context, node);
} else {
return EmptyList<CodeAction>.Instance;
}
}
IEnumerable<CodeAction> GetActionsForType(RefactoringContext context, AstNode node)
{
var rr = context.Resolve(node) as UnknownIdentifierResolveResult;
if (rr == null)
return EmptyList<CodeAction>.Instance;
string identifier = rr.Identifier;
int tc = rr.TypeArgumentCount;
string attributeIdentifier = null;
if (node.Parent is Attribute)
attributeIdentifier = identifier + "Attribute";
var lookup = new MemberLookup(null, context.Compilation.MainAssembly);
List<CodeAction> actions = new List<CodeAction>();
foreach (var typeDefinition in context.Compilation.GetAllTypeDefinitions()) {
if ((typeDefinition.Name == identifier || typeDefinition.Name == attributeIdentifier)
&& typeDefinition.TypeParameterCount == tc
&& lookup.IsAccessible(typeDefinition, false))
{
if (typeDefinition.DeclaringTypeDefinition == null) {
actions.Add(NewUsingAction(context, node, typeDefinition.Namespace));
}
actions.Add(ReplaceWithFullTypeNameAction(context, node, typeDefinition));
}
}
return actions;
}
CodeAction NewUsingAction(RefactoringContext context, AstNode node, string ns)
{
return new CodeAction("using " + ns + ";", s => UsingHelper.InsertUsingAndRemoveRedundantNamespaceUsage(context, s, ns), node);
}
CodeAction ReplaceWithFullTypeNameAction(RefactoringContext context, AstNode node, ITypeDefinition typeDefinition)
{
AstType astType = context.CreateShortType(typeDefinition);
string textWithoutGenerics = astType.ToString();
foreach (var typeArg in node.GetChildrenByRole(Roles.TypeArgument)) {
astType.AddChild(typeArg.Clone(), Roles.TypeArgument);
}
return new CodeAction(textWithoutGenerics, s => s.Replace(node, astType), node);
}
IEnumerable<CodeAction> GetActionsForExtensionMethodInvocation(RefactoringContext context, InvocationExpression invocation)
{
var rr = context.Resolve(invocation) as UnknownMethodResolveResult;
if (rr == null)
return EmptyList<CodeAction>.Instance;
var lookup = new MemberLookup(null, context.Compilation.MainAssembly);
HashSet<string> namespaces = new HashSet<string>();
List<CodeAction> result = new List<CodeAction>();
foreach (var typeDefinition in context.Compilation.GetAllTypeDefinitions()) {
if (!(typeDefinition.HasExtensionMethods && lookup.IsAccessible(typeDefinition, false))) {
continue;
}
foreach (var method in typeDefinition.Methods.Where(m => m.IsExtensionMethod && m.Name == rr.MemberName)) {
IType[] inferredTypes;
if (CSharpResolver.IsEligibleExtensionMethod(rr.TargetType, method, true, out inferredTypes)) {
// avoid offering the same namespace twice
if (namespaces.Add(typeDefinition.Namespace)) {
result.Add(NewUsingAction(context, invocation, typeDefinition.Namespace));
}
break; // continue with the next type
}
}
}
return result;
}
IEnumerable<CodeAction> GetActionsForAddNamespaceUsing(RefactoringContext context, AstNode node)
{
var nrr = context.Resolve(node) as NamespaceResolveResult;
if (nrr == null)
return EmptyList<CodeAction>.Instance;
var trr = context.Resolve(node.Parent) as TypeResolveResult;
if (trr == null)
return EmptyList<CodeAction>.Instance;
ITypeDefinition typeDef = trr.Type.GetDefinition();
if (typeDef == null)
return EmptyList<CodeAction>.Instance;
IList<IType> typeArguments;
ParameterizedType parameterizedType = trr.Type as ParameterizedType;
if (parameterizedType != null)
typeArguments = parameterizedType.TypeArguments;
else
typeArguments = EmptyList<IType>.Instance;
var resolver = context.GetResolverStateBefore(node.Parent);
if (resolver.ResolveSimpleName(typeDef.Name, typeArguments) is UnknownIdentifierResolveResult) {
// It's possible to remove the explicit namespace usage and introduce a using instead
return new[] { NewUsingAction(context, node, typeDef.Namespace) };
}
return EmptyList<CodeAction>.Instance;
}
}
}
|