Search in sources :

Example 21 with SignatureHelp

use of org.eclipse.lsp4j.SignatureHelp in project eclipse.jdt.ls by eclipse.

the class SignatureHelpRequestor method getSignatureHelp.

public SignatureHelp getSignatureHelp(IProgressMonitor monitor) {
    SignatureHelp signatureHelp = new SignatureHelp();
    response.setProposals(proposals);
    CompletionResponses.store(response);
    List<SignatureInformation> infos = new ArrayList<>();
    for (int i = 0; i < proposals.size(); i++) {
        if (!monitor.isCanceled()) {
            CompletionProposal proposal = proposals.get(i);
            if (proposal.getKind() != CompletionProposal.METHOD_REF) {
                typeProposals.add(proposal);
                continue;
            }
            SignatureInformation signatureInformation = this.toSignatureInformation(proposal);
            infoProposals.put(signatureInformation, proposal);
            infos.add(signatureInformation);
        } else {
            return signatureHelp;
        }
    }
    infos.sort((SignatureInformation a, SignatureInformation b) -> a.getParameters().size() - b.getParameters().size());
    signatureHelp.getSignatures().addAll(infos);
    return signatureHelp;
}
Also used : SignatureInformation(org.eclipse.lsp4j.SignatureInformation) InternalCompletionProposal(org.eclipse.jdt.internal.codeassist.InternalCompletionProposal) CompletionProposal(org.eclipse.jdt.core.CompletionProposal) ArrayList(java.util.ArrayList) SignatureHelp(org.eclipse.lsp4j.SignatureHelp)

Example 22 with SignatureHelp

use of org.eclipse.lsp4j.SignatureHelp in project eclipse.jdt.ls by eclipse.

the class SignatureHelpHandlerTest method testSignatureHelp_singleMethod.

@Test
public void testSignatureHelp_singleMethod() 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("   /** This is a method */\n");
    buf.append("   public int foo(String s) { }\n");
    buf.append("   public int bar(String s) { this.foo() }\n");
    buf.append("}\n");
    ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);
    SignatureHelp help = getSignatureHelp(cu, 4, 39);
    assertNotNull(help);
    assertEquals(1, help.getSignatures().size());
    assertEquals("foo(String s) : int", help.getSignatures().get(help.getActiveSignature()).getLabel());
    assertTrue(help.getSignatures().get(help.getActiveSignature()).getDocumentation().getLeft().length() > 0);
    assertEquals((Integer) 0, help.getActiveParameter());
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) SignatureHelp(org.eclipse.lsp4j.SignatureHelp) Test(org.junit.Test)

Example 23 with SignatureHelp

use of org.eclipse.lsp4j.SignatureHelp in project eclipse.jdt.ls by eclipse.

the class SignatureHelpHandlerTest method testSignatureHelp_invalid.

@Test
public void testSignatureHelp_invalid() 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 int bar(String s) { if (  }\n");
    buf.append("}\n");
    ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);
    SignatureHelp help = getSignatureHelp(cu, 2, 34);
    assertNotNull(help);
    assertEquals(0, help.getSignatures().size());
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) SignatureHelp(org.eclipse.lsp4j.SignatureHelp) Test(org.junit.Test)

Example 24 with SignatureHelp

use of org.eclipse.lsp4j.SignatureHelp in project eclipse.jdt.ls by eclipse.

the class SignatureHelpHandlerTest method testSignatureHelp_parameters.

// See https://github.com/eclipse/eclipse.jdt.ls/pull/1015#issuecomment-487997215
@Test
public void testSignatureHelp_parameters() 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 boolean bar() {\n");
    buf.append("     foo(\"\",)\n");
    buf.append("     return true;\n");
    buf.append("   }\n");
    buf.append("   public void foo(String s) {}\n");
    buf.append("   public void foo(String s, boolean bar) {}\n");
    buf.append("}\n");
    ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);
    SignatureHelp help = getSignatureHelp(cu, 3, 12);
    assertNotNull(help);
    assertEquals(2, help.getSignatures().size());
    assertEquals(help.getSignatures().get(help.getActiveSignature()).getLabel(), "foo(String s, boolean bar) : void");
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) SignatureHelp(org.eclipse.lsp4j.SignatureHelp) Test(org.junit.Test)

Example 25 with SignatureHelp

use of org.eclipse.lsp4j.SignatureHelp in project eclipse.jdt.ls by eclipse.

the class SignatureHelpHandlerTest method testSignatureHelp_constructorParameters2.

@Test
public void testSignatureHelp_constructorParameters2() 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(\"foo\")\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(4, help.getSignatures().size());
    assertTrue(help.getSignatures().get(help.getActiveSignature()).getLabel().matches("RuntimeException\\(String \\w+\\)"));
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) SignatureHelp(org.eclipse.lsp4j.SignatureHelp) Test(org.junit.Test)

Aggregations

SignatureHelp (org.eclipse.lsp4j.SignatureHelp)31 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)23 Test (org.junit.Test)21 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)17 SignatureInformation (org.eclipse.lsp4j.SignatureInformation)7 IJavaProject (org.eclipse.jdt.core.IJavaProject)5 IType (org.eclipse.jdt.core.IType)4 ArrayList (java.util.ArrayList)3 CoreException (org.eclipse.core.runtime.CoreException)2 CompletionProposal (org.eclipse.jdt.core.CompletionProposal)2 InternalCompletionProposal (org.eclipse.jdt.internal.codeassist.InternalCompletionProposal)2 SignatureHelpRequestor (org.eclipse.jdt.ls.core.internal.contentassist.SignatureHelpRequestor)2 ParameterInformation (org.eclipse.lsp4j.ParameterInformation)2 Joiner (com.google.common.base.Joiner)1 Objects (com.google.common.base.Objects)1 Preconditions (com.google.common.base.Preconditions)1 Strings (com.google.common.base.Strings)1 Iterables (com.google.common.collect.Iterables)1 Lists (com.google.common.collect.Lists)1 Inject (com.google.inject.Inject)1