Skip to content

Commit

Permalink
Add declaration processing
Browse files Browse the repository at this point in the history
Now completion seemingly mostly works. Fixes #12.
  • Loading branch information
AbigailBuccaneer committed Jul 9, 2015
1 parent b877d4d commit c3f439e
Show file tree
Hide file tree
Showing 10 changed files with 104 additions and 38 deletions.
16 changes: 16 additions & 0 deletions src/glslplugin/lang/elements/declarations/GLSLDeclarationImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
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.GLSLElementImpl;
import glslplugin.lang.elements.GLSLIdentifier;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -108,4 +111,17 @@ protected String getDeclaratorsString() {
}
return b.toString();
}

@Override
public boolean processDeclarations(@NotNull PsiScopeProcessor processor, @NotNull ResolveState state, PsiElement lastParent, @NotNull PsiElement place) {
for (GLSLDeclarator declarator : getDeclarators()) {
if (!processor.execute(declarator, state)) return false;
}

GLSLTypeSpecifier specifier = getTypeSpecifierNode();
if (specifier != null && !specifier.processDeclarations(processor, state, lastParent, place)) return false;


return true;
}
}
65 changes: 37 additions & 28 deletions src/glslplugin/lang/elements/declarations/GLSLTypeDefinition.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@

import com.intellij.lang.ASTNode;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiNameIdentifierOwner;
import com.intellij.psi.ResolveState;
import com.intellij.psi.scope.PsiScopeProcessor;
import com.intellij.util.IncorrectOperationException;
import glslplugin.lang.elements.GLSLElementImpl;
import glslplugin.lang.elements.GLSLIdentifier;
import glslplugin.lang.elements.GLSLTypedElement;
Expand All @@ -39,7 +43,7 @@
* Date: Jan 27, 2009
* Time: 10:31:13 AM
*/
public class GLSLTypeDefinition extends GLSLElementImpl implements GLSLTypedElement {
public class GLSLTypeDefinition extends GLSLElementImpl implements GLSLTypedElement, PsiNameIdentifierOwner {
// Cache this one to enable equals comparison by ==
// this is required to be able to compare types of variables of anonymous types.
// struct {int x;} x, y; <- how to compare types of x and y?
Expand All @@ -49,32 +53,6 @@ public GLSLTypeDefinition(@NotNull ASTNode astNode) {
super(astNode);
}

@Nullable
private String getTypeNameInternal() {
final PsiElement[] children = getChildren();
if (children.length > 1) {
PsiElement id = children[0];
if (id instanceof GLSLIdentifier) {
return ((GLSLIdentifier) id).getName();
}
}
return null;
}

public boolean isNamed() {
return getTypeNameInternal() != null;
}

@NotNull
public String getTypeName() {
String name = getTypeNameInternal();
if (name != null) {
return name;
} else {
return "(anonymous structure)";
}
}

// TODO: Add getMemberDeclarations, findMember(String), etc...

@Nullable
Expand Down Expand Up @@ -103,7 +81,8 @@ public GLSLDeclarator[] getDeclarators() {

@Override
public String toString() {
return "Struct Type: " + getTypeName();
if (getName() == null) return "Anonymous struct";
return "Struct Type: '" + getName() + "'";
}

@NotNull
Expand All @@ -123,4 +102,34 @@ public GLSLDeclarator getDeclarator(@NotNull String name) {
}
return null;
}

@Override
public boolean processDeclarations(@NotNull PsiScopeProcessor processor, @NotNull ResolveState state, PsiElement lastParent, @NotNull PsiElement place) {
if (!processor.execute(this, state)) return false;

for (GLSLDeclarator declarator : getDeclarators()) {
if (!processor.execute(declarator, state)) return false;
}
return true;
}

@Nullable
@Override
public GLSLIdentifier getNameIdentifier() {
return findChildByClass(GLSLIdentifier.class);
}

@Override
public String getName() {
GLSLIdentifier identifier = getNameIdentifier();
if (identifier == null) return null;
return identifier.getName();
}

@Override
public PsiElement setName(@NotNull String name) throws IncorrectOperationException {
GLSLIdentifier identifier = getNameIdentifier();
if (identifier == null) return null;
return identifier.setName(name);
}
}
12 changes: 11 additions & 1 deletion src/glslplugin/lang/elements/declarations/GLSLTypeSpecifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
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.GLSLElementImpl;
import glslplugin.lang.elements.GLSLTypedElement;
import glslplugin.lang.elements.types.GLSLArrayType;
Expand Down Expand Up @@ -73,7 +76,14 @@ public String toString() {
}

@Nullable
public GLSLTypedElement getTypeDefinition() {
public GLSLTypeDefinition getTypeDefinition() {
return findChildByClass(GLSLTypeDefinition.class);
}

@Override
public boolean processDeclarations(@NotNull PsiScopeProcessor processor, @NotNull ResolveState state, PsiElement lastParent, @NotNull PsiElement place) {
GLSLTypeDefinition typeDefinition = getTypeDefinition();
if (typeDefinition != null && !typeDefinition.processDeclarations(processor, state, lastParent, place)) return false;
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ public GLSLIdentifier getMemberIdentifier() {
if (last instanceof GLSLIdentifier) {
return (GLSLIdentifier) last;
} else {
Logger.getLogger("GLSLFieldSelectionExpression").warning("Field selection operator missing identifier after '.'.");
return null;
}
}
Expand Down
9 changes: 3 additions & 6 deletions src/glslplugin/lang/elements/reference/GLSLTypeReference.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,9 @@ public GLSLTypeDefinition resolve() {
private GLSLTypeDefinition checkDeclarationForType(GLSLDeclaration declaration) {
final GLSLTypeSpecifier specifier = declaration.getTypeSpecifierNode();
if(specifier == null)return null;
GLSLTypedElement definition = specifier.getTypeDefinition();
if (definition instanceof GLSLTypeDefinition) {
GLSLTypeDefinition typedef = (GLSLTypeDefinition) definition;
if (typedef.isNamed() && typedef.getTypeName().equals(source.getTypename())) {
return typedef;
}
GLSLTypeDefinition definition = specifier.getTypeDefinition();
if (definition != null) {
if (source.getTypename().equals(definition.getName())) return definition;
}
return null;
}
Expand Down
13 changes: 13 additions & 0 deletions src/glslplugin/lang/elements/statements/GLSLCompoundStatement.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
package glslplugin.lang.elements.statements;

import com.intellij.lang.ASTNode;
import com.intellij.psi.PsiElement;
import com.intellij.psi.ResolveState;
import com.intellij.psi.scope.PsiScopeProcessor;
import com.intellij.psi.util.PsiTreeUtil;
import org.jetbrains.annotations.NotNull;

Expand Down Expand Up @@ -56,4 +59,14 @@ public TerminatorScope getTerminatorScope() {
}
return TerminatorScope.NONE;
}

@Override
public boolean processDeclarations(@NotNull PsiScopeProcessor processor, @NotNull ResolveState state, PsiElement lastParent, @NotNull PsiElement place) {
PsiElement child = lastParent.getPrevSibling();
while (child != null) {
if (!child.processDeclarations(processor, state, lastParent, place)) return false;
child = child.getPrevSibling();
}
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
package glslplugin.lang.elements.statements;

import com.intellij.psi.PsiElement;
import com.intellij.psi.ResolveState;
import com.intellij.psi.scope.PsiScopeProcessor;
import org.jetbrains.annotations.NotNull;
import com.intellij.lang.ASTNode;
import glslplugin.lang.elements.declarations.GLSLVariableDeclaration;
Expand Down Expand Up @@ -51,4 +53,11 @@ public GLSLVariableDeclaration getDeclaration() {
public String toString() {
return "Declaration Statement";
}

@Override
public boolean processDeclarations(@NotNull PsiScopeProcessor processor, @NotNull ResolveState state, PsiElement lastParent, @NotNull PsiElement place) {
GLSLVariableDeclaration declaration = getDeclaration();
if (declaration == null) return true;
return declaration.processDeclarations(processor, state, lastParent, place);
}
}
2 changes: 1 addition & 1 deletion src/glslplugin/lang/elements/types/GLSLStructType.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public GLSLStructType(GLSLTypeDefinition definition) {
this.definition = definition;
final GLSLDeclarator[] declarators = definition.getDeclarators();

typename = definition.getTypeName();
typename = definition.getName();

GLSLType[] memberTypes = new GLSLType[declarators.length];

Expand Down
13 changes: 13 additions & 0 deletions src/glslplugin/lang/parser/GLSLFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
import com.intellij.psi.FileViewProvider;
import com.intellij.extapi.psi.PsiFileBase;
import com.intellij.openapi.fileTypes.FileType;
import com.intellij.psi.PsiElement;
import com.intellij.psi.ResolveState;
import com.intellij.psi.scope.PsiScopeProcessor;
import glslplugin.GLSLSupportLoader;
import org.jetbrains.annotations.NotNull;

Expand All @@ -34,4 +37,14 @@ public GLSLFile(FileViewProvider fileViewProvider) {
public FileType getFileType() {
return GLSLSupportLoader.GLSL;
}

@Override
public boolean processDeclarations(@NotNull PsiScopeProcessor processor, @NotNull ResolveState state, PsiElement lastParent, @NotNull PsiElement place) {
PsiElement child = lastParent.getPrevSibling();
while (child != null) {
if (!child.processDeclarations(processor, state, lastParent, place)) return false;
child = child.getPrevSibling();
}
return true;
}
}
2 changes: 1 addition & 1 deletion src/glslplugin/structureview/GLSLStructTreeElement.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public GLSLStructTreeElement(GLSLTypeDefinition definition) {
}

protected GLSLPresentation createPresentation(GLSLTypeDefinition definition) {
return GLSLPresentation.createStructPresentation(definition.getTypeName());
return GLSLPresentation.createStructPresentation(definition.getName());
}

protected void createChildren(GLSLTypeDefinition definition) {
Expand Down

0 comments on commit c3f439e

Please sign in to comment.