org.eclipse.lsp4j.SignatureHelp Java Examples

The following examples show how to use org.eclipse.lsp4j.SignatureHelp. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SignatureHelpHandlerTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testSignatureHelp_javadoc() 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("/**\n");
	buf.append(" * @see String#substring()\n");
	buf.append(" */\n");
	buf.append("   public int test() {}\n");
	buf.append("}\n");
	ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);
	SignatureHelp help = getSignatureHelp(cu, 3, 25);
	assertNotNull(help);
	assertEquals(2, help.getSignatures().size());
	assertTrue(help.getSignatures().get(help.getActiveSignature()).getLabel().matches("substring\\(\\w+ \\w+\\) : String"));
}
 
Example #2
Source File: SignatureHelpHandlerTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
@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+\\)"));
}
 
Example #3
Source File: SignatureHelpServiceImpl.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public SignatureHelp getSignatureHelp(final Document document, final XtextResource resource, final SignatureHelpParams params, final CancelIndicator cancelIndicator) {
  final int offset = document.getOffSet(params.getPosition());
  Preconditions.<XtextResource>checkNotNull(resource, "resource");
  Preconditions.checkArgument((offset >= 0), ("offset >= 0. Was: " + Integer.valueOf(offset)));
  final EObject object = this.offsetHelper.resolveContainedElementAt(resource, offset);
  if ((object instanceof OperationCall)) {
    final String operationName = this.getOperationName(((OperationCall)object));
    boolean _isNullOrEmpty = StringExtensions.isNullOrEmpty(operationName);
    boolean _not = (!_isNullOrEmpty);
    if (_not) {
      return this.getSignatureHelp(((OperationCall)object), operationName, offset);
    }
  }
  return ISignatureHelpService.EMPTY;
}
 
Example #4
Source File: SignatureHelpHandlerTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
@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(4, help.getSignatures().size());
	assertTrue(help.getSignatures().get(help.getActiveSignature()).getLabel().matches("RuntimeException\\(String \\w+, Throwable \\w+\\)"));
}
 
Example #5
Source File: SignatureHelpHandlerTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
@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(4, help.getSignatures().size());
	assertEquals(help.getSignatures().get(help.getActiveSignature()).getLabel(), "RuntimeException()");
}
 
Example #6
Source File: SignatureHelpHandlerTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testSignatureHelp_activeSignature() 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("     foo(\"a\",\"b\");\n");
	buf.append("   }\n");
	buf.append("   public void foo(String s) {}\n");
	buf.append("   public void foo(String s, String b) {}\n");
	buf.append("   public void foo() {}\n");
	buf.append("}\n");
	ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);
	SignatureHelp help = getSignatureHelp(cu, 3, 12);
	assertNotNull(help);
	assertEquals(3, help.getSignatures().size());
	assertEquals(help.getSignatures().get(help.getActiveSignature()).getLabel(), "foo(String s, String b) : void");
}
 
Example #7
Source File: SignatureHelpHandlerTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testSignatureHelp_parameterObject() 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 static void main(String[] args) {\n");
	buf.append("		 Object foo = new Object();\n");
	buf.append("		 System.err.println(foo);\n");
	buf.append("	}\n");
	buf.append("}\n");
	ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);
	SignatureHelp help = getSignatureHelp(cu, 4, 23);
	assertNotNull(help);
	assertEquals(10, help.getSignatures().size());
	assertNotNull(help.getActiveParameter());
	assertTrue(help.getSignatures().get(help.getActiveSignature()).getLabel().matches("println\\(Object \\w+\\) : void"));
}
 
Example #8
Source File: SignatureHelpHandlerTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testSignatureHelp_parameterTypes() 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 static void main(String[] args) {\n");
	buf.append("		 new RuntimeException(new Exception(),)\n");
	buf.append("	}\n");
	buf.append("}\n");
	ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);
	SignatureHelp help = getSignatureHelp(cu, 3, 40);
	assertNotNull(help);
	assertEquals(4, help.getSignatures().size());
	assertNull(help.getActiveParameter());
}
 
Example #9
Source File: SignatureHelpHandlerTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
@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");
}
 
Example #10
Source File: SignatureHelpRequestor.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
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);
			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;
}
 
Example #11
Source File: SignatureHelpHandlerTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testSignatureHelp_javadocOriginal() throws JavaModelException {
	IPackageFragment pack1 = sourceFolder.createPackageFragment("test1", false, null);
	StringBuilder buf = new StringBuilder();
	buf.append("package test1;\n");
	buf.append("import java.util.LinkedList;\n");
	buf.append("import org.sample.MyList;\n");
	buf.append("public class E {\n\n");
	buf.append("	void test() {\n");
	buf.append("		MyList<String> l = new LinkedList<>();\n");
	buf.append("		l.add(\n");
	buf.append("	}\n");
	buf.append("}\n");
	ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);
	int[] loc = findCompletionLocation(cu, "l.add(");
	SignatureHelp help = getSignatureHelp(cu, loc[0], loc[1]);
	assertNotNull(help);
	assertEquals(2, help.getSignatures().size());
	SignatureInformation signature = help.getSignatures().get(help.getActiveSignature());
	assertTrue(signature.getLabel().equals("add(String e) : boolean"));
	String documentation = signature.getDocumentation().getLeft();
	assertEquals(" Test ", documentation);
}
 
Example #12
Source File: SignatureHelpHandlerTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testSignatureHelp_varargs() throws JavaModelException {
	IPackageFragment pack1 = sourceFolder.createPackageFragment("test1", false, null);
	StringBuilder buf = new StringBuilder();
	buf.append("package test1;\n");
	buf.append("import java.util.Arrays;\n");
	buf.append("public class E {\n");
	buf.append("	public static void main(String[] args) {\n");
	buf.append("		Arrays.asList(1,2,3);\n");
	buf.append("		demo(\"1\", \"2\",\"3\" )\n");
	buf.append("	}\n");
	buf.append("	public static void demo (String s, String... s2) {\n");
	buf.append("	}\n");
	buf.append("}\n");
	ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);
	SignatureHelp help = getSignatureHelp(cu, 4, 21);
	assertNotNull(help);
	assertEquals(1, help.getSignatures().size());
	assertTrue(help.getSignatures().get(help.getActiveSignature()).getLabel().startsWith("asList(T... "));
	help = getSignatureHelp(cu, 5, 19);
	assertNotNull(help);
	assertEquals(1, help.getSignatures().size());
	assertTrue(help.getSignatures().get(help.getActiveSignature()).getLabel().equals("demo(String s, String... s2) : void"));
}
 
Example #13
Source File: SignatureHelpHandlerTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
@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(0).getLabel());
	assertTrue(help.getSignatures().get(0).getDocumentation().getLeft().length() > 0);
	assertEquals((Integer) 0, help.getActiveParameter());
}
 
Example #14
Source File: SignatureHelpHandlerTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testSignatureHelp_multipeMethod() 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 foo(String s) { }\n");
	buf.append("   public int foo(int s) { }\n");
	buf.append("   public int foo(int s, String s) { }\n");
	buf.append("   public int bar(String s) { this.foo(2,  ) }\n");
	buf.append("}\n");
	ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);

	SignatureHelp help = getSignatureHelp(cu, 5, 42);
	assertNotNull(help);
	assertEquals(3, help.getSignatures().size());
	assertEquals((Integer) 1, help.getActiveParameter());
	assertEquals(help.getSignatures().get(help.getActiveSignature()).getLabel(), "foo(int s, String s) : int");
}
 
Example #15
Source File: SignatureHelpHandlerTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testSignatureHelp_binary() 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) { System.out.println(  }\n");
	buf.append("}\n");
	ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);

	SignatureHelp help = getSignatureHelp(cu, 2, 50);
	assertNotNull(help);
	assertTrue(help.getSignatures().size() >= 10);
	assertTrue(help.getSignatures().get(help.getActiveSignature()).getLabel().equals("println() : void"));
	SignatureHelp help2 = getSignatureHelp(cu, 2, 49);
	assertEquals(help.getSignatures().size(), help2.getSignatures().size());
	assertEquals(help.getActiveSignature(), help2.getActiveSignature());
}
 
Example #16
Source File: SignatureHelpHandlerTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testSignatureHelp_varargs2() throws JavaModelException {
	IJavaProject javaProject = JavaCore.create(project);
	IType type = javaProject.findType("test1.Varargs");
	ICompilationUnit cu = type.getCompilationUnit();
	SignatureHelp help = getSignatureHelp(cu, 4, 16);
	assertNotNull(help);
	assertEquals(2, help.getSignatures().size());
	assertTrue(help.getSignatures().get(help.getActiveSignature()).getLabel().equals("run(Class<?> clazz, String... args) : void"));
}
 
Example #17
Source File: GroovyServicesSignatureHelpTests.java    From groovy-language-server with Apache License 2.0 5 votes vote down vote up
@Test
void testSignatureHelpOnMethod() throws Exception {
	Path filePath = srcRoot.resolve("Completion.groovy");
	String uri = filePath.toUri().toString();
	StringBuilder contents = new StringBuilder();
	contents.append("class SignatureHelp {\n");
	contents.append("  public SignatureHelp() {\n");
	contents.append("    method(\n");
	contents.append("  }\n");
	contents.append("  public void method(int param0) {}\n");
	contents.append("}");
	TextDocumentItem textDocumentItem = new TextDocumentItem(uri, LANGUAGE_GROOVY, 1, contents.toString());
	services.didOpen(new DidOpenTextDocumentParams(textDocumentItem));
	TextDocumentIdentifier textDocument = new TextDocumentIdentifier(uri);
	Position position = new Position(2, 11);
	SignatureHelp signatureHelp = services.signatureHelp(new TextDocumentPositionParams(textDocument, position))
			.get();
	List<SignatureInformation> signatures = signatureHelp.getSignatures();
	Assertions.assertEquals(1, signatures.size());
	SignatureInformation signature = signatures.get(0);
	Assertions.assertEquals("public void method(int param0)", signature.getLabel());
	List<ParameterInformation> params = signature.getParameters();
	Assertions.assertEquals(1, params.size());
	ParameterInformation param0 = params.get(0);
	Assertions.assertEquals("int param0", param0.getLabel().get());
	Assertions.assertEquals((int) 0, (int) signatureHelp.getActiveSignature());
	Assertions.assertEquals((int) 0, (int) signatureHelp.getActiveParameter());
}
 
Example #18
Source File: SignatureHelpHandlerTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testSignatureHelp_lambda() throws JavaModelException {
	IJavaProject javaProject = JavaCore.create(project);
	IType type = javaProject.findType("test1.SignatureHelp");
	ICompilationUnit cu = type.getCompilationUnit();
	SignatureHelp help = getSignatureHelp(cu, 8, 14);
	assertNotNull(help);
	assertEquals(1, help.getSignatures().size());
	assertTrue(help.getSignatures().get(help.getActiveSignature()).getLabel().equals("test(Function<String,String> f) : void"));
}
 
Example #19
Source File: SignatureHelpHandlerTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private void testStringLiteral(ICompilationUnit cu, int line, int character) {
	SignatureHelp help = getSignatureHelp(cu, line, character);
	assertNotNull(help);
	assertEquals(1, help.getSignatures().size());
	assertNotNull(help.getActiveParameter());
	assertTrue(help.getSignatures().get(help.getActiveSignature()).getLabel().equals("foo(String p, int x) : void"));
}
 
Example #20
Source File: SignatureHelpHandlerTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private void testAssertEquals(ICompilationUnit cu, int line, int character) {
	SignatureHelp help = getSignatureHelp(cu, line, character);
	assertNotNull(help);
	assertEquals(12, help.getSignatures().size());
	assertNotNull(help.getActiveParameter());
	assertTrue(help.getSignatures().get(help.getActiveSignature()).getLabel().equals("assertEquals(long expected, long actual) : void"));
}
 
Example #21
Source File: ActionScriptServices.java    From vscode-as3mxml with Apache License 2.0 5 votes vote down vote up
/**
 * Displays a function's parameters, including which one is currently
 * active. Called automatically by VSCode any time that the user types "(",
 * so be sure to check that a function call is actually happening at the
 * current position.
 */
@Override
public CompletableFuture<SignatureHelp> signatureHelp(SignatureHelpParams params)
{
    return CompletableFutures.computeAsync(compilerWorkspace.getExecutorService(), cancelToken ->
    {
        cancelToken.checkCanceled();

        //make sure that the latest changes have been passed to
        //workspace.fileChanged() before proceeding
        if(realTimeProblemsChecker != null)
        {
            realTimeProblemsChecker.updateNow();
        }

        compilerWorkspace.startBuilding();
        try
        {
            SignatureHelpProvider provider = new SignatureHelpProvider(workspaceFolderManager, fileTracker);
            return provider.signatureHelp(params, cancelToken);
        }
        finally
        {
            compilerWorkspace.doneBuilding();
        }
    });
}
 
Example #22
Source File: AbstractLanguageServerTest.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
protected String _toExpectation(final SignatureHelp it) {
  String _xblockexpression = null;
  {
    int _size = it.getSignatures().size();
    boolean _tripleEquals = (_size == 0);
    if (_tripleEquals) {
      StringConcatenation _builder = new StringConcatenation();
      _builder.append("Signature index is expected to be null when no signatures are available. Was: ");
      Integer _activeSignature = it.getActiveSignature();
      _builder.append(_activeSignature);
      _builder.append(".");
      Assert.assertNull(_builder.toString(), 
        it.getActiveSignature());
      return "<empty>";
    }
    Assert.assertNotNull("Active signature index must not be null when signatures are available.", it.getActiveSignature());
    String _xifexpression = null;
    Integer _activeParameter = it.getActiveParameter();
    boolean _tripleEquals_1 = (_activeParameter == null);
    if (_tripleEquals_1) {
      _xifexpression = "<empty>";
    } else {
      _xifexpression = it.getSignatures().get((it.getActiveSignature()).intValue()).getParameters().get(
        (it.getActiveParameter()).intValue()).getLabel().getLeft();
    }
    final String param = _xifexpression;
    StringConcatenation _builder_1 = new StringConcatenation();
    final Function1<SignatureInformation, String> _function = (SignatureInformation it_1) -> {
      return it_1.getLabel();
    };
    String _join = IterableExtensions.join(ListExtensions.<SignatureInformation, String>map(it.getSignatures(), _function), " | ");
    _builder_1.append(_join);
    _builder_1.append(" | ");
    _builder_1.append(param);
    _xblockexpression = _builder_1.toString();
  }
  return _xblockexpression;
}
 
Example #23
Source File: LanguageServerImpl.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Compute the signature help. Executed in a read request.
 * @since 2.20
 */
protected SignatureHelp signatureHelp(SignatureHelpParams params, CancelIndicator cancelIndicator) {
	URI uri = getURI(params);
	ISignatureHelpService helper = getService(uri, ISignatureHelpService.class);
	if (helper == null) {
		return ISignatureHelpService.EMPTY;
	}
	return workspaceManager.doRead(uri,
			(doc, resource) -> helper.getSignatureHelp(doc, resource, params, cancelIndicator));
}
 
Example #24
Source File: SignatureHelpHandler.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private boolean isSameParameters(IMethod m, SignatureHelp help, SignatureHelpRequestor collector, IJavaProject javaProject) throws JavaModelException {
	if (m == null || help == null || javaProject == null) {
		return false;
	}
	List<SignatureInformation> infos = help.getSignatures();
	for (int i = 0; i < infos.size(); i++) {
		CompletionProposal proposal = collector.getInfoProposals().get(infos.get(i));
		IMethod method = JDTUtils.resolveMethod(proposal, javaProject);
		if (isSameParameters(method, m)) {
			return true;
		}
	}
	return false;
}
 
Example #25
Source File: GroovyServicesSignatureHelpTests.java    From groovy-language-server with Apache License 2.0 5 votes vote down vote up
@Test
void testSignatureHelpOnMethodWithMultipleParameters() throws Exception {
	Path filePath = srcRoot.resolve("Completion.groovy");
	String uri = filePath.toUri().toString();
	StringBuilder contents = new StringBuilder();
	contents.append("class SignatureHelp {\n");
	contents.append("  public SignatureHelp() {\n");
	contents.append("    method(\n");
	contents.append("  }\n");
	contents.append("  public void method(int param0, String param1) {}\n");
	contents.append("}");
	TextDocumentItem textDocumentItem = new TextDocumentItem(uri, LANGUAGE_GROOVY, 1, contents.toString());
	services.didOpen(new DidOpenTextDocumentParams(textDocumentItem));
	TextDocumentIdentifier textDocument = new TextDocumentIdentifier(uri);
	Position position = new Position(2, 11);
	SignatureHelp signatureHelp = services.signatureHelp(new TextDocumentPositionParams(textDocument, position))
			.get();
	List<SignatureInformation> signatures = signatureHelp.getSignatures();
	Assertions.assertEquals(1, signatures.size());
	SignatureInformation signature = signatures.get(0);
	Assertions.assertEquals("public void method(int param0, String param1)", signature.getLabel());
	List<ParameterInformation> params = signature.getParameters();
	Assertions.assertEquals(2, params.size());
	ParameterInformation param0 = params.get(0);
	Assertions.assertEquals("int param0", param0.getLabel().get());
	ParameterInformation param1 = params.get(1);
	Assertions.assertEquals("String param1", param1.getLabel().get());
	Assertions.assertEquals((int) 0, (int) signatureHelp.getActiveSignature());
	Assertions.assertEquals((int) 0, (int) signatureHelp.getActiveParameter());
}
 
Example #26
Source File: GroovyServicesSignatureHelpTests.java    From groovy-language-server with Apache License 2.0 5 votes vote down vote up
@Test
void testSignatureHelpOnMethodWithActiveParameter() throws Exception {
	Path filePath = srcRoot.resolve("Completion.groovy");
	String uri = filePath.toUri().toString();
	StringBuilder contents = new StringBuilder();
	contents.append("class SignatureHelp {\n");
	contents.append("  public SignatureHelp() {\n");
	contents.append("    method(123,\n");
	contents.append("  }\n");
	contents.append("  public void method(int param0, String param1) {}\n");
	contents.append("}");
	TextDocumentItem textDocumentItem = new TextDocumentItem(uri, LANGUAGE_GROOVY, 1, contents.toString());
	services.didOpen(new DidOpenTextDocumentParams(textDocumentItem));
	TextDocumentIdentifier textDocument = new TextDocumentIdentifier(uri);
	Position position = new Position(2, 15);
	SignatureHelp signatureHelp = services.signatureHelp(new TextDocumentPositionParams(textDocument, position))
			.get();
	List<SignatureInformation> signatures = signatureHelp.getSignatures();
	Assertions.assertEquals(1, signatures.size());
	SignatureInformation signature = signatures.get(0);
	Assertions.assertEquals("public void method(int param0, String param1)", signature.getLabel());
	List<ParameterInformation> params = signature.getParameters();
	Assertions.assertEquals(2, params.size());
	ParameterInformation param0 = params.get(0);
	Assertions.assertEquals("int param0", param0.getLabel().get());
	ParameterInformation param1 = params.get(1);
	Assertions.assertEquals("String param1", param1.getLabel().get());
	Assertions.assertEquals((int) 0, (int) signatureHelp.getActiveSignature());
	Assertions.assertEquals((int) 1, (int) signatureHelp.getActiveParameter());
}
 
Example #27
Source File: StringLSP4J.java    From n4js with Eclipse Public License 1.0 5 votes vote down vote up
/** @return string for given element */
public String toString(SignatureHelp signatureHelp) {
	if (signatureHelp == null) {
		return "";
	}

	String str = Strings.join(", ",
			signatureHelp.getActiveSignature(),
			signatureHelp.getActiveParameter(),
			Strings.toString(this::toString, signatureHelp.getSignatures()));

	return "(" + str + ")";
}
 
Example #28
Source File: XLanguageServerImpl.java    From n4js with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public CompletableFuture<SignatureHelp> signatureHelp(TextDocumentPositionParams params) {
	URI uri = getURI(params);
	return openFilesManager.runInOpenFileContext(uri, "signatureHelp", (ofc, ci) -> {
		return signatureHelp(ofc, params, ci);
	});
}
 
Example #29
Source File: XLanguageServerImpl.java    From n4js with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Compute the signature help. Executed in a read request.
 */
protected SignatureHelp signatureHelp(OpenFileContext ofc, TextDocumentPositionParams params,
		CancelIndicator cancelIndicator) {
	URI uri = ofc.getURI();
	ISignatureHelpService helper = getService(uri, ISignatureHelpService.class);
	if (helper == null) {
		return ISignatureHelpService.EMPTY;
	}
	XtextResource res = ofc.getResource();
	XDocument doc = ofc.getDocument();
	return helper.getSignatureHelp(doc, res, params, cancelIndicator);
}
 
Example #30
Source File: SignatureHelpHandlerTest.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
@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());
}