本文整理汇总了C#中RefactoringContext.GetNameProposal方法的典型用法代码示例。如果您正苦于以下问题:C# RefactoringContext.GetNameProposal方法的具体用法?C# RefactoringContext.GetNameProposal怎么用?C# RefactoringContext.GetNameProposal使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类RefactoringContext
的用法示例。
在下文中一共展示了RefactoringContext.GetNameProposal方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: 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));
});
}
示例2: Run
public void Run (RefactoringContext context)
{
var property = context.GetNode<PropertyDeclaration> ();
string backingStoreName = context.GetNameProposal (property.Name);
// create field
var backingStore = new FieldDeclaration ();
backingStore.ReturnType = property.ReturnType.Clone ();
var initializer = new VariableInitializer (backingStoreName);
backingStore.Variables.Add (initializer);
// create new property & implement the get/set bodies
var newProperty = (PropertyDeclaration)property.Clone ();
var id1 = new IdentifierExpression (backingStoreName);
var id2 = new IdentifierExpression (backingStoreName);
newProperty.Getter.Body = new BlockStatement () {
new ReturnStatement (id1)
};
newProperty.Setter.Body = new BlockStatement () {
new ExpressionStatement (new AssignmentExpression (id2, AssignmentOperatorType.Assign, new IdentifierExpression ("value")))
};
using (var script = context.StartScript ()) {
script.Replace (property, newProperty);
script.InsertBefore (property, backingStore);
script.Link (initializer, id1, id2);
}
}
示例3: GetActions
public override IEnumerable<CodeAction> GetActions(RefactoringContext context)
{
var property = context.GetNode<PropertyDeclaration> ();
if (property == null || !property.NameToken.Contains(context.Location))
yield break;
if (!IsNotImplemented (context, property.Getter.Body) ||
!IsNotImplemented (context, property.Setter.Body)) {
yield break;
}
yield return new CodeAction(context.TranslateString("Implement property"), script => {
string backingStoreName = context.GetNameProposal (property.Name);
// create field
var backingStore = new FieldDeclaration ();
if (property.Modifiers.HasFlag (Modifiers.Static))
backingStore.Modifiers |= Modifiers.Static;
if (property.Setter.IsNull)
backingStore.Modifiers |= Modifiers.Readonly;
backingStore.ReturnType = property.ReturnType.Clone ();
var initializer = new VariableInitializer (backingStoreName);
backingStore.Variables.Add (initializer);
// create new property & implement the get/set bodies
var newProperty = (PropertyDeclaration)property.Clone ();
Expression id1;
if (backingStoreName == "value")
id1 = new ThisReferenceExpression().Member("value");
else
id1 = new IdentifierExpression (backingStoreName);
Expression id2 = id1.Clone();
newProperty.Getter.Body = new BlockStatement () {
new ReturnStatement (id1)
};
if (!property.Setter.IsNull) {
newProperty.Setter.Body = new BlockStatement () {
new AssignmentExpression (id2, AssignmentOperatorType.Assign, new IdentifierExpression ("value"))
};
}
script.Replace (property, newProperty);
script.InsertBefore (property, backingStore);
if (!property.Setter.IsNull)
script.Link (initializer, id1, id2);
else
script.Link (initializer, id1);
}, property.NameToken);
}
示例4: GeneratePropertyDeclaration
static PropertyDeclaration GeneratePropertyDeclaration (RefactoringContext context, FieldDeclaration field, VariableInitializer initializer)
{
var mod = ICSharpCode.NRefactory.CSharp.Modifiers.Public;
if (field.HasModifier (ICSharpCode.NRefactory.CSharp.Modifiers.Static))
mod |= ICSharpCode.NRefactory.CSharp.Modifiers.Static;
return new PropertyDeclaration () {
Modifiers = mod,
Name = context.GetNameProposal (initializer.Name, false),
ReturnType = field.ReturnType.Clone (),
Getter = new Accessor () {
Body = new BlockStatement () {
new ReturnStatement (new IdentifierExpression (initializer.Name))
}
}
};
}
示例5: GetActions
public IEnumerable<CodeAction> GetActions(RefactoringContext context)
{
var property = context.GetNode<PropertyDeclaration>();
if (!(property != null &&
!property.Getter.IsNull && !property.Setter.IsNull && // automatic properties always need getter & setter
property.Getter.Body.IsNull &&
property.Setter.Body.IsNull)) {
yield break;
}
yield return new CodeAction(context.TranslateString("Create backing store"), script => {
string backingStoreName = context.GetNameProposal (property.Name);
// create field
var backingStore = new FieldDeclaration ();
if (property.Modifiers.HasFlag (Modifiers.Static))
backingStore.Modifiers |= Modifiers.Static;
backingStore.ReturnType = property.ReturnType.Clone ();
var initializer = new VariableInitializer (backingStoreName);
backingStore.Variables.Add (initializer);
// create new property & implement the get/set bodies
var newProperty = (PropertyDeclaration)property.Clone ();
Expression id1;
if (backingStoreName == "value")
id1 = new ThisReferenceExpression().Member("value");
else
id1 = new IdentifierExpression (backingStoreName);
Expression id2 = id1.Clone();
newProperty.Getter.Body = new BlockStatement () {
new ReturnStatement (id1)
};
newProperty.Setter.Body = new BlockStatement () {
new AssignmentExpression (id2, AssignmentOperatorType.Assign, new IdentifierExpression ("value"))
};
script.Replace (property, newProperty);
script.InsertBefore (property, backingStore);
script.Link (initializer, id1, id2);
});
}
示例6: GeneratePropertyDeclaration
static PropertyDeclaration GeneratePropertyDeclaration (RefactoringContext context, FieldDeclaration field, string fieldName)
{
var mod = ICSharpCode.NRefactory.CSharp.Modifiers.Public;
if (field.HasModifier (ICSharpCode.NRefactory.CSharp.Modifiers.Static))
mod |= ICSharpCode.NRefactory.CSharp.Modifiers.Static;
return new PropertyDeclaration () {
Modifiers = mod,
Name = context.GetNameProposal (fieldName, false),
ReturnType = field.ReturnType.Clone (),
Getter = new Accessor () {
Body = new BlockStatement () {
new ReturnStatement (new IdentifierExpression (fieldName))
}
},
Setter = new Accessor () {
Body = new BlockStatement () {
new ExpressionStatement (new AssignmentExpression (new IdentifierExpression (fieldName), new IdentifierExpression ("value")))
}
}
};
}
示例7: GetActions
public override IEnumerable<CodeAction> GetActions(RefactoringContext context)
{
Expression node = context.GetNode<IdentifierExpression>();
if (node == null) {
var mr = context.GetNode<MemberReferenceExpression>();
if (mr == null || !mr.MemberNameToken.IsInside(context.Location))
yield break;
node = mr;
}
if (node == null)
yield break;
var rr = context.Resolve(node) as MethodGroupResolveResult;
if (rr == null || rr.IsError)
yield break;
var type = TypeGuessing.GetValidTypes(context.Resolver, node).FirstOrDefault(t => t.Kind == TypeKind.Delegate);
if (type == null)
yield break;
var invocationMethod = type.GetDelegateInvokeMethod();
if (invocationMethod == null)
yield break;
yield return new CodeAction(
context.TranslateString("Convert to lambda expression"),
script => {
var invocation = new InvocationExpression(node.Clone(), invocationMethod.Parameters.Select(p => new IdentifierExpression(context.GetNameProposal(p.Name))));
var lambda = new LambdaExpression {
Body = invocation
};
lambda.Parameters.AddRange(
invocation.Arguments
.Cast<IdentifierExpression>()
.Select(p => new ParameterDeclaration { Name = p.Identifier })
);
script.Replace(node, lambda);
},
node
);
}