|
| 1 | +/* |
| 2 | + * Copyright MapStruct Authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License version 2.0, available at https://www.apache.org/licenses/LICENSE-2.0 |
| 5 | + */ |
| 6 | +package org.mapstruct.intellij.inspection; |
| 7 | + |
| 8 | +import com.intellij.codeInspection.ProblemHighlightType; |
| 9 | +import com.intellij.codeInspection.ProblemsHolder; |
| 10 | +import com.intellij.openapi.util.TextRange; |
| 11 | +import com.intellij.psi.ContributedReferenceHost; |
| 12 | +import com.intellij.psi.PsiElement; |
| 13 | +import com.intellij.psi.PsiElementVisitor; |
| 14 | +import com.intellij.psi.PsiLanguageInjectionHost; |
| 15 | +import com.intellij.psi.PsiReference; |
| 16 | +import org.jetbrains.annotations.NotNull; |
| 17 | +import org.mapstruct.intellij.codeinsight.references.BaseReference; |
| 18 | + |
| 19 | +/** |
| 20 | + * Inspection that checks if mapstruct references can be resolved. |
| 21 | + * @see BaseReference |
| 22 | + * @author hduelme |
| 23 | + */ |
| 24 | +public class MapstructReferenceInspection extends InspectionBase { |
| 25 | + |
| 26 | + @Override |
| 27 | + @NotNull PsiElementVisitor buildVisitorInternal(@NotNull ProblemsHolder holder, boolean isOnTheFly) { |
| 28 | + return new MapstructReferenceVisitor(holder); |
| 29 | + } |
| 30 | + |
| 31 | + private static class MapstructReferenceVisitor extends PsiElementVisitor { |
| 32 | + |
| 33 | + private final ProblemsHolder holder; |
| 34 | + |
| 35 | + private MapstructReferenceVisitor(ProblemsHolder holder) { |
| 36 | + this.holder = holder; |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Based on org.intellij.plugins.intelliLang.references.InjectedReferencesInspection |
| 41 | + */ |
| 42 | + @Override |
| 43 | + public void visitElement(@NotNull PsiElement element) { |
| 44 | + if (element instanceof ContributedReferenceHost r && element instanceof PsiLanguageInjectionHost) { |
| 45 | + for (PsiReference psiReference : r.getReferences()) { |
| 46 | + if (psiReference instanceof BaseReference && psiReference.resolve() == null) { |
| 47 | + TextRange range = psiReference.getRangeInElement(); |
| 48 | + if (range.isEmpty() && range.getStartOffset() == 1 && "\"\"".equals( element.getText() ) ) { |
| 49 | + String message = ProblemsHolder.unresolvedReferenceMessage( psiReference ); |
| 50 | + holder.registerProblem( element, message, ProblemHighlightType.LIKE_UNKNOWN_SYMBOL, |
| 51 | + TextRange.create( 0, 2 ) ); |
| 52 | + } |
| 53 | + else { |
| 54 | + holder.registerProblem( psiReference ); |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + super.visitElement( element ); |
| 60 | + } |
| 61 | + } |
| 62 | +} |
0 commit comments