Search in sources :

Example 1 with JavaTypeInfo

use of com.redhat.qute.commons.JavaTypeInfo in project quarkus-ls by redhat-developer.

the class QuteHover method doHoverForExpressionPartWithNamespace.

public CompletableFuture<Hover> doHoverForExpressionPartWithNamespace(String namespace, Part part, Template template, String projectUri, HoverRequest hoverRequest) {
    if (NamespacePart.DATA_NAMESPACE.equals(namespace)) {
        // {data:it|em}
        // check if it is defined in parameter declaration
        ParameterDeclaration parameterDeclaration = template.findInParameterDeclarationByAlias(part.getPartName());
        if (parameterDeclaration != null) {
            return doHoverForJavaType(part, parameterDeclaration.getJavaType(), projectUri, hoverRequest);
        }
        // check if it is defined with @CheckedTemplate
        if (projectUri != null) {
            return // 
            template.getParameterDataModel(part.getPartName()).thenCompose(parameter -> {
                if (parameter != null) {
                    return doHoverForJavaType(part, parameter.getJavaType(), projectUri, hoverRequest);
                }
                return NO_HOVER;
            });
        }
        return NO_HOVER;
    }
    if (projectUri != null) {
        // {inject:be|an}
        return // 
        javaCache.findJavaElementWithNamespace(namespace, part.getPartName(), projectUri).thenApply(javaElement -> {
            if (javaElement == null) {
                return null;
            }
            if (javaElement instanceof JavaTypeInfo) {
                return doHoverForJavaType(part, (JavaTypeInfo) javaElement, hoverRequest);
            }
            JavaMemberInfo member = (JavaMemberInfo) javaElement;
            boolean hasMarkdown = hoverRequest.canSupportMarkupKind(MarkupKind.MARKDOWN);
            MarkupContent content = DocumentationUtils.getDocumentation(member, null, hasMarkdown);
            Range range = QutePositionUtility.createRange(part);
            return new Hover(content, range);
        });
    }
    return NO_HOVER;
}
Also used : JavaMemberInfo(com.redhat.qute.commons.JavaMemberInfo) JavaTypeInfo(com.redhat.qute.commons.JavaTypeInfo) ResolvedJavaTypeInfo(com.redhat.qute.commons.ResolvedJavaTypeInfo) Hover(org.eclipse.lsp4j.Hover) Range(org.eclipse.lsp4j.Range) MarkupContent(org.eclipse.lsp4j.MarkupContent) ParameterDeclaration(com.redhat.qute.parser.template.ParameterDeclaration)

Example 2 with JavaTypeInfo

use of com.redhat.qute.commons.JavaTypeInfo in project quarkus-ls by redhat-developer.

the class JavaDataModelCache method resolveJavaType.

private CompletableFuture<ResolvedJavaTypeInfo> resolveJavaType(ObjectPart objectPart, String projectUri, boolean nullIfDontMatchWithIterable) {
    CompletableFuture<ResolvedJavaTypeInfo> future = null;
    JavaTypeInfoProvider javaTypeInfo = objectPart.resolveJavaType();
    if (javaTypeInfo == null) {
        return RESOLVED_JAVA_TYPE_INFO_NULL_FUTURE;
    }
    String javaType = javaTypeInfo.getJavaType();
    if (StringUtils.isEmpty(javaType)) {
        Expression expression = javaTypeInfo.getJavaTypeExpression();
        if (expression != null) {
            String literalJavaType = expression.getLiteralJavaType();
            if (literalJavaType != null) {
                future = resolveJavaType(literalJavaType, projectUri);
            } else {
                Part lastPart = expression.getLastPart();
                if (lastPart == null) {
                    return RESOLVED_JAVA_TYPE_INFO_NULL_FUTURE;
                }
                future = resolveJavaType(lastPart, projectUri);
            }
        }
    }
    if (future == null) {
        future = resolveJavaType(javaType, projectUri);
    }
    Node node = javaTypeInfo.getJavaTypeOwnerNode();
    Section section = getOwnerSection(node);
    if (section != null) {
        if (section.isIterable()) {
            future = // 
            future.thenCompose(resolvedType -> {
                if (resolvedType == null) {
                    return RESOLVED_JAVA_TYPE_INFO_NULL_FUTURE;
                }
                if (!resolvedType.isIterable()) {
                    if (resolvedType.isInteger()) {
                        // {/for}
                        return resolveJavaType(resolvedType.getName(), projectUri);
                    }
                    if (nullIfDontMatchWithIterable) {
                        // {item.|}
                        return RESOLVED_JAVA_TYPE_INFO_NULL_FUTURE;
                    }
                }
                // valid case
                // Ex:
                // {@java.util.List<org.acme.Item> items}
                // {#for item in items}
                // {item.|}
                // Here
                // - resolvedType = java.util.List<org.acme.Item>
                // - iterTypeName = org.acme.Item
                // Resolve org.acme.Item
                String iterTypeName = resolvedType.getIterableOf();
                return resolveJavaType(iterTypeName, projectUri);
            });
        }
    }
    return future;
}
Also used : Template(com.redhat.qute.parser.template.Template) ValueResolver(com.redhat.qute.project.datamodel.resolvers.ValueResolver) MethodValueResolver(com.redhat.qute.project.datamodel.resolvers.MethodValueResolver) ObjectPart(com.redhat.qute.parser.expression.ObjectPart) StringUtils(com.redhat.qute.utils.StringUtils) CompletableFuture(java.util.concurrent.CompletableFuture) QuteJavaTypesParams(com.redhat.qute.commons.QuteJavaTypesParams) NodeKind(com.redhat.qute.parser.template.NodeKind) Parameter(com.redhat.qute.parser.template.Parameter) QuteProjectRegistry(com.redhat.qute.project.QuteProjectRegistry) Expression(com.redhat.qute.parser.template.Expression) Section(com.redhat.qute.parser.template.Section) Location(org.eclipse.lsp4j.Location) Parts(com.redhat.qute.parser.expression.Parts) JavaMemberInfo(com.redhat.qute.commons.JavaMemberInfo) InvalidMethodReason(com.redhat.qute.commons.InvalidMethodReason) NamespaceResolverInfo(com.redhat.qute.commons.datamodel.resolvers.NamespaceResolverInfo) Node(com.redhat.qute.parser.template.Node) JavaTypeInfo(com.redhat.qute.commons.JavaTypeInfo) JavaMemberResult(com.redhat.qute.project.JavaMemberResult) JavaTypeInfoProvider(com.redhat.qute.parser.template.JavaTypeInfoProvider) List(java.util.List) CompletionStage(java.util.concurrent.CompletionStage) Part(com.redhat.qute.parser.expression.Part) ResolvedJavaTypeInfo(com.redhat.qute.commons.ResolvedJavaTypeInfo) JavaElementInfo(com.redhat.qute.commons.JavaElementInfo) QuteJavaDefinitionParams(com.redhat.qute.commons.QuteJavaDefinitionParams) JavaTypeInfoProvider(com.redhat.qute.parser.template.JavaTypeInfoProvider) Expression(com.redhat.qute.parser.template.Expression) ObjectPart(com.redhat.qute.parser.expression.ObjectPart) Part(com.redhat.qute.parser.expression.Part) Node(com.redhat.qute.parser.template.Node) ResolvedJavaTypeInfo(com.redhat.qute.commons.ResolvedJavaTypeInfo) Section(com.redhat.qute.parser.template.Section)

Example 3 with JavaTypeInfo

use of com.redhat.qute.commons.JavaTypeInfo in project quarkus-ls by redhat-developer.

the class FieldValueResolver method getJavaTypeInfo.

@Override
public JavaTypeInfo getJavaTypeInfo() {
    JavaTypeInfo javaType = super.getJavaTypeInfo();
    if (javaType == null && sourceType != null) {
        javaType = new JavaTypeInfo();
        javaType.setSignature(sourceType);
        super.setJavaType(javaType);
    }
    return javaType;
}
Also used : JavaTypeInfo(com.redhat.qute.commons.JavaTypeInfo)

Example 4 with JavaTypeInfo

use of com.redhat.qute.commons.JavaTypeInfo in project quarkus-ls by redhat-developer.

the class QuteCompletionsForParameterDeclaration method collectJavaClassesSuggestions.

private CompletableFuture<CompletionList> collectJavaClassesSuggestions(String pattern, boolean hasAlias, boolean closed, JavaTypeRangeOffset rangeOffset, String projectUri, Template template, QuteCompletionSettings completionSettings) {
    QuteJavaTypesParams params = new QuteJavaTypesParams(pattern, projectUri);
    return // 
    javaCache.getJavaTypes(params).thenApply(result -> {
        if (result == null) {
            return null;
        }
        CompletionList list = new CompletionList();
        list.setItems(new ArrayList<>());
        Range range = QutePositionUtility.createRange(rangeOffset, template);
        for (JavaTypeInfo typeInfo : result) {
            String fullClassName = typeInfo.getName();
            CompletionItem item = new CompletionItem();
            item.setLabel(typeInfo.getSignature());
            TextEdit textEdit = new TextEdit();
            textEdit.setRange(range);
            StringBuilder insertText = new StringBuilder(fullClassName);
            if (typeInfo.getJavaTypeKind() == JavaTypeKind.Package) {
                item.setKind(CompletionItemKind.Module);
                if (rangeOffset.isInGeneric() && !rangeOffset.isGenericClosed()) {
                    insertText.append('>');
                }
            } else {
                item.setKind(typeInfo.getJavaTypeKind() == JavaTypeKind.Interface ? CompletionItemKind.Interface : CompletionItemKind.Class);
                int snippetIndex = 1;
                // Generate class
                int index = fullClassName.lastIndexOf('.');
                String typeName = index != -1 ? fullClassName.substring(index + 1, fullClassName.length()) : fullClassName;
                List<JavaParameterInfo> parameters = typeInfo.getTypeParameters();
                if (!parameters.isEmpty()) {
                    insertText.append('<');
                    for (int i = 0; i < parameters.size(); i++) {
                        if (i > 0) {
                            insertText.append(',');
                        }
                        String parameterType = parameters.get(i).getType();
                        if (completionSettings.isCompletionSnippetsSupported()) {
                            SnippetsBuilder.placeholders(snippetIndex++, parameterType, insertText);
                        } else {
                            insertText.append(parameterType);
                        }
                    }
                    insertText.append('>');
                } else if (rangeOffset.isInGeneric() && !rangeOffset.isGenericClosed()) {
                    insertText.append('>');
                }
                if (!hasAlias) {
                    insertText.append(' ');
                }
                // Generate alias
                String alias = String.valueOf(typeName.charAt(0)).toLowerCase() + typeName.substring(1, typeName.length());
                if (completionSettings.isCompletionSnippetsSupported()) {
                    item.setInsertTextFormat(InsertTextFormat.Snippet);
                    if (!hasAlias) {
                        SnippetsBuilder.placeholders(snippetIndex++, alias, insertText);
                        if (!closed) {
                            insertText.append("}");
                        }
                    }
                    // $0
                    SnippetsBuilder.tabstops(0, insertText);
                } else {
                    item.setInsertTextFormat(InsertTextFormat.PlainText);
                    if (!hasAlias) {
                        insertText.append(alias);
                        if (!closed) {
                            insertText.append("}");
                        }
                    }
                }
            }
            textEdit.setNewText(insertText.toString());
            item.setTextEdit(Either.forLeft(textEdit));
            list.getItems().add(item);
        }
        return list;
    });
}
Also used : CompletionList(org.eclipse.lsp4j.CompletionList) QuteJavaTypesParams(com.redhat.qute.commons.QuteJavaTypesParams) JavaTypeInfo(com.redhat.qute.commons.JavaTypeInfo) CompletionItem(org.eclipse.lsp4j.CompletionItem) TextEdit(org.eclipse.lsp4j.TextEdit) Range(org.eclipse.lsp4j.Range) JavaParameterInfo(com.redhat.qute.commons.JavaParameterInfo)

Example 5 with JavaTypeInfo

use of com.redhat.qute.commons.JavaTypeInfo in project quarkus-ls by redhat-developer.

the class JavaTypesSearch method collectClassesAndInterfaces.

private void collectClassesAndInterfaces(IProgressMonitor monitor, List<JavaTypeInfo> javaTypes) throws JavaModelException {
    // Collect classes and interfaces according to the type name
    SearchEngine engine = new SearchEngine();
    // 
    engine.searchAllTypeNames(// 
    packageName == null ? null : packageName.toCharArray(), // 
    SearchPattern.R_EXACT_MATCH, // 
    typeName.toCharArray(), // 
    SearchPattern.R_PATTERN_MATCH | SearchPattern.R_CASE_SENSITIVE, // 
    IJavaSearchConstants.CLASS_AND_INTERFACE, // 
    scope, new TypeNameMatchRequestor() {

        @Override
        public void acceptTypeNameMatch(TypeNameMatch match) {
            IType type = (IType) match.getType();
            String typeSignature = AbstractTypeResolver.resolveJavaTypeSignature(type);
            JavaTypeInfo classInfo = new JavaTypeInfo();
            classInfo.setSignature(typeSignature);
            javaTypes.add(classInfo);
            try {
                classInfo.setKind(type.isInterface() ? JavaTypeKind.Interface : JavaTypeKind.Class);
            } catch (JavaModelException e) {
                LOGGER.log(Level.SEVERE, "Error while collecting Java Types for '" + packageName + " package and Java type '" + typeName + "'.", e);
            }
        }
    }, // 
    IJavaSearchConstants.WAIT_UNTIL_READY_TO_SEARCH, monitor);
}
Also used : JavaModelException(org.eclipse.jdt.core.JavaModelException) BasicSearchEngine(org.eclipse.jdt.internal.core.search.BasicSearchEngine) SearchEngine(org.eclipse.jdt.core.search.SearchEngine) TypeNameMatch(org.eclipse.jdt.core.search.TypeNameMatch) JavaTypeInfo(com.redhat.qute.commons.JavaTypeInfo) TypeNameMatchRequestor(org.eclipse.jdt.core.search.TypeNameMatchRequestor) IType(org.eclipse.jdt.core.IType)

Aggregations

JavaTypeInfo (com.redhat.qute.commons.JavaTypeInfo)14 QuteJavaTypesParams (com.redhat.qute.commons.QuteJavaTypesParams)7 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)5 Test (org.junit.Test)5 ResolvedJavaTypeInfo (com.redhat.qute.commons.ResolvedJavaTypeInfo)4 JavaMemberInfo (com.redhat.qute.commons.JavaMemberInfo)2 List (java.util.List)2 JavaModelException (org.eclipse.jdt.core.JavaModelException)2 Range (org.eclipse.lsp4j.Range)2 InvalidMethodReason (com.redhat.qute.commons.InvalidMethodReason)1 JavaElementInfo (com.redhat.qute.commons.JavaElementInfo)1 JavaParameterInfo (com.redhat.qute.commons.JavaParameterInfo)1 JavaTypeKind (com.redhat.qute.commons.JavaTypeKind)1 QuteJavaDefinitionParams (com.redhat.qute.commons.QuteJavaDefinitionParams)1 NamespaceResolverInfo (com.redhat.qute.commons.datamodel.resolvers.NamespaceResolverInfo)1 QuteMavenProjectName (com.redhat.qute.jdt.QuteProjectTest.QuteMavenProjectName)1 QuteProjectTest.getJDTUtils (com.redhat.qute.jdt.QuteProjectTest.getJDTUtils)1 QuteProjectTest.loadMavenProject (com.redhat.qute.jdt.QuteProjectTest.loadMavenProject)1 QuteSupportForTemplate (com.redhat.qute.jdt.QuteSupportForTemplate)1 ObjectPart (com.redhat.qute.parser.expression.ObjectPart)1