diff --git a/ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/AddCatchTypeAction.cs b/ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/AddCatchTypeAction.cs
index 6a33592aa0..d88295db4a 100644
--- a/ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/AddCatchTypeAction.cs
+++ b/ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/AddCatchTypeAction.cs
@@ -28,8 +28,8 @@ using System.Collections.Generic;
namespace ICSharpCode.NRefactory.CSharp.Refactoring
{
- [ContextAction("Add type",
- Description = "Adds an exception type specifier to catch clauses.")]
+ [ContextAction("Add type to general catch clause",
+ Description = "Adds an exception type specifier to general catch clauses.")]
public class AddCatchTypeAction : ICodeActionProvider
{
#region ICodeActionProvider implementation
@@ -42,15 +42,16 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
if (!catchClause.Type.IsNull)
yield break;
yield return new CodeAction(context.TranslateString("Add type specifier"), script => {
- var newIdentifier = Identifier.Create("e");
var newType = context.CreateShortType("System", "Exception");
- script.Replace(catchClause, new CatchClause() {
+ var namingHelper = new NamingHelper(context);
+ var newIdentifier = Identifier.Create(namingHelper.GenerateVariableName(newType, "e"));
+
+ script.Replace(catchClause, new CatchClause {
Type = newType,
VariableNameToken = newIdentifier,
Body = catchClause.Body.Clone() as BlockStatement
});
- script.Link(newType);
- script.Link(newIdentifier);
+ script.Select(newType);
});
}
diff --git a/ICSharpCode.NRefactory.CSharp/Refactoring/NamingHelper.cs b/ICSharpCode.NRefactory.CSharp/Refactoring/NamingHelper.cs
index b444afcc8c..afd32272a7 100644
--- a/ICSharpCode.NRefactory.CSharp/Refactoring/NamingHelper.cs
+++ b/ICSharpCode.NRefactory.CSharp/Refactoring/NamingHelper.cs
@@ -103,22 +103,26 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
///
/// The type of the variable.
///
- public string GenerateVariableName(AstType type)
+ public string GenerateVariableName(AstType type, string baseName = null)
{
- string firstSuggestion = null;
- foreach (var name in NamingHelper.GenerateNameProposals(type)) {
- firstSuggestion = firstSuggestion ?? name;
- if (NameIsUnused(name)) {
- usedVariableNames.Add(name);
- return name;
+ if (baseName == null) {
+ foreach (var name in NamingHelper.GenerateNameProposals(type)) {
+ baseName = baseName ?? name;
+ if (NameIsUnused(name)) {
+ usedVariableNames.Add(name);
+ return name;
+ }
}
+ } else if (NameIsUnused(baseName)) {
+ return baseName;
}
+
// If we get here, all of the standard suggestions are already used.
// This will at least be the second variable named based on firstSuggestion, so start at 2
int counter = 2;
string proposedName;
do {
- proposedName = firstSuggestion + counter++;
+ proposedName = baseName + counter++;
} while (!NameIsUnused(proposedName));
usedVariableNames.Add(proposedName);
return proposedName;
@@ -138,10 +142,10 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
///
/// The type of the variable.
///
- public string GenerateVariableName(IType type)
+ public string GenerateVariableName(IType type, string baseName = null)
{
AstType astType = ToAstType(type);
- return GenerateVariableName(astType);
+ return GenerateVariableName(astType, baseName);
}
AstType ToAstType(IType type)
diff --git a/ICSharpCode.NRefactory.Tests/CSharp/Refactoring/NamingHelperTests.cs b/ICSharpCode.NRefactory.Tests/CSharp/Refactoring/NamingHelperTests.cs
index 401d93af0a..7f5547588c 100644
--- a/ICSharpCode.NRefactory.Tests/CSharp/Refactoring/NamingHelperTests.cs
+++ b/ICSharpCode.NRefactory.Tests/CSharp/Refactoring/NamingHelperTests.cs
@@ -38,7 +38,7 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
var context = TestRefactoringContext.Create(input, expectErrors);
return context;
}
-
+
[Test]
public void GenerateVariableNameTest()
{
@@ -71,6 +71,21 @@ class A
Assert.NotNull(name);
Assert.IsFalse(name == "i", "i was already used and should not be proposed.");
}
+
+ [Test]
+ public void GenerateVariableNameBasedOnCustomBaseName()
+ {
+ var context = MakeContext(@"
+class A
+{
+ void F()
+ { $ }
+}"
+ );
+ var name = new NamingHelper(context).GenerateVariableName(new PrimitiveType("int"), "integer");
+ Assert.NotNull(name);
+ Assert.AreEqual("integer", name);
+ }
[Test]
public void GenerateVariableNameIgnoresNamesUsedLaterInScope()