Search in sources :

Example 1 with SNode

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

the class YamlCompletionEngine method getCompletions.

@Override
public Collection<ICompletionProposal> getCompletions(TextDocument _doc, int offset) throws Exception {
    YamlDocument doc = new YamlDocument(_doc, structureProvider);
    if (!doc.isCommented(offset)) {
        SRootNode root = doc.getStructure();
        SNode current = root.find(offset);
        int cursorIndent = doc.getColumn(offset);
        int nodeIndent = current.getIndent();
        int baseIndent = YamlIndentUtil.minIndent(cursorIndent, nodeIndent);
        List<SNode> contextNodes = getContextNodes(doc, current, offset, baseIndent);
        if (current.getNodeType() == SNodeType.RAW) {
            // relaxed indentation
            List<ICompletionProposal> completions = new ArrayList<>();
            double deempasizeBy = 0.0;
            for (SNode contextNode : contextNodes) {
                completions.addAll(getRelaxedCompletions(offset, doc, current, contextNode, baseIndent, deempasizeBy));
                deempasizeBy += ScoreableProposal.DEEMP_NEXT_CONTEXT;
            }
            return completions;
        } else {
            // precise indentation only
            Assert.isLegal(contextNodes.size() <= 1);
            for (SNode contextNode : contextNodes) {
                return getBaseCompletions(offset, doc, current, contextNode);
            }
        }
    }
    return Collections.emptyList();
}
Also used : SRootNode(org.springframework.ide.vscode.commons.yaml.structure.YamlStructureParser.SRootNode) SNode(org.springframework.ide.vscode.commons.yaml.structure.YamlStructureParser.SNode) YamlDocument(org.springframework.ide.vscode.commons.yaml.structure.YamlDocument) ICompletionProposal(org.springframework.ide.vscode.commons.languageserver.completion.ICompletionProposal) ArrayList(java.util.ArrayList)

Example 2 with SNode

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

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

the class YamlStructureParserTest method assertFind.

private void assertFind(MockYamlEditor editor, SRootNode root, String snippet, int... expectPath) {
    int start = editor.getRawText().indexOf(snippet);
    int end = start + snippet.length();
    int middle = (start + end) / 2;
    SNode expectNode = getNodeAtPath(root, expectPath);
    assertEquals(expectNode, root.find(start));
    assertEquals(expectNode, root.find(middle));
    assertEquals(expectNode, root.find(end));
}
Also used : SNode(org.springframework.ide.vscode.commons.yaml.structure.YamlStructureParser.SNode)

Example 4 with SNode

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

the class YamlStructureParserTest method testTreeEnd.

@Test
public void testTreeEnd() throws Exception {
    MockYamlEditor editor = new MockYamlEditor("world:\n" + "  europe:\n" + "    france:\n" + "      cheese\n" + "    belgium:\n" + // At same level as key, technically this is a syntax error but we tolerate it
    "    beer\n" + "  canada:\n" + "    montreal: poutine\n" + "    vancouver:\n" + "      salmon\n" + "moon:\n" + "  moonbase-alfa:\n" + "    moonstone\n");
    SRootNode root = editor.parseStructure();
    SNode node = getNodeAtPath(root, 0, 0, 1);
    assertTreeText(editor, node, "  canada:\n" + "    montreal: poutine\n" + "    vancouver:\n" + "      salmon\n");
    node = getNodeAtPath(root, 0, 0, 0, 1, 0);
    assertTreeText(editor, node, "beer");
}
Also used : SRootNode(org.springframework.ide.vscode.commons.yaml.structure.YamlStructureParser.SRootNode) SNode(org.springframework.ide.vscode.commons.yaml.structure.YamlStructureParser.SNode) Test(org.junit.Test)

Example 5 with SNode

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

the class YamlStructureParserTest method testTraverse.

@Test
public void testTraverse() throws Exception {
    MockYamlEditor editor = new MockYamlEditor("world:\n" + "  europe:\n" + "    france:\n" + "      cheese\n" + "    belgium:\n" + // At same level as key, technically this is a syntax error but we tolerate it
    "    beer\n" + "  canada:\n" + "    montreal: poutine\n" + "    vancouver:\n" + "      salmon\n" + "moon:\n" + "  moonbase-alfa:\n" + "    moonstone\n");
    SRootNode root = editor.parseStructure();
    YamlPath pathToFrance = pathWith(0, "world", "europe", "france");
    assertEquals("KEY(4): france:\n" + "  RAW(6): cheese\n", pathToFrance.traverse((SNode) root).toString());
    assertNull(pathWith(0, "world", "europe", "bogus").traverse((SNode) root));
}
Also used : YamlPath(org.springframework.ide.vscode.commons.yaml.path.YamlPath) SRootNode(org.springframework.ide.vscode.commons.yaml.structure.YamlStructureParser.SRootNode) SNode(org.springframework.ide.vscode.commons.yaml.structure.YamlStructureParser.SNode) Test(org.junit.Test)

Aggregations

SNode (org.springframework.ide.vscode.commons.yaml.structure.YamlStructureParser.SNode)13 SRootNode (org.springframework.ide.vscode.commons.yaml.structure.YamlStructureParser.SRootNode)6 YamlPath (org.springframework.ide.vscode.commons.yaml.path.YamlPath)5 Test (org.junit.Test)4 ArrayList (java.util.ArrayList)2 ICompletionProposal (org.springframework.ide.vscode.commons.languageserver.completion.ICompletionProposal)2 ValueParseException (org.springframework.ide.vscode.commons.util.ValueParseException)2 YamlPathSegment (org.springframework.ide.vscode.commons.yaml.path.YamlPathSegment)2 SNodeDynamicSchemaContext (org.springframework.ide.vscode.commons.yaml.schema.SNodeDynamicSchemaContext)2 Strings (com.google.common.base.Strings)1 ImmutableList (com.google.common.collect.ImmutableList)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1 Logger (org.slf4j.Logger)1 LoggerFactory (org.slf4j.LoggerFactory)1 DocumentEdits (org.springframework.ide.vscode.commons.languageserver.completion.DocumentEdits)1