From ac8b0a5c3d7cc0c7e557b3234da15a7a848f3799 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Pol=C3=A1k?= <456647@muni.cz> Date: Wed, 15 Dec 2021 13:44:13 +0100 Subject: [PATCH] Fix UnreachableAnnotation Took 23 minutes --- .../impl/UnreachableAnnotation.java | 63 ++++++++++--------- src/main/resources/META-INF/plugin.xml | 1 + 2 files changed, 36 insertions(+), 28 deletions(-) diff --git a/src/main/java/glslplugin/annotation/impl/UnreachableAnnotation.java b/src/main/java/glslplugin/annotation/impl/UnreachableAnnotation.java index e909b8c..4dc3162 100755 --- a/src/main/java/glslplugin/annotation/impl/UnreachableAnnotation.java +++ b/src/main/java/glslplugin/annotation/impl/UnreachableAnnotation.java @@ -26,57 +26,64 @@ import com.intellij.psi.PsiElement; import com.intellij.psi.TokenType; import com.intellij.psi.tree.IElementType; +import com.intellij.psi.util.PsiTreeUtil; import glslplugin.annotation.Annotator; import glslplugin.lang.elements.GLSLElement; import glslplugin.lang.elements.GLSLElementTypes; import glslplugin.lang.elements.GLSLTokenTypes; +import glslplugin.lang.elements.declarations.GLSLFunctionDefinition; import glslplugin.lang.elements.statements.GLSLLabelStatement; import glslplugin.lang.elements.statements.GLSLStatement; import org.jetbrains.annotations.NotNull; -public class UnreachableAnnotation extends Annotator { +import java.util.Collection; + +public class UnreachableAnnotation extends Annotator { private final TextAttributesKey unreachableAttributes; public UnreachableAnnotation() { unreachableAttributes = TextAttributesKey.createTextAttributesKey("GLSL.UNREACHABLE", CodeInsightColors.NOT_USED_ELEMENT_ATTRIBUTES); } - public void annotate(GLSLStatement expr, AnnotationHolder holder) { - GLSLStatement.TerminatorScope scope = expr.getTerminatorScope(); - if (scope == GLSLStatement.TerminatorScope.NONE) return; + public void annotate(GLSLFunctionDefinition function, AnnotationHolder holder) { + final Collection statements = PsiTreeUtil.findChildrenOfType(function, GLSLStatement.class); + for (GLSLStatement statement : statements) { + GLSLStatement.TerminatorScope scope = statement.getTerminatorScope(); + if (scope == GLSLStatement.TerminatorScope.NONE) continue; - if (expr.getParent() == null - || expr.getParent().getNode().getElementType() != GLSLElementTypes.COMPOUND_STATEMENT) { - return; - } + final PsiElement parent = statement.getParent(); + if (parent == null || parent.getNode().getElementType() != GLSLElementTypes.COMPOUND_STATEMENT) { + continue; + } - PsiElement element = expr.getNextSibling(); - while (element != null) { - if (element instanceof GLSLElement && !GLSLTokenTypes.PREPROCESSOR_DIRECTIVES.contains(element.getNode().getElementType())) { - if (element instanceof GLSLLabelStatement) return; - PsiElement child = element.getFirstChild(); + PsiElement element = statement.getNextSibling(); + while (element != null) { + if (element instanceof GLSLElement && !GLSLTokenTypes.PREPROCESSOR_DIRECTIVES.contains(element.getNode().getElementType())) { + if (element instanceof GLSLLabelStatement) break; + PsiElement child = element.getFirstChild(); - if(child == null){ - holder.newAnnotation(HighlightSeverity.WARNING, "Unreachable expression").range(element).textAttributes(unreachableAttributes).create(); - }else{ - do { - IElementType type = child.getNode().getElementType(); - if(!GLSLTokenTypes.PREPROCESSOR_DIRECTIVES.contains(type) && type != TokenType.WHITE_SPACE){ - if (child instanceof GLSLLabelStatement) return; - holder.newAnnotation(HighlightSeverity.WARNING, "Unreachable expression").range(child).textAttributes(unreachableAttributes).create(); - } - child = child.getNextSibling(); - }while(child != null); - } + if (child == null) { + holder.newAnnotation(HighlightSeverity.WARNING, "Unreachable expression").range(element).textAttributes(unreachableAttributes).create(); + } else { + do { + IElementType type = child.getNode().getElementType(); + if(!GLSLTokenTypes.PREPROCESSOR_DIRECTIVES.contains(type) && type != TokenType.WHITE_SPACE){ + if (child instanceof GLSLLabelStatement) break; + holder.newAnnotation(HighlightSeverity.WARNING, "Unreachable expression").range(child).textAttributes(unreachableAttributes).create(); + } + child = child.getNextSibling(); + } while(child != null); + } + } + element = element.getNextSibling(); } - element = element.getNextSibling(); } } @NotNull @Override - public Class getElementType() { - return GLSLStatement.class; + public Class getElementType() { + return GLSLFunctionDefinition.class; } } diff --git a/src/main/resources/META-INF/plugin.xml b/src/main/resources/META-INF/plugin.xml index e7c4165..96f31cf 100755 --- a/src/main/resources/META-INF/plugin.xml +++ b/src/main/resources/META-INF/plugin.xml @@ -31,6 +31,7 @@ + com.intellij.modules.platform com.intellij.modules.lang