use of org.springframework.ide.vscode.commons.yaml.snippet.Snippet in project sts4 by spring-projects.
the class YTypeAssistContext method getCompletions.
@Override
public Collection<ICompletionProposal> getCompletions(YamlDocument doc, SNode node, int offset) throws Exception {
ISubCompletionEngine customContentAssistant = typeUtil.getCustomContentAssistant(type);
if (customContentAssistant != null) {
DocumentRegion region = getCustomAssistRegion(doc, node, offset);
if (region != null) {
return customContentAssistant.getCompletions(completionFactory(), region, region.toRelative(offset));
}
}
String query = getPrefix(doc, node, offset);
List<ICompletionProposal> completions = getValueCompletions(doc, node, offset, query);
if (completions.isEmpty()) {
TypeBasedSnippetProvider snippetProvider = typeUtil.getSnippetProvider();
if (snippetProvider != null) {
Collection<Snippet> snippets = snippetProvider.getSnippets(type);
YamlIndentUtil indenter = new YamlIndentUtil(doc);
for (Snippet snippet : snippets) {
String snippetName = snippet.getName();
double score = FuzzyMatcher.matchScore(query, snippetName);
if (score != 0.0 && snippet.isApplicable(getSchemaContext())) {
DocumentEdits edits = createEditFromSnippet(doc, node, offset, query, indenter, snippet);
completions.add(completionFactory().valueProposal(snippetName, query, snippetName, type, null, score, edits, typeUtil));
}
}
}
completions.addAll(getKeyCompletions(doc, node, offset, query));
}
if (typeUtil.isSequencable(type)) {
completions = new ArrayList<>(completions);
completions.addAll(getDashedCompletions(doc, node, offset));
}
return completions;
}
use of org.springframework.ide.vscode.commons.yaml.snippet.Snippet in project sts4 by spring-projects.
the class SchemaBasedYamlASTReconciler method checkRequiredProperties.
private void checkRequiredProperties(Node parent, MappingNode map, YType type, Map<String, YTypedProperty> beanProperties, DynamicSchemaContext dc) {
Set<String> foundProps = NodeUtil.getScalarKeys(map);
boolean allPropertiesKnown = beanProperties.keySet().containsAll(foundProps);
// Don't check for missing properties if some properties look like they might be spelled incorrectly.
if (allPropertiesKnown) {
// Check for missing required properties:
List<YTypedProperty> missingProps = beanProperties.values().stream().filter(YTypedProperty::isRequired).filter(prop -> !foundProps.contains(prop.getName())).collect(CollectorUtil.toImmutableList());
Set<String> missingPropNames = missingProps.stream().map(YTypedProperty::getName).collect(Collectors.toCollection(TreeSet::new));
if (!missingPropNames.isEmpty()) {
String message;
if (missingPropNames.size() == 1) {
// slightly more specific message when only one missing property
String missing = missingPropNames.stream().findFirst().get();
message = "Property '" + missing + "' is required for '" + type + "'";
} else {
message = "Properties " + missingPropNames + " are required for '" + type + "'";
}
SchemaBasedSnippetGenerator snippetProvider = new SchemaBasedSnippetGenerator(typeUtil, SnippetBuilder::gimped);
Snippet snippet = snippetProvider.getSnippet(missingProps);
problems.accept(YamlSchemaProblems.missingProperties(message, dc, missingPropNames, snippet.getSnippet(), snippet.getPlaceHolder(1).getOffset(), parent, map, quickfixes.MISSING_PROP_FIX));
}
}
}
use of org.springframework.ide.vscode.commons.yaml.snippet.Snippet 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();
}
Aggregations