From be0ed2af12854519c8f58220dbc5a88715fa6cb2 Mon Sep 17 00:00:00 2001 From: Maxim Shafirov Date: Mon, 10 Jan 2011 17:11:41 +0300 Subject: [PATCH] property/accessors --- idea/src/org/jetbrains/jet/JetNodeTypes.java | 2 +- .../jetbrains/jet/lang/psi/JetProperty.java | 43 ++++++++++++++++++- .../jet/lang/psi/JetPropertyAccessor.java | 39 +++++++++++++++++ .../jetbrains/jet/lang/psi/JetVisitor.java | 4 ++ 4 files changed, 86 insertions(+), 2 deletions(-) create mode 100644 idea/src/org/jetbrains/jet/lang/psi/JetPropertyAccessor.java diff --git a/idea/src/org/jetbrains/jet/JetNodeTypes.java b/idea/src/org/jetbrains/jet/JetNodeTypes.java index cba0d7239fd..7d918688f59 100644 --- a/idea/src/org/jetbrains/jet/JetNodeTypes.java +++ b/idea/src/org/jetbrains/jet/JetNodeTypes.java @@ -54,7 +54,7 @@ public interface JetNodeTypes { JetNodeType DECOMPOSER_PROPERTY_LIST = new JetNodeType("DECOMPOSER_PROPERTY_LIST", JetDecomposerPropertyList.class); // TODO: review JetNodeType RECEIVER_TYPE_ATTRIBUTES = new JetNodeType("RECEIVER_TYPE_ATTRIBUTES"); - JetNodeType PROPERTY_ACCESSOR = new JetNodeType("PROPERTY_ACCESSOR"); + JetNodeType PROPERTY_ACCESSOR = new JetNodeType("PROPERTY_ACCESSOR", JetPropertyAccessor.class); JetNodeType INITIALIZER_LIST = new JetNodeType("INITIALIZER_LIST", JetInitializerList.class); JetNodeType THIS_CALL = new JetNodeType("THIS_CALL", JetDelegatorToThisCall.class); JetNodeType TYPE_CONSTRAINT_LIST = new JetNodeType("TYPE_CONSTRAINT_LIST"); diff --git a/idea/src/org/jetbrains/jet/lang/psi/JetProperty.java b/idea/src/org/jetbrains/jet/lang/psi/JetProperty.java index a1c8259a886..04c41ff9416 100644 --- a/idea/src/org/jetbrains/jet/lang/psi/JetProperty.java +++ b/idea/src/org/jetbrains/jet/lang/psi/JetProperty.java @@ -7,6 +7,8 @@ import org.jetbrains.annotations.Nullable; import org.jetbrains.jet.JetNodeTypes; import org.jetbrains.jet.lexer.JetTokens; +import java.util.List; + /** * @author max */ @@ -21,7 +23,24 @@ public class JetProperty extends JetNamedDeclaration { } @Nullable - public JetTypeReference getTypeReference() { + public JetTypeReference getReceiverTypeRef() { + ASTNode node = getNode().getFirstChildNode(); + while (node != null) { + IElementType tt = node.getElementType(); + if (tt == JetTokens.COLON) break; + + if (tt == JetNodeTypes.TYPE_REFERENCE) { + return (JetTypeReference) node.getPsi(); + } + node = node.getTreeNext(); + } + + return null; + } + + + @Nullable + public JetTypeReference getPropertyTypeRef() { ASTNode node = getNode().getFirstChildNode(); boolean passedColon = false; while (node != null) { @@ -37,4 +56,26 @@ public class JetProperty extends JetNamedDeclaration { return null; } + + @NotNull + public List getAccessors() { + return findChildrenByType(JetNodeTypes.PROPERTY_ACCESSOR); + } + + @Nullable + public JetPropertyAccessor getGetter() { + for (JetPropertyAccessor accessor : getAccessors()) { + if (accessor.isGetter()) return accessor; + } + + return null; + } + + public JetPropertyAccessor getSetter() { + for (JetPropertyAccessor accessor : getAccessors()) { + if (accessor.isSetter()) return accessor; + } + + return null; + } } diff --git a/idea/src/org/jetbrains/jet/lang/psi/JetPropertyAccessor.java b/idea/src/org/jetbrains/jet/lang/psi/JetPropertyAccessor.java new file mode 100644 index 00000000000..14c8a029e4c --- /dev/null +++ b/idea/src/org/jetbrains/jet/lang/psi/JetPropertyAccessor.java @@ -0,0 +1,39 @@ +package org.jetbrains.jet.lang.psi; + +import com.intellij.lang.ASTNode; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.jetbrains.jet.JetNodeTypes; +import org.jetbrains.jet.lexer.JetTokens; + +/** + * @author max + */ +public class JetPropertyAccessor extends JetDeclaration { + public JetPropertyAccessor(@NotNull ASTNode node) { + super(node); + } + + @Override + public void accept(JetVisitor visitor) { + visitor.visitPropertyAccessor(this); + } + + public boolean isSetter() { + return findChildByType(JetTokens.SET_KEYWORD) != null; + } + + public boolean isGetter() { + return findChildByType(JetTokens.GET_KEYWORD) != null; + } + + @Nullable + public JetParameter getParameter() { + return (JetParameter) findChildByType(JetNodeTypes.VALUE_PARAMETER); + } + + @Nullable + public JetExpression getBody() { + return findChildByClass(JetExpression.class); + } +} diff --git a/idea/src/org/jetbrains/jet/lang/psi/JetVisitor.java b/idea/src/org/jetbrains/jet/lang/psi/JetVisitor.java index 797e37cd23b..628c8d0683d 100644 --- a/idea/src/org/jetbrains/jet/lang/psi/JetVisitor.java +++ b/idea/src/org/jetbrains/jet/lang/psi/JetVisitor.java @@ -281,4 +281,8 @@ public class JetVisitor extends PsiElementVisitor { public void visitAnonymousInitializer(JetClassInitializer initializer) { visitDeclaration(initializer); } + + public void visitPropertyAccessor(JetPropertyAccessor accessor) { + visitDeclaration(accessor); + } }