Search in sources :

Example 1 with Hint

use of org.structr.schema.action.Hint in project structr by structr.

the class AbstractHintProvider method getAllHints.

protected List<Hint> getAllHints(final GraphObject currentNode, final String currentToken, final String previousToken, final String thirdToken) {
    final boolean isDeclaration = isJavascript() && "var".equals(previousToken);
    final boolean isAssignment = isJavascript() && "=".equals(previousToken);
    final boolean isDotNotationRequest = ".".equals(currentToken);
    final ConfigurationProvider config = StructrApp.getConfiguration();
    final Map<String, DataKey> dataKeys = new TreeMap<>();
    final List<Hint> hints = new LinkedList<>();
    final List<Hint> local = new LinkedList<>();
    Class currentObjectType = null;
    // data key etc. hints
    if (currentNode != null) {
        recursivelyFindDataKeys(currentNode, dataKeys);
    }
    switch(previousToken) {
        case "current":
            currentObjectType = AbstractNode.class;
            break;
        case "this":
            currentObjectType = DOMNode.class;
            break;
        case "me":
            currentObjectType = User.class;
            break;
        case "page":
            currentObjectType = Page.class;
            break;
        case "link":
            currentObjectType = File.class;
            break;
        case "template":
            currentObjectType = Template.class;
            break;
        case "parent":
            currentObjectType = DOMElement.class;
            break;
        default:
            DataKey key = dataKeys.get(previousToken);
            if (key != null) {
                currentObjectType = key.identifyType(config);
            } else if (StringUtils.isNotBlank(thirdToken)) {
                key = dataKeys.get(thirdToken);
                if (key != null) {
                    currentObjectType = key.identifyType(config);
                    if (currentObjectType != null) {
                        final PropertyKey nestedKey = StructrApp.key(currentObjectType, previousToken);
                        if (nestedKey != null) {
                            currentObjectType = nestedKey.relatedType();
                        }
                    }
                }
            }
            break;
    }
    if (!keywords.contains(previousToken) && !isDotNotationRequest && !dataKeys.containsKey(previousToken)) {
        if (!isAssignment) {
            for (final Function<Object, Object> func : Functions.getFunctions()) {
                hints.add(func);
            }
        }
        Collections.sort(hints, comparator);
        // non-function hints
        local.add(createHint("current", "", "Current data object", !isJavascript() ? null : "get('current')"));
        local.add(createHint("request", "", "Current request object", !isJavascript() ? null : "get('request')"));
        local.add(createHint("this", "", "Current object", !isJavascript() ? null : "get('this')"));
        local.add(createHint("element", "", "Current object", !isJavascript() ? null : "get('element')"));
        local.add(createHint("page", "", "Current page", !isJavascript() ? null : "get('page')"));
        local.add(createHint("link", "", "Current link", !isJavascript() ? null : "get('link')"));
        local.add(createHint("template", "", "Closest template node", !isJavascript() ? null : "get('template')"));
        local.add(createHint("parent", "", "Parent node", !isJavascript() ? null : "get('parent')"));
        local.add(createHint("children", "", "Collection of child nodes", !isJavascript() ? null : "get('children')"));
        local.add(createHint("host", "", "Client's host name", !isJavascript() ? null : "get('host')"));
        local.add(createHint("port", "", "Client's port", !isJavascript() ? null : "get('port')"));
        local.add(createHint("path_info", "", "URL path", !isJavascript() ? null : "get('path_info')"));
        local.add(createHint("now", "", "Current date", !isJavascript() ? null : "get('now')"));
        local.add(createHint("me", "", "Current user", !isJavascript() ? null : "get('me)"));
        local.add(createHint("locale", "", "Current locale", !isJavascript() ? null : "get('locale')"));
    }
    // add local hints to the beginning of the list
    Collections.sort(local, comparator);
    hints.addAll(0, local);
    // prepend data keys
    if (currentObjectType == null && !dataKeys.containsKey(previousToken) && !isDotNotationRequest || isAssignment) {
        for (final DataKey dataKey : dataKeys.values()) {
            final String replacement = isJavascript() && !isDeclaration ? "get('" + dataKey.getDataKey() + "')" : null;
            final Hint dataKeyHint = createHint(dataKey.getDataKey(), "", dataKey.getDescription(), replacement);
            // disable replacement with "Structr.get(...)" when in Javascript declaration
            dataKeyHint.allowNameModification(!isDeclaration);
            hints.add(0, dataKeyHint);
        }
    }
    // prepend property keys of current object type
    collectHintsForType(hints, config, currentObjectType);
    return hints;
}
Also used : Hint(org.structr.schema.action.Hint) ConfigurationProvider(org.structr.schema.ConfigurationProvider) TreeMap(java.util.TreeMap) LinkedList(java.util.LinkedList) GraphObject(org.structr.core.GraphObject) PropertyKey(org.structr.core.property.PropertyKey)

Example 2 with Hint

use of org.structr.schema.action.Hint in project structr by structr.

the class AbstractHintProvider method collectHintsForType.

private void collectHintsForType(final List<Hint> hints, final ConfigurationProvider config, final Class type) {
    if (type != null) {
        final List<Hint> propertyHints = new LinkedList<>();
        // create hints based on schema type information
        for (final PropertyKey propertyKey : config.getPropertySet(type, PropertyView.All)) {
            final String keyName = propertyKey.jsonName();
            if (!keyName.startsWith(PropertyView.Html) && !keyName.startsWith("data-structr-")) {
                final Hint propertyHint = createHint(keyName, "", type.getSimpleName() + " property");
                // allow sorting by dynamic / static properties
                propertyHint.setIsDynamic(propertyKey.isDynamic());
                propertyHint.allowNameModification(false);
                propertyHints.add(propertyHint);
            }
        }
        Collections.sort(propertyHints, comparator);
        hints.addAll(0, propertyHints);
    }
}
Also used : Hint(org.structr.schema.action.Hint) LinkedList(java.util.LinkedList) PropertyKey(org.structr.core.property.PropertyKey)

Example 3 with Hint

use of org.structr.schema.action.Hint in project structr by structr.

the class AbstractHintProvider method getHints.

public List<GraphObject> getHints(final GraphObject currentEntity, final String type, final String currentToken, final String previousToken, final String thirdToken, final int cursorLine, final int cursorPosition) {
    final List<Hint> allHints = getAllHints(currentEntity, currentToken, previousToken, thirdToken);
    final List<GraphObject> hints = new LinkedList<>();
    int maxNameLength = 0;
    if (StringUtils.isBlank(currentToken) || startChars.contains(currentToken)) {
        // display all possible hints
        for (final Hint hint : allHints) {
            final GraphObjectMap item = new GraphObjectMap();
            String functionName = getFunctionName(hint.getReplacement());
            if (hint.mayModify()) {
                item.put(text, visitReplacement(functionName));
            } else {
                item.put(text, functionName);
            }
            item.put(displayText, getFunctionName(hint.getName()) + " - " + textOrPlaceholder(hint.shortDescription()));
            addPosition(item, hint, cursorLine, cursorPosition, cursorPosition);
            if (functionName.length() > maxNameLength) {
                maxNameLength = functionName.length();
            }
            hints.add(item);
        }
    } else {
        final int currentTokenLength = currentToken.length();
        for (final Hint hint : allHints) {
            final String functionName = getFunctionName(hint.getName());
            final String replacement = hint.getReplacement();
            if (functionName.startsWith(currentToken) || (currentToken.length() > 2 && functionName.contains(currentToken))) {
                final GraphObjectMap item = new GraphObjectMap();
                if (hint.mayModify()) {
                    item.put(text, visitReplacement(replacement));
                } else {
                    item.put(text, replacement);
                }
                item.put(displayText, getFunctionName(hint.getName()) + " - " + textOrPlaceholder(hint.shortDescription()));
                addPosition(item, hint, cursorLine, cursorPosition - currentTokenLength, cursorPosition);
                if (functionName.length() > maxNameLength) {
                    maxNameLength = functionName.length();
                }
                hints.add(item);
            }
        }
    }
    alignHintDescriptions(hints, maxNameLength);
    return hints;
}
Also used : Hint(org.structr.schema.action.Hint) GraphObjectMap(org.structr.core.GraphObjectMap) GraphObject(org.structr.core.GraphObject) LinkedList(java.util.LinkedList) Hint(org.structr.schema.action.Hint)

Aggregations

LinkedList (java.util.LinkedList)3 Hint (org.structr.schema.action.Hint)3 GraphObject (org.structr.core.GraphObject)2 PropertyKey (org.structr.core.property.PropertyKey)2 TreeMap (java.util.TreeMap)1 GraphObjectMap (org.structr.core.GraphObjectMap)1 ConfigurationProvider (org.structr.schema.ConfigurationProvider)1