use of org.springframework.ide.vscode.commons.yaml.structure.YamlStructureParser.SNode in project sts4 by spring-projects.
the class YTypeAssistContext method getKeyCompletions.
public List<ICompletionProposal> getKeyCompletions(YamlDocument doc, SNode node, int offset, String query) throws Exception {
int queryOffset = offset - query.length();
DynamicSchemaContext dynamicCtxt = getSchemaContext();
List<YTypedProperty> allProperties = typeUtil.getProperties(type);
if (CollectionUtil.hasElements(allProperties)) {
List<List<YTypedProperty>> tieredProperties = sortIntoTiers(allProperties);
Set<String> definedProps = dynamicCtxt.getDefinedProperties();
List<ICompletionProposal> proposals = new ArrayList<>();
boolean suggestDeprecated = typeUtil.suggestDeprecatedProperties();
YamlIndentUtil indenter = new YamlIndentUtil(doc);
for (List<YTypedProperty> thisTier : tieredProperties) {
List<YTypedProperty> undefinedProps = thisTier.stream().filter(p -> !definedProps.contains(p.getName()) && (suggestDeprecated || !p.isDeprecated())).collect(Collectors.toList());
if (!undefinedProps.isEmpty()) {
for (YTypedProperty p : undefinedProps) {
String name = p.getName();
double score = FuzzyMatcher.matchScore(query, name);
if (score != 0) {
TypeBasedSnippetProvider snippetProvider = typeUtil.getSnippetProvider();
DocumentEdits edits;
if (snippetProvider != null) {
// Generate edits from snippet
Snippet snippet = snippetProvider.getSnippet(p);
edits = createEditFromSnippet(doc, node, offset, query, indenter, snippet);
} else {
// Generate edits the old-fashioned way
edits = new DocumentEdits(doc.getDocument());
YType YType = p.getType();
edits.delete(queryOffset, query);
int referenceIndent = doc.getColumn(queryOffset);
boolean needNewline = node.getNodeType() == SNodeType.KEY;
StringBuilder snippet = new StringBuilder();
if (needNewline) {
snippet.append("\n");
referenceIndent = YamlIndentUtil.getNewChildKeyIndent(node);
} else if (queryOffset > 0 && !Character.isWhitespace(doc.getChar(queryOffset - 1))) {
// See https://www.pivotaltracker.com/story/show/137722057
snippet.append(" ");
referenceIndent++;
}
snippet.append(p.getName());
snippet.append(":");
snippet.append(appendTextFor(YType));
edits.insert(queryOffset, indenter.applyIndentation(snippet.toString(), referenceIndent));
}
ICompletionProposal completion = completionFactory().beanProperty(doc.getDocument(), contextPath.toPropString(), getType(), query, p, score, edits, typeUtil);
if (p.isDeprecated() && completion instanceof ScoreableProposal) {
completion.deemphasize(DEEMP_DEPRECATION);
}
proposals.add(completion);
}
}
}
// We should only move on to the next tier if all required properties in this tier are defined.
if (undefinedProps.stream().anyMatch(p -> p.isRequired())) {
// stop here, take no more from next tier!
return proposals;
}
}
return proposals;
}
return Collections.emptyList();
}
use of org.springframework.ide.vscode.commons.yaml.structure.YamlStructureParser.SNode in project sts4 by spring-projects.
the class YTypeAssistContext method getSchemaContext.
protected DynamicSchemaContext getSchemaContext() {
try {
SNode contextNode = getContextNode();
YamlPath fullContextPath = contextPath.prepend(YamlPathSegment.valueAt(documentSelector));
return new SNodeDynamicSchemaContext(contextNode, fullContextPath);
} catch (Exception e) {
Log.log(e);
return DynamicSchemaContext.NULL;
}
}
use of org.springframework.ide.vscode.commons.yaml.structure.YamlStructureParser.SNode in project sts4 by spring-projects.
the class YamlCompletionEngine method isLesserIndentRelaxable.
protected final boolean isLesserIndentRelaxable(final SNode currentNode, final SNode contextNode) {
if (!options.includeDeindentedProposals()) {
return false;
}
SChildBearingNode parent = currentNode.getParent();
while (parent != null && parent != contextNode) {
SNode lastChild = parent.getLastRealChild();
if (lastChild != null && lastChild.getStart() >= currentNode.getNodeEnd()) {
return false;
}
parent = parent.getParent();
}
return true;
}
use of org.springframework.ide.vscode.commons.yaml.structure.YamlStructureParser.SNode in project sts4 by spring-projects.
the class YamlStructureParserTest method testTraverseSeq.
@Test
public void testTraverseSeq() throws Exception {
MockYamlEditor editor = new MockYamlEditor("foo:\n" + "- - - - a\n" + " - c\n" + "- e");
SRootNode root = editor.parseStructure();
YamlPath path;
path = pathWith(0, "foo", 0, 0, 0, 0);
assertEquals("SEQ(6): - a\n", path.traverse((SNode) root).toString());
path = pathWith(0, "foo", -1);
assertNull(path.traverse((SNode) root));
path = pathWith(0, "foo", 1);
assertEquals("SEQ(0): - e\n", path.traverse((SNode) root).toString());
path = pathWith(0, "foo", 2);
assertNull(path.traverse((SNode) root));
}
use of org.springframework.ide.vscode.commons.yaml.structure.YamlStructureParser.SNode in project sts4 by spring-projects.
the class YamlStructureParserTest method assertFirstRealChild.
private void assertFirstRealChild(MockYamlEditor editor, String testNodeName, String expectedNodeSnippet) throws Exception {
SDocNode doc = getOnlyDocument(editor.parseStructure());
SKeyNode testNode = doc.getChildWithKey(testNodeName);
assertNotNull(testNode);
SNode expected = null;
if (expectedNodeSnippet != null) {
int offset = editor.getRawText().indexOf(expectedNodeSnippet);
expected = doc.find(offset);
assertTrue(editor.textUnder(expected).contains(expectedNodeSnippet));
}
assertEquals(expected, testNode.getFirstRealChild());
}
Aggregations