-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Fred Bricon <[email protected]>
- Loading branch information
Showing
7 changed files
with
199 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
src/main/java/com/redhat/devtools/intellij/quarkus/QuarkusBundle.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2023 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.devtools.intellij.quarkus; | ||
|
||
import com.intellij.DynamicBundle; | ||
import org.jetbrains.annotations.Nls; | ||
import org.jetbrains.annotations.NonNls; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.PropertyKey; | ||
|
||
import java.util.function.Supplier; | ||
|
||
/** | ||
* Quarkus messages bundle. | ||
*/ | ||
public final class QuarkusBundle extends DynamicBundle { | ||
|
||
@NonNls public static final String BUNDLE = "messages.QuarkusBundle"; | ||
private static final QuarkusBundle INSTANCE = new QuarkusBundle(); | ||
|
||
private QuarkusBundle() { | ||
super(BUNDLE); | ||
} | ||
|
||
@NotNull | ||
public static @Nls String message(@NotNull @PropertyKey(resourceBundle = BUNDLE) String key, Object @NotNull ... params) { | ||
return INSTANCE.getMessage(key, params); | ||
} | ||
|
||
@NotNull | ||
public static Supplier<@Nls String> messagePointer(@NotNull @PropertyKey(resourceBundle = BUNDLE) String key, Object @NotNull ... params) { | ||
return INSTANCE.getLazyMessage(key, params); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
...m/redhat/devtools/intellij/quarkus/psi/internal/builditems/QuarkusBuildItemErrorCode.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2023 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.devtools.intellij.quarkus.psi.internal.builditems; | ||
|
||
import com.redhat.devtools.intellij.lsp4mp4ij.psi.core.java.diagnostics.IJavaErrorCode; | ||
|
||
/** | ||
* Represents error codes for validation issues in classes inheriting <code>io.quarkus.builder.item.BuildItem</code>. | ||
*/ | ||
public enum QuarkusBuildItemErrorCode implements IJavaErrorCode { | ||
|
||
InvalidModifierBuildItem; | ||
|
||
@Override | ||
public String getCode() { | ||
return name(); | ||
} | ||
} | ||
|
92 changes: 92 additions & 0 deletions
92
...ools/intellij/quarkus/psi/internal/validators/QuarkusBuildItemDiagnosticsParticipant.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2023 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.devtools.intellij.quarkus.psi.internal.validators; | ||
|
||
import com.intellij.openapi.module.Module; | ||
import com.intellij.psi.PsiClass; | ||
import com.intellij.psi.PsiElement; | ||
import com.intellij.psi.PsiFile; | ||
import com.intellij.psi.PsiModifier; | ||
import com.intellij.psi.util.InheritanceUtil; | ||
import com.redhat.devtools.intellij.lsp4mp4ij.psi.core.java.diagnostics.IJavaDiagnosticsParticipant; | ||
import com.redhat.devtools.intellij.lsp4mp4ij.psi.core.java.diagnostics.JavaDiagnosticsContext; | ||
import com.redhat.devtools.intellij.lsp4mp4ij.psi.core.utils.PositionUtils; | ||
import com.redhat.devtools.intellij.lsp4mp4ij.psi.core.utils.PsiTypeUtils; | ||
import com.redhat.devtools.intellij.quarkus.QuarkusBundle; | ||
import com.redhat.devtools.intellij.quarkus.QuarkusConstants; | ||
import com.redhat.devtools.intellij.quarkus.psi.internal.builditems.QuarkusBuildItemErrorCode; | ||
import org.eclipse.lsp4j.Diagnostic; | ||
import org.eclipse.lsp4j.DiagnosticSeverity; | ||
import org.eclipse.lsp4j.Range; | ||
import org.eclipse.lsp4mp.commons.DocumentFormat; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class QuarkusBuildItemDiagnosticsParticipant implements IJavaDiagnosticsParticipant { | ||
|
||
@Override | ||
public boolean isAdaptedForDiagnostics(JavaDiagnosticsContext context) { | ||
// Collection of diagnostics for Quarkus Build Items is done only if | ||
// io.quarkus.builder.item.BuildItem is on the classpath | ||
Module javaProject = context.getJavaProject(); | ||
return PsiTypeUtils.findType(javaProject, QuarkusConstants.QUARKUS_BUILD_ITEM_CLASS_NAME) != null; | ||
} | ||
|
||
@Override | ||
public List<Diagnostic> collectDiagnostics(JavaDiagnosticsContext context) { | ||
PsiFile typeRoot = context.getTypeRoot(); | ||
PsiElement[] elements = typeRoot.getChildren(); | ||
List<Diagnostic> diagnostics = new ArrayList<>(); | ||
collectDiagnostics(elements, diagnostics, context); | ||
return diagnostics; | ||
} | ||
|
||
private static void collectDiagnostics(PsiElement[] elements, List<Diagnostic> diagnostics, | ||
JavaDiagnosticsContext context) { | ||
for (PsiElement element : elements) { | ||
if (element instanceof PsiClass) { | ||
PsiClass psiClass = (PsiClass) element; | ||
if (isBuildItem(psiClass)) { | ||
validateBuildItem(psiClass, diagnostics, context); | ||
} | ||
} | ||
} | ||
} | ||
|
||
private static boolean isBuildItem(PsiClass type) { | ||
return InheritanceUtil.isInheritor(type, QuarkusConstants.QUARKUS_BUILD_ITEM_CLASS_NAME); | ||
} | ||
|
||
private static void validateBuildItem(PsiClass psiClass, List<Diagnostic> diagnostics, JavaDiagnosticsContext context) { | ||
if (psiClass.hasModifierProperty(PsiModifier.FINAL) | ||
|| psiClass.hasModifierProperty(PsiModifier.ABSTRACT) | ||
) { | ||
return; | ||
} | ||
Range range = PositionUtils.toClassDeclarationRange(psiClass, context.getUtils()); | ||
Diagnostic d = context.createDiagnostic(context.getUri(), | ||
createDiagnosticMessage(psiClass, context.getDocumentFormat()), | ||
range, QuarkusConstants.QUARKUS_DIAGNOSTIC_SOURCE, | ||
QuarkusBuildItemErrorCode.InvalidModifierBuildItem, | ||
DiagnosticSeverity.Error | ||
); | ||
diagnostics.add(d); | ||
} | ||
|
||
private static String createDiagnosticMessage(PsiClass classType, DocumentFormat documentFormat) { | ||
String quote = DocumentFormat.Markdown.equals(documentFormat)?"`":"'"; | ||
return QuarkusBundle.message("quarkus.validation.buildItem.invalidmodifier", classType.getQualifiedName(), quote); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
############################################################################### | ||
# Copyright (c) 2023 Red Hat Inc. and others. | ||
# All rights reserved. This program and the accompanying materials | ||
# are made available under the terms of the Eclipse Public License v2.0 | ||
# which accompanies this distribution, and is available at | ||
# http://www.eclipse.org/legal/epl-v20.html | ||
# | ||
# SPDX-License-Identifier: EPL-2.0 | ||
# | ||
# Contributors: | ||
# Red Hat Inc. - initial API and implementation | ||
############################################################################### | ||
|
||
quarkus.validation.buildItem.invalidmodifier=BuildItem class {1}{0}{1} must either be declared final or abstract |