Skip to content

Commit

Permalink
Consider variable outside of selection to guess better extracted meth…
Browse files Browse the repository at this point in the history
…od name

Signed-off-by: Jessica He <jhe@redhat.com>
  • Loading branch information
JessicaJHee authored and rgrunber committed Jun 8, 2023
1 parent edd4b20 commit 20e5b1b
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.eclipse.jdt.core.dom.ASTNode;
import org.eclipse.jdt.core.dom.AbstractTypeDeclaration;
import org.eclipse.jdt.core.dom.AnonymousClassDeclaration;
import org.eclipse.jdt.core.dom.Assignment;
import org.eclipse.jdt.core.dom.Block;
import org.eclipse.jdt.core.dom.BodyDeclaration;
import org.eclipse.jdt.core.dom.CompilationUnit;
Expand Down Expand Up @@ -840,6 +841,16 @@ private static String proposeMethodNameHeuristic(IInvocationContext context, AST
SelectionAnalyzer analyzer = new SelectionAnalyzer(Selection.createFromStartLength(context.getSelectionOffset(), context.getSelectionLength()), true);
cu.accept(analyzer);
List<ASTNode> varDeclStatements = Arrays.asList(analyzer.getSelectedNodes()).stream().filter(e -> e.getNodeType() == ASTNode.VARIABLE_DECLARATION_STATEMENT).collect(Collectors.toList());
// Check for entire covering node for local variable
if (varDeclStatements.isEmpty() && coveringNode.getNodeType() == ASTNode.VARIABLE_DECLARATION_STATEMENT) {
varDeclStatements.add(coveringNode);
// Check for assignment statement for left hand side variable
} else if (coveringNode.getNodeType() == ASTNode.EXPRESSION_STATEMENT && ((ExpressionStatement) coveringNode).getExpression().getNodeType() == ASTNode.ASSIGNMENT) {
Expression leftHandSide = ((Assignment) ((ExpressionStatement) coveringNode).getExpression()).getLeftHandSide();
if (leftHandSide.getNodeType() == ASTNode.SIMPLE_NAME) {
return "get" + StringUtils.capitalize(((SimpleName) leftHandSide).getIdentifier());
}
}
if (!varDeclStatements.isEmpty()) {
VariableDeclarationStatement lastStatement = (VariableDeclarationStatement) varDeclStatements.get(varDeclStatements.size() - 1);
List<?> fragments = lastStatement.fragments();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -647,10 +647,10 @@ public void testExtractMethodVar() throws Exception {
buf.append("\n");
buf.append(" public void foo() {\n");
buf.append(" var test = new String(\"blah\");\n");
buf.append(" extracted(test);\n");
buf.append(" getTest(test);\n");
buf.append(" }\n");
buf.append("\n");
buf.append(" private void extracted(String test) {\n");
buf.append(" private void getTest(String test) {\n");
buf.append(" test = test + test;\n");
buf.append(" }\n");
buf.append("}\n");
Expand Down Expand Up @@ -932,4 +932,80 @@ public void testExtractMethodInferNameInContext() throws Exception {
assertCodeActions(cu, e1);
}

@Test
public void testExtractMethodInferNameNoVarSelected() throws Exception {
IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);

StringBuilder buf = new StringBuilder();
buf.append("package test1;\n");
buf.append("\n");
buf.append("public class E {\n");
buf.append(" public static void main(String[] args) {\n");
buf.append(" int numArgs = /*[*/args == null ? 0 : args.length/*]*/;\n");
buf.append(" System.out.println(numArgs);\n");
buf.append(" }\n");
buf.append("\n");
buf.append("}\n");

ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);

buf = new StringBuilder();
buf.append("package test1;\n");
buf.append("\n");
buf.append("public class E {\n");
buf.append(" public static void main(String[] args) {\n");
buf.append(" int numArgs = getNumArgs(args);\n");
buf.append(" System.out.println(numArgs);\n");
buf.append(" }\n");
buf.append("\n");
buf.append(" private static int getNumArgs(String[] args) {\n");
buf.append(" return /*[*/args == null ? 0 : args.length/*]*/;\n");
buf.append(" }\n");
buf.append("\n");
buf.append("}\n");

Expected e1 = new Expected("Extract to method", buf.toString(), JavaCodeActionKind.REFACTOR_EXTRACT_METHOD);

assertCodeActions(cu, e1);
}

@Test
public void testExtractMethodInferNameNoVarSelectedAssign() throws Exception {
IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);

StringBuilder buf = new StringBuilder();
buf.append("package test1;\n");
buf.append("\n");
buf.append("public class E {\n");
buf.append(" public static void main(String[] args) {\n");
buf.append(" int numArgs;\n");
buf.append(" numArgs = /*[*/args == null ? 0 : args.length/*]*/;\n");
buf.append(" System.out.println(numArgs);\n");
buf.append(" }\n");
buf.append("\n");
buf.append("}\n");

ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);

buf = new StringBuilder();
buf.append("package test1;\n");
buf.append("\n");
buf.append("public class E {\n");
buf.append(" public static void main(String[] args) {\n");
buf.append(" int numArgs;\n");
buf.append(" numArgs = getNumArgs(args);\n");
buf.append(" System.out.println(numArgs);\n");
buf.append(" }\n");
buf.append("\n");
buf.append(" private static int getNumArgs(String[] args) {\n");
buf.append(" return /*[*/args == null ? 0 : args.length/*]*/;\n");
buf.append(" }\n");
buf.append("\n");
buf.append("}\n");

Expected e1 = new Expected("Extract to method", buf.toString(), JavaCodeActionKind.REFACTOR_EXTRACT_METHOD);

assertCodeActions(cu, e1);
}

}

0 comments on commit 20e5b1b

Please sign in to comment.