Skip to content

Commit

Permalink
Fix UnreachableAnnotation
Browse files Browse the repository at this point in the history
Took 23 minutes
  • Loading branch information
Jan Polák committed Dec 15, 2021
1 parent ee14492 commit ac8b0a5
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 28 deletions.
63 changes: 35 additions & 28 deletions src/main/java/glslplugin/annotation/impl/UnreachableAnnotation.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<GLSLStatement> {
import java.util.Collection;

public class UnreachableAnnotation extends Annotator<GLSLFunctionDefinition> {
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<GLSLStatement> 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<GLSLStatement> getElementType() {
return GLSLStatement.class;
public Class<GLSLFunctionDefinition> getElementType() {
return GLSLFunctionDefinition.class;
}
}
1 change: 1 addition & 0 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
<!-- https://intellij-support.jetbrains.com/hc/en-us/articles/206544879-Selecting-the-JDK-version-the-IDE-will-run-under -->
<!-- Until build is not set, because we hope it will work. -->
<idea-version since-build="203.5981.155"/>
<depends>com.intellij.modules.platform</depends>
<depends>com.intellij.modules.lang</depends>

<change-notes><![CDATA[
Expand Down

0 comments on commit ac8b0a5

Please sign in to comment.