Browse Source

[CodeIssues] Fixed naming issue.

pull/32/merge
Mike Krüger 13 years ago
parent
commit
dc10a67ce7
  1. 39
      ICSharpCode.NRefactory.CSharp/Refactoring/CodeIssues/InconsistentNamingIssue/NamingRule.cs
  2. 18
      ICSharpCode.NRefactory.Tests/CSharp/CodeIssues/InconsistentNamingTests.cs

39
ICSharpCode.NRefactory.CSharp/Refactoring/CodeIssues/InconsistentNamingIssue/NamingRule.cs

@ -122,9 +122,9 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
case NamingStyle.AllUpper: case NamingStyle.AllUpper:
return !id.Any(ch => char.IsLetter(ch) && char.IsLower(ch)); return !id.Any(ch => char.IsLetter(ch) && char.IsLower(ch));
case NamingStyle.CamelCase: case NamingStyle.CamelCase:
return id.Length == 0 || (char.IsLower(id [0]) && NoUnderscoreWithoutNumber(id)); return id.Length == 0 || (char.IsLower(id [0]) && NoUnderscore(id));
case NamingStyle.PascalCase: case NamingStyle.PascalCase:
return id.Length == 0 || (char.IsUpper(id [0]) && NoUnderscoreWithoutNumber(id)); return id.Length == 0 || (char.IsUpper(id [0]) && NoUnderscore(id));
case NamingStyle.FirstUpper: case NamingStyle.FirstUpper:
return id.Length == 0 && char.IsUpper(id [0]) && !id.Skip(1).Any(ch => char.IsLetter(ch) && char.IsUpper(ch)); return id.Length == 0 && char.IsUpper(id [0]) && !id.Skip(1).Any(ch => char.IsLetter(ch) && char.IsUpper(ch));
} }
@ -139,18 +139,23 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
IncludeInstanceMembers = true; IncludeInstanceMembers = true;
} }
static bool NoUnderscoreWithoutNumber(string id) static bool NoUnderscore(string id)
{ {
int idx = id.IndexOf('_'); return id.IndexOf('_') < 0;
while (idx >= 0 && idx < id.Length) {
if ((idx + 2 >= id.Length || !char.IsDigit(id [idx + 1])) && (idx == 0 || !char.IsDigit(id [idx - 1]))) {
return false;
}
idx = id.IndexOf('_', idx + 1);
}
return true;
} }
// static bool NoUnderscoreWithoutNumber(string id)
// {
// int idx = id.IndexOf('_');
// while (idx >= 0 && idx < id.Length) {
// if ((idx + 2 >= id.Length || !char.IsDigit(id [idx + 1])) && (idx == 0 || !char.IsDigit(id [idx - 1]))) {
// return false;
// }
// idx = id.IndexOf('_', idx + 1);
// }
// return true;
// }
public string GetErrorMessage(BaseRefactoringContext ctx, string name, out IList<string> suggestedNames) public string GetErrorMessage(BaseRefactoringContext ctx, string name, out IList<string> suggestedNames)
{ {
@ -224,7 +229,7 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
case NamingStyle.CamelCase: case NamingStyle.CamelCase:
if (id.Length > 0 && char.IsUpper(id [0])) { if (id.Length > 0 && char.IsUpper(id [0])) {
errorMessage = string.Format(ctx.TranslateString("'{0}' should start with a lower case letter."), name); errorMessage = string.Format(ctx.TranslateString("'{0}' should start with a lower case letter."), name);
} else if (!NoUnderscoreWithoutNumber(id)) { } else if (!NoUnderscore(id)) {
errorMessage = string.Format(ctx.TranslateString("'{0}' should not separate words with an underscore."), name); errorMessage = string.Format(ctx.TranslateString("'{0}' should not separate words with an underscore."), name);
} else { } else {
suggestedNames.Add(id); suggestedNames.Add(id);
@ -235,7 +240,7 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
case NamingStyle.PascalCase: case NamingStyle.PascalCase:
if (id.Length > 0 && char.IsLower(id [0])) { if (id.Length > 0 && char.IsLower(id [0])) {
errorMessage = string.Format(ctx.TranslateString("'{0}' should start with an upper case letter."), name); errorMessage = string.Format(ctx.TranslateString("'{0}' should start with an upper case letter."), name);
} else if (!NoUnderscoreWithoutNumber(id)) { } else if (!NoUnderscore(id)) {
errorMessage = string.Format(ctx.TranslateString("'{0}' should not separate words with an underscore."), name); errorMessage = string.Format(ctx.TranslateString("'{0}' should not separate words with an underscore."), name);
} else { } else {
suggestedNames.Add(id); suggestedNames.Add(id);
@ -310,8 +315,8 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
var sb = new StringBuilder (); var sb = new StringBuilder ();
sb.Append (words[0].ToLower ()); sb.Append (words[0].ToLower ());
for (int i = 1; i < words.Count; i++) { for (int i = 1; i < words.Count; i++) {
if (sb.Length > 0 && (char.IsDigit (sb[sb.Length-1]) || char.IsDigit (words[i][0]))) // if (sb.Length > 0 && (char.IsDigit (sb[sb.Length-1]) || char.IsDigit (words[i][0])))
sb.Append ('_'); // sb.Append ('_');
AppendCapitalized (words[i], sb); AppendCapitalized (words[i], sb);
} }
return sb.ToString (); return sb.ToString ();
@ -321,8 +326,8 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
{ {
var sb = new StringBuilder (); var sb = new StringBuilder ();
for (int i = 0; i < words.Count; i++) { for (int i = 0; i < words.Count; i++) {
if (sb.Length > 0 && (char.IsDigit (sb[sb.Length-1]) || char.IsDigit (words[i][0]))) // if (sb.Length > 0 && (char.IsDigit (sb[sb.Length-1]) || char.IsDigit (words[i][0])))
sb.Append ('_'); // sb.Append ('_');
AppendCapitalized (words[i], sb); AppendCapitalized (words[i], sb);
} }
return sb.ToString (); return sb.ToString ();

18
ICSharpCode.NRefactory.Tests/CSharp/CodeIssues/InconsistentNamingTests.cs

@ -46,6 +46,14 @@ namespace ICSharpCode.NRefactory.CSharp.CodeIssues
CheckFix (context, issues [0], output); CheckFix (context, issues [0], output);
} }
[Test]
public void TestUnderscoreFix ()
{
var input = @"namespace FIX_1 {}";
var output = @"namespace Fix1 {}";
CheckNaming (input, output);
}
[Test] [Test]
public void TestNamespaceName () public void TestNamespaceName ()
{ {
@ -311,6 +319,16 @@ class MyClass : Base { public override int Method (int Param) {} }";
Assert.AreEqual ("long", result [2]); Assert.AreEqual ("long", result [2]);
Assert.AreEqual ("NAME", result [3]); Assert.AreEqual ("NAME", result [3]);
} }
[Test]
public void TestUnderscoreCase1 ()
{
var result = WordParser.BreakWords ("FIX_1");
Assert.AreEqual (2, result.Count);
Assert.AreEqual ("FIX", result [0]);
Assert.AreEqual ("1", result [1]);
}
} }
} }

Loading…
Cancel
Save