Search in sources :

Example 1 with SKeyNode

use of org.springframework.ide.vscode.commons.yaml.structure.YamlStructureParser.SKeyNode in project sts4 by spring-projects.

the class AbstractYamlAssistContext method getCustomAssistRegion.

protected DocumentRegion getCustomAssistRegion(YamlDocument doc, SNode node, int offset) {
    if (node.getNodeType() == SNodeType.KEY) {
        SKeyNode keyNode = (SKeyNode) node;
        if (keyNode.isInValue(offset)) {
            int valueStart = keyNode.getColonOffset() + 1;
            // assumes we only look at the current line, good enough for now
            int valueEnd = keyNode.getNodeEnd();
            DocumentRegion region = new DocumentRegion(doc.getDocument(), valueStart, valueEnd);
            if (region.startsWith(" ")) {
                region = region.subSequence(1);
            }
            return region;
        }
    }
    // TODO Reaching here might mean support for calling the custom assistant isn't
    return null;
// implemented for this kind of context yet. It will have to be expanded upon
// as the need for it arises in real use-cases.
}
Also used : SKeyNode(org.springframework.ide.vscode.commons.yaml.structure.YamlStructureParser.SKeyNode) DocumentRegion(org.springframework.ide.vscode.commons.util.text.DocumentRegion)

Example 2 with SKeyNode

use of org.springframework.ide.vscode.commons.yaml.structure.YamlStructureParser.SKeyNode 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 3 with SKeyNode

use of org.springframework.ide.vscode.commons.yaml.structure.YamlStructureParser.SKeyNode in project sts4 by spring-projects.

the class YamlStructureParserTest method assertValueRange.

private void assertValueRange(MockYamlEditor editor, SRootNode root, String nodeText, String expectedValue) throws Exception {
    int start = editor.getText().indexOf(nodeText);
    SKeyNode node = (SKeyNode) root.find(start);
    int valueRangeStart;
    int valueRangeEnd;
    if (expectedValue == null) {
        valueRangeStart = valueRangeEnd = start + nodeText.length();
    } else {
        valueRangeStart = editor.getRawText().lastIndexOf(expectedValue);
        valueRangeEnd = valueRangeStart + expectedValue.length();
        assertEquals(expectedValue, editor.textBetween(valueRangeStart, valueRangeEnd));
    }
    assertTrue(node.isInValue(valueRangeStart));
    assertFalse(node.isInValue(valueRangeStart - 1));
    assertTrue(node.isInValue(valueRangeEnd));
    assertFalse(node.isInValue(valueRangeEnd + 1));
}
Also used : SKeyNode(org.springframework.ide.vscode.commons.yaml.structure.YamlStructureParser.SKeyNode)

Example 4 with SKeyNode

use of org.springframework.ide.vscode.commons.yaml.structure.YamlStructureParser.SKeyNode in project sts4 by spring-projects.

the class YamlCompletionEngine method isBarrenKey.

private boolean isBarrenKey(SNode node) {
    try {
        if (node.getNodeType() == SNodeType.KEY) {
            SKeyNode keyNode = (SKeyNode) node;
            String value = keyNode.getSimpleValue();
            return value.trim().isEmpty();
        }
    } catch (Exception e) {
        Log.log(e);
    }
    return false;
}
Also used : SKeyNode(org.springframework.ide.vscode.commons.yaml.structure.YamlStructureParser.SKeyNode)

Example 5 with SKeyNode

use of org.springframework.ide.vscode.commons.yaml.structure.YamlStructureParser.SKeyNode in project sts4 by spring-projects.

the class YamlStructureParserTest method assertKey.

private void assertKey(MockYamlEditor editor, SRootNode root, String nodeText, String expectedKey) throws Exception {
    int start = editor.getText().indexOf(nodeText);
    SKeyNode node = (SKeyNode) root.find(start);
    String key = node.getKey();
    assertEquals(expectedKey, key);
    // test the key range as well
    int startOfKeyRange = node.getStart();
    int keyRangeLen = key.length();
    int endOfKeyRange = startOfKeyRange + keyRangeLen;
    assertTrue(node.isInKey(startOfKeyRange));
    assertFalse(node.isInKey(startOfKeyRange - 1));
    assertTrue(node.isInKey(endOfKeyRange));
    assertFalse(node.isInKey(endOfKeyRange + 1));
}
Also used : SKeyNode(org.springframework.ide.vscode.commons.yaml.structure.YamlStructureParser.SKeyNode)

Aggregations

SKeyNode (org.springframework.ide.vscode.commons.yaml.structure.YamlStructureParser.SKeyNode)6 SNode (org.springframework.ide.vscode.commons.yaml.structure.YamlStructureParser.SNode)2 DocumentRegion (org.springframework.ide.vscode.commons.util.text.DocumentRegion)1 YamlPathSegment (org.springframework.ide.vscode.commons.yaml.path.YamlPathSegment)1 SDocNode (org.springframework.ide.vscode.commons.yaml.structure.YamlStructureParser.SDocNode)1