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;
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;
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");
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;
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;
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";
}
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);
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;
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.*;
/**
* @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);
}
@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
@Override
@NotNull
public String getName() {
+ T stub = getStub();
+ if (stub != null) {
+ String name = stub.getName();
+ if (name != null) {
+ return name;
+ }
+ }
return PsiImplUtil.getName(this);
}
@Nullable
private GrVariableDeclaration getDeclaration() {
- PsiElement parent = getParent();
- if (parent instanceof GrVariableDeclaration) {
- return (GrVariableDeclaration)parent;
- }
- return null;
+ return getStubOrPsiParentOfType(GrVariableDeclaration.class);
}
@Override
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;
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;
/**
* @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);
}
@Override
@Nullable
public GrTypeElement getTypeElementGroovy() {
+ GrVariableDeclarationStub stub = getStub();
+ if (stub != null) {
+ return stub.getTypeElement();
+ }
if (isTuple()) return null;
return findChildByClass(GrTypeElement.class);
}
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);
/**
* @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);
}
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;
}
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
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;
/**
* @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
return myFlags;
}
- @Nullable
- public String getTypeText() {
- return myTypeText;
- }
-
public static byte buildFlags(GrField field) {
byte f = 0;
if (field instanceof GrEnumConstant) {
}
if (PsiImplUtil.isDeprecatedByDocTag(field)) {
- f|= IS_DEPRECATED_BY_DOC_TAG;
+ f |= IS_DEPRECATED_BY_DOC_TAG;
}
return f;
}
*/
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;
}
--- /dev/null
+/*
+ * 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;
+ }
+}
--- /dev/null
+/*
+ * 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);
+ }
+}
--- /dev/null
+/*
+ * 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;
+ }
+}
* @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);
--- /dev/null
+/*
+ * 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();
+ }
+}
--- /dev/null
+/*
+ * 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);
+ }
+}