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();
}
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);
}
}
}
}
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));
}
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");
}
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));
}
Aggregations