Browse Source

[CodeActions] Adjust the behaviour of AddCatchTypeAction.

newNRvisualizers
Simon Lindgren 14 years ago
parent
commit
23fdd83224
  1. 13
      ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/AddCatchTypeAction.cs
  2. 24
      ICSharpCode.NRefactory.CSharp/Refactoring/NamingHelper.cs
  3. 17
      ICSharpCode.NRefactory.Tests/CSharp/Refactoring/NamingHelperTests.cs

13
ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/AddCatchTypeAction.cs

@ -28,8 +28,8 @@ using System.Collections.Generic; @@ -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 @@ -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);
});
}

24
ICSharpCode.NRefactory.CSharp/Refactoring/NamingHelper.cs

@ -103,22 +103,26 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -103,22 +103,26 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
/// <param name='type'>
/// The type of the variable.
/// </param>
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 @@ -138,10 +142,10 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
/// <param name='type'>
/// The type of the variable.
/// </param>
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)

17
ICSharpCode.NRefactory.Tests/CSharp/Refactoring/NamingHelperTests.cs

@ -38,7 +38,7 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -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 @@ -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()

Loading…
Cancel
Save