From 2418c840e65d77ee8766949ee8179acd3deea528 Mon Sep 17 00:00:00 2001 From: azerr Date: Thu, 8 Aug 2024 10:22:24 +0200 Subject: [PATCH] feat: New Feature: outline extension BuildItems Fixes #1094 Signed-off-by: azerr --- .../builditems/QuarkusBuildItemUtils.java | 53 ++++++++++++++ ...uarkusBuildItemDiagnosticsParticipant.java | 8 +- .../BuildItemWorkspaceSymbolParticipant.java | 73 +++++++++++++++++++ src/main/resources/META-INF/plugin.xml | 1 + 4 files changed, 131 insertions(+), 4 deletions(-) create mode 100644 src/main/java/com/redhat/devtools/intellij/quarkus/psi/internal/builditems/QuarkusBuildItemUtils.java create mode 100644 src/main/java/com/redhat/microprofile/psi/internal/quarkus/builditems/java/BuildItemWorkspaceSymbolParticipant.java diff --git a/src/main/java/com/redhat/devtools/intellij/quarkus/psi/internal/builditems/QuarkusBuildItemUtils.java b/src/main/java/com/redhat/devtools/intellij/quarkus/psi/internal/builditems/QuarkusBuildItemUtils.java new file mode 100644 index 000000000..d55e7887f --- /dev/null +++ b/src/main/java/com/redhat/devtools/intellij/quarkus/psi/internal/builditems/QuarkusBuildItemUtils.java @@ -0,0 +1,53 @@ +package com.redhat.devtools.intellij.quarkus.psi.internal.builditems; + +import com.intellij.openapi.module.Module; +import com.intellij.openapi.progress.ProgressIndicator; +import com.intellij.psi.PsiClass; +import com.intellij.psi.PsiModifier; +import com.intellij.psi.search.ProjectScope; +import com.intellij.psi.search.searches.DefinitionsScopedSearch; +import com.redhat.devtools.intellij.lsp4mp4ij.psi.core.utils.PsiTypeUtils; +import com.redhat.devtools.intellij.quarkus.QuarkusConstants; +import com.redhat.microprofile.psi.internal.quarkus.renarde.java.RenardeConstants; + +import java.util.Collections; +import java.util.HashSet; +import java.util.Set; + +public class QuarkusBuildItemUtils { + + private QuarkusBuildItemUtils() { + + } + + /** + * Returns a set of all classes in the given project that extend Renarde's + * Controller class. + * + * @param project the project to search in + * @param monitor the progress monitor + * @return a set of all classes in the given project that extend Renarde's + * Controller class + */ + public static Set getAllBuildItemClasses(Module project, ProgressIndicator monitor) { + PsiClass buildItemType = PsiTypeUtils.findType(project, QuarkusConstants.QUARKUS_BUILD_ITEM_CLASS_NAME); + if (buildItemType == null) { + return Collections.emptySet(); + } + + Set types = new HashSet<>(); + DefinitionsScopedSearch.search(buildItemType, ProjectScope.getAllScope(project.getProject()), true) + .forEach(element -> { + if (element instanceof PsiClass type && isValidBuildItem(type)) { + types.add(type); + } + }); + return types; + } + + + public static boolean isValidBuildItem(PsiClass psiClass) { + return psiClass.hasModifierProperty(PsiModifier.FINAL) + || psiClass.hasModifierProperty(PsiModifier.ABSTRACT); + } +} diff --git a/src/main/java/com/redhat/devtools/intellij/quarkus/psi/internal/validators/QuarkusBuildItemDiagnosticsParticipant.java b/src/main/java/com/redhat/devtools/intellij/quarkus/psi/internal/validators/QuarkusBuildItemDiagnosticsParticipant.java index b467e94e0..50465090b 100644 --- a/src/main/java/com/redhat/devtools/intellij/quarkus/psi/internal/validators/QuarkusBuildItemDiagnosticsParticipant.java +++ b/src/main/java/com/redhat/devtools/intellij/quarkus/psi/internal/validators/QuarkusBuildItemDiagnosticsParticipant.java @@ -33,6 +33,8 @@ import java.util.ArrayList; import java.util.List; +import static com.redhat.devtools.intellij.quarkus.psi.internal.builditems.QuarkusBuildItemUtils.isValidBuildItem; + /** * Validates io.quarkus.builder.item.BuildItem subclasses. *
    @@ -77,9 +79,7 @@ private static boolean isBuildItem(PsiClass type) { } private static void validateBuildItem(PsiClass psiClass, List diagnostics, JavaDiagnosticsContext context) { - if (psiClass.hasModifierProperty(PsiModifier.FINAL) - || psiClass.hasModifierProperty(PsiModifier.ABSTRACT) - ) { + if (isValidBuildItem(psiClass)) { return; } Range range = PositionUtils.toClassDeclarationRange(psiClass, context.getUtils()); @@ -93,7 +93,7 @@ private static void validateBuildItem(PsiClass psiClass, List diagno } private static String createDiagnosticMessage(PsiClass classType, DocumentFormat documentFormat) { - String quote = DocumentFormat.Markdown.equals(documentFormat)?"`":"'"; + String quote = DocumentFormat.Markdown.equals(documentFormat) ? "`" : "'"; return String.format(INVALID_MODIFIER, classType.getQualifiedName(), quote); } } diff --git a/src/main/java/com/redhat/microprofile/psi/internal/quarkus/builditems/java/BuildItemWorkspaceSymbolParticipant.java b/src/main/java/com/redhat/microprofile/psi/internal/quarkus/builditems/java/BuildItemWorkspaceSymbolParticipant.java new file mode 100644 index 000000000..cd2761feb --- /dev/null +++ b/src/main/java/com/redhat/microprofile/psi/internal/quarkus/builditems/java/BuildItemWorkspaceSymbolParticipant.java @@ -0,0 +1,73 @@ +/******************************************************************************* +* Copyright (c) 2024 Red Hat Inc. and others. +* +* This program and the accompanying materials are made available under the +* terms of the Eclipse Public License v. 2.0 which is available at +* http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 +* which is available at https://www.apache.org/licenses/LICENSE-2.0. +* +* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 +* +* Contributors: +* Red Hat Inc. - initial API and implementation +*******************************************************************************/ +package com.redhat.microprofile.psi.internal.quarkus.builditems.java; + +import com.intellij.openapi.module.Module; +import com.intellij.openapi.progress.ProgressIndicator; +import com.intellij.openapi.util.TextRange; +import com.intellij.psi.PsiClass; +import com.redhat.devtools.intellij.lsp4mp4ij.psi.core.java.symbols.IJavaWorkspaceSymbolsParticipant; +import com.redhat.devtools.intellij.lsp4mp4ij.psi.core.jaxrs.IJaxRsInfoProvider; +import com.redhat.devtools.intellij.lsp4mp4ij.psi.core.jaxrs.JaxRsContext; +import com.redhat.devtools.intellij.lsp4mp4ij.psi.core.jaxrs.JaxRsMethodInfo; +import com.redhat.devtools.intellij.lsp4mp4ij.psi.core.utils.IPsiUtils; +import com.redhat.devtools.intellij.lsp4mp4ij.psi.internal.jaxrs.java.JaxRsInfoProviderRegistry; +import com.redhat.devtools.intellij.quarkus.psi.internal.builditems.QuarkusBuildItemUtils; +import org.eclipse.lsp4j.Location; +import org.eclipse.lsp4j.Range; +import org.eclipse.lsp4j.SymbolInformation; +import org.eclipse.lsp4j.SymbolKind; + +import java.net.MalformedURLException; +import java.net.URL; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * Collects workspace symbols for Quarkus BuildItem. + */ +public class BuildItemWorkspaceSymbolParticipant implements IJavaWorkspaceSymbolsParticipant { + + private static final Logger LOGGER = Logger.getLogger(BuildItemWorkspaceSymbolParticipant.class.getName()); + + @Override + public void collectSymbols(Module project, IPsiUtils utils, List symbols, ProgressIndicator monitor) { + if (monitor.isCanceled()) { + return; + } + + QuarkusBuildItemUtils.getAllBuildItemClasses(project, monitor) + .forEach(buildType -> { + symbols.add(createSymbol(buildType, utils )); + }); + } + + private static SymbolInformation createSymbol(PsiClass buildItemType, IPsiUtils utils) { + Location location = utils.toLocation(buildItemType); + + StringBuilder nameBuilder = new StringBuilder("&"); + nameBuilder.append(buildItemType.getName()); + + SymbolInformation symbol = new SymbolInformation(); + symbol.setName(nameBuilder.toString()); + symbol.setKind(SymbolKind.Class); + symbol.setLocation(location); + return symbol; + } + +} \ No newline at end of file diff --git a/src/main/resources/META-INF/plugin.xml b/src/main/resources/META-INF/plugin.xml index 521a1461c..fa86f956d 100644 --- a/src/main/resources/META-INF/plugin.xml +++ b/src/main/resources/META-INF/plugin.xml @@ -540,6 +540,7 @@ implementationClass="com.redhat.devtools.intellij.lsp4mp4ij.psi.internal.openapi.java.MicroProfileGenerateOpenAPIOperation"/> +