Search in sources :

Example 1 with TopProposalFinder

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

the class SysContentProvider method provideSystemProperties.

private AutoCompleteResult provideSystemProperties(SysContentDescriptor sysDesc, int limit) {
    var result = new AutoCompleteResult();
    var count = 0;
    var dotIndex = sysDesc.getValue().indexOf(SYSTEM_SEPARATOR);
    var propValue = sysDesc.getValue().substring(dotIndex + 1);
    var 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<>();
    var systemProperties = System.getProperties();
    Enumeration<?> enuProp = systemProperties.propertyNames();
    var offset = SysContentParser.SYS_SOURCE.length() + 7;
    while (enuProp.hasMoreElements()) {
        var propertyName = (String) enuProp.nextElement();
        var propertyValue = systemProperties.getProperty(propertyName);
        var m = valuePattern.matcher(propertyName);
        if (m.find()) {
            var propDisplay = SYSTEM_FUNCTION + SYSTEM_SEPARATOR + propertyName;
            propDisplay = SysContentParser.SYS_SOURCE + propDisplay;
            var 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
    var tpf = new TopProposalFinder(SYSTEM_SEPARATOR);
    for (var tp : tpf.getTopProposals(propValue, matchingProperties)) {
        var propDisplay = SYSTEM_FUNCTION + SYSTEM_SEPARATOR + tp.getValue();
        propDisplay = SysContentParser.SYS_SOURCE + propDisplay;
        var proposal = new Proposal(propDisplay, tp.isPartial());
        var propertyValue = systemProperties.getProperty(tp.getValue());
        proposal.setDescription(propertyValue);
        var 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.yamcs.studio.autocomplete.proposals.TopProposalFinder) AutoCompleteResult(org.yamcs.studio.autocomplete.AutoCompleteResult) ArrayList(java.util.ArrayList) Proposal(org.yamcs.studio.autocomplete.proposals.Proposal)

Aggregations

ArrayList (java.util.ArrayList)1 Pattern (java.util.regex.Pattern)1 AutoCompleteResult (org.yamcs.studio.autocomplete.AutoCompleteResult)1 Proposal (org.yamcs.studio.autocomplete.proposals.Proposal)1 TopProposalFinder (org.yamcs.studio.autocomplete.proposals.TopProposalFinder)1