use of org.springframework.ide.vscode.commons.yaml.schema.YTypedProperty in project sts4 by spring-projects.
the class SchemaBasedSnippetGenerator method generateBeanSnippet.
private void generateBeanSnippet(List<YTypedProperty> props, SnippetBuilder builder, int indent, int nestingLimit) {
if (nestingLimit > 0 && !props.isEmpty()) {
boolean first = true;
for (YTypedProperty p : props) {
if (!first) {
builder.newline(indent);
}
builder.text(p.getName());
builder.text(":");
generateNestedSnippet(false, p.getType(), builder, indent, nestingLimit - 1);
first = false;
}
} else {
// reached the limit of bean number of nested property expansions allowed.
builder.placeHolder();
}
}
use of org.springframework.ide.vscode.commons.yaml.schema.YTypedProperty 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.schema.YTypedProperty in project sts4 by spring-projects.
the class YTypeAssistContext method sortIntoTiers.
/**
* Divides a given list of properties into tiers of decreasing significance. Property tiering
* is a mechanism to reduce 'noise' in content assist proposals. Only properties of the
* first X tiers will be used, until those tiers no longer have undefined required properties.
* <p>
* This allows, for example, to only suggest a 'name' property when starting to define
* a new named entity. This is what a sane user would probably want, even though
* in theory they would be free to define the properties in any order they want.
*/
protected List<List<YTypedProperty>> sortIntoTiers(List<YTypedProperty> properties) {
boolean tieredOptionals = typeUtil.tieredOptionalPropertyProposals();
if (properties.isEmpty()) {
// Nothing to sort
return ImmutableList.of();
} else {
ImmutableList.Builder<YTypedProperty> primary = ImmutableList.builder();
ImmutableList.Builder<YTypedProperty> tier2 = ImmutableList.builder();
ImmutableList.Builder<YTypedProperty> tier3 = ImmutableList.builder();
for (YTypedProperty p : properties) {
if (p.isPrimary()) {
primary.add(p);
} else if (!tieredOptionals || p.isRequired()) {
tier2.add(p);
} else {
tier3.add(p);
}
}
if (tieredOptionals) {
return ImmutableList.of(primary.build(), tier2.build(), tier3.build());
} else {
return ImmutableList.of(primary.build(), tier2.build());
}
}
}
use of org.springframework.ide.vscode.commons.yaml.schema.YTypedProperty in project sts4 by spring-projects.
the class SchemaBasedYamlASTReconciler method reconcile.
private void reconcile(YamlFileAST ast, YamlPath path, Node parent, Node node, YType _type) {
// IDocument doc = ast.getDocument();
if (_type != null && !skipReconciling(node)) {
DynamicSchemaContext schemaContext = new ASTDynamicSchemaContext(ast, path, node);
YType type = typeUtil.inferMoreSpecificType(_type, schemaContext);
if (typeCollector != null) {
typeCollector.accept(node, type);
}
checkConstraints(parent, node, type, schemaContext);
switch(getNodeId(node)) {
case mapping:
MappingNode map = (MappingNode) node;
checkForDuplicateKeys(map);
if (typeUtil.isMap(type)) {
for (NodeTuple entry : map.getValue()) {
String key = NodeUtil.asScalar(entry.getKeyNode());
reconcile(ast, keyAt(path, key), map, entry.getKeyNode(), typeUtil.getKeyType(type));
reconcile(ast, valueAt(path, key), map, entry.getValueNode(), typeUtil.getDomainType(type));
}
} else if (typeUtil.isBean(type)) {
Map<String, YTypedProperty> beanProperties = typeUtil.getPropertiesMap(type);
checkRequiredProperties(parent, map, type, beanProperties, schemaContext);
for (NodeTuple entry : map.getValue()) {
Node keyNode = entry.getKeyNode();
String key = NodeUtil.asScalar(keyNode);
if (key == null) {
expectScalar(node);
} else {
YTypedProperty prop = beanProperties.get(key);
if (prop == null) {
unknownBeanProperty(keyNode, type, key);
} else {
if (prop.isDeprecated()) {
String msg = prop.getDeprecationMessage();
if (StringUtil.hasText(msg)) {
problems.accept(YamlSchemaProblems.deprecatedProperty(msg, keyNode));
} else {
problems.accept(YamlSchemaProblems.deprecatedProperty(keyNode, type, prop));
}
}
reconcile(ast, valueAt(path, key), map, entry.getValueNode(), prop.getType());
}
}
}
} else {
expectTypeButFoundMap(type, node);
}
break;
case sequence:
SequenceNode seq = (SequenceNode) node;
if (typeUtil.isSequencable(type)) {
for (int i = 0; i < seq.getValue().size(); i++) {
Node el = seq.getValue().get(i);
reconcile(ast, valueAt(path, i), seq, el, typeUtil.getDomainType(type));
}
} else {
expectTypeButFoundSequence(type, node);
}
break;
case scalar:
if (typeUtil.isAtomic(type)) {
SchemaContextAware<ValueParser> parserProvider = typeUtil.getValueParser(type);
if (parserProvider != null) {
// Take care not to execute parserProvider early just to check how long it should be delayed.
delayedConstraints.add(() -> {
parserProvider.safeWithContext(schemaContext).ifPresent(parser -> {
if (parser.longRunning()) {
slowDelayedConstraints.add(() -> {
parse(ast, node, type, parser);
});
} else {
parse(ast, node, type, parser);
}
});
});
}
} else {
expectTypeButFoundScalar(type, node);
}
break;
default:
}
}
}
use of org.springframework.ide.vscode.commons.yaml.schema.YTypedProperty 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;
}
Aggregations