Search in sources :

Example 1 with SignatureHelp

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

the class SignatureHelpHandlerTest method testSignatureHelp_multipeMethod.

@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(help.getSignatures().size(), 3);
    assertEquals(help.getActiveParameter(), (Integer) 1);
    assertEquals(help.getSignatures().get(help.getActiveSignature()).getLabel(), "foo(int s, String s) : int");
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) SignatureHelp(org.eclipse.lsp4j.SignatureHelp) Test(org.junit.Test)

Example 2 with SignatureHelp

use of org.eclipse.lsp4j.SignatureHelp in project ballerina by ballerina-lang.

the class SignatureHelpUtil method getFunctionSignatureHelp.

/**
 * Get the functionSignatureHelp instance.
 *
 * @param context                   Signature help context
 * @return {@link SignatureHelp}    Signature help for the completion
 */
public static SignatureHelp getFunctionSignatureHelp(TextDocumentServiceContext context) {
    // Get the functions List
    List<SymbolInfo> functions = context.get(SignatureKeys.FILTERED_FUNCTIONS);
    List<SignatureInformation> signatureInformationList = functions.stream().map(symbolInfo -> getSignatureInformation((BInvokableSymbol) symbolInfo.getScopeEntry().symbol, context)).filter(Objects::nonNull).collect(Collectors.toList());
    SignatureHelp signatureHelp = new SignatureHelp();
    signatureHelp.setSignatures(signatureInformationList);
    signatureHelp.setActiveParameter(context.get(SignatureKeys.PARAMETER_COUNT));
    signatureHelp.setActiveSignature(0);
    return signatureHelp;
}
Also used : SignatureInformation(org.eclipse.lsp4j.SignatureInformation) BInvokableSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BInvokableSymbol) SignatureHelp(org.eclipse.lsp4j.SignatureHelp) SymbolInfo(org.ballerinalang.langserver.completions.SymbolInfo)

Example 3 with SignatureHelp

use of org.eclipse.lsp4j.SignatureHelp in project ballerina by ballerina-lang.

the class BallerinaTextDocumentService method signatureHelp.

@Override
public CompletableFuture<SignatureHelp> signatureHelp(TextDocumentPositionParams position) {
    return CompletableFuture.supplyAsync(() -> {
        String uri = position.getTextDocument().getUri();
        String fileContent = this.documentManager.getFileContent(Paths.get(URI.create(uri)));
        TextDocumentServiceContext signatureContext = new TextDocumentServiceContext();
        SignatureHelpUtil.captureCallableItemInfo(position.getPosition(), fileContent, signatureContext);
        signatureContext.put(DocumentServiceKeys.POSITION_KEY, position);
        signatureContext.put(DocumentServiceKeys.FILE_URI_KEY, uri);
        SignatureHelp signatureHelp;
        try {
            BLangPackage bLangPackage = TextDocumentServiceUtil.getBLangPackage(signatureContext, documentManager, false, LSCustomErrorStrategy.class, false).get(0);
            signatureContext.put(DocumentServiceKeys.CURRENT_PACKAGE_NAME_KEY, bLangPackage.symbol.getName().getValue());
            SignatureTreeVisitor signatureTreeVisitor = new SignatureTreeVisitor(signatureContext);
            bLangPackage.accept(signatureTreeVisitor);
            signatureContext.put(DocumentServiceKeys.LS_PACKAGE_CACHE_KEY, lSPackageCache);
            signatureHelp = SignatureHelpUtil.getFunctionSignatureHelp(signatureContext);
        } catch (Exception e) {
            signatureHelp = new SignatureHelp();
        }
        return signatureHelp;
    });
}
Also used : BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage) SignatureHelp(org.eclipse.lsp4j.SignatureHelp) MarkedString(org.eclipse.lsp4j.MarkedString) LSCustomErrorStrategy(org.ballerinalang.langserver.common.LSCustomErrorStrategy) SignatureTreeVisitor(org.ballerinalang.langserver.signature.SignatureTreeVisitor)

Example 4 with SignatureHelp

use of org.eclipse.lsp4j.SignatureHelp in project xtext-core by eclipse.

the class AbstractLanguageServerTest method testSignatureHelp.

protected void testSignatureHelp(final Procedure1<? super SignatureHelpConfiguration> configurator) {
    try {
        @Extension final SignatureHelpConfiguration configuration = new SignatureHelpConfiguration();
        configuration.setFilePath(("MyModel." + this.fileExtension));
        configurator.apply(configuration);
        final String fileUri = this.initializeContext(configuration).getUri();
        SignatureHelpParams _signatureHelpParams = new SignatureHelpParams();
        final Procedure1<SignatureHelpParams> _function = (SignatureHelpParams it) -> {
            TextDocumentIdentifier _textDocumentIdentifier = new TextDocumentIdentifier(fileUri);
            it.setTextDocument(_textDocumentIdentifier);
            int _line = configuration.getLine();
            int _column = configuration.getColumn();
            Position _position = new Position(_line, _column);
            it.setPosition(_position);
        };
        SignatureHelpParams _doubleArrow = ObjectExtensions.<SignatureHelpParams>operator_doubleArrow(_signatureHelpParams, _function);
        final CompletableFuture<SignatureHelp> signatureHelpFuture = this.languageServer.signatureHelp(_doubleArrow);
        final SignatureHelp signatureHelp = signatureHelpFuture.get();
        Procedure1<? super SignatureHelp> _assertSignatureHelp = configuration.getAssertSignatureHelp();
        boolean _tripleNotEquals = (_assertSignatureHelp != null);
        if (_tripleNotEquals) {
            configuration.getAssertSignatureHelp().apply(signatureHelp);
        } else {
            final String actualSignatureHelp = this.toExpectation(signatureHelp);
            this.assertEquals(configuration.getExpectedSignatureHelp().trim(), actualSignatureHelp.trim());
        }
    } catch (Throwable _e) {
        throw Exceptions.sneakyThrow(_e);
    }
}
Also used : Extension(org.eclipse.xtext.xbase.lib.Extension) VersionedTextDocumentIdentifier(org.eclipse.lsp4j.VersionedTextDocumentIdentifier) TextDocumentIdentifier(org.eclipse.lsp4j.TextDocumentIdentifier) Position(org.eclipse.lsp4j.Position) SignatureHelp(org.eclipse.lsp4j.SignatureHelp) SignatureHelpParams(org.eclipse.lsp4j.SignatureHelpParams)

Example 5 with SignatureHelp

use of org.eclipse.lsp4j.SignatureHelp in project vscode-nextgenas by BowlerHatLLC.

the class ActionScriptTextDocumentService method signatureHelp.

/**
     * 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(TextDocumentPositionParams position) {
    String textDocumentUri = position.getTextDocument().getUri();
    if (!textDocumentUri.endsWith(AS_EXTENSION) && !textDocumentUri.endsWith(MXML_EXTENSION)) {
        //we couldn't find a node at the specified location
        return CompletableFuture.completedFuture(new SignatureHelp(Collections.emptyList(), -1, -1));
    }
    IASNode offsetNode = null;
    IMXMLTagData offsetTag = getOffsetMXMLTag(position);
    if (offsetTag != null) {
        IMXMLTagAttributeData attributeData = getMXMLTagAttributeWithValueAtOffset(offsetTag, currentOffset);
        if (attributeData != null) {
            //some attributes can have ActionScript completion, such as
            //events and properties with data binding
            IClassDefinition tagDefinition = (IClassDefinition) currentProject.resolveXMLNameToDefinition(offsetTag.getXMLName(), offsetTag.getMXMLDialect());
            IDefinition attributeDefinition = currentProject.resolveSpecifier(tagDefinition, attributeData.getShortName());
            if (attributeDefinition instanceof IEventDefinition) {
                IMXMLInstanceNode mxmlNode = (IMXMLInstanceNode) getOffsetNode(position);
                IMXMLEventSpecifierNode eventNode = mxmlNode.getEventSpecifierNode(attributeData.getShortName());
                for (IASNode asNode : eventNode.getASNodes()) {
                    IASNode containingNode = getContainingNodeIncludingStart(asNode, currentOffset);
                    if (containingNode != null) {
                        offsetNode = containingNode;
                    }
                }
                if (offsetNode == null) {
                    offsetNode = eventNode;
                }
            }
        }
    }
    if (offsetNode == null) {
        offsetNode = getOffsetNode(position);
    }
    if (offsetNode == null) {
        //we couldn't find a node at the specified location
        return CompletableFuture.completedFuture(new SignatureHelp(Collections.emptyList(), -1, -1));
    }
    IFunctionCallNode functionCallNode = getAncestorFunctionCallNode(offsetNode);
    IFunctionDefinition functionDefinition = null;
    if (functionCallNode != null) {
        IExpressionNode nameNode = functionCallNode.getNameNode();
        IDefinition definition = nameNode.resolve(currentUnit.getProject());
        if (definition instanceof IFunctionDefinition) {
            functionDefinition = (IFunctionDefinition) definition;
        } else if (definition instanceof IClassDefinition) {
            IClassDefinition classDefinition = (IClassDefinition) definition;
            functionDefinition = classDefinition.getConstructor();
        } else if (nameNode instanceof IIdentifierNode) {
            //special case for super()
            IIdentifierNode identifierNode = (IIdentifierNode) nameNode;
            if (identifierNode.getName().equals(IASKeywordConstants.SUPER)) {
                ITypeDefinition typeDefinition = nameNode.resolveType(currentProject);
                if (typeDefinition instanceof IClassDefinition) {
                    IClassDefinition classDefinition = (IClassDefinition) typeDefinition;
                    functionDefinition = classDefinitionToConstructor(classDefinition);
                }
            }
        }
    }
    if (functionDefinition != null) {
        SignatureHelp result = new SignatureHelp();
        List<SignatureInformation> signatures = new ArrayList<>();
        SignatureInformation signatureInfo = new SignatureInformation();
        signatureInfo.setLabel(getSignatureLabel(functionDefinition));
        List<ParameterInformation> parameters = new ArrayList<>();
        for (IParameterDefinition param : functionDefinition.getParameters()) {
            ParameterInformation paramInfo = new ParameterInformation();
            paramInfo.setLabel(param.getBaseName());
            parameters.add(paramInfo);
        }
        signatureInfo.setParameters(parameters);
        signatures.add(signatureInfo);
        result.setSignatures(signatures);
        result.setActiveSignature(0);
        int index = getFunctionCallNodeArgumentIndex(functionCallNode, offsetNode);
        IParameterDefinition[] params = functionDefinition.getParameters();
        int paramCount = params.length;
        if (paramCount > 0 && index >= paramCount) {
            if (index >= paramCount) {
                IParameterDefinition lastParam = params[paramCount - 1];
                if (lastParam.isRest()) {
                    //functions with rest parameters may accept any
                    //number of arguments, so continue to make the rest
                    //parameter active
                    index = paramCount - 1;
                } else {
                    //if there's no rest parameter, and we're beyond the
                    //final parameter, none should be active
                    index = -1;
                }
            }
        }
        if (index != -1) {
            result.setActiveParameter(index);
        }
        return CompletableFuture.completedFuture(result);
    }
    return CompletableFuture.completedFuture(new SignatureHelp(Collections.emptyList(), -1, -1));
}
Also used : IMXMLEventSpecifierNode(org.apache.flex.compiler.tree.mxml.IMXMLEventSpecifierNode) IClassDefinition(org.apache.flex.compiler.definitions.IClassDefinition) IMXMLInstanceNode(org.apache.flex.compiler.tree.mxml.IMXMLInstanceNode) ITypeDefinition(org.apache.flex.compiler.definitions.ITypeDefinition) ArrayList(java.util.ArrayList) IIdentifierNode(org.apache.flex.compiler.tree.as.IIdentifierNode) SignatureHelp(org.eclipse.lsp4j.SignatureHelp) IMXMLTagData(org.apache.flex.compiler.mxml.IMXMLTagData) IFunctionCallNode(org.apache.flex.compiler.tree.as.IFunctionCallNode) ParameterInformation(org.eclipse.lsp4j.ParameterInformation) IDefinition(org.apache.flex.compiler.definitions.IDefinition) IFunctionDefinition(org.apache.flex.compiler.definitions.IFunctionDefinition) SignatureInformation(org.eclipse.lsp4j.SignatureInformation) IASNode(org.apache.flex.compiler.tree.as.IASNode) IParameterDefinition(org.apache.flex.compiler.definitions.IParameterDefinition) IEventDefinition(org.apache.flex.compiler.definitions.IEventDefinition) IExpressionNode(org.apache.flex.compiler.tree.as.IExpressionNode) IMXMLTagAttributeData(org.apache.flex.compiler.mxml.IMXMLTagAttributeData)

Aggregations

SignatureHelp (org.eclipse.lsp4j.SignatureHelp)10 SignatureInformation (org.eclipse.lsp4j.SignatureInformation)5 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)4 ArrayList (java.util.ArrayList)3 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)3 Test (org.junit.Test)3 ParameterInformation (org.eclipse.lsp4j.ParameterInformation)2 SignatureHelpParams (org.eclipse.lsp4j.SignatureHelpParams)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 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 Comparator (java.util.Comparator)1 List (java.util.List)1 IClassDefinition (org.apache.flex.compiler.definitions.IClassDefinition)1