Search in sources :

Example 6 with PsiIdentifier

use of com.intellij.psi.PsiIdentifier in project intellij-community by JetBrains.

the class FieldSelectioner method select.

@Override
public List<TextRange> select(PsiElement e, CharSequence editorText, int cursorOffset, Editor editor) {
    List<TextRange> result = super.select(e, editorText, cursorOffset, editor);
    final PsiField field = (PsiField) e;
    final TextRange range = field.getTextRange();
    final PsiIdentifier first = field.getNameIdentifier();
    final TextRange firstRange = first.getTextRange();
    final PsiElement last = field.getInitializer();
    final int end = last == null ? firstRange.getEndOffset() : last.getTextRange().getEndOffset();
    PsiDocComment comment = field.getDocComment();
    if (comment != null) {
        TextRange commentTextRange = comment.getTextRange();
        addRangeElem(result, editorText, comment, commentTextRange.getEndOffset());
    }
    addRangeElem(result, editorText, first, end);
    //addRangeElem (result, editorText, field, textLength, field.getTypeElement(), end);
    addRangeElem(result, editorText, field.getModifierList(), range.getEndOffset());
    //addRangeElem (result, editorText, field, textLength, field.getDocComment(), end);
    result.addAll(expandToWholeLine(editorText, range));
    return result;
}
Also used : PsiDocComment(com.intellij.psi.javadoc.PsiDocComment) PsiField(com.intellij.psi.PsiField) PsiIdentifier(com.intellij.psi.PsiIdentifier) TextRange(com.intellij.openapi.util.TextRange) PsiElement(com.intellij.psi.PsiElement)

Example 7 with PsiIdentifier

use of com.intellij.psi.PsiIdentifier in project intellij-community by JetBrains.

the class HighlightSeverityTest method testErrorLikeUnusedSymbol.

public void testErrorLikeUnusedSymbol() throws Exception {
    enableInspectionTool(new LocalInspectionTool() {

        @NotNull
        @Override
        public String getShortName() {
            return getDisplayName();
        }

        @NotNull
        @Override
        public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, boolean isOnTheFly, @NotNull LocalInspectionToolSession session) {
            return new JavaElementVisitor() {

                @Override
                public void visitIdentifier(PsiIdentifier identifier) {
                    if (identifier.getText().equals("k")) {
                        holder.registerProblem(identifier, "Variable 'k' is never used");
                    }
                }
            };
        }

        @NotNull
        @Override
        public HighlightDisplayLevel getDefaultLevel() {
            return HighlightDisplayLevel.ERROR;
        }

        @Nls
        @NotNull
        @Override
        public String getDisplayName() {
            return "x";
        }

        @Nls
        @NotNull
        @Override
        public String getGroupDisplayName() {
            return getDisplayName();
        }
    });
    doTest(BASE_PATH + "/" + getTestName(false) + ".java", true, false);
}
Also used : HighlightDisplayLevel(com.intellij.codeHighlighting.HighlightDisplayLevel) JavaElementVisitor(com.intellij.psi.JavaElementVisitor) Nls(org.jetbrains.annotations.Nls) NonNls(org.jetbrains.annotations.NonNls) LocalInspectionToolSession(com.intellij.codeInspection.LocalInspectionToolSession) PsiIdentifier(com.intellij.psi.PsiIdentifier) PsiElementVisitor(com.intellij.psi.PsiElementVisitor) LocalInspectionTool(com.intellij.codeInspection.LocalInspectionTool) NotNull(org.jetbrains.annotations.NotNull) ProblemsHolder(com.intellij.codeInspection.ProblemsHolder)

Example 8 with PsiIdentifier

use of com.intellij.psi.PsiIdentifier in project intellij-community by JetBrains.

the class NamedElementTokenizer method tokenize.

@Override
public void tokenize(@NotNull T element, TokenConsumer consumer) {
    final PsiIdentifier psiIdentifier = PsiTreeUtil.getChildOfType(element, PsiIdentifier.class);
    final PsiTypeElement psiType = PsiTreeUtil.getChildOfType(element, PsiTypeElement.class);
    if (psiIdentifier == null) {
        return;
    }
    final String identifier = psiIdentifier.getText();
    final String type = psiType == null ? null : psiType.getText();
    if (identifier == null) {
        return;
    }
    if (type == null || !type.equalsIgnoreCase(identifier)) {
        myIdentifierTokenizer.tokenize(psiIdentifier, consumer);
    }
    if (psiType != null) {
        myTypeTokenizer.tokenize(psiType, consumer);
    }
}
Also used : PsiTypeElement(com.intellij.psi.PsiTypeElement) PsiIdentifier(com.intellij.psi.PsiIdentifier)

Example 9 with PsiIdentifier

use of com.intellij.psi.PsiIdentifier in project intellij-community by JetBrains.

the class ClassOnlyUsedInOnePackageInspection method checkElement.

@Nullable
@Override
public CommonProblemDescriptor[] checkElement(@NotNull RefEntity refEntity, @NotNull AnalysisScope scope, @NotNull InspectionManager manager, @NotNull GlobalInspectionContext globalContext) {
    if (!(refEntity instanceof RefClass)) {
        return null;
    }
    final RefClass refClass = (RefClass) refEntity;
    final RefEntity owner = refClass.getOwner();
    if (!(owner instanceof RefPackage)) {
        return null;
    }
    final Set<RefClass> dependencies = DependencyUtils.calculateDependenciesForClass(refClass);
    RefPackage otherPackage = null;
    for (RefClass dependency : dependencies) {
        final RefPackage refPackage = RefJavaUtil.getPackage(dependency);
        if (owner == refPackage) {
            return null;
        }
        if (otherPackage != refPackage) {
            if (otherPackage == null) {
                otherPackage = refPackage;
            } else {
                return null;
            }
        }
    }
    final Set<RefClass> dependents = DependencyUtils.calculateDependentsForClass(refClass);
    for (RefClass dependent : dependents) {
        final RefPackage refPackage = RefJavaUtil.getPackage(dependent);
        if (owner == refPackage) {
            return null;
        }
        if (otherPackage != refPackage) {
            if (otherPackage == null) {
                otherPackage = refPackage;
            } else {
                return null;
            }
        }
    }
    if (otherPackage == null) {
        return null;
    }
    final PsiClass aClass = refClass.getElement();
    final PsiIdentifier identifier = aClass.getNameIdentifier();
    if (identifier == null) {
        return null;
    }
    final String packageName = otherPackage.getName();
    return new CommonProblemDescriptor[] { manager.createProblemDescriptor(identifier, InspectionGadgetsBundle.message("class.only.used.in.one.package.problem.descriptor", packageName), true, ProblemHighlightType.GENERIC_ERROR_OR_WARNING, false) };
}
Also used : CommonProblemDescriptor(com.intellij.codeInspection.CommonProblemDescriptor) PsiClass(com.intellij.psi.PsiClass) PsiIdentifier(com.intellij.psi.PsiIdentifier) Nullable(org.jetbrains.annotations.Nullable)

Example 10 with PsiIdentifier

use of com.intellij.psi.PsiIdentifier in project intellij-community by JetBrains.

the class ClassOnlyUsedInOneModuleInspection method checkElement.

@Nullable
@Override
public CommonProblemDescriptor[] checkElement(@NotNull RefEntity refEntity, @NotNull AnalysisScope scope, @NotNull InspectionManager manager, @NotNull GlobalInspectionContext globalContext) {
    if (!(refEntity instanceof RefClass)) {
        return null;
    }
    final RefClass refClass = (RefClass) refEntity;
    final RefEntity owner = refClass.getOwner();
    if (!(owner instanceof RefPackage)) {
        return null;
    }
    final Set<RefClass> dependencies = DependencyUtils.calculateDependenciesForClass(refClass);
    RefModule otherModule = null;
    for (RefClass dependency : dependencies) {
        final RefModule module = dependency.getModule();
        if (refClass.getModule() == module) {
            return null;
        }
        if (otherModule != module) {
            if (otherModule == null) {
                otherModule = module;
            } else {
                return null;
            }
        }
    }
    final Set<RefClass> dependents = DependencyUtils.calculateDependentsForClass(refClass);
    for (RefClass dependent : dependents) {
        final RefModule module = dependent.getModule();
        if (refClass.getModule() == module) {
            return null;
        }
        if (otherModule != module) {
            if (otherModule == null) {
                otherModule = module;
            } else {
                return null;
            }
        }
    }
    if (otherModule == null) {
        return null;
    }
    final PsiClass aClass = refClass.getElement();
    final PsiIdentifier identifier = aClass.getNameIdentifier();
    if (identifier == null) {
        return null;
    }
    final String moduleName = otherModule.getName();
    return new CommonProblemDescriptor[] { manager.createProblemDescriptor(identifier, InspectionGadgetsBundle.message("class.only.used.in.one.module.problem.descriptor", moduleName), true, ProblemHighlightType.GENERIC_ERROR_OR_WARNING, false) };
}
Also used : RefModule(com.intellij.codeInspection.reference.RefModule) RefClass(com.intellij.codeInspection.reference.RefClass) CommonProblemDescriptor(com.intellij.codeInspection.CommonProblemDescriptor) RefEntity(com.intellij.codeInspection.reference.RefEntity) RefPackage(com.intellij.codeInspection.reference.RefPackage) PsiClass(com.intellij.psi.PsiClass) PsiIdentifier(com.intellij.psi.PsiIdentifier) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

PsiIdentifier (com.intellij.psi.PsiIdentifier)12 PsiClass (com.intellij.psi.PsiClass)6 Nullable (org.jetbrains.annotations.Nullable)5 CommonProblemDescriptor (com.intellij.codeInspection.CommonProblemDescriptor)4 LocalInspectionTool (com.intellij.codeInspection.LocalInspectionTool)2 RefClass (com.intellij.codeInspection.reference.RefClass)2 RefEntity (com.intellij.codeInspection.reference.RefEntity)2 RefPackage (com.intellij.codeInspection.reference.RefPackage)2 PsiElement (com.intellij.psi.PsiElement)2 PsiFile (com.intellij.psi.PsiFile)2 NotNull (org.jetbrains.annotations.NotNull)2 HighlightDisplayLevel (com.intellij.codeHighlighting.HighlightDisplayLevel)1 IntentionAction (com.intellij.codeInsight.intention.IntentionAction)1 GlobalInspectionTool (com.intellij.codeInspection.GlobalInspectionTool)1 LocalInspectionToolSession (com.intellij.codeInspection.LocalInspectionToolSession)1 ProblemsHolder (com.intellij.codeInspection.ProblemsHolder)1 RefModule (com.intellij.codeInspection.reference.RefModule)1 Result (com.intellij.openapi.application.Result)1 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)1 Document (com.intellij.openapi.editor.Document)1