use of org.springframework.ide.vscode.boot.metadata.hints.StsValueHint in project sts4 by spring-projects.
the class TypeUtil method niceTypeName.
public void niceTypeName(Type type, StringBuilder buf) {
if (type == null) {
buf.append("null");
return;
}
String typeStr = type.getErasure();
String primTypeName = PRIMITIVE_TYPE_NAMES.get(typeStr);
if (primTypeName != null) {
buf.append(primTypeName);
} else if (typeStr.startsWith("java.lang.")) {
buf.append(typeStr.substring("java.lang.".length()));
} else if (typeStr.startsWith("java.util.")) {
buf.append(typeStr.substring("java.util.".length()));
} else {
buf.append(typeStr);
}
if (isEnum(type)) {
Collection<StsValueHint> values = getAllowedValues(type, EnumCaseMode.ORIGNAL);
if (values != null && !values.isEmpty()) {
buf.append("[");
int i = 0;
for (StsValueHint hint : values) {
if (i > 0) {
buf.append(", ");
}
buf.append(hint.getValue());
i++;
if (i >= 4) {
break;
}
}
if (i < values.size()) {
buf.append(", ...");
}
buf.append("]");
}
} else if (type.isGeneric()) {
Type[] params = type.getParams();
buf.append("<");
for (int i = 0; i < params.length; i++) {
if (i > 0) {
buf.append(", ");
}
niceTypeName(params[i], buf);
}
buf.append(">");
}
}
use of org.springframework.ide.vscode.boot.metadata.hints.StsValueHint in project sts4 by spring-projects.
the class TypeUtil method getProperties.
/**
* Determine properties that are setable on object of given type.
* <p>
* Note that this may return both null or an empty list, but they mean
* different things. Null means that the properties on the object are not known,
* and therefore reconciling should not check property validity. On the other hand
* returning an empty list means that there are no properties. In this case,
* accessing properties is invalid and reconciler should show an error message
* for any property access.
*
* @return A list of known properties or null if the list of properties is unknown.
*/
public List<TypedProperty> getProperties(Type type, EnumCaseMode enumMode, BeanPropertyNameMode beanMode) {
if (type == null) {
return null;
}
if (!isDotable(type)) {
// If dot navigation is not valid then really this is just like saying the type has no properties.
return Collections.emptyList();
}
if (isMap(type)) {
Type keyType = getKeyType(type);
if (keyType != null) {
Collection<StsValueHint> keyHints = getAllowedValues(keyType, enumMode);
if (CollectionUtil.hasElements(keyHints)) {
Type valueType = getDomainType(type);
ArrayList<TypedProperty> properties = new ArrayList<>(keyHints.size());
for (StsValueHint hint : keyHints) {
String propName = hint.getValue();
properties.add(new TypedProperty(propName, valueType, hint.getDescription(), hint.getDeprecation()));
}
return properties;
}
}
} else {
String typename = type.getErasure();
IType typeFromIndex = findType(typename);
// TODO: handle type parameters.
if (typeFromIndex != null) {
ArrayList<TypedProperty> properties = new ArrayList<>();
getGetterMethods(typeFromIndex).forEach(m -> {
Deprecation deprecation = DeprecationUtil.extract(m);
Type propType = null;
try {
propType = Type.fromJavaType(m.getReturnType());
} catch (Exception e) {
Log.log(e);
}
if (beanMode.includesHyphenated()) {
properties.add(new TypedProperty(getterOrSetterNameToProperty(m.getElementName()), propType, deprecation));
}
if (beanMode.includesCamelCase()) {
properties.add(new TypedProperty(getterOrSetterNameToCamelName(m.getElementName()), propType, deprecation));
}
});
return properties;
}
}
return null;
}
use of org.springframework.ide.vscode.boot.metadata.hints.StsValueHint in project sts4 by spring-projects.
the class TypeUtil method getBareValues.
private String[] getBareValues(Collection<StsValueHint> hints) {
if (hints != null) {
String[] values = new String[hints.size()];
int i = 0;
for (StsValueHint h : hints) {
values[i++] = h.getValue();
}
return values;
}
return null;
}
use of org.springframework.ide.vscode.boot.metadata.hints.StsValueHint in project sts4 by spring-projects.
the class PropertiesHoverCalculator method getValueHover.
private Tuple2<Renderable, IRegion> getValueHover(Value value) {
DocumentRegion valueRegion = createRegion(doc, value).trimStart(SPACES).trimEnd(SPACES);
if (valueRegion.getStart() <= offset && offset < valueRegion.getEnd()) {
String valueString = valueRegion.toString();
String propertyName = value.getParent().getKey().decode();
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);
}
if (TypeUtil.isClass(type)) {
// Special case. We want to provide hoverinfos more liberally than what's suggested for completions (i.e. even class names
// that are not suggested by the hints because they do not meet subtyping constraints should be hoverable and linkable!
StsValueHint hint = StsValueHint.className(valueString, typeUtil);
if (hint != null) {
return Tuples.of(createRenderable(hint), valueRegion.asRegion());
}
}
// Hack: pretend to invoke content-assist at the end of the value text. This should provide hints applicable to that value
// then show hoverinfo based on that. That way we can avoid duplication a lot of similar logic to compute hoverinfos and hyperlinks.
Collection<StsValueHint> hints = getValueHints(index, typeUtil, valueString, propertyName, EnumCaseMode.ALIASED);
if (hints != null) {
Optional<StsValueHint> hint = hints.stream().filter(h -> valueString.equals(h.getValue())).findFirst();
if (hint.isPresent()) {
return Tuples.of(createRenderable(hint.get()), valueRegion.asRegion());
}
}
}
return null;
}
use of org.springframework.ide.vscode.boot.metadata.hints.StsValueHint in project sts4 by spring-projects.
the class ClassReferenceProvider method getValuesAsync.
@Override
protected Flux<StsValueHint> getValuesAsync(IJavaProject javaProject, String query) {
IType targetType = target == null || target.isEmpty() ? javaProject.getClasspath().findType("java.lang.Object") : javaProject.getClasspath().findType(target);
if (targetType == null) {
return Flux.empty();
}
Set<IType> allSubclasses = javaProject.getClasspath().allSubtypesOf(targetType).filter(t -> Flags.isPublic(t.getFlags()) && !concrete || !isAbstract(t)).collect(Collectors.toSet()).block();
if (allSubclasses.isEmpty()) {
return Flux.empty();
} else {
return javaProject.getClasspath().fuzzySearchTypes(query, type -> allSubclasses.contains(type)).collectSortedList((o1, o2) -> o2.getT2().compareTo(o1.getT2())).flatMapIterable(l -> l).map(t -> StsValueHint.create(t.getT1()));
}
}
Aggregations