Search in sources :

Example 6 with StsValueHint

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(">");
    }
}
Also used : StsValueHint(org.springframework.ide.vscode.boot.metadata.hints.StsValueHint) StsValueHint(org.springframework.ide.vscode.boot.metadata.hints.StsValueHint)

Example 7 with StsValueHint

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;
}
Also used : IType(org.springframework.ide.vscode.commons.java.IType) StsValueHint(org.springframework.ide.vscode.boot.metadata.hints.StsValueHint) Deprecation(org.springframework.ide.vscode.boot.configurationmetadata.Deprecation) ArrayList(java.util.ArrayList) IType(org.springframework.ide.vscode.commons.java.IType)

Example 8 with StsValueHint

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;
}
Also used : StsValueHint(org.springframework.ide.vscode.boot.metadata.hints.StsValueHint) StsValueHint(org.springframework.ide.vscode.boot.metadata.hints.StsValueHint)

Example 9 with StsValueHint

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;
}
Also used : CommonLanguageTools.getValueType(org.springframework.ide.vscode.boot.common.CommonLanguageTools.getValueType) IDocument(org.springframework.ide.vscode.commons.util.text.IDocument) IJavaProject(org.springframework.ide.vscode.commons.java.IJavaProject) SpringPropertyIndex(org.springframework.ide.vscode.boot.metadata.SpringPropertyIndex) Tuples(reactor.util.function.Tuples) Type(org.springframework.ide.vscode.boot.metadata.types.Type) Tuple2(reactor.util.function.Tuple2) AntlrParser(org.springframework.ide.vscode.java.properties.antlr.parser.AntlrParser) ImmutableList(com.google.common.collect.ImmutableList) FuzzyMap(org.springframework.ide.vscode.commons.util.FuzzyMap) Renderables.paragraph(org.springframework.ide.vscode.commons.util.Renderables.paragraph) IRegion(org.springframework.ide.vscode.commons.util.text.IRegion) StsValueHint(org.springframework.ide.vscode.boot.metadata.hints.StsValueHint) BadLocationException(org.springframework.ide.vscode.commons.util.BadLocationException) DocumentRegion(org.springframework.ide.vscode.commons.util.text.DocumentRegion) EnumCaseMode(org.springframework.ide.vscode.boot.metadata.types.TypeUtil.EnumCaseMode) Collection(java.util.Collection) SPACES(org.springframework.ide.vscode.boot.common.CommonLanguageTools.SPACES) Renderables.concat(org.springframework.ide.vscode.commons.util.Renderables.concat) PropertyInfo(org.springframework.ide.vscode.boot.metadata.PropertyInfo) ParseResults(org.springframework.ide.vscode.java.properties.parser.ParseResults) Renderables.bold(org.springframework.ide.vscode.commons.util.Renderables.bold) Renderables.text(org.springframework.ide.vscode.commons.util.Renderables.text) Key(org.springframework.ide.vscode.java.properties.parser.PropertiesAst.Key) InformationTemplates(org.springframework.ide.vscode.boot.common.InformationTemplates) Node(org.springframework.ide.vscode.java.properties.parser.PropertiesAst.Node) CommonLanguageTools.getValueHints(org.springframework.ide.vscode.boot.common.CommonLanguageTools.getValueHints) Value(org.springframework.ide.vscode.java.properties.parser.PropertiesAst.Value) Optional(java.util.Optional) Renderable(org.springframework.ide.vscode.commons.util.Renderable) TypeUtil(org.springframework.ide.vscode.boot.metadata.types.TypeUtil) Builder(com.google.common.collect.ImmutableList.Builder) CommonLanguageTools.getValueType(org.springframework.ide.vscode.boot.common.CommonLanguageTools.getValueType) Type(org.springframework.ide.vscode.boot.metadata.types.Type) StsValueHint(org.springframework.ide.vscode.boot.metadata.hints.StsValueHint) DocumentRegion(org.springframework.ide.vscode.commons.util.text.DocumentRegion)

Example 10 with StsValueHint

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()));
    }
}
Also used : StsValueHint(org.springframework.ide.vscode.boot.metadata.hints.StsValueHint) Log(org.springframework.ide.vscode.commons.util.Log) IJavaProject(org.springframework.ide.vscode.commons.java.IJavaProject) Set(java.util.Set) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) StringUtil(org.springframework.ide.vscode.commons.util.StringUtil) Flux(reactor.core.publisher.Flux) Flags(org.springframework.ide.vscode.commons.java.Flags) Map(java.util.Map) ValueProviderStrategy(org.springframework.ide.vscode.boot.metadata.ValueProviderRegistry.ValueProviderStrategy) IType(org.springframework.ide.vscode.commons.java.IType) CacheBuilder(com.google.common.cache.CacheBuilder) Cache(com.google.common.cache.Cache) IType(org.springframework.ide.vscode.commons.java.IType)

Aggregations

StsValueHint (org.springframework.ide.vscode.boot.metadata.hints.StsValueHint)10 ArrayList (java.util.ArrayList)4 IType (org.springframework.ide.vscode.commons.java.IType)4 Collection (java.util.Collection)3 ValueProviderStrategy (org.springframework.ide.vscode.boot.metadata.ValueProviderRegistry.ValueProviderStrategy)3 IJavaProject (org.springframework.ide.vscode.commons.java.IJavaProject)3 ImmutableList (com.google.common.collect.ImmutableList)2 Map (java.util.Map)2 Optional (java.util.Optional)2 Set (java.util.Set)2 Collectors (java.util.stream.Collectors)2 Deprecation (org.springframework.ide.vscode.boot.configurationmetadata.Deprecation)2 PropertyInfo (org.springframework.ide.vscode.boot.metadata.PropertyInfo)2 Type (org.springframework.ide.vscode.boot.metadata.types.Type)2 EnumCaseMode (org.springframework.ide.vscode.boot.metadata.types.TypeUtil.EnumCaseMode)2 Flags (org.springframework.ide.vscode.commons.java.Flags)2 AlwaysFailingParser (org.springframework.ide.vscode.commons.util.AlwaysFailingParser)2 EnumValueParser (org.springframework.ide.vscode.commons.util.EnumValueParser)2 ValueParser (org.springframework.ide.vscode.commons.util.ValueParser)2 Cache (com.google.common.cache.Cache)1