本文整理汇总了C#中RefactoringContext.GetService方法的典型用法代码示例。如果您正苦于以下问题:C# RefactoringContext.GetService方法的具体用法?C# RefactoringContext.GetService怎么用?C# RefactoringContext.GetService使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类RefactoringContext
的用法示例。
在下文中一共展示了RefactoringContext.GetService方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: 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);
});
}
示例2: 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);
}
示例3: 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.Class || resolveResult.Type.GetDefinition() == null || !resolveResult.Type.GetDefinition().IsAbstract)
yield break;
var toImplement = CollectMembersToImplement(state.CurrentTypeDefinition, resolveResult.Type);
if (toImplement.Count == 0)
yield break;
yield return new CodeAction(
context.TranslateString("Implement abstract members"),
script => script.InsertWithCursor(
context.TranslateString("Implement abstract members"),
state.CurrentTypeDefinition, (s, c) => ImplementInterfaceAction.GenerateImplementation(c, toImplement.Select(m => Tuple.Create(m, false)), true)
.Select(entity => {
var decl = entity as EntityDeclaration;
if (decl != null)
decl.Modifiers |= Modifiers.Override;
return entity;
}).ToList()), type);
}
示例4: GenerateImplementation
public static IEnumerable<AstNode> GenerateImplementation(RefactoringContext context, IEnumerable<Tuple<IMember, bool>> toImplement, bool generateRegion)
{
var service = (CodeGenerationService)context.GetService(typeof(CodeGenerationService));
if (service == null)
yield break;
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(service.GenerateMemberImplementation(context, member.Item1, member.Item2));
}
foreach (var kv in nodes) {
if (generateRegion) {
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;
if (generateRegion) {
yield return new PreProcessorDirective(
PreProcessorDirectiveType.Endregion
);
}
}
}
示例5: GetActions
static IEnumerable<CodeAction> GetActions(RefactoringContext context, AstNode node)
{
var resolveResult = context.Resolve(node) as UnknownIdentifierResolveResult;
if (resolveResult == null)
yield break;
var service = (NamingConventionService)context.GetService(typeof(NamingConventionService));
if (service != null && !service.IsValidName(resolveResult.Identifier, AffectedEntity.Class)) {
yield break;
}
yield return new CodeAction(context.TranslateString("Create class"), script => {
script.CreateNewType(CreateType(context, service, node));
});
yield return new CodeAction(context.TranslateString("Create nested class"), script => {
script.InsertWithCursor(context.TranslateString("Create nested class"), CreateType(context, service, node), Script.InsertPosition.Before);
});
}
示例6: GetActions
static IEnumerable<CodeAction> GetActions(RefactoringContext context, AstNode node)
{
var resolveResult = context.Resolve(node) as UnknownIdentifierResolveResult;
if (resolveResult == null)
yield break;
var service = (NamingConventionService)context.GetService(typeof(NamingConventionService));
if (service != null && !service.IsValidName(resolveResult.Identifier, AffectedEntity.Class)) {
yield break;
}
ClassType classType = GuessClassTypeByName(service, node);
ModifyClassTypeBasedOnTypeGuessing(context, node, ref classType);
string message;
switch (classType) {
case ClassType.Struct:
message = context.TranslateString("Create struct");
break;
case ClassType.Interface:
message = context.TranslateString("Create interface");
break;
default:
message = context.TranslateString("Create class");
break;
}
yield return new CodeAction(message, script => {
script.CreateNewType(CreateType(context, service, node, classType));
});
if (node.Parent is TypeDeclaration || classType != ClassType.Class)
yield break;
yield return new CodeAction(context.TranslateString("Create nested class"), script => {
script.InsertWithCursor(
context.TranslateString("Create nested class"),
Script.InsertPosition.Before,
CreateType(context, service, node, classType)
);
});
}
示例7: GetActionsFromIdentifier
IEnumerable<CodeAction> GetActionsFromIdentifier(RefactoringContext context, IdentifierExpression identifier)
{
if (!(context.Resolve(identifier).IsError))
yield break;
var methodName = identifier.Identifier;
var guessedType = TypeGuessing.GuessType(context, identifier);
if (guessedType.Kind != TypeKind.Delegate)
yield break;
var invocationMethod = guessedType.GetDelegateInvokeMethod();
if (invocationMethod == null)
yield break;
var state = context.GetResolverStateBefore(identifier);
if (state.CurrentMember == null || state.CurrentTypeDefinition == null)
yield break;
bool isStatic = state.CurrentMember.IsStatic || state.CurrentTypeDefinition.IsStatic;
var service = (NamingConventionService)context.GetService(typeof(NamingConventionService));
if (service != null && !service.IsValidName(methodName, AffectedEntity.Method, Modifiers.Private, isStatic))
yield break;
yield return CreateAction(
context,
identifier,
methodName,
context.CreateShortType(invocationMethod.ReturnType),
invocationMethod.Parameters.Select(parameter => new ParameterDeclaration(context.CreateShortType(parameter.Type), parameter.Name) {
ParameterModifier = GetModifiers(parameter)
}),
false,
isStatic,
null);
}
示例8: 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 visitor = new DeclareLocalVariableAction.SearchNodeVisitior(pexpr);
var node = context.GetNode <BlockStatement>();
if (node != null)
node.AcceptVisitor(visitor);
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);
});
if (visitor.Matches.Count > 1) {
yield return new CodeAction(string.Format(context.TranslateString("Create local constant (replace '{0}' occurrences)"), visitor.Matches.Count), 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 linkedNodes = new List<AstNode>();
linkedNodes.Add(initializer.NameToken);
for (int i = 0; i < visitor.Matches.Count; i++) {
var identifierExpression = new IdentifierExpression(name);
linkedNodes.Add(identifierExpression);
script.Replace(visitor.Matches [i], identifierExpression);
}
script.Link(linkedNodes.ToArray ());
});
yield return new CodeAction(string.Format(context.TranslateString("Create constant field (replace '{0}' occurrences)"), visitor.Matches.Count), 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 linkedNodes = new List<AstNode>();
linkedNodes.Add(initializer.NameToken);
for (int i = 0; i < visitor.Matches.Count; i++) {
//.........这里部分代码省略.........