Search in sources :

Example 1 with FunctionDescriptor

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

the class FormulaContentParser method handleVariable.

private void handleVariable(ExprVariable v) {
    // No variables, only functions
    String value = v.getName();
    int startIndex = contentToParse.length() - value.length() + 1;
    currentToken = new FunctionDescriptor();
    currentToken.setValue(value);
    currentToken.setStartIndex(startIndex);
    currentToken.setFunctionName(value);
    currentToken.setContentType(ContentType.FormulaFunction);
    currentToken.setCurrentArgIndex(-1);
    currentToken.setOpenBracket(false);
}
Also used : FunctionDescriptor(org.csstudio.autocomplete.parser.FunctionDescriptor)

Example 2 with FunctionDescriptor

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

the class FormulaContentParser method handleFunction.

private void handleFunction(ExprFunction f) {
    if (f.isComplete())
        return;
    currentToken = new FunctionDescriptor();
    currentToken.setValue(f.toString());
    currentToken.setFunctionName(f.getName());
    currentToken.setContentType(ContentType.FormulaFunction);
    currentToken.setOpenBracket(true);
    if (f.size() == 0) {
        currentToken.setCurrentArgIndex(0);
        return;
    }
    currentToken.setCurrentArgIndex(f.size() - 1);
    Expr lastArg = f.getArg(f.size() - 1);
    if (lastArg == null)
        return;
    handleExpr(lastArg);
}
Also used : Expr(org.csstudio.autocomplete.parser.engine.expr.Expr) FunctionDescriptor(org.csstudio.autocomplete.parser.FunctionDescriptor)

Example 3 with FunctionDescriptor

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

the class FormulaFunctionProvider method listResult.

@Override
public AutoCompleteResult listResult(final ContentDescriptor desc, final int limit) {
    AutoCompleteResult result = new AutoCompleteResult();
    FunctionDescriptor functionDesc = null;
    if (desc instanceof FunctionDescriptor) {
        functionDesc = (FunctionDescriptor) desc;
    } else {
        // empty result
        return result;
    }
    String nameToFind = functionDesc.getFunctionName();
    // handle proposals
    int count = 0;
    // insertionPos is not yet provided for formula
    // TODO: improve parser
    String originalContent = desc.getOriginalContent();
    int insertionPos = originalContent.lastIndexOf(nameToFind);
    if (!functionDesc.hasOpenBracket()) {
        Proposal topProposal = null;
        String closestMatchingFunction = null;
        for (String functionName : functions.keySet()) {
            if (functionName.startsWith(nameToFind)) {
                Proposal proposal = new Proposal(functionName + "(", false);
                String description = functions.get(functionName).get(0).getDescription() + "\n\n";
                for (FormulaFunction ff : functions.get(functionName)) description += generateSignature(ff);
                proposal.setDescription(description);
                for (FormulaFunction ff : functions.get(functionName)) proposal.addTooltipData(generateTooltipData(ff, 0));
                proposal.addStyle(ProposalStyle.getDefault(0, nameToFind.length() - 1));
                proposal.setInsertionPos(insertionPos);
                // display function icon
                proposal.setFunction(true);
                result.addProposal(proposal);
                count++;
                if (closestMatchingFunction == null || closestMatchingFunction.compareTo(functionName) > 0) {
                    closestMatchingFunction = functionName;
                    topProposal = proposal;
                }
            }
        }
        // handle top proposals
        if (closestMatchingFunction != null)
            result.addTopProposal(topProposal);
    }
    result.setCount(count);
    // handle tooltip
    if (functionDesc.hasOpenBracket() && !functionDesc.isComplete()) {
        for (String setName : FormulaRegistry.getDefault().listFunctionSets()) {
            FormulaFunctionSet set = FormulaRegistry.getDefault().findFunctionSet(setName);
            for (FormulaFunction function : set.findFunctions(nameToFind)) {
                if (function.getName().equals(nameToFind))
                    if (function.getArgumentNames().size() >= functionDesc.getArgs().size() || function.isVarArgs())
                        result.addTooltipData(generateTooltipData(function, functionDesc.getCurrentArgIndex()));
            }
        }
    }
    return result;
}
Also used : FormulaFunctionSet(org.diirt.datasource.formula.FormulaFunctionSet) FormulaFunction(org.diirt.datasource.formula.FormulaFunction) AutoCompleteResult(org.csstudio.autocomplete.AutoCompleteResult) FunctionDescriptor(org.csstudio.autocomplete.parser.FunctionDescriptor) Proposal(org.csstudio.autocomplete.proposals.Proposal)

Example 4 with FunctionDescriptor

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

the class SimContentParser method parse.

@Override
public ContentDescriptor parse(final ContentDescriptor desc) {
    int startIndex = 0;
    String contentToParse = desc.getValue();
    if (contentToParse.startsWith(SIM_SOURCE)) {
        contentToParse = contentToParse.substring(SIM_SOURCE.length());
    // startIndex = SIM_SOURCE.length();
    }
    FunctionDescriptor currentDesc = null;
    if (contentToParse.contains(AutoCompleteConstants.WILDCARD_MULTI_REPLACE) || contentToParse.contains(AutoCompleteConstants.WILDCARD_SINGLE_REPLACE)) {
        currentDesc = new FunctionDescriptor();
        currentDesc.setFunctionName(contentToParse);
    } else {
        currentDesc = ContentParserHelper.parseStandardFunction(contentToParse);
    }
    currentDesc.setContentType(SimContentType.SimFunction);
    currentDesc.setStartIndex(startIndex);
    currentDesc.setValue(contentToParse);
    return currentDesc;
}
Also used : FunctionDescriptor(org.csstudio.autocomplete.parser.FunctionDescriptor)

Example 5 with FunctionDescriptor

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

the class SimContentProvider method listResult.

@Override
public AutoCompleteResult listResult(final ContentDescriptor desc, final int limit) {
    AutoCompleteResult result = new AutoCompleteResult();
    FunctionDescriptor functionDesc = null;
    if (desc instanceof FunctionDescriptor) {
        functionDesc = (FunctionDescriptor) desc;
    } else {
        // empty result
        return result;
    }
    String functionName = functionDesc.getFunctionName();
    DSFunctionSet set = DSFunctionRegistry.getDefault().findFunctionSet(SimDSFunctionSet.name);
    // handle proposals
    int count = 0;
    if (!functionDesc.hasOpenBracket()) {
        String 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;
        int offset = SimContentParser.SIM_SOURCE.length();
        for (DSFunction function : set.getFunctions()) {
            Matcher m = valuePattern.matcher(function.getName());
            if (m.find()) {
                String proposalStr = function.getName();
                if (hasMandatoryArgument(function))
                    proposalStr += "(";
                if (desc.getDefaultDataSource() != SimContentParser.SIM_SOURCE)
                    proposalStr = SimContentParser.SIM_SOURCE + proposalStr;
                Proposal proposal = new Proposal(proposalStr, false);
                String description = function.getDescription() + "\n\n" + generateSignature(function);
                for (DSFunction poly : function.getPolymorphicFunctions()) description += "\n" + generateSignature(poly);
                proposal.setDescription(description);
                int currentArgIndex = -1;
                if (hasMandatoryArgument(function))
                    currentArgIndex = 0;
                proposal.addTooltipData(generateTooltipData(function, currentArgIndex));
                for (DSFunction 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 (DSFunction 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 (DSFunction poly : function.getPolymorphicFunctions()) if (checkToken(poly, functionDesc))
                    result.addTooltipData(generateTooltipData(poly, functionDesc.getCurrentArgIndex()));
            }
        }
    }
    return result;
}
Also used : Pattern(java.util.regex.Pattern) AutoCompleteResult(org.csstudio.autocomplete.AutoCompleteResult) Matcher(java.util.regex.Matcher) FunctionDescriptor(org.csstudio.autocomplete.parser.FunctionDescriptor) Proposal(org.csstudio.autocomplete.proposals.Proposal)

Aggregations

FunctionDescriptor (org.csstudio.autocomplete.parser.FunctionDescriptor)6 AutoCompleteResult (org.csstudio.autocomplete.AutoCompleteResult)2 Proposal (org.csstudio.autocomplete.proposals.Proposal)2 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 Expr (org.csstudio.autocomplete.parser.engine.expr.Expr)1 FormulaFunction (org.diirt.datasource.formula.FormulaFunction)1 FormulaFunctionSet (org.diirt.datasource.formula.FormulaFunctionSet)1