Browse Source

[CodeIssues] VariableDeclaredInWideScope: Handle moves into SwitchSections.

newNRvisualizers
Simon Lindgren 14 years ago
parent
commit
9c634c73fc
  1. 9
      ICSharpCode.NRefactory.CSharp/Refactoring/CodeIssues/VariableDeclaredInWideScopeIssue.cs
  2. 43
      ICSharpCode.NRefactory.Tests/CSharp/CodeIssues/VariableDeclaredInWideScopeTests.cs

9
ICSharpCode.NRefactory.CSharp/Refactoring/CodeIssues/VariableDeclaredInWideScopeIssue.cs

@ -266,7 +266,10 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -266,7 +266,10 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
IEnumerable<CodeAction> GetActions(Statement oldStatement, Statement followingStatement)
{
yield return new CodeAction(context.TranslateString("Move to nested scope"), script => {
if (!(followingStatement.Parent is BlockStatement)) {
var parent = followingStatement.Parent;
if (parent is SwitchSection || parent is BlockStatement) {
script.InsertBefore(followingStatement, oldStatement.Clone());
} else {
var newBlockStatement = new BlockStatement {
Statements = {
oldStatement.Clone(),
@ -274,9 +277,7 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -274,9 +277,7 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
}
};
script.Replace(followingStatement, newBlockStatement);
script.FormatText(followingStatement.Parent);
} else {
script.InsertBefore(followingStatement, oldStatement.Clone());
script.FormatText(parent);
}
script.Remove(oldStatement);
});

43
ICSharpCode.NRefactory.Tests/CSharp/CodeIssues/VariableDeclaredInWideScopeTests.cs

@ -553,6 +553,49 @@ class A @@ -553,6 +553,49 @@ class A
SetValue (k);
}
}
}");
}
[Test]
public void CaseTarget ()
{
var input = @"
using System;
class A
{
int GetValue () { return 0; }
int SetValue (int i) { }
void F (int i)
{
int j = GetValue ();
switch (i) {
case 2:
SetValue (j + 1);
break;
}
}
}";
TestRefactoringContext context;
var issues = GetIssues(new VariableDeclaredInWideScopeIssue(), input, out context);
Assert.AreEqual(1, issues.Count);
CheckFix(context, issues[0], @"
using System;
class A
{
int GetValue () { return 0; }
int SetValue (int i) { }
void F (int i)
{
switch (i) {
case 2:
int j = GetValue ();
SetValue (j + 1);
break;
}
}
}");
}
}

Loading…
Cancel
Save