Skip to content

Commit

Permalink
Can trigger convert var code action on types
Browse files Browse the repository at this point in the history
Signed-off-by: Sheng Chen <sheche@microsoft.com>
  • Loading branch information
jdneo authored and fbricon committed Sep 14, 2020
1 parent 3aaea8f commit 6ca5602
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -525,10 +525,11 @@ private static boolean getConvertVarTypeToResolvedTypeProposal(IInvocationContex
return false;
}

if (!(node instanceof SimpleName)) {
SimpleName name = getSimpleNameForVariable(node);
if (name == null) {
return false;
}
SimpleName name = (SimpleName) node;

IBinding binding = name.resolveBinding();
if (!(binding instanceof IVariableBinding)) {
return false;
Expand Down Expand Up @@ -569,6 +570,26 @@ private static boolean getConvertVarTypeToResolvedTypeProposal(IInvocationContex
return true;
}

private static SimpleName getSimpleNameForVariable(ASTNode node) {
if (!(node instanceof SimpleName)) {
return null;
}
SimpleName name = (SimpleName) node;
if (!name.isDeclaration()) {
while (node instanceof Name || node instanceof Type) {
node = node.getParent();
}
if (node instanceof VariableDeclarationStatement) {
List<VariableDeclarationFragment> fragments = ((VariableDeclarationStatement) node).fragments();
if (fragments.size() > 0) {
// var is not allowed in a compound declaration
name = fragments.get(0).getName();
}
}
}
return name;
}

private static boolean getConvertResolvedTypeToVarTypeProposal(IInvocationContext context, ASTNode node, Collection<ChangeCorrectionProposal> proposals) {
CompilationUnit astRoot = context.getASTRoot();
IJavaElement root = astRoot.getJavaElement();
Expand All @@ -583,10 +604,11 @@ private static boolean getConvertResolvedTypeToVarTypeProposal(IInvocationContex
return false;
}

if (!(node instanceof SimpleName)) {
SimpleName name = getSimpleNameForVariable(node);
if (name == null) {
return false;
}
SimpleName name = (SimpleName) node;

IBinding binding = name.resolveBinding();
if (!(binding instanceof IVariableBinding)) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,12 @@
import org.eclipse.jdt.core.IPackageFragment;
import org.eclipse.jdt.core.IPackageFragmentRoot;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.ls.core.internal.CodeActionUtil;
import org.eclipse.jdt.ls.core.internal.WorkspaceHelper;
import org.eclipse.lsp4j.CodeAction;
import org.eclipse.lsp4j.CodeActionKind;
import org.eclipse.lsp4j.Command;
import org.eclipse.lsp4j.Range;
import org.eclipse.lsp4j.jsonrpc.messages.Either;
import org.junit.Before;
import org.junit.Test;
Expand Down Expand Up @@ -58,6 +61,31 @@ public void testConvertVarTypeToResolvedType() throws Exception {
assertNotNull(codeAction);
}

@Test
public void testConvertVarTypeToResolvedType2() throws Exception {
IPackageFragment pack1 = sourceFolder.createPackageFragment("foo.bar", false, null);
StringBuilder buf = new StringBuilder();
buf.append("package foo.bar;\n");
buf.append("public class Test {\n");
buf.append(" public void test() {\n");
buf.append(" var/*cursor*/ name = \"test\";\n");
buf.append(" }\n");
buf.append("}\n");
ICompilationUnit cu = pack1.createCompilationUnit("Test.java", buf.toString(), false, null);

buf = new StringBuilder();
buf.append("package foo.bar;\n");
buf.append("public class Test {\n");
buf.append(" public void test() {\n");
buf.append(" String name = \"test\";\n");
buf.append(" }\n");
buf.append("}\n");

Expected expected = new Expected("Change type of 'name' to 'String'", buf.toString(), CodeActionKind.Refactor);
Range range = CodeActionUtil.getRange(cu, "/*cursor*/", 0);
assertCodeActions(cu, range, expected);
}

@Test
public void testConvertResolvedTypeToVar() throws Exception {
IPackageFragment pack1 = sourceFolder.createPackageFragment("foo.bar", false, null);
Expand All @@ -74,4 +102,29 @@ public void testConvertResolvedTypeToVar() throws Exception {
assertNotNull(codeAction);
}

@Test
public void testConvertResolvedTypeToVar2() throws Exception {
IPackageFragment pack1 = sourceFolder.createPackageFragment("foo.bar", false, null);
StringBuilder buf = new StringBuilder();
buf.append("package foo.bar;\n");
buf.append("public class Test {\n");
buf.append(" public void test() {\n");
buf.append(" String/*cursor*/ name = \"test\";\n");
buf.append(" }\n");
buf.append("}\n");
ICompilationUnit cu = pack1.createCompilationUnit("Test.java", buf.toString(), false, null);

buf = new StringBuilder();
buf.append("package foo.bar;\n");
buf.append("public class Test {\n");
buf.append(" public void test() {\n");
buf.append(" var name = \"test\";\n");
buf.append(" }\n");
buf.append("}\n");

Expected expected = new Expected("Change type of 'name' to 'var'", buf.toString(), CodeActionKind.Refactor);
Range range = CodeActionUtil.getRange(cu, "/*cursor*/", 0);
assertCodeActions(cu, range, expected);
}

}

0 comments on commit 6ca5602

Please sign in to comment.