Search in sources :

Example 1 with TopProposalFinder

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

the class AutoCompleteHistoryProvider method listResult.

@Override
public AutoCompleteResult listResult(final ContentDescriptor desc, final int limit) {
    String content = desc.getOriginalContent();
    int startIndex = 0;
    if (desc.getContentType().equals(ContentType.PVName)) {
        content = desc.getValue();
        startIndex = desc.getStartIndex();
    }
    AutoCompleteResult result = new AutoCompleteResult();
    String cleanedName = AutoCompleteHelper.trimWildcards(content);
    Pattern namePattern = AutoCompleteHelper.convertToPattern(cleanedName);
    if (namePattern == null)
        return result;
    String entryType = AutoCompleteTypes.PV;
    if (content.startsWith("="))
        entryType = AutoCompleteTypes.Formula;
    LinkedList<String> fifo = new LinkedList<String>();
    fifo.addAll(AutoCompleteUIPlugin.getDefault().getHistory(entryType));
    if (fifo.isEmpty())
        // Empty result
        return result;
    int count = 0;
    for (String entry : fifo) {
        Matcher m = namePattern.matcher(entry);
        if (m.find()) {
            if (count < limit) {
                Proposal proposal = new Proposal(entry, false);
                proposal.addStyle(ProposalStyle.getDefault(m.start(), m.end() - 1));
                proposal.setInsertionPos(startIndex);
                result.addProposal(proposal);
            }
            count++;
        }
    }
    result.setCount(count);
    TopProposalFinder trf = new TopProposalFinder(Preferences.getSeparators());
    for (Proposal p : trf.getTopProposals(Pattern.quote(cleanedName), fifo)) result.addTopProposal(p);
    return result;
}
Also used : Pattern(java.util.regex.Pattern) TopProposalFinder(org.csstudio.autocomplete.proposals.TopProposalFinder) AutoCompleteResult(org.csstudio.autocomplete.AutoCompleteResult) Matcher(java.util.regex.Matcher) LinkedList(java.util.LinkedList) Proposal(org.csstudio.autocomplete.proposals.Proposal)

Example 2 with TopProposalFinder

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

the class SysContentProvider method provideSystemProperties.

private AutoCompleteResult provideSystemProperties(final SysContentDescriptor sysDesc, final int limit) {
    AutoCompleteResult result = new AutoCompleteResult();
    int count = 0;
    int dotIndex = sysDesc.getValue().indexOf(SYSTEM_SEPARATOR);
    String propValue = sysDesc.getValue().substring(dotIndex + 1);
    String regex = propValue.replaceAll("\\.", "\\\\.");
    ;
    regex = regex.replaceAll("\\*", ".*");
    regex = regex.replaceAll("\\?", ".");
    Pattern valuePattern = null;
    try {
        // start with !
        valuePattern = Pattern.compile("^" + regex);
    } catch (Exception e) {
        // empty result
        return result;
    }
    List<String> matchingProperties = new ArrayList<String>();
    Properties systemProperties = System.getProperties();
    Enumeration<?> enuProp = systemProperties.propertyNames();
    int offset = SysContentParser.SYS_SOURCE.length() + 7;
    while (enuProp.hasMoreElements()) {
        String propertyName = (String) enuProp.nextElement();
        String propertyValue = systemProperties.getProperty(propertyName);
        Matcher m = valuePattern.matcher(propertyName);
        if (m.find()) {
            String propDisplay = SYSTEM_FUNCTION + SYSTEM_SEPARATOR + propertyName;
            if (sysDesc.getDefaultDataSource() != SysContentParser.SYS_SOURCE)
                propDisplay = SysContentParser.SYS_SOURCE + propDisplay;
            Proposal proposal = new Proposal(propDisplay, false);
            proposal.setDescription(propertyValue);
            proposal.addStyle(ProposalStyle.getDefault(0, offset + m.end() - 1));
            proposal.setInsertionPos(sysDesc.getStartIndex());
            if (count <= limit)
                result.addProposal(proposal);
            matchingProperties.add(propertyName);
            count++;
        }
    }
    // handle top proposals
    TopProposalFinder tpf = new TopProposalFinder(SYSTEM_SEPARATOR);
    for (Proposal tp : tpf.getTopProposals(propValue, matchingProperties)) {
        String propDisplay = SYSTEM_FUNCTION + SYSTEM_SEPARATOR + tp.getValue();
        if (sysDesc.getDefaultDataSource() != SysContentParser.SYS_SOURCE)
            propDisplay = SysContentParser.SYS_SOURCE + propDisplay;
        Proposal proposal = new Proposal(propDisplay, tp.isPartial());
        String propertyValue = systemProperties.getProperty(tp.getValue());
        proposal.setDescription(propertyValue);
        ProposalStyle tpStyle = tp.getStyles().get(0);
        proposal.addStyle(ProposalStyle.getDefault(tpStyle.from, (offset + tpStyle.to)));
        proposal.setInsertionPos(sysDesc.getStartIndex());
        result.addTopProposal(proposal);
    }
    result.setCount(count);
    return result;
}
Also used : Pattern(java.util.regex.Pattern) TopProposalFinder(org.csstudio.autocomplete.proposals.TopProposalFinder) AutoCompleteResult(org.csstudio.autocomplete.AutoCompleteResult) ProposalStyle(org.csstudio.autocomplete.proposals.ProposalStyle) Matcher(java.util.regex.Matcher) ArrayList(java.util.ArrayList) Properties(java.util.Properties) Proposal(org.csstudio.autocomplete.proposals.Proposal)

Aggregations

Matcher (java.util.regex.Matcher)2 Pattern (java.util.regex.Pattern)2 AutoCompleteResult (org.csstudio.autocomplete.AutoCompleteResult)2 Proposal (org.csstudio.autocomplete.proposals.Proposal)2 TopProposalFinder (org.csstudio.autocomplete.proposals.TopProposalFinder)2 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1 Properties (java.util.Properties)1 ProposalStyle (org.csstudio.autocomplete.proposals.ProposalStyle)1