Search in sources :

Example 6 with Proposal

use of org.csstudio.autocomplete.proposals.Proposal 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 7 with Proposal

use of org.csstudio.autocomplete.proposals.Proposal 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)

Example 8 with Proposal

use of org.csstudio.autocomplete.proposals.Proposal in project yamcs-studio by yamcs.

the class SysContentProvider method provideFunctions.

private AutoCompleteResult provideFunctions(final SysContentDescriptor sysDesc, final int limit) {
    AutoCompleteResult result = new AutoCompleteResult();
    int count = 0;
    String regex = sysDesc.getValue();
    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;
    String closestMatchingFunction = null;
    int offset = SysContentParser.SYS_SOURCE.length();
    for (String function : SysContentDescriptor.listFunctions()) {
        Matcher m = valuePattern.matcher(function);
        if (m.find()) {
            String fctDisplay = function;
            if (sysDesc.getDefaultDataSource() != SysContentParser.SYS_SOURCE)
                fctDisplay = SysContentParser.SYS_SOURCE + function;
            if (function.equals(SYSTEM_FUNCTION))
                fctDisplay += SYSTEM_SEPARATOR;
            Proposal proposal = new Proposal(fctDisplay, false);
            proposal.setDescription(SysContentDescriptor.getDescription(function));
            proposal.addStyle(ProposalStyle.getDefault(0, offset + m.end() - 1));
            proposal.setInsertionPos(sysDesc.getStartIndex());
            if (count <= limit)
                result.addProposal(proposal);
            count++;
            if (closestMatchingFunction == null || closestMatchingFunction.compareTo(function) > 0) {
                closestMatchingFunction = function;
                topProposal = proposal;
            }
        }
    }
    // handle top proposals
    if (closestMatchingFunction != null)
        result.addTopProposal(topProposal);
    result.setCount(count);
    return result;
}
Also used : Pattern(java.util.regex.Pattern) AutoCompleteResult(org.csstudio.autocomplete.AutoCompleteResult) Matcher(java.util.regex.Matcher) Proposal(org.csstudio.autocomplete.proposals.Proposal)

Example 9 with Proposal

use of org.csstudio.autocomplete.proposals.Proposal in project yamcs-studio by yamcs.

the class AutoCompleteProposalProvider method getProposals.

/**
 * {@inheritDoc}
 */
@Override
public void getProposals(final String contents, final IContentProposalSearchHandler handler) {
    currentId = System.currentTimeMillis();
    synchronized (currentList) {
        currentList.clear();
        currentList.setOriginalValue(contents);
    }
    AutoCompleteService cns = AutoCompleteService.getInstance();
    int expected = cns.get(currentId, AutoCompleteType.valueOf(type), contents, new IAutoCompleteResultListener() {

        @Override
        public void handleResult(Long uniqueId, Integer index, AutoCompleteResult result) {
            if (uniqueId == currentId) {
                synchronized (currentList) {
                    currentList.responseReceived();
                }
                if (result == null)
                    return;
                List<Proposal> contentProposals = new ArrayList<Proposal>();
                if (result.getProposals() != null)
                    for (final Proposal proposal : result.getProposals()) contentProposals.add(proposal);
                Proposal[] contentProposalsArray = contentProposals.toArray(new Proposal[contentProposals.size()]);
                List<Proposal> topContentProposals = new ArrayList<Proposal>();
                if (result.getTopProposals() != null)
                    for (final Proposal proposal : result.getTopProposals()) topContentProposals.add(proposal);
                ContentProposalList cpl = null;
                synchronized (currentList) {
                    if (result.getProvider() != null)
                        currentList.addProposals(result.getProvider(), contentProposalsArray, result.getCount(), index);
                    currentList.addTopProposals(topContentProposals);
                    cpl = currentList.clone();
                }
                handler.handleResult(cpl);
                handler.handleTooltips(result.getTooltips());
            // System.out.println("PROCESSED: " + uniqueId + ", " + index);
            }
        }
    });
    currentList.setExpected(expected);
}
Also used : ContentProposalList(org.csstudio.autocomplete.ui.content.ContentProposalList) AutoCompleteResult(org.csstudio.autocomplete.AutoCompleteResult) AutoCompleteService(org.csstudio.autocomplete.AutoCompleteService) IAutoCompleteResultListener(org.csstudio.autocomplete.IAutoCompleteResultListener) List(java.util.List) ContentProposalList(org.csstudio.autocomplete.ui.content.ContentProposalList) ArrayList(java.util.ArrayList) Proposal(org.csstudio.autocomplete.proposals.Proposal)

Example 10 with Proposal

use of org.csstudio.autocomplete.proposals.Proposal in project yamcs-studio by yamcs.

the class ContentProposalList method getTopProposalList.

public List<Proposal> getTopProposalList() {
    List<Proposal> list = new ArrayList<Proposal>();
    if (topProposalList.size() == 0) {
        return list;
    }
    Proposal originalTopProposal = new Proposal(originalValue, hasContentMatchingProposal ? false : true);
    originalTopProposal.addStyle(ProposalStyle.getDefault(0, originalValue.length()));
    if (topProposalList.size() == 1 && !originalValue.contains("*") && topProposalList.get(0).getStartWithContent() == true) {
        list.add(topProposalList.get(0));
        list.add(originalTopProposal);
    } else {
        list.add(originalTopProposal);
        int index = 0;
        for (Proposal tp : topProposalList) {
            if (maxTopProposals == 0) {
                list.add(tp);
            } else if (index <= maxTopProposals - 1) {
                list.add(tp);
            }
            index++;
        }
    }
    // We do not display top proposals if the content match a proposal and all
    // provided top proposals match a complete proposal
    boolean allComplete = true;
    for (Proposal tp : list) if (tp.isPartial())
        allComplete = false;
    if (allComplete)
        list.clear();
    return list;
}
Also used : ArrayList(java.util.ArrayList) Proposal(org.csstudio.autocomplete.proposals.Proposal)

Aggregations

Proposal (org.csstudio.autocomplete.proposals.Proposal)19 AutoCompleteResult (org.csstudio.autocomplete.AutoCompleteResult)9 Matcher (java.util.regex.Matcher)5 Pattern (java.util.regex.Pattern)5 Point (org.eclipse.swt.graphics.Point)5 ArrayList (java.util.ArrayList)3 ProposalStyle (org.csstudio.autocomplete.proposals.ProposalStyle)3 Display (org.eclipse.swt.widgets.Display)3 TableItem (org.eclipse.swt.widgets.TableItem)3 FunctionDescriptor (org.csstudio.autocomplete.parser.FunctionDescriptor)2 TopProposalFinder (org.csstudio.autocomplete.proposals.TopProposalFinder)2 ControlEvent (org.eclipse.swt.events.ControlEvent)2 DisposeEvent (org.eclipse.swt.events.DisposeEvent)2 DisposeListener (org.eclipse.swt.events.DisposeListener)2 FocusEvent (org.eclipse.swt.events.FocusEvent)2 SelectionEvent (org.eclipse.swt.events.SelectionEvent)2 SelectionListener (org.eclipse.swt.events.SelectionListener)2 Color (org.eclipse.swt.graphics.Color)2 Font (org.eclipse.swt.graphics.Font)2 FontData (org.eclipse.swt.graphics.FontData)2