Search in sources :

Example 1 with StsValueHint

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;
}
Also used : Arrays(java.util.Arrays) Provider(javax.inject.Provider) IJavaProject(org.springframework.ide.vscode.commons.java.IJavaProject) SimpleDateFormat(java.text.SimpleDateFormat) HashMap(java.util.HashMap) IMethod(org.springframework.ide.vscode.commons.java.IMethod) LazyProvider(org.springframework.ide.vscode.commons.util.LazyProvider) ArrayUtils.firstElement(org.springframework.ide.vscode.commons.util.ArrayUtils.firstElement) ArrayList(java.util.ArrayList) IJavaElement(org.springframework.ide.vscode.commons.java.IJavaElement) InetAddress(java.net.InetAddress) HashSet(java.util.HashSet) StringUtil(org.springframework.ide.vscode.commons.util.StringUtil) Charset(java.nio.charset.Charset) ImmutableList(com.google.common.collect.ImmutableList) IField(org.springframework.ide.vscode.commons.java.IField) Locale(java.util.Locale) Flags(org.springframework.ide.vscode.commons.java.Flags) Map(java.util.Map) DeprecationUtil(org.springframework.ide.vscode.boot.metadata.util.DeprecationUtil) ValueProviderStrategy(org.springframework.ide.vscode.boot.metadata.ValueProviderRegistry.ValueProviderStrategy) ResourceHintProvider(org.springframework.ide.vscode.boot.metadata.ResourceHintProvider) ValueParser(org.springframework.ide.vscode.commons.util.ValueParser) ImmutableSet(com.google.common.collect.ImmutableSet) StsValueHint(org.springframework.ide.vscode.boot.metadata.hints.StsValueHint) Log(org.springframework.ide.vscode.commons.util.Log) Collection(java.util.Collection) ArrayUtils(org.springframework.ide.vscode.commons.util.ArrayUtils) Set(java.util.Set) Collectors(java.util.stream.Collectors) Assert(org.springframework.ide.vscode.commons.util.Assert) Flux(reactor.core.publisher.Flux) ArrayUtils.lastElement(org.springframework.ide.vscode.commons.util.ArrayUtils.lastElement) List(java.util.List) CollectionUtil(org.springframework.ide.vscode.commons.util.CollectionUtil) Stream(java.util.stream.Stream) EnumValueParser(org.springframework.ide.vscode.commons.util.EnumValueParser) MimeTypes(org.springframework.ide.vscode.commons.util.MimeTypes) Entry(java.util.Map.Entry) Optional(java.util.Optional) IType(org.springframework.ide.vscode.commons.java.IType) Collections(java.util.Collections) Deprecation(org.springframework.ide.vscode.boot.configurationmetadata.Deprecation) AlwaysFailingParser(org.springframework.ide.vscode.commons.util.AlwaysFailingParser) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableList(com.google.common.collect.ImmutableList) StsValueHint(org.springframework.ide.vscode.boot.metadata.hints.StsValueHint) StsValueHint(org.springframework.ide.vscode.boot.metadata.hints.StsValueHint) IType(org.springframework.ide.vscode.commons.java.IType)

Example 2 with StsValueHint

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

Example 3 with StsValueHint

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

Example 4 with StsValueHint

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();
}
Also used : EnumCaseMode(org.springframework.ide.vscode.boot.metadata.types.TypeUtil.EnumCaseMode) DocumentEdits(org.springframework.ide.vscode.commons.languageserver.completion.DocumentEdits) StsValueHint(org.springframework.ide.vscode.boot.metadata.hints.StsValueHint) DocumentRegion(org.springframework.ide.vscode.commons.util.text.DocumentRegion) ICompletionProposal(org.springframework.ide.vscode.commons.languageserver.completion.ICompletionProposal) ArrayList(java.util.ArrayList) StsValueHint(org.springframework.ide.vscode.boot.metadata.hints.StsValueHint)

Example 5 with StsValueHint

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;
}
Also used : Type(org.springframework.ide.vscode.boot.metadata.types.Type) StsValueHint(org.springframework.ide.vscode.boot.metadata.hints.StsValueHint) ArrayList(java.util.ArrayList) Collection(java.util.Collection) HintProvider(org.springframework.ide.vscode.boot.metadata.hints.HintProvider) PropertyInfo(org.springframework.ide.vscode.boot.metadata.PropertyInfo)

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