Browse Source

[CodeActions] Extract method now works with comments.

newNRvisualizers
Mike Krüger 14 years ago
parent
commit
140fb308f0
  1. 2
      ICSharpCode.NRefactory.CSharp/Ast/AstNode.cs
  2. 14
      ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/ExtractMethod/ExtractMethodAction.cs
  3. 2
      ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/ExtractMethod/VariableLookupVisitor.cs
  4. 29
      ICSharpCode.NRefactory.Tests/CSharp/CodeActions/ExtractMethodTests.cs

2
ICSharpCode.NRefactory.CSharp/Ast/AstNode.cs

@ -369,7 +369,7 @@ namespace ICSharpCode.NRefactory.CSharp @@ -369,7 +369,7 @@ namespace ICSharpCode.NRefactory.CSharp
/// <summary>
/// Adds a child without performing any safety checks.
/// </summary>
void AddChildUnsafe (AstNode child, Role role)
internal void AddChildUnsafe (AstNode child, Role role)
{
child.parent = this;
child.SetRole(role);

14
ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/ExtractMethod/ExtractMethodAction.cs

@ -54,10 +54,10 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring.ExtractMethod @@ -54,10 +54,10 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring.ExtractMethod
}
foreach (var node in selected) {
if (!(node is Statement))
if (!(node is Statement) && !(node is Comment) && !(node is PreProcessorDirective))
yield break;
}
var action = CreateFromStatements (context, new List<Statement> (selected.OfType<Statement> ()));
var action = CreateFromStatements (context, new List<AstNode> (selected));
if (action != null)
yield return action;
}
@ -95,7 +95,7 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring.ExtractMethod @@ -95,7 +95,7 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring.ExtractMethod
});
}
CodeAction CreateFromStatements(RefactoringContext context, List<Statement> statements)
CodeAction CreateFromStatements(RefactoringContext context, List<AstNode> statements)
{
if (!(statements [0].Parent is Statement))
return null;
@ -108,9 +108,13 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring.ExtractMethod @@ -108,9 +108,13 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring.ExtractMethod
Body = new BlockStatement()
};
bool usesNonStaticMember = false;
foreach (Statement node in statements) {
foreach (var node in statements) {
usesNonStaticMember |= StaticVisitor.UsesNotStaticMember(context, node);
method.Body.Add(node.Clone());
if (node is Statement) {
method.Body.Add((Statement)node.Clone());
} else {
method.Body.AddChildUnsafe (node.Clone (), node.Role);
}
}
if (!usesNonStaticMember)
method.Modifiers |= Modifiers.Static;

2
ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/ExtractMethod/VariableLookupVisitor.cs

@ -91,7 +91,7 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring.ExtractMethod @@ -91,7 +91,7 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring.ExtractMethod
return visitor.UsedVariables;
}
public static List<IVariable> Analyze(RefactoringContext context, List<Statement> statements)
public static List<IVariable> Analyze(RefactoringContext context, List<AstNode> statements)
{
var visitor = new VariableLookupVisitor(context);
statements.ForEach(stmt => stmt.AcceptVisitor(visitor));

29
ICSharpCode.NRefactory.Tests/CSharp/CodeActions/ExtractMethodTests.cs

@ -412,6 +412,35 @@ class TestClass @@ -412,6 +412,35 @@ class TestClass
Console.WriteLine (test);
}
}
");
}
/// <summary>
/// Bug 8835 - missing "extract method" in the code editor
/// </summary>
[Test()]
public void TestBug8835 ()
{
Test<ExtractMethodAction>(@"class TestClass
{
void TestMethod ()
{
<-// comment
Foo ();->
}
}
", @"class TestClass
{
static void NewMethod ()
{
// comment
Foo ();
}
void TestMethod ()
{
NewMethod ();
}
}
");
}
}

Loading…
Cancel
Save