Skip to content
This repository has been archived by the owner on Sep 24, 2020. It is now read-only.

Commit

Permalink
[CodeActions] Extract method now works with comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike Krüger committed Dec 11, 2012
1 parent ad6ce10 commit 140fb30
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 7 deletions.
2 changes: 1 addition & 1 deletion ICSharpCode.NRefactory.CSharp/Ast/AstNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ public void AddChild<T> (T child, Role<T> role) where T : AstNode
/// <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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ public IEnumerable<CodeAction> GetActions(RefactoringContext context)
}

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;
}
Expand Down Expand Up @@ -95,7 +95,7 @@ CodeAction CreateFromExpression(RefactoringContext context, Expression expressio
});
}

CodeAction CreateFromStatements(RefactoringContext context, List<Statement> statements)
CodeAction CreateFromStatements(RefactoringContext context, List<AstNode> statements)
{
if (!(statements [0].Parent is Statement))
return null;
Expand All @@ -108,9 +108,13 @@ CodeAction CreateFromStatements(RefactoringContext context, List<Statement> stat
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public static List<IVariable> Analyze(RefactoringContext context, Expression exp
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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,35 @@ void TestMethod ()
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 ();
}
}
");
}
}
Expand Down

0 comments on commit 140fb30

Please sign in to comment.