本文整理汇总了C#中RefactoringContext.GetNode方法的典型用法代码示例。如果您正苦于以下问题:C# RefactoringContext.GetNode方法的具体用法?C# RefactoringContext.GetNode怎么用?C# RefactoringContext.GetNode使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类RefactoringContext
的用法示例。
在下文中一共展示了RefactoringContext.GetNode方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetActions
public override IEnumerable<CodeAction> GetActions(RefactoringContext context)
{
var variableDeclaration = context.GetNode<VariableDeclarationStatement>();
if (variableDeclaration == null)
yield break;
var entryNode = FindCurrentScopeEntryNode(variableDeclaration);
if (entryNode == null)
yield break;
var selectedInitializer = context.GetNode<VariableInitializer>();
if (selectedInitializer != null) {
if (!selectedInitializer.NameToken.Contains(context.Location))
yield break;
if (HasDependency(context, entryNode, selectedInitializer)) {
yield return MoveDeclarationAction(context, entryNode, variableDeclaration, selectedInitializer);
} else {
yield return MoveInitializerAction(context, entryNode, variableDeclaration, selectedInitializer);
}
} else {
if (!variableDeclaration.Type.Contains(context.Location) || variableDeclaration.Variables.Count <= 1)
yield break;
yield return new CodeAction(context.TranslateString("Move declaration to outer scope"), script => {
script.Remove(variableDeclaration);
script.InsertBefore(entryNode, variableDeclaration.Clone());
}, variableDeclaration);
}
}
示例2: GetActions
public IEnumerable<CodeAction> GetActions(RefactoringContext context)
{
if (!context.IsSomethingSelected) {
yield break;
}
var pexpr = context.GetNode<PrimitiveExpression>();
if (pexpr == null || !(pexpr.Value is string)) {
yield break;
}
if (pexpr.LiteralValue.StartsWith("@", StringComparison.Ordinal)) {
if (!(pexpr.StartLocation < new TextLocation(context.Location.Line, context.Location.Column - 1) && new TextLocation(context.Location.Line, context.Location.Column + 1) < pexpr.EndLocation)) {
yield break;
}
} else {
if (!(pexpr.StartLocation < context.Location && context.Location < pexpr.EndLocation)) {
yield break;
}
}
yield return new CodeAction (context.TranslateString("Introduce format item"), script => {
var invocation = context.GetNode<InvocationExpression>();
if (invocation != null && invocation.Target.IsMatch(PrototypeFormatReference)) {
AddFormatCallToInvocation(context, script, pexpr, invocation);
return;
}
var arg = CreateFormatArgument(context);
var newInvocation = new InvocationExpression (PrototypeFormatReference.Clone()) {
Arguments = { CreateFormatString(context, pexpr, 0), arg }
};
script.Replace(pexpr, newInvocation);
script.Select(arg);
});
}
示例3: GetActions
public IEnumerable<CodeAction> GetActions(RefactoringContext context)
{
var createExpression = context.GetNode<ObjectCreateExpression>();
if (createExpression != null)
return GetActions(context, createExpression);
var simpleType = context.GetNode<SimpleType>();
if (simpleType != null && !(simpleType.Parent is EventDeclaration || simpleType.Parent is CustomEventDeclaration))
return GetActions(context, simpleType);
return Enumerable.Empty<CodeAction>();
}
示例4: GetActions
public IEnumerable<CodeAction> GetActions(RefactoringContext context)
{
var pexpr = context.GetNode<PrimitiveExpression>();
if (pexpr == null)
yield break;
var statement = context.GetNode<Statement>();
if (statement == null) {
yield break;
}
var resolveResult = context.Resolve(pexpr);
yield return new CodeAction(context.TranslateString("Create local constant"), script => {
string name = CreateMethodDeclarationAction.CreateBaseName(pexpr, resolveResult.Type);
var service = (NamingConventionService)context.GetService(typeof(NamingConventionService));
if (service != null)
name = service.CheckName(context, name, AffectedEntity.LocalConstant);
var initializer = new VariableInitializer(name, pexpr.Clone());
var decl = new VariableDeclarationStatement() {
Type = context.CreateShortType(resolveResult.Type),
Modifiers = Modifiers.Const,
Variables = { initializer }
};
script.InsertBefore(statement, decl);
var variableUsage = new IdentifierExpression(name);
script.Replace(pexpr, variableUsage);
script.Link(initializer.NameToken, variableUsage);
});
yield return new CodeAction(context.TranslateString("Create constant field"), script => {
string name = CreateMethodDeclarationAction.CreateBaseName(pexpr, resolveResult.Type);
var service = (NamingConventionService)context.GetService(typeof(NamingConventionService));
if (service != null)
name = service.CheckName(context, name, AffectedEntity.ConstantField);
var initializer = new VariableInitializer(name, pexpr.Clone());
var decl = new FieldDeclaration() {
ReturnType = context.CreateShortType(resolveResult.Type),
Modifiers = Modifiers.Const,
Variables = { initializer }
};
var variableUsage = new IdentifierExpression(name);
script.Replace(pexpr, variableUsage);
// script.Link(initializer.NameToken, variableUsage);
script.InsertWithCursor(context.TranslateString("Create constant"), Script.InsertPosition.Before, decl);
});
}
示例5: GetActions
public override IEnumerable<CodeAction> GetActions(RefactoringContext context)
{
var identifier = context.GetNode<IdentifierExpression>();
if (identifier != null && !(identifier.Parent is InvocationExpression && ((InvocationExpression)identifier.Parent).Target == identifier))
return GetActionsFromIdentifier(context, identifier);
var memberReference = context.GetNode<MemberReferenceExpression>();
if (memberReference != null && !(memberReference.Parent is InvocationExpression && ((InvocationExpression)memberReference.Parent).Target == memberReference))
return GetActionsFromMemberReferenceExpression(context, memberReference);
var invocation = context.GetNode<InvocationExpression>();
if (invocation != null)
return GetActionsFromInvocation(context, invocation);
return Enumerable.Empty<CodeAction>();
}
示例6: GetActions
public IEnumerable<CodeAction> GetActions(RefactoringContext context)
{
var initializer = context.GetNode<VariableInitializer>();
if (initializer != null) {
var action = HandleInitializer(context, initializer);
if (action != null)
yield return action;
}
var expressionStatement = context.GetNode<ExpressionStatement>();
if (expressionStatement != null) {
var action = HandleExpressionStatement(context, expressionStatement);
if (action != null)
yield return action;
}
}
示例7: 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);
}
});
}
示例8: GetIfElseStatement
static IfElseStatement GetIfElseStatement (RefactoringContext context)
{
var result = context.GetNode<IfElseStatement> ();
if (result != null && result.IfToken.Contains (context.Location))
return result;
return null;
}
示例9: GetActions
public override IEnumerable<CodeAction> GetActions(RefactoringContext context)
{
var node = context.GetNode<InvocationExpression>();
if (node == null)
yield break;
if ((node.Target is IdentifierExpression) && !node.Target.IsInside(context.Location))
yield break;
if ((node.Target is MemberReferenceExpression) && !((MemberReferenceExpression)node.Target).MemberNameToken.IsInside(context.Location))
yield break;
var rr = context.Resolve(node) as CSharpInvocationResolveResult;
if (rr == null || rr.IsError || rr.Member.Name != "Equals" || !rr.Member.DeclaringType.IsKnownType(KnownTypeCode.Object))
yield break;
Expression expr = node;
bool useEquality = true;
var uOp = node.Parent as UnaryOperatorExpression;
if (uOp != null && uOp.Operator == UnaryOperatorType.Not) {
expr = uOp;
useEquality = false;
}
yield return new CodeAction(
useEquality ? context.TranslateString("Use '=='") : context.TranslateString("Use '!='"),
script => {
script.Replace(
expr,
new BinaryOperatorExpression(
node.Arguments.First().Clone(),
useEquality ? BinaryOperatorType.Equality : BinaryOperatorType.InEquality,
node.Arguments.Last().Clone()
)
);
},
node.Target
);
}
示例10: GetActions
public override IEnumerable<CodeAction> GetActions(RefactoringContext context)
{
var entity = context.GetNode<ConstructorDeclaration>();
if (entity == null)
yield break;
var type = entity.Parent as TypeDeclaration;
if (type == null || entity.Name == type.Name)
yield break;
var typeDeclaration = entity.GetParent<TypeDeclaration>();
yield return new CodeAction(context.TranslateString("This is a constructor"), script => script.Replace(entity.NameToken, Identifier.Create(typeDeclaration.Name, TextLocation.Empty)), entity) {
Severity = ICSharpCode.NRefactory.Refactoring.Severity.Error
};
yield return new CodeAction(context.TranslateString("This is a void method"), script => {
var generatedMethod = new MethodDeclaration();
generatedMethod.Modifiers = entity.Modifiers;
generatedMethod.ReturnType = new PrimitiveType("void");
generatedMethod.Name = entity.Name;
generatedMethod.Parameters.AddRange(entity.Parameters.Select(parameter => (ParameterDeclaration)parameter.Clone()));
generatedMethod.Body = (BlockStatement)entity.Body.Clone();
generatedMethod.Attributes.AddRange(entity.Attributes.Select(attribute => (AttributeSection)attribute.Clone()));
script.Replace(entity, generatedMethod);
}, entity) {
Severity = ICSharpCode.NRefactory.Refactoring.Severity.Error
};
}
示例11: 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);
}
示例12: GetForeachStatement
static ForeachStatement GetForeachStatement (RefactoringContext context)
{
var result = context.GetNode<ForeachStatement> ();
if (result != null && result.VariableType.Contains (context.Location) && !result.VariableType.IsVar ())
return result;
return null;
}
示例13: GetSwitchStatement
static SwitchStatement GetSwitchStatement (RefactoringContext context)
{
var switchStatment = context.GetNode<SwitchStatement> ();
if (switchStatment != null && switchStatment.SwitchSections.Count == 0)
return switchStatment;
return null;
}
示例14: GetDirective
static PreProcessorDirective GetDirective(RefactoringContext context)
{
var directive = context.GetNode<PreProcessorDirective> ();
if (directive == null || directive.Type != PreProcessorDirectiveType.Region)
return null;
return directive;
}
示例15: GetActions
public IEnumerable<CodeAction> GetActions(RefactoringContext context)
{
var initializer = context.GetNode<VariableInitializer>();
if (initializer == null || !initializer.NameToken.Contains(context.Location.Line, context.Location.Column)) {
yield break;
}
var type = initializer.Parent.Parent as TypeDeclaration;
if (type == null) {
yield break;
}
foreach (var member in type.Members) {
if (member is PropertyDeclaration && ContainsGetter((PropertyDeclaration)member, initializer)) {
yield break;
}
}
var field = initializer.Parent as FieldDeclaration;
if (field == null || field.HasModifier(Modifiers.Readonly) || field.HasModifier(Modifiers.Const)) {
yield break;
}
var resolveResult = context.Resolve(initializer) as MemberResolveResult;
if (resolveResult == null)
yield break;
yield return new CodeAction(context.TranslateString("Create property"), script => {
var fieldName = context.GetNameProposal(initializer.Name, true);
if (initializer.Name == context.GetNameProposal(initializer.Name, false)) {
script.Rename(resolveResult.Member, fieldName);
}
script.InsertWithCursor(
context.TranslateString("Create property"),
Script.InsertPosition.After, GeneratePropertyDeclaration(context, field, fieldName));
});
}