本文整理汇总了C#中RefactoringContext类的典型用法代码示例。如果您正苦于以下问题:C# RefactoringContext类的具体用法?C# RefactoringContext怎么用?C# RefactoringContext使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
RefactoringContext类属于命名空间,在下文中一共展示了RefactoringContext类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetActions
public IEnumerable<CodeAction> GetActions(RefactoringContext context)
{
var expr = GetCreatePropertyOrFieldNode(context);
if (expr == null)
yield break;
if (!(expr is MemberReferenceExpression))
yield break;
var propertyName = CreatePropertyAction.GetPropertyName(expr);
if (propertyName == null)
yield break;
if (IsInvocationTarget(expr))
yield break;
var statement = expr.GetParent<Statement>();
if (statement == null)
yield break;
if (!(context.Resolve(expr).IsError))
yield break;
var guessedType = CreateFieldAction.GuessType(context, expr);
if (guessedType == null || guessedType.Kind != TypeKind.Enum)
yield break;
var state = context.GetResolverStateBefore(expr);
if (state.CurrentMember == null || state.CurrentTypeDefinition == null)
yield break;
yield return new CodeAction(context.TranslateString("Create enum value"), script => {
var decl = new EnumMemberDeclaration {
Name = propertyName
};
script.InsertWithCursor(context.TranslateString("Create enum value"), guessedType.GetDefinition (), decl);
});
}
示例2: GetActions
public IEnumerable<CodeAction> GetActions(RefactoringContext context)
{
var type = context.GetNode<AstType>();
if (type == null || type.Role != Roles.BaseType)
yield break;
var state = context.GetResolverStateBefore(type);
if (state.CurrentTypeDefinition == null)
yield break;
var resolveResult = context.Resolve(type);
if (resolveResult.Type.Kind != TypeKind.Interface)
yield break;
var toImplement = ImplementInterfaceAction.CollectMembersToImplement(
state.CurrentTypeDefinition,
resolveResult.Type,
false
);
if (toImplement.Count == 0)
yield break;
yield return new CodeAction(context.TranslateString("Implement interface explicit"), script => {
script.InsertWithCursor(
context.TranslateString("Implement Interface"),
state.CurrentTypeDefinition,
ImplementInterfaceAction.GenerateImplementation (context, toImplement.Select (t => Tuple.Create (t.Item1, true)))
);
});
}
示例3: GetSwitchStatement
static SwitchStatement GetSwitchStatement (RefactoringContext context)
{
var switchStatment = context.GetNode<SwitchStatement> ();
if (switchStatment != null && switchStatment.SwitchSections.Count == 0)
return switchStatment;
return null;
}
示例4: CreateFromExpression
CodeAction CreateFromExpression(RefactoringContext context, Expression expression)
{
var resolveResult = context.Resolve(expression);
if (resolveResult.IsError)
return null;
return new CodeAction(context.TranslateString("Extract method"), script => {
string methodName = "NewMethod";
var method = new MethodDeclaration() {
ReturnType = context.CreateShortType(resolveResult.Type),
Name = methodName,
Body = new BlockStatement() {
new ReturnStatement(expression.Clone())
}
};
if (!StaticVisitor.UsesNotStaticMember(context, expression))
method.Modifiers |= Modifiers.Static;
var task = script.InsertWithCursor(context.TranslateString("Extract method"), Script.InsertPosition.Before, method);
Action<Task> replaceStatements = delegate {
var target = new IdentifierExpression(methodName);
script.Replace(expression, new InvocationExpression(target));
script.Link(target, method.NameToken);
};
if (task.IsCompleted) {
replaceStatements (null);
} else {
task.ContinueWith (replaceStatements, TaskScheduler.FromCurrentSynchronizationContext ());
}
});
}
示例5: GetVariableDeclarationStatement
static VariableDeclarationStatement GetVariableDeclarationStatement (RefactoringContext context)
{
var result = context.GetNode<VariableDeclarationStatement> ();
if (result != null && result.Variables.Count == 1 && !result.Variables.First ().Initializer.IsNull && result.Type.Contains (context.Location) && !result.Type.IsVar ())
return result;
return null;
}
示例6: GetActionsForAddNamespaceUsing
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;
}
示例7: GetActions
public IEnumerable<CodeAction> GetActions(RefactoringContext context)
{
if (context.IsSomethingSelected) {
yield break;
}
AstType type;
var varDecl = GetVariableDeclarationStatement(context, out type);
if (varDecl == null) {
yield break;
}
yield return new CodeAction(context.TranslateString("Split local variable declaration and assignment"), script => {
var assign = new AssignmentExpression (new IdentifierExpression (varDecl.Variables.First().Name), AssignmentOperatorType.Assign, varDecl.Variables.First().Initializer.Clone());
var newVarDecl = (VariableDeclarationStatement)varDecl.Clone();
newVarDecl.Role = BlockStatement.StatementRole;
if (newVarDecl.Type.IsMatch(new SimpleType ("var"))) {
newVarDecl.Type = type;
}
newVarDecl.Variables.First().Initializer = Expression.Null;
script.InsertBefore(varDecl, newVarDecl);
script.Replace(varDecl, varDecl.Parent is ForStatement ? (AstNode)assign : new ExpressionStatement (assign));
}, varDecl.Variables.First ().AssignToken);
}
示例8: GetAnonymousMethodExpression
static AnonymousMethodExpression GetAnonymousMethodExpression (RefactoringContext context, out ITypeDefinition delegateType)
{
delegateType = null;
var anonymousMethodExpression = context.GetNode<AnonymousMethodExpression> ();
if (anonymousMethodExpression == null || !anonymousMethodExpression.DelegateToken.Contains (context.Location.Line, context.Location.Column) || anonymousMethodExpression.HasParameterList)
return null;
AstType resolvedType = null;
var parent = anonymousMethodExpression.Parent;
if (parent is AssignmentExpression) {
resolvedType = context.ResolveType (((AssignmentExpression)parent).Left);
} else if (parent is VariableDeclarationStatement) {
resolvedType = context.ResolveType (((VariableDeclarationStatement)parent).Type);
} else if (parent is InvocationExpression) {
// TODO: handle invocations
}
if (resolvedType == null)
return null;
delegateType = context.GetDefinition (resolvedType);
if (delegateType == null || delegateType.ClassType != ClassType.Delegate)
return null;
return anonymousMethodExpression;
}
示例9: GetActions
public override IEnumerable<CodeAction> GetActions(RefactoringContext context)
{
var service = (CodeGenerationService)context.GetService(typeof(CodeGenerationService));
if (service == null)
yield break;
var type = context.GetNode<AstType>();
if (type == null || type.Role != Roles.BaseType)
yield break;
var state = context.GetResolverStateBefore(type);
if (state.CurrentTypeDefinition == null)
yield break;
var resolveResult = context.Resolve(type);
if (resolveResult.Type.Kind != TypeKind.Interface)
yield break;
bool interfaceMissing;
var toImplement = ImplementInterfaceAction.CollectMembersToImplement(
state.CurrentTypeDefinition,
resolveResult.Type,
false,
out interfaceMissing
);
if (toImplement.Count == 0)
yield break;
yield return new CodeAction(context.TranslateString("Implement interface explicit"), script =>
script.InsertWithCursor(
context.TranslateString("Implement Interface"),
state.CurrentTypeDefinition,
(s, c) => ImplementInterfaceAction.GenerateImplementation (c, toImplement.Select (t => Tuple.Create (t.Item1, true)), interfaceMissing).ToList()
)
, type);
}
示例10: GetActions
public override IEnumerable<CodeAction> GetActions(RefactoringContext context)
{
var property = context.GetNode<PropertyDeclaration>();
if (property == null || !property.NameToken.Contains(context.Location))
yield break;
var field = GetBackingField(context, property);
if (!IsValidField(field, property.GetParent<TypeDeclaration>())) {
yield break;
}
// create new auto property
var newProperty = (PropertyDeclaration)property.Clone();
newProperty.Getter.Body = BlockStatement.Null;
newProperty.Setter.Body = BlockStatement.Null;
yield return new CodeAction(context.TranslateString("Convert to auto property"), script => {
script.Rename((IEntity)field, newProperty.Name);
var oldField = context.RootNode.GetNodeAt<FieldDeclaration>(field.Region.Begin);
if (oldField.Variables.Count == 1) {
script.Remove(oldField);
} else {
var newField = (FieldDeclaration)oldField.Clone();
foreach (var init in newField.Variables) {
if (init.Name == field.Name) {
init.Remove();
break;
}
}
script.Replace(oldField, newField);
}
script.Replace (property, newProperty);
}, property.NameToken);
}
示例11: GetActions
public override IEnumerable<CodeAction> GetActions(RefactoringContext context)
{
if (context.IsSomethingSelected) {
yield break;
}
var node = context.GetNode<VariableDeclarationStatement>();
if (node == null || node.Variables.Count != 1) {
yield break;
}
var initializer = node.Variables.First();
if (!initializer.NameToken.Contains(context.Location) || initializer.Initializer.IsNull) {
yield break;
}
var resolveResult = context.Resolve(initializer) as LocalResolveResult;
if (resolveResult == null || resolveResult.IsError) {
yield break;
}
var unit = context.RootNode as SyntaxTree;
if (unit == null) {
yield break;
}
yield return new CodeAction(context.TranslateString("Inline local variable"), script => {
refFinder.FindLocalReferences(resolveResult.Variable, context.UnresolvedFile, unit, context.Compilation, (n, r) => script.Replace(n, AddParensIfRequired (n, initializer.Initializer.Clone())), default(CancellationToken));
script.Remove(node);
}, initializer);
}
示例12: GetActions
public IEnumerable<CodeAction> GetActions(RefactoringContext context)
{
var createExpression = context.GetNode<Expression>() as ObjectCreateExpression;
if (createExpression == null)
yield break;
var resolveResult = context.Resolve(createExpression) as CSharpInvocationResolveResult;
if (resolveResult == null || !resolveResult.IsError || resolveResult.Member.DeclaringTypeDefinition == null || resolveResult.Member.DeclaringTypeDefinition.IsSealed || resolveResult.Member.DeclaringTypeDefinition.Region.IsEmpty)
yield break;
yield return new CodeAction(context.TranslateString("Create constructor"), script => {
var decl = new ConstructorDeclaration() {
Name = resolveResult.Member.DeclaringTypeDefinition.Name,
Modifiers = Modifiers.Public,
Body = new BlockStatement() {
new ThrowStatement(new ObjectCreateExpression(context.CreateShortType("System", "NotImplementedException")))
}
};
decl.Parameters.AddRange(CreateMethodDeclarationAction.GenerateParameters(context, createExpression.Arguments));
script.InsertWithCursor(
context.TranslateString("Create constructor"),
resolveResult.Member.DeclaringTypeDefinition,
decl
);
}, createExpression);
}
示例13: GenerateImplementation
public static IEnumerable<AstNode> GenerateImplementation(RefactoringContext context, IEnumerable<Tuple<IMember, bool>> toImplement)
{
var nodes = new Dictionary<IType, List<AstNode>>();
foreach (var member in toImplement) {
if (!nodes.ContainsKey(member.Item1.DeclaringType))
nodes [member.Item1.DeclaringType] = new List<AstNode>();
nodes [member.Item1.DeclaringType].Add(GenerateMemberImplementation(context, member.Item1, member.Item2));
}
foreach (var kv in nodes) {
if (kv.Key.Kind == TypeKind.Interface) {
yield return new PreProcessorDirective(
PreProcessorDirectiveType.Region,
string.Format("{0} implementation", kv.Key.Name));
} else {
yield return new PreProcessorDirective(
PreProcessorDirectiveType.Region,
string.Format("implemented abstract members of {0}", kv.Key.Name));
}
foreach (var member in kv.Value)
yield return member;
yield return new PreProcessorDirective(
PreProcessorDirectiveType.Endregion
);
}
}
示例14: GetAnonymousMethodExpression
static AnonymousMethodExpression GetAnonymousMethodExpression (RefactoringContext context, out IType delegateType)
{
delegateType = null;
var anonymousMethodExpression = context.GetNode<AnonymousMethodExpression> ();
if (anonymousMethodExpression == null || !anonymousMethodExpression.DelegateToken.Contains (context.Location) || anonymousMethodExpression.HasParameterList)
return null;
IType resolvedType = null;
var parent = anonymousMethodExpression.Parent;
if (parent is AssignmentExpression) {
resolvedType = context.Resolve (((AssignmentExpression)parent).Left).Type;
} else if (parent is VariableInitializer) {
resolvedType = context.Resolve (((VariableDeclarationStatement)parent.Parent).Type).Type;
} else if (parent is InvocationExpression) {
// TODO: handle invocations
}
if (resolvedType == null)
return null;
delegateType = resolvedType;
if (delegateType.Kind != TypeKind.Delegate)
return null;
return anonymousMethodExpression;
}
示例15: ActionFromUsingStatement
CodeAction ActionFromUsingStatement(RefactoringContext context)
{
var initializer = context.GetNode<VariableInitializer>();
if (initializer == null)
return null;
var initializerRR = context.Resolve(initializer) as LocalResolveResult;
if (initializerRR == null)
return null;
var elementType = GetElementType(initializerRR, context);
if (elementType == null)
return null;
var usingStatement = initializer.Parent.Parent as UsingStatement;
if (usingStatement == null)
return null;
return new CodeAction(context.TranslateString("Iterate via foreach"), script => {
var iterator = MakeForeach(new IdentifierExpression(initializer.Name), elementType, context);
if (usingStatement.EmbeddedStatement is EmptyStatement) {
var blockStatement = new BlockStatement();
blockStatement.Statements.Add(iterator);
script.Replace(usingStatement.EmbeddedStatement, blockStatement);
script.FormatText(blockStatement);
} else if (usingStatement.EmbeddedStatement is BlockStatement) {
var anchorNode = usingStatement.EmbeddedStatement.FirstChild;
script.InsertAfter(anchorNode, iterator);
script.FormatText(usingStatement.EmbeddedStatement);
}
});
}