Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Signature help does not work for constructors #1036

Merged
merged 1 commit into from
May 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.dom.ASTNode;
import org.eclipse.jdt.core.dom.Block;
import org.eclipse.jdt.core.dom.ClassInstanceCreation;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.MethodInvocation;
import org.eclipse.jdt.core.dom.MethodRef;
Expand Down Expand Up @@ -73,13 +74,11 @@ public SignatureHelp signatureHelp(TextDocumentPositionParams position, IProgres
int size = -1;
int currentParameter = contextInfomation[1];
if (node instanceof MethodInvocation) {
try {
size = ((MethodInvocation) node).arguments().size();
} catch (UnsupportedOperationException e) {
// ignore
}
size = ((MethodInvocation) node).arguments().size();
} else if (node instanceof MethodRef) {
size = ((MethodRef) node).parameters().size();
} else if (node instanceof ClassInstanceCreation) {
size = ((ClassInstanceCreation) node).arguments().size();
}
size = Math.max(currentParameter + 1, size);
List<SignatureInformation> infos = help.getSignatures();
Expand All @@ -102,7 +101,7 @@ private ASTNode getNode(ICompilationUnit unit, int[] contextInfomation, IProgres
if (contextInfomation[0] != -1) {
CompilationUnit ast = CoreASTProvider.getInstance().getAST(unit, CoreASTProvider.WAIT_YES, monitor);
ASTNode node = NodeFinder.perform(ast, contextInfomation[0], 1);
if (node instanceof MethodInvocation || node instanceof MethodRef || (contextInfomation[1] > 0 && node instanceof Block)) {
if (node instanceof MethodInvocation || node instanceof ClassInstanceCreation || node instanceof MethodRef || (contextInfomation[1] > 0 && node instanceof Block)) {
return node;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,40 @@ public void testSignatureHelp_activeSignature() throws JavaModelException {
assertEquals(help.getSignatures().get(help.getActiveSignature()).getLabel(), "foo(String s, String b) : void");
}

@Test
public void testSignatureHelp_constructor() throws JavaModelException {
IPackageFragment pack1 = sourceFolder.createPackageFragment("test1", false, null);
StringBuilder buf = new StringBuilder();
buf.append("package test1;\n");
buf.append("public class E {\n");
buf.append(" public void bar() {\n");
buf.append(" new RuntimeException()\n");
buf.append(" }\n");
buf.append("}\n");
ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);
SignatureHelp help = getSignatureHelp(cu, 3, 26);
assertNotNull(help);
assertEquals(help.getSignatures().size(), 4);
assertEquals(help.getSignatures().get(help.getActiveSignature()).getLabel(), "RuntimeException()");
}

@Test
public void testSignatureHelp_constructorParameters() throws JavaModelException {
IPackageFragment pack1 = sourceFolder.createPackageFragment("test1", false, null);
StringBuilder buf = new StringBuilder();
buf.append("package test1;\n");
buf.append("public class E {\n");
buf.append(" public void bar() {\n");
buf.append(" new RuntimeException(\"t\", )\n");
buf.append(" }\n");
buf.append("}\n");
ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);
SignatureHelp help = getSignatureHelp(cu, 3, 31);
assertNotNull(help);
assertEquals(help.getSignatures().size(), 4);
assertTrue(help.getSignatures().get(help.getActiveSignature()).getLabel().matches("RuntimeException\\(String \\w+, Throwable \\w+\\)"));
}

@Test
public void testSignatureHelp_javadoc() throws JavaModelException {
IPackageFragment pack1 = sourceFolder.createPackageFragment("test1", false, null);
Expand Down