use of org.springframework.ide.vscode.commons.languageserver.util.SnippetBuilder 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.languageserver.util.SnippetBuilder in project sts4 by spring-projects.
the class ConcourseModel method addExtraInsertion.
private YValueHint addExtraInsertion(YValueHint h, DynamicSchemaContext dc) {
return new BasicYValueHint(h.getValue(), h.getLabel()).setExtraInsertion(() -> {
String resourceTypeName = h.getValue();
AbstractType sourceType = (AbstractType) resourceTypes.getSourceType(resourceTypeName);
if (sourceType != null && getParentPropertyNode("source", dc) == null) {
// don't auto insert what's already there!
List<YTypedProperty> requiredProps = sourceType.getProperties().stream().filter(p -> p.isRequired()).collect(Collectors.toList());
if (!requiredProps.isEmpty()) {
SnippetBuilder snippet = snippetBuilderFactory.get();
snippet.text("\nsource:");
for (YTypedProperty p : requiredProps) {
snippet.text("\n " + p.getName() + ": ");
snippet.placeHolder();
}
return snippet.toString();
}
}
return null;
});
}
use of org.springframework.ide.vscode.commons.languageserver.util.SnippetBuilder in project sts4 by spring-projects.
the class JavaSnippetBuilder method createSnippet.
private String createSnippet(String template) {
Matcher matcher = PLACE_HOLDER.matcher(template);
int start = 0;
SnippetBuilder snippet = snippetBuilderFactory.get();
while (matcher.find(start)) {
int matchStart = matcher.start();
snippet.text(template.substring(start, matchStart));
int matchEnd = matcher.end();
String placeHolderImage = template.substring(matcher.start(1), matcher.end(1));
int colon = placeHolderImage.indexOf(':');
String id, value;
if (colon >= 0) {
id = placeHolderImage.substring(0, colon);
value = placeHolderImage.substring(colon + 1);
} else {
id = placeHolderImage;
value = id;
}
snippet.placeHolder(id, value);
start = matchEnd;
}
snippet.text(template.substring(start));
return snippet.build().toString();
}
use of org.springframework.ide.vscode.commons.languageserver.util.SnippetBuilder in project sts4 by spring-projects.
the class SchemaBasedSnippetGenerator method generateFullSnippet.
private Snippet generateFullSnippet(YType type, int indent) {
if (typeUtil.isBean(type)) {
SnippetBuilder builder = snippetBuilderFactory.get();
List<YTypedProperty> requiredProps = typeUtil.getProperties(type).stream().filter(p -> p.isRequired()).collect(CollectorUtil.toImmutableList());
if (!requiredProps.isEmpty()) {
generateBeanSnippet(requiredProps, builder, indent, maxNesting);
}
if (builder.getPlaceholderCount() >= 2) {
return new Snippet(typeUtil.niceTypeName(type) + " Snippet", builder.build(), (dc) -> requiredProps.stream().noneMatch(p -> dc.getDefinedProperties().contains(p.getName())));
}
}
return null;
}
use of org.springframework.ide.vscode.commons.languageserver.util.SnippetBuilder in project sts4 by spring-projects.
the class SchemaBasedSnippetGenerator method getSnippet.
@Override
public Snippet getSnippet(List<YTypedProperty> props) {
SnippetBuilder builder = snippetBuilderFactory.get();
generateBeanSnippet(props, builder, 0, maxNesting);
String snippetName;
if (props.size() == 1) {
snippetName = props.get(0).getName();
} else {
snippetName = props.stream().map(p -> p.getName()).collect(Collectors.toList()).toString() + " Snippet";
}
return new Snippet(snippetName, builder.build(), (dc) -> props.stream().allMatch(p -> !dc.getDefinedProperties().contains(p.getName())));
}
Aggregations