Search in sources :

Example 6 with FunctionDescriptor

use of org.yamcs.studio.autocomplete.parser.FunctionDescriptor in project yamcs-studio by yamcs.

the class SimContentProvider method listResult.

@Override
public AutoCompleteResult listResult(ContentDescriptor desc, int limit) {
    var result = new AutoCompleteResult();
    FunctionDescriptor functionDesc = null;
    if (desc instanceof FunctionDescriptor) {
        functionDesc = (FunctionDescriptor) desc;
    } else {
        // empty result
        return result;
    }
    var functionName = functionDesc.getFunctionName();
    var set = DSFunctionRegistry.getDefault().findFunctionSet(SimDSFunctionSet.name);
    // handle proposals
    var count = 0;
    if (!functionDesc.hasOpenBracket()) {
        var regex = functionName;
        regex = regex.replaceAll("\\*", ".*");
        regex = regex.replaceAll("\\?", ".");
        Pattern valuePattern = null;
        try {
            // start with !
            valuePattern = Pattern.compile("^" + regex);
        } catch (Exception e) {
            // empty result
            return result;
        }
        Proposal topProposal = null;
        DSFunction closestMatchingFunction = null;
        var offset = SimContentParser.SIM_SOURCE.length();
        for (var function : set.getFunctions()) {
            var m = valuePattern.matcher(function.getName());
            if (m.find()) {
                var proposalStr = function.getName();
                if (hasMandatoryArgument(function)) {
                    proposalStr += "(";
                }
                proposalStr = SimContentParser.SIM_SOURCE + proposalStr;
                var proposal = new Proposal(proposalStr, false);
                var description = function.getDescription() + "\n\n" + generateSignature(function);
                for (var poly : function.getPolymorphicFunctions()) {
                    description += "\n" + generateSignature(poly);
                }
                proposal.setDescription(description);
                var currentArgIndex = -1;
                if (hasMandatoryArgument(function)) {
                    currentArgIndex = 0;
                }
                proposal.addTooltipData(generateTooltipData(function, currentArgIndex));
                for (var poly : function.getPolymorphicFunctions()) {
                    proposal.addTooltipData(generateTooltipData(poly, currentArgIndex));
                }
                proposal.addStyle(ProposalStyle.getDefault(0, offset + m.end() - 1));
                proposal.setInsertionPos(desc.getStartIndex());
                result.addProposal(proposal);
                count++;
                if (closestMatchingFunction == null || closestMatchingFunction.getName().compareTo(function.getName()) > 0) {
                    closestMatchingFunction = function;
                    topProposal = proposal;
                }
            }
        }
        // handle top proposals
        if (closestMatchingFunction != null && !functionName.isEmpty()) {
            result.addTopProposal(topProposal);
        }
    }
    result.setCount(count);
    // handle tooltip
    if (!functionDesc.isComplete()) {
        for (var function : set.findFunctions(functionName)) {
            // no tooltip for incomplete functions => use proposals
            if (function.getName().equals(functionName)) {
                if (checkToken(function, functionDesc)) {
                    result.addTooltipData(generateTooltipData(function, functionDesc.getCurrentArgIndex()));
                }
                for (var poly : function.getPolymorphicFunctions()) {
                    if (checkToken(poly, functionDesc)) {
                        result.addTooltipData(generateTooltipData(poly, functionDesc.getCurrentArgIndex()));
                    }
                }
            }
        }
    }
    return result;
}
Also used : Pattern(java.util.regex.Pattern) AutoCompleteResult(org.yamcs.studio.autocomplete.AutoCompleteResult) FunctionDescriptor(org.yamcs.studio.autocomplete.parser.FunctionDescriptor) Proposal(org.yamcs.studio.autocomplete.proposals.Proposal)

Aggregations

FunctionDescriptor (org.yamcs.studio.autocomplete.parser.FunctionDescriptor)6 AutoCompleteResult (org.yamcs.studio.autocomplete.AutoCompleteResult)2 Proposal (org.yamcs.studio.autocomplete.proposals.Proposal)2 Pattern (java.util.regex.Pattern)1