Search in sources :

Example 1 with SignatureHelpRequestor

use of org.eclipse.jdt.ls.core.internal.contentassist.SignatureHelpRequestor in project eclipse.jdt.ls by eclipse.

the class SignatureHelpHandler method signatureHelp.

public SignatureHelp signatureHelp(TextDocumentPositionParams position, IProgressMonitor monitor) {
    SignatureHelp help = new SignatureHelp();
    if (!preferenceManager.getPreferences(null).isSignatureHelpEnabled()) {
        return help;
    }
    try {
        ICompilationUnit unit = JDTUtils.resolveCompilationUnit(position.getTextDocument().getUri());
        final int offset = JsonRpcHelpers.toOffset(unit.getBuffer(), position.getPosition().getLine(), position.getPosition().getCharacter());
        int[] contextInfomation = getContextInfomation(unit.getBuffer(), offset);
        if (contextInfomation[0] == -1) {
            return help;
        }
        SignatureHelpRequestor collector = new SignatureHelpRequestor(unit, contextInfomation[0] + 1);
        if (offset > -1 && !monitor.isCanceled()) {
            unit.codeComplete(contextInfomation[0] + 1, collector, monitor);
            help = collector.getSignatureHelp(monitor);
            if (help != null) {
                int currentParameter = contextInfomation[1];
                List<SignatureInformation> infos = help.getSignatures();
                for (int i = 0; i < infos.size(); i++) {
                    if (infos.get(i).getParameters().size() >= currentParameter + 1) {
                        help.setActiveSignature(i);
                        help.setActiveParameter(currentParameter);
                        break;
                    }
                }
            }
        }
    } catch (CoreException ex) {
        JavaLanguageServerPlugin.logException("Find signatureHelp failure ", ex);
    }
    return help;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) SignatureInformation(org.eclipse.lsp4j.SignatureInformation) CoreException(org.eclipse.core.runtime.CoreException) SignatureHelp(org.eclipse.lsp4j.SignatureHelp) SignatureHelpRequestor(org.eclipse.jdt.ls.core.internal.contentassist.SignatureHelpRequestor)

Example 2 with SignatureHelpRequestor

use of org.eclipse.jdt.ls.core.internal.contentassist.SignatureHelpRequestor in project eclipse.jdt.ls by eclipse.

the class SignatureHelpHandler method signatureHelp.

public SignatureHelp signatureHelp(SignatureHelpParams position, IProgressMonitor monitor) {
    SignatureHelp help = new SignatureHelp();
    if (!preferenceManager.getPreferences(null).isSignatureHelpEnabled()) {
        return help;
    }
    try {
        ICompilationUnit unit = JDTUtils.resolveCompilationUnit(position.getTextDocument().getUri());
        final int offset = JsonRpcHelpers.toOffset(unit.getBuffer(), position.getPosition().getLine(), position.getPosition().getCharacter());
        int[] contextInfomation = getContextInfomation(unit.getBuffer(), offset);
        ASTNode node = getNode(unit, contextInfomation, monitor);
        if (node == null) {
            return help;
        }
        IMethod method = getMethod(node);
        String name = method != null ? method.getElementName() : getMethodName(node, unit, contextInfomation);
        SignatureHelpRequestor collector = new SignatureHelpRequestor(unit, name, contextInfomation[0] + 1);
        if (offset > -1 && !monitor.isCanceled()) {
            unit.codeComplete(contextInfomation[0] + 1, collector, monitor);
            help = collector.getSignatureHelp(monitor);
            if (help.getSignatures().isEmpty()) {
                int pos = offset;
                if (method != null) {
                    int start;
                    if (node instanceof MethodInvocation) {
                        start = ((MethodInvocation) node).getName().getStartPosition();
                    } else if (node instanceof ClassInstanceCreation) {
                        start = ((ClassInstanceCreation) node).getType().getStartPosition();
                    } else {
                        start = node.getStartPosition();
                    }
                    pos = start + method.getElementName().length();
                }
                unit.codeComplete(pos, collector, monitor);
                help = collector.getSignatureHelp(monitor);
                addConstructorProposals(help, node, method, collector, pos);
            }
            if (!monitor.isCanceled() && help != null) {
                SignatureHelp help2 = null;
                SignatureHelpRequestor collector2 = null;
                if (contextInfomation[0] + 1 != offset) {
                    collector2 = new SignatureHelpRequestor(unit, offset, name, true);
                    unit.codeComplete(offset, collector2, monitor);
                    help2 = collector2.getSignatureHelp(monitor);
                }
                int currentParameter = contextInfomation[1];
                int size = currentParameter + 1;
                List<SignatureInformation> infos = help.getSignatures();
                int activeParameter = currentParameter < 0 ? 0 : currentParameter;
                if (node != null) {
                    IJavaProject javaProject = unit.getJavaProject();
                    if (help2 != null) {
                        if (method != null) {
                            for (int i = 0; i < infos.size(); i++) {
                                if (infos.get(i).getParameters().size() >= size) {
                                    CompletionProposal proposal = collector.getInfoProposals().get(infos.get(i));
                                    IMethod m = JDTUtils.resolveMethod(proposal, javaProject, monitor);
                                    if (!monitor.isCanceled() && JDTUtils.isSameParameters(m, method)) {
                                        help.setActiveSignature(i);
                                        help.setActiveParameter(activeParameter);
                                        return help;
                                    }
                                }
                            }
                        }
                        if (!monitor.isCanceled() && help.getActiveSignature() == null) {
                            for (int i = 0; i < infos.size(); i++) {
                                if (infos.get(i).getParameters().size() >= size) {
                                    CompletionProposal proposal = collector.getInfoProposals().get(infos.get(i));
                                    IMethod m = JDTUtils.resolveMethod(proposal, javaProject, monitor);
                                    if (!monitor.isCanceled() && isSameParameters(m, help2, collector2, javaProject, monitor)) {
                                        help.setActiveSignature(i);
                                        help.setActiveParameter(activeParameter);
                                        return help;
                                    }
                                    for (CompletionProposal typeProposal : collector2.getTypeProposals()) {
                                        if (isSameParameters(m, method, typeProposal)) {
                                            help.setActiveSignature(i);
                                            help.setActiveParameter(activeParameter);
                                            return help;
                                        }
                                    }
                                }
                            }
                        }
                    }
                    if (!monitor.isCanceled() && help.getActiveSignature() == null) {
                        if (method != null) {
                            for (int i = 0; i < infos.size(); i++) {
                                if (infos.get(i).getParameters().size() >= size) {
                                    CompletionProposal proposal = collector.getInfoProposals().get(infos.get(i));
                                    IMethod m = JDTUtils.resolveMethod(proposal, javaProject, monitor);
                                    if (!monitor.isCanceled() && JDTUtils.isSameParameters(method, m)) {
                                        help.setActiveSignature(i);
                                        help.setActiveParameter(activeParameter);
                                        return help;
                                    }
                                }
                            }
                        }
                    }
                    if (!monitor.isCanceled() && help.getActiveSignature() == null) {
                        for (int i = 0; i < infos.size(); i++) {
                            CompletionProposal proposal = collector.getInfoProposals().get(infos.get(i));
                            if (Flags.isVarargs(proposal.getFlags())) {
                                help.setActiveSignature(i);
                                char[][] infoTypes = Signature.getParameterTypes(SignatureUtil.fix83600(proposal.getSignature()));
                                if (infoTypes.length <= activeParameter) {
                                    help.setActiveParameter(infoTypes.length - 1);
                                } else {
                                    help.setActiveParameter(activeParameter);
                                }
                                return help;
                            }
                        }
                    }
                    if (!monitor.isCanceled() && help.getActiveSignature() == null && node instanceof Block) {
                        String methodName = getMethodName(node, unit, contextInfomation);
                        for (int i = 0; i < infos.size(); i++) {
                            if (infos.get(i).getParameters().size() >= activeParameter) {
                                CompletionProposal proposal = collector.getInfoProposals().get(infos.get(i));
                                IMethod m = JDTUtils.resolveMethod(proposal, javaProject, monitor);
                                if (!monitor.isCanceled() && m != null && m.getElementName().equals(methodName)) {
                                    help.setActiveSignature(i);
                                    help.setActiveParameter(activeParameter);
                                    return help;
                                }
                            }
                        }
                    }
                    if (method != null && !monitor.isCanceled() && help.getActiveSignature() == null) {
                        for (int i = 0; i < infos.size(); i++) {
                            if (infos.get(i).getParameters().size() >= size) {
                                CompletionProposal proposal = collector.getInfoProposals().get(infos.get(i));
                                IMethod m = JDTUtils.resolveMethod(proposal, javaProject, monitor);
                                if (!monitor.isCanceled() && isSameParameters(m, method, null)) {
                                    help.setActiveSignature(i);
                                    help.setActiveParameter(activeParameter);
                                    return help;
                                }
                            }
                        }
                    }
                }
            }
        }
    } catch (CoreException ex) {
        JavaLanguageServerPlugin.logException("Find signatureHelp failure ", ex);
    }
    return help;
}
Also used : ClassInstanceCreation(org.eclipse.jdt.core.dom.ClassInstanceCreation) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) InternalCompletionProposal(org.eclipse.jdt.internal.codeassist.InternalCompletionProposal) CompletionProposal(org.eclipse.jdt.core.CompletionProposal) SignatureHelp(org.eclipse.lsp4j.SignatureHelp) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) SignatureInformation(org.eclipse.lsp4j.SignatureInformation) IJavaProject(org.eclipse.jdt.core.IJavaProject) CoreException(org.eclipse.core.runtime.CoreException) ASTNode(org.eclipse.jdt.core.dom.ASTNode) Block(org.eclipse.jdt.core.dom.Block) IMethod(org.eclipse.jdt.core.IMethod) SignatureHelpRequestor(org.eclipse.jdt.ls.core.internal.contentassist.SignatureHelpRequestor)

Aggregations

CoreException (org.eclipse.core.runtime.CoreException)2 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)2 SignatureHelpRequestor (org.eclipse.jdt.ls.core.internal.contentassist.SignatureHelpRequestor)2 SignatureHelp (org.eclipse.lsp4j.SignatureHelp)2 SignatureInformation (org.eclipse.lsp4j.SignatureInformation)2 CompletionProposal (org.eclipse.jdt.core.CompletionProposal)1 IJavaProject (org.eclipse.jdt.core.IJavaProject)1 IMethod (org.eclipse.jdt.core.IMethod)1 ASTNode (org.eclipse.jdt.core.dom.ASTNode)1 Block (org.eclipse.jdt.core.dom.Block)1 ClassInstanceCreation (org.eclipse.jdt.core.dom.ClassInstanceCreation)1 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)1 InternalCompletionProposal (org.eclipse.jdt.internal.codeassist.InternalCompletionProposal)1