use of org.springframework.ide.vscode.boot.metadata.hints.StsValueHint in project sts4 by spring-projects.
the class TypeUtil method getAllowedValues.
/**
* @return An array of allowed values for a given type. If an array is returned then
* *only* values in the array are valid and using any other value constitutes an error.
* This may return null if allowedValues list is unknown or the type is not characterizable
* as a simple enumaration of allowed values.
* @param caseMode determines whether Enum values are returned in 'lower case form', 'orignal form',
* or 'aliased' (meaning both forms are returned).
*/
public Collection<StsValueHint> getAllowedValues(Type enumType, EnumCaseMode caseMode) {
if (enumType != null) {
try {
String[] values = TYPE_VALUES.get(enumType.getErasure());
if (values != null) {
if (caseMode == EnumCaseMode.ALIASED) {
ImmutableSet.Builder<String> aliased = ImmutableSet.builder();
aliased.add(values);
for (int i = 0; i < values.length; i++) {
aliased.add(values[i].toUpperCase());
}
return aliased.build().stream().map(StsValueHint::create).collect(Collectors.toList());
} else {
return Arrays.stream(values).map(StsValueHint::create).collect(Collectors.toList());
}
}
IType type = findType(enumType.getErasure());
if (type != null && type.isEnum()) {
ImmutableList.Builder<StsValueHint> enums = ImmutableList.builder();
boolean addOriginal = caseMode == EnumCaseMode.ORIGNAL || caseMode == EnumCaseMode.ALIASED;
boolean addLowerCased = caseMode == EnumCaseMode.LOWER_CASE || caseMode == EnumCaseMode.ALIASED;
type.getFields().filter(f -> f.isEnumConstant()).forEach(f -> {
String rawName = f.getElementName();
if (addOriginal) {
enums.add(StsValueHint.create(rawName, f));
}
if (addLowerCased) {
enums.add(StsValueHint.create(StringUtil.upperCaseToHyphens(rawName), f));
}
});
return enums.build();
}
} catch (Exception e) {
Log.log(e);
}
}
return null;
}
use of org.springframework.ide.vscode.boot.metadata.hints.StsValueHint in project sts4 by spring-projects.
the class TypeUtil method getValueParser.
public ValueParser getValueParser(Type type) {
ValueParser simpleParser = VALUE_PARSERS.get(type.getErasure());
if (simpleParser != null) {
return simpleParser;
}
Collection<StsValueHint> enumValues = getAllowedValues(type, EnumCaseMode.ALIASED);
if (enumValues != null) {
// assigning anything to it is an error.
return new EnumValueParser(niceTypeName(type), getBareValues(enumValues));
}
if (isMap(type)) {
// provide a parser that allows throws
return new AlwaysFailingParser(niceTypeName(type));
}
if (isSequencable(type)) {
// Trying to parse list from scalars is possible if the domain type is parseable. Spring boot
// will try to interpret the string as a comma-separated list
Type elType = getDomainType(type);
if (elType != null) {
ValueParser elParser = getValueParser(elType);
if (elParser != null) {
return new DelimitedStringParser(elParser);
}
}
}
return null;
}
use of org.springframework.ide.vscode.boot.metadata.hints.StsValueHint in project sts4 by spring-projects.
the class TypeUtil method getHintValues.
public Collection<StsValueHint> getHintValues(Type type, String query, EnumCaseMode enumCaseMode) {
if (type != null) {
Collection<StsValueHint> allowed = getAllowedValues(type, enumCaseMode);
if (allowed != null) {
return allowed;
}
ValueProviderStrategy valueHinter = VALUE_HINTERS.get(type.getErasure());
if (valueHinter != null) {
return valueHinter.getValuesNow(javaProject, query);
}
}
return null;
}
use of org.springframework.ide.vscode.boot.metadata.hints.StsValueHint in project sts4 by spring-projects.
the class PropertiesCompletionProposalsCalculator method getValueCompletions.
private Collection<ICompletionProposal> getValueCompletions(Value value) {
DocumentRegion valueRegion = createRegion(doc, value).trimStart(SPACES).trimEnd(SPACES);
String query = valuePrefixFinder.getPrefix(doc, offset, valueRegion.getStart());
int startOfValue = offset - query.length();
EnumCaseMode caseMode = caseMode(query);
// note: no need to skip whitespace backwards.
String propertyName = /*fuzzySearchPrefix.getPrefix(doc, pair.getOffset())*/
value.getParent().getKey().decode();
// because value partition includes whitespace around the assignment
if (propertyName != null) {
Collection<StsValueHint> valueCompletions = getValueHints(index, typeUtil, query, propertyName, caseMode);
if (valueCompletions != null && !valueCompletions.isEmpty()) {
ArrayList<ICompletionProposal> proposals = new ArrayList<ICompletionProposal>();
for (StsValueHint hint : valueCompletions) {
String valueCandidate = hint.getValue();
double score = FuzzyMatcher.matchScore(query, valueCandidate);
if (score != 0) {
DocumentEdits edits = new DocumentEdits(doc);
edits.delete(startOfValue, offset);
edits.insert(offset, valueCandidate);
String valueTypeName = typeUtil.niceTypeName(getValueType(index, typeUtil, propertyName));
proposals.add(completionFactory.valueProposal(valueCandidate, query, valueTypeName, score, edits, ValueHintHoverInfo.create(hint)));
}
}
return proposals;
}
}
return Collections.emptyList();
}
use of org.springframework.ide.vscode.boot.metadata.hints.StsValueHint in project sts4 by spring-projects.
the class CommonLanguageTools method getValueHints.
public static Collection<StsValueHint> getValueHints(FuzzyMap<PropertyInfo> index, TypeUtil typeUtil, String query, String propertyName, EnumCaseMode caseMode) {
Type type = getValueType(index, typeUtil, propertyName);
if (TypeUtil.isSequencable(type)) {
// It is useful to provide content assist for the values in the list when entering a list
type = TypeUtil.getDomainType(type);
}
List<StsValueHint> allHints = new ArrayList<>();
{
Collection<StsValueHint> hints = typeUtil.getHintValues(type, query, caseMode);
if (CollectionUtil.hasElements(hints)) {
allHints.addAll(hints);
}
}
{
PropertyInfo prop = index.findLongestCommonPrefixEntry(propertyName);
if (prop != null) {
HintProvider hintProvider = prop.getHints(typeUtil, false);
if (!HintProviders.isNull(hintProvider)) {
allHints.addAll(hintProvider.getValueHints(query));
}
}
}
return allHints;
}
Aggregations