[groovy] store script annotated variables in stubs
authorDaniil Ovchinnikov <daniil.ovchinnikov@jetbrains.com>
Tue, 1 Nov 2016 14:40:24 +0000 (17:40 +0300)
committerDaniil Ovchinnikov <daniil.ovchinnikov@jetbrains.com>
Tue, 1 Nov 2016 16:35:04 +0000 (19:35 +0300)
14 files changed:
plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/lang/parser/GroovyElementTypes.java
plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/lang/psi/impl/statements/GrFieldImpl.java
plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/lang/psi/impl/statements/GrVariableBaseImpl.java
plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/lang/psi/impl/statements/GrVariableDeclarationImpl.java
plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/lang/psi/impl/statements/GrVariableImpl.java
plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/lang/psi/impl/statements/params/GrParameterImpl.java
plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/lang/psi/stubs/GrFieldStub.java
plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/lang/psi/stubs/GrParameterStub.java
plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/lang/psi/stubs/GrVariableDeclarationStub.java [new file with mode: 0644]
plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/lang/psi/stubs/GrVariableStub.java [new file with mode: 0644]
plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/lang/psi/stubs/GrVariableStubBase.java [new file with mode: 0644]
plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/lang/psi/stubs/elements/GrStubFileElementType.java
plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/lang/psi/stubs/elements/GrVariableDeclarationElementType.java [new file with mode: 0644]
plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/lang/psi/stubs/elements/GrVariableElementType.java [new file with mode: 0644]

index 23f5c0b5c611a592393e8b09e4043a8633ce9a09..4c03f969df24b7bef86bd0358f7190f52867489d 100644 (file)
@@ -17,7 +17,6 @@
 package org.jetbrains.plugins.groovy.lang.parser;
 
 import com.intellij.lang.ASTNode;
-import com.intellij.psi.impl.source.tree.SharedImplUtil;
 import com.intellij.psi.stubs.*;
 import com.intellij.util.io.StringRef;
 import org.jetbrains.annotations.NotNull;
@@ -37,7 +36,6 @@ import org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMe
 import org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeParameter;
 import org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeParameterList;
 import org.jetbrains.plugins.groovy.lang.psi.impl.auxiliary.GrThrowsClauseImpl;
-import org.jetbrains.plugins.groovy.lang.psi.impl.statements.GrVariableDeclarationImpl;
 import org.jetbrains.plugins.groovy.lang.psi.impl.statements.blocks.GrBlockImpl;
 import org.jetbrains.plugins.groovy.lang.psi.impl.statements.blocks.GrClosableBlockImpl;
 import org.jetbrains.plugins.groovy.lang.psi.impl.statements.blocks.GrOpenBlockImpl;
@@ -415,22 +413,11 @@ public interface GroovyElementTypes {
       throw new UnsupportedOperationException("Not implemented");
     }
   };
-  EmptyStubElementType<GrVariableDeclaration> VARIABLE_DEFINITION =
-    new EmptyStubElementType<GrVariableDeclaration>("variable definitions", GroovyLanguage.INSTANCE) {
-      @Override
-      public GrVariableDeclaration createPsi(@NotNull EmptyStub stub) {
-        return new GrVariableDeclarationImpl(stub);
-      }
-
-      @Override
-      public boolean shouldCreateStub(ASTNode node) {
-        return SharedImplUtil.getParent(node) instanceof GrTypeDefinitionBody;
-      }
-    };
+  GrVariableDeclarationElementType VARIABLE_DEFINITION = new GrVariableDeclarationElementType();
   GroovyElementType TUPLE_DECLARATION = new GroovyElementType("tuple declaration");
   GroovyElementType TUPLE_EXPRESSION = new GroovyElementType("tuple expression");
 
-  GroovyElementType VARIABLE = new GroovyElementType("assigned variable");
+  GrVariableElementType VARIABLE = new GrVariableElementType();
 
   //modifiers
   GrStubElementType<GrModifierListStub, GrModifierList> MODIFIERS = new GrModifierListElementType("modifier list");
index 03567665557c14b902737e5a848a573c6fa631ce..2ec053b33e00cac136bde102aaa8d923e3df8c28 100644 (file)
@@ -24,7 +24,6 @@ import com.intellij.psi.presentation.java.JavaPresentationUtil;
 import com.intellij.psi.search.SearchScope;
 import com.intellij.psi.stubs.IStubElementType;
 import com.intellij.ui.LayeredIcon;
-import com.intellij.util.Function;
 import com.intellij.util.IncorrectOperationException;
 import com.intellij.util.containers.ContainerUtil;
 import icons.JetgroovyIcons;
@@ -43,7 +42,6 @@ import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrNamedArgumentSearc
 import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression;
 import org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinitionBody;
 import org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrAccessorMethod;
-import org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement;
 import org.jetbrains.plugins.groovy.lang.psi.dataFlow.types.TypeInferenceHelper;
 import org.jetbrains.plugins.groovy.lang.psi.stubs.GrFieldStub;
 import org.jetbrains.plugins.groovy.lang.psi.typeEnhancers.GrVariableEnhancer;
@@ -77,21 +75,6 @@ public class GrFieldImpl extends GrVariableBaseImpl<GrFieldStub> implements GrFi
     visitor.visitField(this);
   }
 
-  @Override
-  public GrTypeElement getTypeElementGroovy() {
-    final GrFieldStub stub = getStub();
-    if (stub != null) {
-      final String typeText = stub.getTypeText();
-      if (typeText == null) {
-        return null;
-      }
-
-      return GroovyPsiElementFactory.getInstance(getProject()).createTypeElement(typeText, this);
-    }
-
-    return super.getTypeElementGroovy();
-  }
-
   public String toString() {
     return "Field";
   }
@@ -203,16 +186,6 @@ public class GrFieldImpl extends GrVariableBaseImpl<GrFieldStub> implements GrFi
     return PsiImplUtil.getMemberUseScope(this);
   }
 
-  @NotNull
-  @Override
-  public String getName() {
-    final GrFieldStub stub = getStub();
-    if (stub != null) {
-      return stub.getName();
-    }
-    return super.getName();
-  }
-
   @Override
   public ItemPresentation getPresentation() {
     return JavaPresentationUtil.getFieldPresentation(this);
index 3d538f0e243a56fbb447c60b3c2224652b2d6454..3d6809f5fb465e0862634a518410abd92e125e40 100644 (file)
@@ -26,7 +26,6 @@ import com.intellij.psi.impl.source.tree.LeafPsiElement;
 import com.intellij.psi.search.LocalSearchScope;
 import com.intellij.psi.search.SearchScope;
 import com.intellij.psi.stubs.IStubElementType;
-import com.intellij.psi.stubs.StubElement;
 import com.intellij.psi.util.PsiTreeUtil;
 import com.intellij.util.ArrayUtil;
 import com.intellij.util.IncorrectOperationException;
@@ -49,6 +48,7 @@ import org.jetbrains.plugins.groovy.lang.psi.impl.GrStubElementBase;
 import org.jetbrains.plugins.groovy.lang.psi.impl.GroovyPsiElementImpl;
 import org.jetbrains.plugins.groovy.lang.psi.impl.PsiImplUtil;
 import org.jetbrains.plugins.groovy.lang.psi.impl.statements.expressions.TypesUtil;
+import org.jetbrains.plugins.groovy.lang.psi.stubs.GrVariableStubBase;
 import org.jetbrains.plugins.groovy.lang.psi.util.PsiUtil;
 
 import javax.swing.*;
@@ -56,12 +56,12 @@ import javax.swing.*;
 /**
  * @author ilyas
  */
-public abstract class GrVariableBaseImpl<T extends StubElement> extends GrStubElementBase<T> implements GrVariable {
+public abstract class GrVariableBaseImpl<T extends GrVariableStubBase> extends GrStubElementBase<T> implements GrVariable, StubBasedPsiElement<T> {
   public static final Logger LOG = Logger.getInstance("org.jetbrains.plugins.groovy.lang.psi.impl.statements.GrVariableImpl");
 
   private static final RecursionGuard ourGuard = RecursionManager.createGuard("grVariableInitializer");
 
-  public GrVariableBaseImpl(ASTNode node) {
+  protected GrVariableBaseImpl(ASTNode node) {
     super(node);
   }
 
@@ -131,11 +131,17 @@ public abstract class GrVariableBaseImpl<T extends StubElement> extends GrStubEl
   @Override
   @Nullable
   public GrTypeElement getTypeElementGroovy() {
+    T stub = getStub();
+    if (stub != null) {
+      return stub.getTypeElement();
+    }
+
     PsiElement parent = getParent();
     if (parent instanceof GrVariableDeclaration) {
       return ((GrVariableDeclaration)parent).getTypeElementGroovyForVariable(this);
     }
-    return null;
+
+    return findChildByClass(GrTypeElement.class);
   }
 
   @Override
@@ -266,6 +272,13 @@ public abstract class GrVariableBaseImpl<T extends StubElement> extends GrStubEl
   @Override
   @NotNull
   public String getName() {
+    T stub = getStub();
+    if (stub != null) {
+      String name = stub.getName();
+      if (name != null) {
+        return name;
+      }
+    }
     return PsiImplUtil.getName(this);
   }
 
@@ -291,11 +304,7 @@ public abstract class GrVariableBaseImpl<T extends StubElement> extends GrStubEl
 
   @Nullable
   private GrVariableDeclaration getDeclaration() {
-    PsiElement parent = getParent();
-    if (parent instanceof GrVariableDeclaration) {
-      return (GrVariableDeclaration)parent;
-    }
-    return null;
+    return getStubOrPsiParentOfType(GrVariableDeclaration.class);
   }
 
   @Override
index 7d000b6b3227277bb93d0a130a45bc3fc0537b81..3a184aed921468027af2723a3fbf631bcd142c9c 100644 (file)
@@ -22,7 +22,6 @@ import com.intellij.psi.*;
 import com.intellij.psi.codeStyle.JavaCodeStyleManager;
 import com.intellij.psi.scope.ElementClassHint;
 import com.intellij.psi.scope.PsiScopeProcessor;
-import com.intellij.psi.stubs.EmptyStub;
 import com.intellij.util.IncorrectOperationException;
 import org.jetbrains.annotations.NotNull;
 import org.jetbrains.annotations.Nullable;
@@ -46,6 +45,7 @@ import org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement;
 import org.jetbrains.plugins.groovy.lang.psi.impl.GrStubElementBase;
 import org.jetbrains.plugins.groovy.lang.psi.impl.GroovyPsiElementImpl;
 import org.jetbrains.plugins.groovy.lang.psi.impl.statements.expressions.TypesUtil;
+import org.jetbrains.plugins.groovy.lang.psi.stubs.GrVariableDeclarationStub;
 import org.jetbrains.plugins.groovy.lang.psi.util.PsiUtil;
 import org.jetbrains.plugins.groovy.lang.resolve.ResolveUtil;
 
@@ -55,14 +55,16 @@ import java.util.List;
 /**
  * @author: Dmitry.Krasilschikov
  */
-public class GrVariableDeclarationImpl extends GrStubElementBase<EmptyStub> implements GrVariableDeclaration, StubBasedPsiElement<EmptyStub> {
+public class GrVariableDeclarationImpl extends GrStubElementBase<GrVariableDeclarationStub>
+  implements GrVariableDeclaration, StubBasedPsiElement<GrVariableDeclarationStub> {
+
   private static final Logger LOG = Logger.getInstance(GrVariableDeclarationImpl.class);
 
   public GrVariableDeclarationImpl(@NotNull ASTNode node) {
     super(node);
   }
 
-  public GrVariableDeclarationImpl(EmptyStub stub) {
+  public GrVariableDeclarationImpl(@NotNull GrVariableDeclarationStub stub) {
     super(stub, GroovyElementTypes.VARIABLE_DEFINITION);
   }
 
@@ -159,6 +161,10 @@ public class GrVariableDeclarationImpl extends GrStubElementBase<EmptyStub> impl
   @Override
   @Nullable
   public GrTypeElement getTypeElementGroovy() {
+    GrVariableDeclarationStub stub = getStub();
+    if (stub != null) {
+      return stub.getTypeElement();
+    }
     if (isTuple()) return null;
     return findChildByClass(GrTypeElement.class);
   }
index 55fa39bb4d2d5cb9fef9ac2493357b6715245f1e..2b8d4f89e39d43331a135d7400722d0b82819ce8 100644 (file)
@@ -19,23 +19,28 @@ package org.jetbrains.plugins.groovy.lang.psi.impl.statements;
 import com.intellij.lang.ASTNode;
 import com.intellij.psi.PsiElement;
 import com.intellij.psi.search.SearchScope;
-import com.intellij.psi.stubs.StubElement;
 import org.jetbrains.annotations.NotNull;
+import org.jetbrains.plugins.groovy.lang.parser.GroovyElementTypes;
 import org.jetbrains.plugins.groovy.lang.psi.GroovyElementVisitor;
 import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable;
 import org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GrScriptField;
+import org.jetbrains.plugins.groovy.lang.psi.stubs.GrVariableStub;
 import org.jetbrains.plugins.groovy.lang.resolve.ResolveUtil;
 
 /**
  * @author Dmitry.Krasilschikov
  * @date 11.04.2007
  */
-public class GrVariableImpl extends GrVariableBaseImpl<StubElement> implements GrVariable {
+public class GrVariableImpl extends GrVariableBaseImpl<GrVariableStub> implements GrVariable {
 
   public GrVariableImpl(@NotNull ASTNode node) {
     super(node);
   }
 
+  public GrVariableImpl(@NotNull GrVariableStub stub) {
+    super(stub, GroovyElementTypes.VARIABLE);
+  }
+
   @Override
   public void accept(GroovyElementVisitor visitor) {
     visitor.visitVariable(this);
index a7eb095eecae551b6c4ff6e01f5ffa17f4c419e6..59d0d2fc11c8e79176c25071133bf4d5d0ddfd5b 100644 (file)
@@ -52,7 +52,7 @@ import org.jetbrains.plugins.groovy.lang.psi.typeEnhancers.GrVariableEnhancer;
 /**
  * @author: Dmitry.Krasilschikov
  */
-public class GrParameterImpl extends GrVariableBaseImpl<GrParameterStub> implements GrParameter, StubBasedPsiElement<GrParameterStub> {
+public class GrParameterImpl extends GrVariableBaseImpl<GrParameterStub> implements GrParameter {
   public GrParameterImpl(@NotNull ASTNode node) {
     super(node);
   }
@@ -161,29 +161,13 @@ public class GrParameterImpl extends GrVariableBaseImpl<GrParameterStub> impleme
     JavaCodeStyleManager.getInstance(getProject()).shortenClassReferences(newTypeElement);
   }
 
-  @Override
-  @Nullable
-  public GrTypeElement getTypeElementGroovy() {
-    final GrParameterStub stub = getStub();
-    if (stub != null) {
-      final String typeText = stub.getTypeText();
-      if (typeText == null) {
-        return null;
-      }
-
-      return GroovyPsiElementFactory.getInstance(getProject()).createTypeElement(typeText, this);
-    }
-
-    return findChildByClass(GrTypeElement.class);
-  }
-
   @Override
   public boolean isOptional() {
     final GrParameterStub stub = getStub();
     if (stub != null) {
       return GrParameterStub.hasInitializer(stub.getFlags());
     }
-    
+
     return getInitializerGroovy() != null;
   }
 
@@ -215,20 +199,10 @@ public class GrParameterImpl extends GrVariableBaseImpl<GrParameterStub> impleme
     return new LocalSearchScope(scope);
   }
 
-  @NotNull
-  @Override
-  public String getName() {
-    final GrParameterStub stub = getStub();
-    if (stub != null) {
-      return stub.getName();
-    }
-    return super.getName();
-  }
-
   @Override
   @NotNull
   public GrModifierList getModifierList() {
-    return getStubOrPsiChild(GroovyElementTypes.MODIFIERS);
+    return getRequiredStubOrPsiChild(GroovyElementTypes.MODIFIERS);
   }
 
   @Override
index 27bc0007c369bca0e99f5b994c4ff466bdee31ac..7de270aa84b1a68df524f193ef4b6732f42896e1 100644 (file)
@@ -17,8 +17,6 @@ package org.jetbrains.plugins.groovy.lang.psi.stubs;
 
 import com.intellij.psi.impl.PsiImplUtil;
 import com.intellij.psi.stubs.IStubElementType;
-import com.intellij.psi.stubs.NamedStub;
-import com.intellij.psi.stubs.StubBase;
 import com.intellij.psi.stubs.StubElement;
 import com.intellij.util.io.StringRef;
 import org.jetbrains.annotations.NotNull;
@@ -29,39 +27,25 @@ import org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrEn
 /**
  * @author ilyas
  */
-public class GrFieldStub extends StubBase<GrField> implements NamedStub<GrField> {
+public class GrFieldStub extends GrVariableStubBase<GrField> {
+
   public static final byte IS_PROPERTY = 0x01;
   public static final byte IS_ENUM_CONSTANT = 0x02;
   public static final byte IS_DEPRECATED_BY_DOC_TAG = 0x04;
 
-  private final byte myFlags;
-  private final StringRef myName;
-  private final String[] myAnnotations;
   private final String[] myNamedParameters;
-  private final String myTypeText;
+  private final byte myFlags;
 
   public GrFieldStub(StubElement parent,
-                         StringRef name,
-                         final String[] annotations,
-                         String[] namedParameters,
-                         @NotNull IStubElementType elemType,
-                         byte flags, @Nullable String typeText) {
-    super(parent, elemType);
-    myName = name;
-    myAnnotations = annotations;
+                     @Nullable StringRef name,
+                     @NotNull final String[] annotations,
+                     @NotNull String[] namedParameters,
+                     @NotNull IStubElementType elemType,
+                     byte flags,
+                     @Nullable String typeText) {
+    super(parent, elemType, name, annotations, typeText);
     myNamedParameters = namedParameters;
     myFlags = flags;
-    myTypeText = typeText;
-  }
-
-  @Override
-  @NotNull
-  public String getName() {
-    return StringRef.toString(myName);
-  }
-
-  public String[] getAnnotations() {
-    return myAnnotations;
   }
 
   @NotNull
@@ -81,11 +65,6 @@ public class GrFieldStub extends StubBase<GrField> implements NamedStub<GrField>
     return myFlags;
   }
 
-  @Nullable
-  public String getTypeText() {
-    return myTypeText;
-  }
-
   public static byte buildFlags(GrField field) {
     byte f = 0;
     if (field instanceof GrEnumConstant) {
@@ -97,7 +76,7 @@ public class GrFieldStub extends StubBase<GrField> implements NamedStub<GrField>
     }
 
     if (PsiImplUtil.isDeprecatedByDocTag(field)) {
-      f|= IS_DEPRECATED_BY_DOC_TAG;
+      f |= IS_DEPRECATED_BY_DOC_TAG;
     }
     return f;
   }
index d12eb41f752c73679b936e89b55f5b04570c8fe8..c1c5878a0bb35c9e345505dc6ab73d6082c4b40f 100644 (file)
  */
 package org.jetbrains.plugins.groovy.lang.psi.stubs;
 
-import com.intellij.psi.stubs.NamedStub;
-import com.intellij.psi.stubs.StubBase;
 import com.intellij.psi.stubs.StubElement;
 import com.intellij.util.BitUtil;
 import com.intellij.util.io.StringRef;
 import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
 import org.jetbrains.plugins.groovy.lang.parser.GroovyElementTypes;
 import org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter;
 
 /**
  * @author peter
  */
-public class GrParameterStub extends StubBase<GrParameter> implements NamedStub<GrParameter> {
-  private final StringRef myName;
-  private final String[] myAnnotations;
-  private final String myTypeText;
+public class GrParameterStub extends GrVariableStubBase<GrParameter> {
+
   private final int myFlags;
 
   public GrParameterStub(StubElement parent,
-                         StringRef name,
-                         final String[] annotations,
-                         String typeText, 
+                         @NotNull StringRef name,
+                         @NotNull final String[] annotations,
+                         @Nullable String typeText,
                          int flags) {
-    super(parent, GroovyElementTypes.PARAMETER);
-    myName = name;
-    myAnnotations = annotations;
-    myTypeText = typeText;
+    super(parent, GroovyElementTypes.PARAMETER, name, annotations, typeText);
     myFlags = flags;
   }
 
-  @Override
-  @NotNull
-  public String getName() {
-    return StringRef.toString(myName);
-  }
-
-  public String[] getAnnotations() {
-    return myAnnotations;
-  }
-
-  public String getTypeText() {
-    return myTypeText;
-  }
-
   public int getFlags() {
     return myFlags;
   }
diff --git a/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/lang/psi/stubs/GrVariableDeclarationStub.java b/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/lang/psi/stubs/GrVariableDeclarationStub.java
new file mode 100644 (file)
index 0000000..1b7e8dd
--- /dev/null
@@ -0,0 +1,56 @@
+/*
+ * Copyright 2000-2016 JetBrains s.r.o.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.jetbrains.plugins.groovy.lang.psi.stubs;
+
+import com.intellij.psi.stubs.StubBase;
+import com.intellij.psi.stubs.StubElement;
+import com.intellij.reference.SoftReference;
+import org.jetbrains.annotations.Nullable;
+import org.jetbrains.plugins.groovy.lang.parser.GroovyElementTypes;
+import org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory;
+import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration;
+import org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement;
+
+public class GrVariableDeclarationStub extends StubBase<GrVariableDeclaration> {
+
+  private final @Nullable String myTypeString;
+
+  private SoftReference<GrTypeElement> myTypeElement;
+
+  public GrVariableDeclarationStub(StubElement parent, @Nullable String typeString) {
+    super(parent, GroovyElementTypes.VARIABLE_DEFINITION);
+    myTypeString = typeString;
+  }
+
+  @Nullable
+  public String getTypeString() {
+    return myTypeString;
+  }
+
+  @Nullable
+  public GrTypeElement getTypeElement() {
+    String typeString = getTypeString();
+    if (typeString == null) return null;
+
+    GrTypeElement typeElement = SoftReference.dereference(myTypeElement);
+    if (typeElement == null) {
+      typeElement = GroovyPsiElementFactory.getInstance(getProject()).createTypeElement(typeString, getPsi());
+      myTypeElement = new SoftReference<>(typeElement);
+    }
+
+    return typeElement;
+  }
+}
diff --git a/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/lang/psi/stubs/GrVariableStub.java b/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/lang/psi/stubs/GrVariableStub.java
new file mode 100644 (file)
index 0000000..72b69ab
--- /dev/null
@@ -0,0 +1,34 @@
+/*
+ * Copyright 2000-2016 JetBrains s.r.o.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.jetbrains.plugins.groovy.lang.psi.stubs;
+
+import com.intellij.psi.stubs.IStubElementType;
+import com.intellij.psi.stubs.StubElement;
+import com.intellij.util.io.StringRef;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
+import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable;
+
+public class GrVariableStub extends GrVariableStubBase<GrVariable> {
+
+  public GrVariableStub(StubElement parent,
+                        IStubElementType elementType,
+                        @Nullable StringRef ref,
+                        @NotNull String[] annotations,
+                        @Nullable String text) {
+    super(parent, elementType, ref, annotations, text);
+  }
+}
diff --git a/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/lang/psi/stubs/GrVariableStubBase.java b/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/lang/psi/stubs/GrVariableStubBase.java
new file mode 100644 (file)
index 0000000..565d534
--- /dev/null
@@ -0,0 +1,78 @@
+/*
+ * Copyright 2000-2016 JetBrains s.r.o.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.jetbrains.plugins.groovy.lang.psi.stubs;
+
+import com.intellij.psi.stubs.IStubElementType;
+import com.intellij.psi.stubs.NamedStub;
+import com.intellij.psi.stubs.StubBase;
+import com.intellij.psi.stubs.StubElement;
+import com.intellij.reference.SoftReference;
+import com.intellij.util.io.StringRef;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
+import org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory;
+import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable;
+import org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement;
+
+public abstract class GrVariableStubBase<V extends GrVariable> extends StubBase<V> implements NamedStub<V> {
+
+  private final @Nullable StringRef myNameRef;
+  private final @NotNull String[] myAnnotations;
+  private final @Nullable String myTypeText;
+
+  private SoftReference<GrTypeElement> myTypeElement;
+
+  protected GrVariableStubBase(StubElement parent,
+                               IStubElementType elementType,
+                               @Nullable StringRef ref,
+                               @NotNull String[] annotations,
+                               @Nullable String text) {
+    super(parent, elementType);
+    myNameRef = ref;
+    myAnnotations = annotations;
+    myTypeText = text;
+  }
+
+  @Nullable
+  @Override
+  public String getName() {
+    return StringRef.toString(myNameRef);
+  }
+
+  @NotNull
+  public String[] getAnnotations() {
+    return myAnnotations;
+  }
+
+  @Nullable
+  public String getTypeText() {
+    return myTypeText;
+  }
+
+  @Nullable
+  public GrTypeElement getTypeElement() {
+    String typeText = getTypeText();
+    if (typeText == null) return null;
+
+    GrTypeElement typeElement = SoftReference.dereference(myTypeElement);
+    if (typeElement == null) {
+      typeElement = GroovyPsiElementFactory.getInstance(getProject()).createTypeElement(typeText, getPsi());
+      myTypeElement = new SoftReference<>(typeElement);
+    }
+
+    return typeElement;
+  }
+}
index fec05d8d0d31fb6d10bb53556e0119477084580b..7c9689f48740d49152d8cf2b1c66568019df8d3e 100644 (file)
@@ -41,7 +41,7 @@ import java.io.IOException;
  * @author ilyas
  */
 public class GrStubFileElementType extends IStubFileElementType<GrFileStub> {
-  public static final int STUB_VERSION = 33;
+  public static final int STUB_VERSION = 34;
 
   public GrStubFileElementType(Language language) {
     super(language);
diff --git a/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/lang/psi/stubs/elements/GrVariableDeclarationElementType.java b/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/lang/psi/stubs/elements/GrVariableDeclarationElementType.java
new file mode 100644 (file)
index 0000000..c1edcb9
--- /dev/null
@@ -0,0 +1,87 @@
+/*
+ * Copyright 2000-2016 JetBrains s.r.o.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.jetbrains.plugins.groovy.lang.psi.stubs.elements;
+
+import com.intellij.lang.ASTNode;
+import com.intellij.psi.PsiElement;
+import com.intellij.psi.PsiFile;
+import com.intellij.psi.impl.source.tree.SharedImplUtil;
+import com.intellij.psi.stubs.StubElement;
+import com.intellij.psi.stubs.StubInputStream;
+import com.intellij.psi.stubs.StubOutputStream;
+import com.intellij.psi.util.PsiTreeUtil;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.plugins.groovy.lang.psi.GroovyFile;
+import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration;
+import org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition;
+import org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinitionBody;
+import org.jetbrains.plugins.groovy.lang.psi.impl.statements.GrVariableDeclarationImpl;
+import org.jetbrains.plugins.groovy.lang.psi.stubs.GrStubUtils;
+import org.jetbrains.plugins.groovy.lang.psi.stubs.GrVariableDeclarationStub;
+
+import java.io.IOException;
+
+public class GrVariableDeclarationElementType extends GrStubElementType<GrVariableDeclarationStub, GrVariableDeclaration> {
+
+  public GrVariableDeclarationElementType() {
+    super("variable definitions");
+  }
+
+  @Override
+  public void serialize(@NotNull GrVariableDeclarationStub stub, @NotNull StubOutputStream dataStream) throws IOException {
+    GrStubUtils.writeNullableString(dataStream, stub.getTypeString());
+  }
+
+  @NotNull
+  @Override
+  public GrVariableDeclarationStub deserialize(@NotNull StubInputStream dataStream, StubElement parentStub) throws IOException {
+    return new GrVariableDeclarationStub(
+      parentStub,
+      GrStubUtils.readNullableString(dataStream)
+    );
+  }
+
+  @Override
+  public GrVariableDeclaration createPsi(@NotNull GrVariableDeclarationStub stub) {
+    return new GrVariableDeclarationImpl(stub);
+  }
+
+  @NotNull
+  @Override
+  public GrVariableDeclarationStub createStub(@NotNull GrVariableDeclaration psi, StubElement parentStub) {
+    return new GrVariableDeclarationStub(parentStub, GrStubUtils.getTypeText(psi.getTypeElementGroovy()));
+  }
+
+  @Override
+  public boolean shouldCreateStub(ASTNode node) {
+    PsiElement parent = SharedImplUtil.getParent(node);
+    if (parent instanceof GrTypeDefinitionBody) {
+      // store fields
+      return true;
+    }
+    if (PsiTreeUtil.getParentOfType(parent, GrTypeDefinition.class) != null) {
+      // do not store variable declarations within classes, as they are not scripts
+      return false;
+    }
+    PsiElement psi = node.getPsi();
+    if (!(psi instanceof GrVariableDeclaration) || ((GrVariableDeclaration)psi).getModifierList().getRawAnnotations().length == 0) {
+      // store only annotated declarations
+      return false;
+    }
+    PsiFile file = psi.getContainingFile();
+    return file instanceof GroovyFile && ((GroovyFile)file).isScript();
+  }
+}
diff --git a/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/lang/psi/stubs/elements/GrVariableElementType.java b/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/lang/psi/stubs/elements/GrVariableElementType.java
new file mode 100644 (file)
index 0000000..6c7948f
--- /dev/null
@@ -0,0 +1,78 @@
+/*
+ * Copyright 2000-2016 JetBrains s.r.o.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.jetbrains.plugins.groovy.lang.psi.stubs.elements;
+
+import com.intellij.lang.ASTNode;
+import com.intellij.psi.stubs.StubElement;
+import com.intellij.psi.stubs.StubInputStream;
+import com.intellij.psi.stubs.StubOutputStream;
+import com.intellij.util.io.StringRef;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.plugins.groovy.lang.parser.GroovyElementTypes;
+import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable;
+import org.jetbrains.plugins.groovy.lang.psi.impl.statements.GrVariableImpl;
+import org.jetbrains.plugins.groovy.lang.psi.stubs.GrStubUtils;
+import org.jetbrains.plugins.groovy.lang.psi.stubs.GrVariableStub;
+
+import java.io.IOException;
+
+public final class GrVariableElementType extends GrStubElementType<GrVariableStub, GrVariable> {
+
+  public GrVariableElementType() {
+    super("assigned variable");
+  }
+
+  @Override
+  public void serialize(@NotNull GrVariableStub stub, @NotNull StubOutputStream dataStream) throws IOException {
+    dataStream.writeName(stub.getName());
+    GrStubUtils.writeStringArray(dataStream, stub.getAnnotations());
+    GrStubUtils.writeNullableString(dataStream, stub.getTypeText());
+  }
+
+  @Override
+  public GrVariable createPsi(@NotNull GrVariableStub stub) {
+    return new GrVariableImpl(stub);
+  }
+
+  @NotNull
+  @Override
+  public GrVariableStub createStub(@NotNull GrVariable psi, StubElement parentStub) {
+    return new GrVariableStub(
+      parentStub,
+      this,
+      StringRef.fromNullableString(psi.getName()),
+      GrStubUtils.getAnnotationNames(psi),
+      GrStubUtils.getTypeText(psi.getTypeElementGroovy())
+    );
+  }
+
+  @NotNull
+  @Override
+  public GrVariableStub deserialize(@NotNull StubInputStream dataStream, StubElement parentStub) throws IOException {
+    final StringRef name = dataStream.readName();
+    final String[] annNames = GrStubUtils.readStringArray(dataStream);
+    final String typeText = GrStubUtils.readNullableString(dataStream);
+    return new GrVariableStub(parentStub, this, name, annNames, typeText);
+  }
+
+  @Override
+  public boolean shouldCreateStub(ASTNode node) {
+    ASTNode parent = node.getTreeParent();
+    return parent != null &&
+           parent.getElementType() == GroovyElementTypes.VARIABLE_DEFINITION &&
+           GroovyElementTypes.VARIABLE_DEFINITION.shouldCreateStub(parent);
+  }
+}