Search in sources :

Example 41 with LeafElement

use of com.intellij.psi.impl.source.tree.LeafElement in project phpinspectionsea by kalessil.

the class MisorderedModifiersInspector method buildVisitor.

@Override
@NotNull
public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder problemsHolder, final boolean isOnTheFly) {
    return new BasePhpElementVisitor() {

        @Override
        public void visitPhpMethod(@NotNull Method method) {
            if (method.isStatic() || method.isAbstract() || method.isFinal()) {
                final PhpModifierList modifiersNode = PsiTreeUtil.findChildOfType(method, PhpModifierList.class);
                final List<LeafPsiElement> modifiers = PsiTreeUtil.findChildrenOfType(modifiersNode, LeafPsiElement.class).stream().filter(element -> !(element instanceof PsiWhiteSpace)).collect(Collectors.toList());
                if (modifiersNode != null && modifiers.size() >= 2) {
                    final String original = this.getOriginalOrder(modifiers);
                    final String expected = this.getExpectedOrder(original, standardOrder);
                    if (!original.equals(expected)) {
                        problemsHolder.registerProblem(modifiersNode, MessagesPresentationUtil.prefixWithEa(message), new TheLocalFix(expected));
                    }
                }
            }
        }

        @NotNull
        private String getOriginalOrder(@NotNull Collection<LeafPsiElement> original) {
            return original.stream().map(LeafElement::getText).collect(Collectors.joining(" ")).toLowerCase();
        }

        @NotNull
        private String getExpectedOrder(@NotNull String original, @NotNull Collection<String> expected) {
            return expected.stream().filter(original::contains).collect(Collectors.joining(" "));
        }
    };
}
Also used : PhpPsiElementFactory(com.jetbrains.php.lang.psi.PhpPsiElementFactory) BasePhpInspection(com.kalessil.phpStorm.phpInspectionsEA.openApi.BasePhpInspection) Collection(java.util.Collection) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) Method(com.jetbrains.php.lang.psi.elements.Method) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) PsiTreeUtil(com.intellij.psi.util.PsiTreeUtil) BasePhpElementVisitor(com.kalessil.phpStorm.phpInspectionsEA.openApi.BasePhpElementVisitor) List(java.util.List) ProblemDescriptor(com.intellij.codeInspection.ProblemDescriptor) LeafElement(com.intellij.psi.impl.source.tree.LeafElement) MessagesPresentationUtil(com.kalessil.phpStorm.phpInspectionsEA.utils.MessagesPresentationUtil) PsiElement(com.intellij.psi.PsiElement) PsiWhiteSpace(com.intellij.psi.PsiWhiteSpace) Project(com.intellij.openapi.project.Project) NotNull(org.jetbrains.annotations.NotNull) LocalQuickFix(com.intellij.codeInspection.LocalQuickFix) PsiElementVisitor(com.intellij.psi.PsiElementVisitor) ProblemsHolder(com.intellij.codeInspection.ProblemsHolder) PhpModifierList(com.jetbrains.php.lang.psi.elements.PhpModifierList) BasePhpElementVisitor(com.kalessil.phpStorm.phpInspectionsEA.openApi.BasePhpElementVisitor) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) Collection(java.util.Collection) Method(com.jetbrains.php.lang.psi.elements.Method) NotNull(org.jetbrains.annotations.NotNull) LeafElement(com.intellij.psi.impl.source.tree.LeafElement) PhpModifierList(com.jetbrains.php.lang.psi.elements.PhpModifierList) PsiWhiteSpace(com.intellij.psi.PsiWhiteSpace) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

LeafElement (com.intellij.psi.impl.source.tree.LeafElement)41 ASTNode (com.intellij.lang.ASTNode)15 PsiElement (com.intellij.psi.PsiElement)9 NotNull (org.jetbrains.annotations.NotNull)8 Nullable (org.jetbrains.annotations.Nullable)6 TreeElement (com.intellij.psi.impl.source.tree.TreeElement)4 IElementType (com.intellij.psi.tree.IElementType)4 PsiFragment (com.intellij.dupLocator.util.PsiFragment)3 CompositeElement (com.intellij.psi.impl.source.tree.CompositeElement)3 FileElement (com.intellij.psi.impl.source.tree.FileElement)3 EquivalenceDescriptorProvider (com.intellij.dupLocator.equivalence.EquivalenceDescriptorProvider)2 Project (com.intellij.openapi.project.Project)2 VirtualFile (com.intellij.openapi.vfs.VirtualFile)2 PsiFile (com.intellij.psi.PsiFile)2 PsiWhiteSpace (com.intellij.psi.PsiWhiteSpace)2 LeafPsiElement (com.intellij.psi.impl.source.tree.LeafPsiElement)2 CharTable (com.intellij.util.CharTable)2 LookupElement (com.intellij.codeInsight.lookup.LookupElement)1 LocalQuickFix (com.intellij.codeInspection.LocalQuickFix)1 ProblemDescriptor (com.intellij.codeInspection.ProblemDescriptor)1