Search in sources :

Example 1 with TypedProperty

use of org.springframework.ide.vscode.boot.metadata.types.TypedProperty in project sts4 by spring-projects.

the class PropertiesCompletionProposalsCalculator method createPropertyProposals.

protected Collection<ICompletionProposal> createPropertyProposals(Type type, int navOffset, String prefix, List<TypedProperty> objectProperties) {
    ArrayList<ICompletionProposal> proposals = new ArrayList<ICompletionProposal>();
    for (TypedProperty prop : objectProperties) {
        double score = FuzzyMatcher.matchScore(prefix, prop.getName());
        if (score != 0) {
            Type valueType = prop.getType();
            String postFix = propertyCompletionPostfix(typeUtil, valueType);
            DocumentEdits edits = new DocumentEdits(doc);
            edits.delete(navOffset + 1, offset);
            edits.insert(offset, prop.getName() + postFix);
            proposals.add(completionFactory.beanProperty(doc, null, type, prefix, prop, score, edits, typeUtil));
        }
    }
    return proposals;
}
Also used : CommonLanguageTools.getValueType(org.springframework.ide.vscode.boot.common.CommonLanguageTools.getValueType) Type(org.springframework.ide.vscode.boot.metadata.types.Type) DocumentEdits(org.springframework.ide.vscode.commons.languageserver.completion.DocumentEdits) ICompletionProposal(org.springframework.ide.vscode.commons.languageserver.completion.ICompletionProposal) ArrayList(java.util.ArrayList) TypedProperty(org.springframework.ide.vscode.boot.metadata.types.TypedProperty)

Example 2 with TypedProperty

use of org.springframework.ide.vscode.boot.metadata.types.TypedProperty in project sts4 by spring-projects.

the class PropertiesCompletionProposalsCalculator method getNavigationProposals.

/**
 * @param type Type of the expression leading upto the 'nav' operator
 * @param navOffset Offset of the nav operator (either ']' or '.'
 * @param offset Offset of the cursor where CA was requested.
 */
private Collection<ICompletionProposal> getNavigationProposals(Type type, int navOffset) {
    try {
        char navOp = doc.getChar(navOffset);
        if (navOp == '.') {
            String prefix = doc.get(navOffset + 1, offset - (navOffset + 1));
            EnumCaseMode caseMode = caseMode(prefix);
            List<TypedProperty> objectProperties = typeUtil.getProperties(type, caseMode, BeanPropertyNameMode.HYPHENATED);
            // Note: properties editor itself deals with relaxed names. So it expects the properties here to be returned in hyphenated form only.
            if (objectProperties != null && !objectProperties.isEmpty()) {
                return createPropertyProposals(type, navOffset, prefix, objectProperties);
            }
        } else {
        // TODO: other cases ']' or '[' ?
        }
    } catch (Exception e) {
        Log.log(e);
    }
    return Collections.emptyList();
}
Also used : EnumCaseMode(org.springframework.ide.vscode.boot.metadata.types.TypeUtil.EnumCaseMode) BadLocationException(org.springframework.ide.vscode.commons.util.BadLocationException) TypedProperty(org.springframework.ide.vscode.boot.metadata.types.TypedProperty)

Example 3 with TypedProperty

use of org.springframework.ide.vscode.boot.metadata.types.TypedProperty in project sts4 by spring-projects.

the class PropertyNavigator method dotNavigate.

/**
 * Handle dot navigation into given type, after a '.' was
 * was found at given offset. Assumes the type has already been
 * checked to be 'dotable'.
 */
private Type dotNavigate(int offset, Type type) {
    if (TypeUtil.isMap(type)) {
        int keyStart = offset + 1;
        Type domainType = TypeUtil.getDomainType(type);
        int keyEnd = -1;
        if (typeUtil.isDotable(domainType)) {
            // '.' should be interpreted as navigation.
            keyEnd = nextNavOp(".[", offset + 1);
        } else {
            // '.' should *not* be interpreted as navigation.
            keyEnd = nextNavOp("[", offset + 1);
        }
        String key = textBetween(keyStart, keyEnd);
        Type keyType = typeUtil.getKeyType(type);
        if (keyType != null) {
            ValueParser keyParser = typeUtil.getValueParser(keyType);
            if (keyParser != null) {
                try {
                    keyParser.parse(key);
                } catch (Exception e) {
                    problemCollector.accept(problem(ApplicationPropertiesProblemType.PROP_VALUE_TYPE_MISMATCH, "Expecting " + typeUtil.niceTypeName(keyType), keyStart, keyEnd - keyStart));
                }
            }
        }
        return navigate(keyEnd, domainType);
    } else {
        // dot navigation into object properties
        int keyStart = offset + 1;
        int keyEnd = nextNavOp(".[", offset + 1);
        if (keyEnd < 0) {
            keyEnd = region.getEnd();
        }
        String key = StringUtil.camelCaseToHyphens(textBetween(keyStart, keyEnd));
        List<TypedProperty> properties = typeUtil.getProperties(type, EnumCaseMode.ALIASED, BeanPropertyNameMode.ALIASED);
        if (properties != null) {
            TypedProperty prop = null;
            for (TypedProperty p : properties) {
                if (p.getName().equals(key)) {
                    prop = p;
                    break;
                }
            }
            if (prop == null) {
                problemCollector.accept(problem(ApplicationPropertiesProblemType.PROP_INVALID_BEAN_PROPERTY, "Type '" + typeUtil.niceTypeName(type) + "' has no property '" + key + "'", keyStart, keyEnd - keyStart));
            } else {
                if (prop.isDeprecated()) {
                    problemCollector.accept(problemDeprecated(type, prop, keyStart, keyEnd - keyStart));
                }
                return navigate(keyEnd, prop.getType());
            }
        }
    }
    return null;
}
Also used : Type(org.springframework.ide.vscode.boot.metadata.types.Type) ValueParser(org.springframework.ide.vscode.commons.util.ValueParser) BadLocationException(org.springframework.ide.vscode.commons.util.BadLocationException) TypedProperty(org.springframework.ide.vscode.boot.metadata.types.TypedProperty)

Example 4 with TypedProperty

use of org.springframework.ide.vscode.boot.metadata.types.TypedProperty in project sts4 by spring-projects.

the class TypeUtilTest method getPropertyType.

private Type getPropertyType(Type type, String propName, EnumCaseMode enumMode, BeanPropertyNameMode beanMode) {
    List<TypedProperty> props = getProperties(type, enumMode, beanMode);
    assertNotNull(props);
    for (TypedProperty prop : props) {
        if (prop.getName().equals(propName)) {
            return prop.getType();
        }
    }
    return null;
}
Also used : TypedProperty(org.springframework.ide.vscode.boot.metadata.types.TypedProperty)

Example 5 with TypedProperty

use of org.springframework.ide.vscode.boot.metadata.types.TypedProperty in project sts4 by spring-projects.

the class PropertiesCompletionProposalsCalculator method getKeyHintProposals.

private Collection<ICompletionProposal> getKeyHintProposals(PropertyInfo prop, int navOffset) {
    HintProvider hintProvider = prop.getHints(typeUtil, false);
    if (!HintProviders.isNull(hintProvider)) {
        String query = textBetween(doc, navOffset + 1, offset);
        List<TypedProperty> hintProperties = hintProvider.getPropertyHints(query);
        if (CollectionUtil.hasElements(hintProperties)) {
            return createPropertyProposals(TypeParser.parse(prop.getType()), navOffset, query, hintProperties);
        }
    }
    return ImmutableList.of();
}
Also used : HintProvider(org.springframework.ide.vscode.boot.metadata.hints.HintProvider) TypedProperty(org.springframework.ide.vscode.boot.metadata.types.TypedProperty)

Aggregations

TypedProperty (org.springframework.ide.vscode.boot.metadata.types.TypedProperty)6 ArrayList (java.util.ArrayList)2 Type (org.springframework.ide.vscode.boot.metadata.types.Type)2 BadLocationException (org.springframework.ide.vscode.commons.util.BadLocationException)2 CommonLanguageTools.getValueType (org.springframework.ide.vscode.boot.common.CommonLanguageTools.getValueType)1 HintProvider (org.springframework.ide.vscode.boot.metadata.hints.HintProvider)1 EnumCaseMode (org.springframework.ide.vscode.boot.metadata.types.TypeUtil.EnumCaseMode)1 DocumentEdits (org.springframework.ide.vscode.commons.languageserver.completion.DocumentEdits)1 ICompletionProposal (org.springframework.ide.vscode.commons.languageserver.completion.ICompletionProposal)1 ValueParser (org.springframework.ide.vscode.commons.util.ValueParser)1 YamlPathSegment (org.springframework.ide.vscode.commons.yaml.path.YamlPathSegment)1