|
|
|
@ -26,6 +26,8 @@
@@ -26,6 +26,8 @@
|
|
|
|
|
using System; |
|
|
|
|
using System.Collections.Generic; |
|
|
|
|
using System.Linq; |
|
|
|
|
using ICSharpCode.NRefactory.Semantics; |
|
|
|
|
using ICSharpCode.NRefactory.TypeSystem; |
|
|
|
|
|
|
|
|
|
namespace ICSharpCode.NRefactory.CSharp.Refactoring |
|
|
|
|
{ |
|
|
|
@ -54,7 +56,35 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
@@ -54,7 +56,35 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
|
|
|
|
|
rules = new List<NamingRule> (ctx.RequestData<IEnumerable<NamingRule>> () ?? Enumerable.Empty<NamingRule> ()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void CheckName(AffectedEntity entity, Identifier identifier, Modifiers accessibilty) |
|
|
|
|
void CheckName(AstNode node, AffectedEntity entity, Identifier identifier, Modifiers accessibilty) |
|
|
|
|
{ |
|
|
|
|
ResolveResult resolveResult = null; |
|
|
|
|
if (node != null) { |
|
|
|
|
resolveResult = ctx.Resolve(node); |
|
|
|
|
} |
|
|
|
|
CheckNamedResolveResult(resolveResult, entity, identifier, accessibilty); |
|
|
|
|
if (resolveResult is TypeResolveResult) { |
|
|
|
|
var type = ((TypeResolveResult)resolveResult).Type; |
|
|
|
|
if (type.DirectBaseTypes.Any(t => t.FullName == "System.Attribute")) { |
|
|
|
|
CheckNamedResolveResult(resolveResult, AffectedEntity.CustomAttributes, identifier, accessibilty); |
|
|
|
|
} else if (type.DirectBaseTypes.Any(t => t.FullName == "System.EventArgs")) { |
|
|
|
|
CheckNamedResolveResult(resolveResult, AffectedEntity.CustomEventArgs, identifier, accessibilty); |
|
|
|
|
} else if (type.DirectBaseTypes.Any(t => t.FullName == "System.Exception")) { |
|
|
|
|
CheckNamedResolveResult(resolveResult, AffectedEntity.CustomExceptions, identifier, accessibilty); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (type.GetDefinition().Attributes.Any(attr => attr.AttributeType.FullName == "NUnit.Framework.TestFixtureAttribute")) { |
|
|
|
|
CheckNamedResolveResult(resolveResult, AffectedEntity.TestType, identifier, accessibilty); |
|
|
|
|
} |
|
|
|
|
} else if (resolveResult is MemberResolveResult) { |
|
|
|
|
var member = ((MemberResolveResult)resolveResult).Member; |
|
|
|
|
if (member.EntityType == EntityType.Method && member.Attributes.Any(attr => attr.AttributeType.FullName == "NUnit.Framework.TestAttribute")) { |
|
|
|
|
CheckNamedResolveResult(resolveResult, AffectedEntity.TestMethod, identifier, accessibilty); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void CheckNamedResolveResult(ResolveResult resolveResult, AffectedEntity entity, Identifier identifier, Modifiers accessibilty) |
|
|
|
|
{ |
|
|
|
|
foreach (var rule in rules) { |
|
|
|
|
if (!rule.AffectedEntity.HasFlag(entity)) { |
|
|
|
@ -63,10 +93,36 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
@@ -63,10 +93,36 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
|
|
|
|
|
if (!rule.IsValid(identifier.Name)) { |
|
|
|
|
IList<string> suggestedNames; |
|
|
|
|
var msg = rule.GetErrorMessage(ctx, identifier.Name, out suggestedNames); |
|
|
|
|
AddIssue(identifier, msg, suggestedNames.Select(n => new CodeAction(string.Format(ctx.TranslateString("Rename to '{0}'"), n), (Script script) => { |
|
|
|
|
// TODO: Real rename.
|
|
|
|
|
script.Replace(identifier, Identifier.Create(n)); |
|
|
|
|
}))); |
|
|
|
|
|
|
|
|
|
var actions = new List<CodeAction>(suggestedNames.Select(n => new CodeAction(string.Format(ctx.TranslateString("Rename to '{0}'"), n), (Script script) => { |
|
|
|
|
if (resolveResult is MemberResolveResult) { |
|
|
|
|
script.Rename(((MemberResolveResult)resolveResult).Member, n); |
|
|
|
|
} else if (resolveResult is TypeResolveResult) { |
|
|
|
|
script.Rename(((TypeResolveResult)resolveResult).Type.GetDefinition(), n); |
|
|
|
|
} else if (resolveResult is LocalResolveResult) { |
|
|
|
|
script.Rename(((LocalResolveResult)resolveResult).Variable, n); |
|
|
|
|
} else { |
|
|
|
|
script.Replace(identifier, Identifier.Create(n)); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
))); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (resolveResult is MemberResolveResult || resolveResult is TypeResolveResult || resolveResult is LocalResolveResult) { |
|
|
|
|
actions.Add(new CodeAction(string.Format(ctx.TranslateString("Rename '{0}'..."), identifier.Name), (Script script) => { |
|
|
|
|
if (resolveResult is MemberResolveResult) { |
|
|
|
|
script.Rename(((MemberResolveResult)resolveResult).Member); |
|
|
|
|
} else if (resolveResult is TypeResolveResult) { |
|
|
|
|
script.Rename(((TypeResolveResult)resolveResult).Type.GetDefinition ()); |
|
|
|
|
} else if (resolveResult is LocalResolveResult) { |
|
|
|
|
script.Rename(((LocalResolveResult)resolveResult).Variable); |
|
|
|
|
} |
|
|
|
|
})); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
AddIssue(identifier, msg, actions); |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
@ -75,11 +131,11 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
@@ -75,11 +131,11 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
|
|
|
|
|
{ |
|
|
|
|
base.VisitNamespaceDeclaration(namespaceDeclaration); |
|
|
|
|
foreach (var id in namespaceDeclaration.Identifiers) { |
|
|
|
|
CheckName(AffectedEntity.Namespace, id, Modifiers.None); |
|
|
|
|
CheckName(null, AffectedEntity.Namespace, id, Modifiers.None); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
Modifiers GetAccessibiltiy(EntityDeclaration decl, Modifiers defaultModifier) |
|
|
|
|
Modifiers GetAccessibiltiy(EntityDeclaration decl, Modifiers defaultModifier) |
|
|
|
|
{ |
|
|
|
|
var accessibility = (decl.Modifiers & Modifiers.VisibilityMask); |
|
|
|
|
if (accessibility == Modifiers.None) { |
|
|
|
@ -109,85 +165,85 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
@@ -109,85 +165,85 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
|
|
|
|
|
throw new System.ArgumentOutOfRangeException(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
CheckName(entity, typeDeclaration.NameToken, GetAccessibiltiy(typeDeclaration, typeDeclaration.Parent is TypeDeclaration ? Modifiers.Private : Modifiers.Internal)); |
|
|
|
|
CheckName(typeDeclaration, entity, typeDeclaration.NameToken, GetAccessibiltiy(typeDeclaration, typeDeclaration.Parent is TypeDeclaration ? Modifiers.Private : Modifiers.Internal)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public override void VisitDelegateDeclaration(DelegateDeclaration delegateDeclaration) |
|
|
|
|
{ |
|
|
|
|
base.VisitDelegateDeclaration(delegateDeclaration); |
|
|
|
|
CheckName(AffectedEntity.Delegate, delegateDeclaration.NameToken, GetAccessibiltiy(delegateDeclaration, delegateDeclaration.Parent is TypeDeclaration ? Modifiers.Private : Modifiers.Internal)); |
|
|
|
|
CheckName(delegateDeclaration, AffectedEntity.Delegate, delegateDeclaration.NameToken, GetAccessibiltiy(delegateDeclaration, delegateDeclaration.Parent is TypeDeclaration ? Modifiers.Private : Modifiers.Internal)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public override void VisitPropertyDeclaration(PropertyDeclaration propertyDeclaration) |
|
|
|
|
{ |
|
|
|
|
base.VisitPropertyDeclaration(propertyDeclaration); |
|
|
|
|
CheckName(AffectedEntity.Property, propertyDeclaration.NameToken, GetAccessibiltiy(propertyDeclaration, Modifiers.Private)); |
|
|
|
|
CheckName(propertyDeclaration, AffectedEntity.Property, propertyDeclaration.NameToken, GetAccessibiltiy(propertyDeclaration, Modifiers.Private)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public override void VisitMethodDeclaration(MethodDeclaration methodDeclaration) |
|
|
|
|
{ |
|
|
|
|
base.VisitMethodDeclaration(methodDeclaration); |
|
|
|
|
CheckName(AffectedEntity.Method, methodDeclaration.NameToken, GetAccessibiltiy(methodDeclaration, Modifiers.Private)); |
|
|
|
|
CheckName(methodDeclaration, AffectedEntity.Method, methodDeclaration.NameToken, GetAccessibiltiy(methodDeclaration, Modifiers.Private)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public override void VisitFieldDeclaration(FieldDeclaration fieldDeclaration) |
|
|
|
|
{ |
|
|
|
|
base.VisitFieldDeclaration(fieldDeclaration); |
|
|
|
|
foreach (var init in fieldDeclaration.Variables) { |
|
|
|
|
CheckName(AffectedEntity.Field, init.NameToken, GetAccessibiltiy(fieldDeclaration, Modifiers.Private)); |
|
|
|
|
CheckName(init, AffectedEntity.Field, init.NameToken, GetAccessibiltiy(fieldDeclaration, Modifiers.Private)); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public override void VisitFixedFieldDeclaration(FixedFieldDeclaration fixedFieldDeclaration) |
|
|
|
|
{ |
|
|
|
|
base.VisitFixedFieldDeclaration(fixedFieldDeclaration); |
|
|
|
|
CheckName(AffectedEntity.Field, fixedFieldDeclaration.NameToken, GetAccessibiltiy(fixedFieldDeclaration, Modifiers.Private)); |
|
|
|
|
CheckName(fixedFieldDeclaration, AffectedEntity.Field, fixedFieldDeclaration.NameToken, GetAccessibiltiy(fixedFieldDeclaration, Modifiers.Private)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public override void VisitEventDeclaration(EventDeclaration eventDeclaration) |
|
|
|
|
{ |
|
|
|
|
base.VisitEventDeclaration(eventDeclaration); |
|
|
|
|
foreach (var init in eventDeclaration.Variables) { |
|
|
|
|
CheckName(AffectedEntity.Event, init.NameToken, GetAccessibiltiy(eventDeclaration, Modifiers.Private)); |
|
|
|
|
CheckName(init, AffectedEntity.Event, init.NameToken, GetAccessibiltiy(eventDeclaration, Modifiers.Private)); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public override void VisitCustomEventDeclaration(CustomEventDeclaration eventDeclaration) |
|
|
|
|
{ |
|
|
|
|
base.VisitCustomEventDeclaration(eventDeclaration); |
|
|
|
|
CheckName(AffectedEntity.Event, eventDeclaration.NameToken, GetAccessibiltiy(eventDeclaration, Modifiers.Private)); |
|
|
|
|
CheckName(eventDeclaration, AffectedEntity.Event, eventDeclaration.NameToken, GetAccessibiltiy(eventDeclaration, Modifiers.Private)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public override void VisitEnumMemberDeclaration(EnumMemberDeclaration enumMemberDeclaration) |
|
|
|
|
{ |
|
|
|
|
base.VisitEnumMemberDeclaration(enumMemberDeclaration); |
|
|
|
|
CheckName(AffectedEntity.EnumMember, enumMemberDeclaration.NameToken, GetAccessibiltiy(enumMemberDeclaration, Modifiers.Private)); |
|
|
|
|
CheckName(enumMemberDeclaration, AffectedEntity.EnumMember, enumMemberDeclaration.NameToken, GetAccessibiltiy(enumMemberDeclaration, Modifiers.Private)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public override void VisitParameterDeclaration(ParameterDeclaration parameterDeclaration) |
|
|
|
|
{ |
|
|
|
|
base.VisitParameterDeclaration(parameterDeclaration); |
|
|
|
|
CheckName(parameterDeclaration.Parent is LambdaExpression ? AffectedEntity.LambdaParameter : AffectedEntity.Parameter, parameterDeclaration.NameToken, Modifiers.None); |
|
|
|
|
CheckName(parameterDeclaration, parameterDeclaration.Parent is LambdaExpression ? AffectedEntity.LambdaParameter : AffectedEntity.Parameter, parameterDeclaration.NameToken, Modifiers.None); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public override void VisitTypeParameterDeclaration(TypeParameterDeclaration typeParameterDeclaration) |
|
|
|
|
{ |
|
|
|
|
base.VisitTypeParameterDeclaration(typeParameterDeclaration); |
|
|
|
|
CheckName(AffectedEntity.TypeParameter, typeParameterDeclaration.NameToken, Modifiers.None); |
|
|
|
|
CheckName(typeParameterDeclaration, AffectedEntity.TypeParameter, typeParameterDeclaration.NameToken, Modifiers.None); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public override void VisitVariableDeclarationStatement(VariableDeclarationStatement variableDeclarationStatement) |
|
|
|
|
{ |
|
|
|
|
base.VisitVariableDeclarationStatement(variableDeclarationStatement); |
|
|
|
|
foreach (var init in variableDeclarationStatement.Variables) { |
|
|
|
|
CheckName(AffectedEntity.LocalVariable, init.NameToken, Modifiers.None); |
|
|
|
|
CheckName(init, AffectedEntity.LocalVariable, init.NameToken, Modifiers.None); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public override void VisitLabelStatement(LabelStatement labelStatement) |
|
|
|
|
{ |
|
|
|
|
base.VisitLabelStatement(labelStatement); |
|
|
|
|
CheckName(AffectedEntity.Label, labelStatement.LabelToken, Modifiers.None); |
|
|
|
|
CheckName(null, AffectedEntity.Label, labelStatement.LabelToken, Modifiers.None); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|