Search in sources :

Example 1 with YamlPathSegment

use of org.springframework.ide.vscode.commons.yaml.path.YamlPathSegment in project sts4 by spring-projects.

the class YamlPathEdits method createPath.

/**
 * Create the necessary edits to ensure that a given property
 * path exists, placing cursor in the right place also to start
 * start typing the property value.
 * <p>
 * This also handles cases where all or some of the path already
 * exists. In the former case no edits are performed only cursor
 * movement. In the latter case, the right place to start inserting
 * the 'missing' portion of the path is found and the edits
 * are created there.
 */
public void createPath(SChildBearingNode node, YamlPath path, String appendText) throws Exception {
    // This code doesn't handle selection of subddocuments
    // or creation of new subdocuments so must not call it on
    // ROOT node but start at an appropriate SDocNode (or below)
    Assert.isLegal(node.getNodeType() != SNodeType.ROOT);
    if (!path.isEmpty()) {
        YamlPathSegment s = path.getSegment(0);
        if (s.getType() == YamlPathSegmentType.VAL_AT_KEY) {
            String key = s.toPropString();
            SKeyNode existing = node.getChildWithKey(key);
            if (existing == null) {
                createNewPath(node, path, appendText);
            } else {
                createPath(existing, path.tail(), appendText);
            }
        }
    } else {
        // whole path already exists. Just try to move cursor somewhere
        // sensible in the existing tail-end-node of the path.
        SNode child = node.getFirstRealChild();
        if (child != null) {
            moveCursorTo(child.getStart());
        } else if (node.getNodeType() == SNodeType.KEY) {
            SKeyNode keyNode = (SKeyNode) node;
            int colonOffset = keyNode.getColonOffset();
            char c = doc.getChar(colonOffset + 1);
            if (c == ' ') {
                // cursor after the ": "
                moveCursorTo(colonOffset + 2);
            } else {
                // cursor after the ":"
                moveCursorTo(colonOffset + 1);
            }
        }
    }
}
Also used : SKeyNode(org.springframework.ide.vscode.commons.yaml.structure.YamlStructureParser.SKeyNode) SNode(org.springframework.ide.vscode.commons.yaml.structure.YamlStructureParser.SNode) YamlPathSegment(org.springframework.ide.vscode.commons.yaml.path.YamlPathSegment)

Example 2 with YamlPathSegment

use of org.springframework.ide.vscode.commons.yaml.path.YamlPathSegment in project sts4 by spring-projects.

the class HintProviders method forMap.

public static HintProvider forMap(HintProvider _keyProvider, HintProvider _valueProvider, final Type valueType, final boolean dimensionAware) {
    final HintProvider keyProvider = notNull(_keyProvider);
    final HintProvider valueProvider = notNull(_valueProvider);
    if (isNull(keyProvider) && isNull(valueProvider)) {
        return NULL;
    }
    return new HintProvider() {

        @Override
        public HintProvider traverse(YamlPathSegment s) throws Exception {
            switch(s.getType()) {
                case VAL_AT_INDEX:
                case VAL_AT_KEY:
                    if (dimensionAware) {
                        return forHere(valueProvider);
                    } else {
                        return forAllValueContexts(valueProvider);
                    }
                default:
                    return NULL;
            }
        }

        @Override
        public List<StsValueHint> getValueHints(String query) {
            if (dimensionAware) {
                // pickier, completions only suggested in the domain of map, but not for map itself.
                return ImmutableList.of();
            } else {
                return valueProvider.getValueHints(query);
            }
        }

        @Override
        public List<TypedProperty> getPropertyHints(String query) {
            List<StsValueHint> keyHints = keyProvider.getValueHints(query);
            if (CollectionUtil.hasElements(keyHints)) {
                List<TypedProperty> props = new ArrayList<>(keyHints.size());
                for (StsValueHint keyHint : keyHints) {
                    Object key = keyHint.getValue();
                    if (key instanceof String) {
                        props.add(new TypedProperty((String) key, valueType, null));
                    }
                }
                return props;
            }
            return ImmutableList.of();
        }
    };
}
Also used : YamlPathSegment(org.springframework.ide.vscode.commons.yaml.path.YamlPathSegment) ArrayList(java.util.ArrayList) TypedProperty(org.springframework.ide.vscode.boot.metadata.types.TypedProperty)

Aggregations

YamlPathSegment (org.springframework.ide.vscode.commons.yaml.path.YamlPathSegment)2 ArrayList (java.util.ArrayList)1 TypedProperty (org.springframework.ide.vscode.boot.metadata.types.TypedProperty)1 SKeyNode (org.springframework.ide.vscode.commons.yaml.structure.YamlStructureParser.SKeyNode)1 SNode (org.springframework.ide.vscode.commons.yaml.structure.YamlStructureParser.SNode)1