From 1af6f93441f23676a0dab5baed9aa1201a2e7e65 Mon Sep 17 00:00:00 2001 From: Darkyenus Date: Wed, 6 Mar 2019 19:25:48 +0100 Subject: [PATCH] Improve processDeclarations() implementation, fixing #142 Took 32 minutes --- .../declarations/GLSLDeclarationImpl.java | 7 +++++-- .../GLSLFunctionDefinitionImpl.java | 21 +++++++++++++++++++ .../declarations/GLSLStructDefinition.java | 13 ++++++++++-- .../statements/GLSLCompoundStatement.java | 17 +++++++++------ .../elements/statements/GLSLStatement.java | 2 +- 5 files changed, 49 insertions(+), 11 deletions(-) diff --git a/src/glslplugin/lang/elements/declarations/GLSLDeclarationImpl.java b/src/glslplugin/lang/elements/declarations/GLSLDeclarationImpl.java index e30e2250..e026b183 100755 --- a/src/glslplugin/lang/elements/declarations/GLSLDeclarationImpl.java +++ b/src/glslplugin/lang/elements/declarations/GLSLDeclarationImpl.java @@ -114,10 +114,13 @@ protected String getDeclaratorsString() { @Override public boolean processDeclarations(@NotNull PsiScopeProcessor processor, @NotNull ResolveState state, @Nullable PsiElement lastParent, @NotNull PsiElement place) { for (GLSLDeclarator declarator : getDeclarators()) { - if (!processor.execute(declarator, state)) return false; + if (declarator == lastParent) + continue; + if (!processor.execute(declarator, state)) + return false; } GLSLTypeSpecifier specifier = getTypeSpecifierNode(); - return specifier == null || specifier.processDeclarations(processor, state, lastParent, place); + return specifier == null || lastParent == specifier || specifier.processDeclarations(processor, state, lastParent, place); } } diff --git a/src/glslplugin/lang/elements/declarations/GLSLFunctionDefinitionImpl.java b/src/glslplugin/lang/elements/declarations/GLSLFunctionDefinitionImpl.java index f72db82b..7cd67947 100755 --- a/src/glslplugin/lang/elements/declarations/GLSLFunctionDefinitionImpl.java +++ b/src/glslplugin/lang/elements/declarations/GLSLFunctionDefinitionImpl.java @@ -20,7 +20,11 @@ package glslplugin.lang.elements.declarations; import com.intellij.lang.ASTNode; +import com.intellij.psi.PsiElement; +import com.intellij.psi.ResolveState; +import com.intellij.psi.scope.PsiScopeProcessor; import glslplugin.lang.elements.statements.GLSLCompoundStatement; +import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; /** @@ -40,6 +44,23 @@ public GLSLCompoundStatement getBody() { return findChildByClass(GLSLCompoundStatement.class); } + @Override + public boolean processDeclarations(@NotNull PsiScopeProcessor processor, @NotNull ResolveState state, @Nullable PsiElement lastParent, @NotNull PsiElement place) { + if (lastParent == null) { + // Do not show declarations of parameters to outside scopes + return true; + } + + for (GLSLParameterDeclaration parameter : getParameters()) { + if (parameter == lastParent) // TODO(jp): sloppy, parameter is probably not direct child, so this will fail + continue; + + if (!parameter.processDeclarations(processor, state, lastParent, place)) return false; + } + + return true; + } + @Override public String toString() { return "Function Definition: " + getSignature(); diff --git a/src/glslplugin/lang/elements/declarations/GLSLStructDefinition.java b/src/glslplugin/lang/elements/declarations/GLSLStructDefinition.java index 993fbc25..69a67a3e 100755 --- a/src/glslplugin/lang/elements/declarations/GLSLStructDefinition.java +++ b/src/glslplugin/lang/elements/declarations/GLSLStructDefinition.java @@ -114,10 +114,19 @@ public GLSLDeclarator getDeclarator(@NotNull String name) { @Override public boolean processDeclarations(@NotNull PsiScopeProcessor processor, @NotNull ResolveState state, @Nullable PsiElement lastParent, @NotNull PsiElement place) { - if (!processor.execute(this, state)) return false; + if (!processor.execute(this, state)) + return false; + + if (lastParent == null) { + // Do not show declarations of parameters to outside scopes + return true; + } for (GLSLDeclarator declarator : getDeclarators()) { - if (!processor.execute(declarator, state)) return false; + if (declarator == lastParent) + continue; + if (!processor.execute(declarator, state)) + return false; } return true; } diff --git a/src/glslplugin/lang/elements/statements/GLSLCompoundStatement.java b/src/glslplugin/lang/elements/statements/GLSLCompoundStatement.java index 739655fc..09554d61 100755 --- a/src/glslplugin/lang/elements/statements/GLSLCompoundStatement.java +++ b/src/glslplugin/lang/elements/statements/GLSLCompoundStatement.java @@ -28,7 +28,8 @@ import org.jetbrains.annotations.Nullable; /** - * GLSLCompoundStatement is ... + * A block of statements, surrounded by {@code { and } }. + * Appears as a scope creator inside functions and as a function body itself. * * @author Yngve Devik Hammersland * Date: Jan 28, 2009 @@ -62,15 +63,19 @@ public TerminatorScope getTerminatorScope() { } @Override - public boolean processDeclarations(@NotNull PsiScopeProcessor processor, @NotNull ResolveState state, @Nullable PsiElement lastParent, @NotNull PsiElement place) { + public boolean processDeclarations(@NotNull PsiScopeProcessor processor, @NotNull ResolveState state, + @Nullable PsiElement lastParent, @NotNull PsiElement place) { if (lastParent == null) { + // Do not show declarations of nested variables to outside scopes return true; } - PsiElement child = lastParent.getPrevSibling(); - while (child != null) { - if (!child.processDeclarations(processor, state, lastParent, place)) return false; - child = child.getPrevSibling(); + + for (GLSLStatement statement : getStatements()) { + if (statement == null) + continue; + if (!statement.processDeclarations(processor, state, lastParent, place)) return false; } + return true; } } diff --git a/src/glslplugin/lang/elements/statements/GLSLStatement.java b/src/glslplugin/lang/elements/statements/GLSLStatement.java index ae5ec0e9..f43bf448 100755 --- a/src/glslplugin/lang/elements/statements/GLSLStatement.java +++ b/src/glslplugin/lang/elements/statements/GLSLStatement.java @@ -24,7 +24,7 @@ import org.jetbrains.annotations.NotNull; /** - * GLSLStatement is ... + * Base for all code statements, such as variable declarations, procedure calls, assignments, compound statements, etc. * * @author Yngve Devik Hammersland * Date: Jan 28, 2009