IDEA-95631 GroovyFile#getPackageDefinition causes stub->ast switch

This commit is contained in:
Max Medvedev
2012-11-23 13:50:21 +04:00
parent fb22eb3557
commit c5058bec2e
8 changed files with 144 additions and 27 deletions
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2009 JetBrains s.r.o.
* Copyright 2000-2012 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.
@@ -150,7 +150,7 @@ public interface GroovyElementTypes extends GroovyTokenTypes, GroovyDocElementTy
GroovyElementType WRONGWAY = new GroovyElementType("Wrong way!");
GroovyElementType LITERAL = new GroovyElementType("Literal");
//Packaging
GroovyElementType PACKAGE_DEFINITION = new GroovyElementType("Package definition");
GrPackageDefinitionElementType PACKAGE_DEFINITION = new GrPackageDefinitionElementType("Package definition");
GrCodeBlockElementType CLOSABLE_BLOCK = new GrCodeBlockElementType("Closable block") {
@NotNull
@@ -85,18 +85,25 @@ public class GroovyFileImpl extends GroovyFileBaseImpl implements GroovyFile {
@NotNull
public String getPackageName() {
final StubElement stub = getStub();
if (stub instanceof GrFileStub) {
return ((GrFileStub)stub).getPackageName().toString();
}
GrPackageDefinition packageDef = getPackageDefinition();
if (packageDef != null) {
return packageDef.getPackageName();
final String name = packageDef.getPackageName();
if (name != null) {
return name;
}
}
return "";
}
public GrPackageDefinition getPackageDefinition() {
final StubElement<?> stub = getStub();
if (stub != null) {
for (StubElement element : stub.getChildrenStubs()) {
if (element instanceof GrPackageDefinition) return (GrPackageDefinition)element;
}
return null;
}
ASTNode node = calcTreeElement().findChildByType(GroovyElementTypes.PACKAGE_DEFINITION);
return node != null ? (GrPackageDefinition)node.getPsi() : null;
}
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2009 JetBrains s.r.o.
* Copyright 2000-2012 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.
@@ -17,7 +17,9 @@
package org.jetbrains.plugins.groovy.lang.psi.impl.toplevel.packaging;
import com.intellij.lang.ASTNode;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiModifierList;
import com.intellij.psi.StubBasedPsiElement;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.plugins.groovy.lang.parser.GroovyElementTypes;
@@ -25,18 +27,28 @@ import org.jetbrains.plugins.groovy.lang.psi.GroovyElementVisitor;
import org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.GrModifierList;
import org.jetbrains.plugins.groovy.lang.psi.api.toplevel.packaging.GrPackageDefinition;
import org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement;
import org.jetbrains.plugins.groovy.lang.psi.impl.GroovyPsiElementImpl;
import org.jetbrains.plugins.groovy.lang.psi.impl.GrStubElementBase;
import org.jetbrains.plugins.groovy.lang.psi.stubs.GrPackageDefinitionStub;
import org.jetbrains.plugins.groovy.lang.psi.util.PsiUtil;
/**
* @author ilyas
*/
public class GrPackageDefinitionImpl extends GroovyPsiElementImpl implements GrPackageDefinition {
public class GrPackageDefinitionImpl extends GrStubElementBase<GrPackageDefinitionStub> implements GrPackageDefinition, StubBasedPsiElement<GrPackageDefinitionStub> {
public GrPackageDefinitionImpl(@NotNull ASTNode node) {
super(node);
}
@Override
public PsiElement getParent() {
return getParentByStub();
}
public GrPackageDefinitionImpl(@NotNull GrPackageDefinitionStub stub) {
super(stub, GroovyElementTypes.PACKAGE_DEFINITION);
}
public void accept(GroovyElementVisitor visitor) {
visitor.visitPackageDefinition(this);
}
@@ -46,6 +58,11 @@ public class GrPackageDefinitionImpl extends GroovyPsiElementImpl implements GrP
}
public String getPackageName() {
final GrPackageDefinitionStub stub = getStub();
if (stub != null) {
return stub.getPackageName();
}
GrCodeReferenceElement ref = getPackageReference();
if (ref == null) return "";
return PsiUtil.getQualifiedReferenceText(ref);
@@ -56,7 +73,7 @@ public class GrPackageDefinitionImpl extends GroovyPsiElementImpl implements GrP
}
public GrModifierList getAnnotationList() {
return (GrModifierList)findChildByType(GroovyElementTypes.MODIFIERS);
return getStubOrPsiChild(GroovyElementTypes.MODIFIERS);
}
@Override
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2009 JetBrains s.r.o.
* Copyright 2000-2012 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.
@@ -29,13 +29,11 @@ import org.jetbrains.plugins.groovy.lang.psi.api.toplevel.packaging.GrPackageDef
*/
public class GrFileStub extends PsiFileStubImpl<GroovyFile> {
private final String[] myAnnotations;
private final StringRef myPackageName;
private final StringRef myName;
private final boolean isScript;
public GrFileStub(GroovyFile file) {
super(file);
myPackageName = StringRef.fromString(file.getPackageName());
myName = StringRef.fromString(StringUtil.trimEnd(file.getName(), ".groovy"));
isScript = file.isScript();
final GrPackageDefinition definition = file.getPackageDefinition();
@@ -46,9 +44,8 @@ public class GrFileStub extends PsiFileStubImpl<GroovyFile> {
}
}
public GrFileStub(StringRef packName, StringRef name, boolean isScript, String[] annotations) {
public GrFileStub(StringRef name, boolean isScript, String[] annotations) {
super(null);
myPackageName = packName;
myName = name;
this.isScript = isScript;
myAnnotations = annotations;
@@ -58,10 +55,6 @@ public class GrFileStub extends PsiFileStubImpl<GroovyFile> {
return GroovyParserDefinition.GROOVY_FILE;
}
public StringRef getPackageName() {
return myPackageName;
}
public StringRef getName() {
return myName;
}
@@ -0,0 +1,36 @@
/*
* Copyright 2000-2012 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.StubBase;
import com.intellij.psi.stubs.StubElement;
import com.intellij.util.io.StringRef;
import org.jetbrains.plugins.groovy.lang.psi.api.toplevel.packaging.GrPackageDefinition;
public class GrPackageDefinitionStub extends StubBase<GrPackageDefinition> implements StubElement<GrPackageDefinition> {
private final StringRef myPackageName;
public GrPackageDefinitionStub(StubElement parent, IStubElementType elementType, StringRef packageName) {
super(parent, elementType);
this.myPackageName = packageName;
}
public String getPackageName() {
return StringRef.toString(myPackageName);
}
}
@@ -127,4 +127,14 @@ public class GrStubUtils {
}
return PsiNameHelper.getShortClassName(text.substring(0, i)) + text.substring(i);
}
@Nullable
public static String getPackageName(final GrFileStub stub) {
for (StubElement child : stub.getChildrenStubs()) {
if (child instanceof GrPackageDefinitionStub) {
return ((GrPackageDefinitionStub)child).getPackageName();
}
}
return null;
}
}
@@ -0,0 +1,55 @@
/*
* Copyright 2000-2012 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.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.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.plugins.groovy.lang.parser.GroovyElementTypes;
import org.jetbrains.plugins.groovy.lang.psi.api.toplevel.packaging.GrPackageDefinition;
import org.jetbrains.plugins.groovy.lang.psi.impl.toplevel.packaging.GrPackageDefinitionImpl;
import org.jetbrains.plugins.groovy.lang.psi.stubs.GrPackageDefinitionStub;
import java.io.IOException;
public class GrPackageDefinitionElementType extends GrStubElementType<GrPackageDefinitionStub, GrPackageDefinition> {
public GrPackageDefinitionElementType(@NonNls @NotNull String debugName) {
super(debugName);
}
@Override
public GrPackageDefinition createPsi(@NotNull GrPackageDefinitionStub stub) {
return new GrPackageDefinitionImpl(stub);
}
@Override
public GrPackageDefinitionStub createStub(@NotNull GrPackageDefinition psi, StubElement parentStub) {
return new GrPackageDefinitionStub(parentStub, GroovyElementTypes.PACKAGE_DEFINITION, StringRef.fromString(psi.getPackageName()));
}
@Override
public void serialize(GrPackageDefinitionStub stub, StubOutputStream dataStream) throws IOException {
dataStream.writeName(stub.getPackageName());
}
@Override
public GrPackageDefinitionStub deserialize(StubInputStream dataStream, StubElement parentStub) throws IOException {
return new GrPackageDefinitionStub(parentStub, GroovyElementTypes.PACKAGE_DEFINITION, dataStream.readName());
}
}
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2009 JetBrains s.r.o.
* Copyright 2000-2012 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.
@@ -16,6 +16,7 @@
package org.jetbrains.plugins.groovy.lang.psi.stubs.elements;
import com.intellij.lang.Language;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.psi.PsiFile;
import com.intellij.psi.StubBuilder;
import com.intellij.psi.stubs.*;
@@ -54,7 +55,7 @@ public class GrStubFileElementType extends IStubFileElementType<GrFileStub> {
@Override
public int getStubVersion() {
return super.getStubVersion() + 13;
return super.getStubVersion() + 14;
}
public String getExternalId() {
@@ -68,7 +69,6 @@ public class GrStubFileElementType extends IStubFileElementType<GrFileStub> {
@Override
public void serialize(final GrFileStub stub, final StubOutputStream dataStream) throws IOException {
dataStream.writeName(stub.getPackageName().toString());
dataStream.writeName(stub.getName().toString());
dataStream.writeBoolean(stub.isScript());
GrStubUtils.writeStringArray(dataStream, stub.getAnnotations());
@@ -76,18 +76,17 @@ public class GrStubFileElementType extends IStubFileElementType<GrFileStub> {
@Override
public GrFileStub deserialize(final StubInputStream dataStream, final StubElement parentStub) throws IOException {
StringRef packName = dataStream.readName();
StringRef name = dataStream.readName();
boolean isScript = dataStream.readBoolean();
return new GrFileStub(packName, name, isScript, GrStubUtils.readStringArray(dataStream));
return new GrFileStub(name, isScript, GrStubUtils.readStringArray(dataStream));
}
public void indexStub(GrFileStub stub, IndexSink sink) {
String name = stub.getName().toString();
if (stub.isScript() && name != null) {
sink.occurrence(GrScriptClassNameIndex.KEY, name);
final String pName = stub.getPackageName().toString();
final String fqn = pName == null || pName.length() == 0 ? name : pName + "." + name;
final String pName = GrStubUtils.getPackageName(stub);
final String fqn = StringUtil.isEmpty(pName) ? name : pName + "." + name;
sink.occurrence(GrFullScriptNameIndex.KEY, fqn.hashCode());
}