[groovy] get rid of GrTopLevelDefinition, organize GrTopStatement hierarchy

This commit is contained in:
Daniil Ovchinnikov
2016-12-14 22:56:48 +03:00
parent 40e7a22106
commit d59f4dd651
11 changed files with 21 additions and 58 deletions
@@ -23,7 +23,6 @@ import com.intellij.psi.PsiImportHolder;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrTopLevelDefinition;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod;
import org.jetbrains.plugins.groovy.lang.psi.api.toplevel.GrTopStatement;
@@ -53,9 +52,6 @@ public interface GroovyFileBase extends PsiFile, GrVariableDeclarationOwner, GrS
@NotNull
GrTypeDefinition[] getTypeDefinitions();
@NotNull
GrTopLevelDefinition[] getTopLevelDefinitions();
@NotNull
GrMethod[] getMethods();
@@ -28,7 +28,7 @@ import java.util.Map;
/**
* @author ven
*/
public interface GrField extends GrVariable, GrMember, PsiField, GrTopLevelDefinition, GrDocCommentOwner {
public interface GrField extends GrVariable, GrMember, PsiField, GrDocCommentOwner {
GrField[] EMPTY_ARRAY = new GrField[0];
boolean isProperty();
@@ -1,27 +0,0 @@
/*
* Copyright 2000-2014 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.api.statements;
import org.jetbrains.plugins.groovy.lang.psi.GrNamedElement;
import org.jetbrains.plugins.groovy.lang.psi.api.toplevel.GrTopStatement;
/**
* author ven
*/
public interface GrTopLevelDefinition extends GrNamedElement, GrTopStatement {
GrTopLevelDefinition[] EMPTY_ARRAY = new GrTopLevelDefinition[0];
}
@@ -25,15 +25,16 @@ import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocCommentOwner;
import org.jetbrains.plugins.groovy.lang.psi.GrNamedElement;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrClassInitializer;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrTopLevelDefinition;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMember;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMembersDeclaration;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod;
import org.jetbrains.plugins.groovy.lang.psi.api.toplevel.GrTopStatement;
import org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeParameterList;
public interface GrTypeDefinition extends PsiClass, GrTopLevelDefinition, GrDocCommentOwner, GrMember {
public interface GrTypeDefinition extends PsiClass, GrDocCommentOwner, GrMember, GrNamedElement, GrTopStatement {
GrTypeDefinition[] EMPTY_ARRAY = new GrTypeDefinition[0];
ArrayFactory<GrTypeDefinition> ARRAY_FACTORY = GrTypeDefinition[]::new;
@@ -19,12 +19,11 @@ import com.intellij.psi.PsiModifierListOwner;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement;
import org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.GrModifierList;
import org.jetbrains.plugins.groovy.lang.psi.api.toplevel.GrTopStatement;
/**
* @author ven
*/
public interface GrMembersDeclaration extends GroovyPsiElement, PsiModifierListOwner, GrTopStatement {
public interface GrMembersDeclaration extends GroovyPsiElement, PsiModifierListOwner {
GrMembersDeclaration[] EMPTY_ARRAY = new GrMembersDeclaration[0];
GrMember[] getMembers();
@@ -27,10 +27,10 @@ import org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocCommentOwner;
import org.jetbrains.plugins.groovy.lang.psi.GrNamedElement;
import org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.GrModifierList;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrParametersOwner;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrTopLevelDefinition;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrCodeBlock;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameterList;
import org.jetbrains.plugins.groovy.lang.psi.api.toplevel.GrTopStatement;
import org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement;
import org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeParameterListOwner;
@@ -41,7 +41,7 @@ import java.util.Map;
* @date: 26.03.2007
*/
public interface GrMethod extends GrMembersDeclaration, GrNamedElement, PsiMethod, GrMember,
GrParametersOwner, GrTopLevelDefinition, GrTypeParameterListOwner, GrDocCommentOwner {
GrParametersOwner, GrTopStatement, GrTypeParameterListOwner, GrDocCommentOwner {
GrMethod[] EMPTY_ARRAY = new GrMethod[0];
ArrayFactory<GrMethod> ARRAY_FACTORY = count -> new GrMethod[count];
@@ -19,11 +19,12 @@ package org.jetbrains.plugins.groovy.lang.psi.api.toplevel;
import org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement;
/**
* Describes elemenets which may appear at top-level of file.
*
* @author: Dmitry.Krasilschikov
* @date: 21.03.2007
*/
public interface GrTopStatement extends GroovyPsiElement {
GrTopStatement[] EMPTY_ARRAY = new GrTopStatement[0];
}
@@ -37,7 +37,6 @@ import org.jetbrains.plugins.groovy.lang.psi.GroovyElementVisitor;
import org.jetbrains.plugins.groovy.lang.psi.GroovyFileBase;
import org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrTopLevelDefinition;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition;
@@ -82,12 +81,6 @@ public abstract class GroovyFileBaseImpl extends PsiFileBase implements GroovyFi
return calcTreeElement().getChildrenAsPsiElements(TokenSets.TYPE_DEFINITIONS, GrTypeDefinition.ARRAY_FACTORY);
}
@Override
@NotNull
public GrTopLevelDefinition[] getTopLevelDefinitions() {
return findChildrenByClass(GrTopLevelDefinition.class);
}
@Override
@NotNull
public GrMethod[] getMethods() {
@@ -40,7 +40,6 @@ import org.jetbrains.plugins.groovy.lang.lexer.GroovyTokenTypes;
import org.jetbrains.plugins.groovy.lang.parser.GroovyElementTypes;
import org.jetbrains.plugins.groovy.lang.psi.GroovyFile;
import org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrTopLevelDefinition;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter;
@@ -319,8 +318,8 @@ public class GroovyFileImpl extends GroovyFileBaseImpl implements GroovyFile, Ps
}
private static boolean shouldProcess(@Nullable PsiElement lastParent, @NotNull PsiElement run) {
return run instanceof GrAssignmentExpression // binding variables
|| !(run instanceof GrTopLevelDefinition || run instanceof GrImportStatement || lastParent instanceof GrMember);
return run instanceof GrAssignmentExpression || // binding variables
run instanceof GrVariableDeclaration && !(lastParent instanceof GrMember); // local variables
}
@Override
@@ -486,7 +486,7 @@ public class GroovyPsiElementFactoryImpl extends GroovyPsiElementFactory {
@Override
public PsiElement createModifierFromText(@NotNull String name) {
final GroovyFileBase file = createGroovyFileChecked(name + " foo() {}");
final GrTopLevelDefinition[] definitions = file.getTopLevelDefinitions();
final GrTopStatement[] definitions = file.getTopStatements();
if (definitions.length != 1) throw new IncorrectOperationException(name);
return definitions[0].getFirstChild().getFirstChild();
}
@@ -495,7 +495,7 @@ public class GroovyPsiElementFactoryImpl extends GroovyPsiElementFactory {
@Override
public GrCodeBlock createMethodBodyFromText(@NotNull String text) {
final GroovyFileBase file = createGroovyFileChecked("def foo () {" + text + "}");
final GrMethod method = (GrMethod) file.getTopLevelDefinitions()[0];
final GrMethod method = (GrMethod) file.getTopStatements()[0];
return method.getBlock();
}
@@ -535,15 +535,17 @@ public class GroovyPsiElementFactoryImpl extends GroovyPsiElementFactory {
@Override
public GrMethod createConstructorFromText(String constructorName, CharSequence constructorText, @Nullable PsiElement context) {
GroovyFile file = createGroovyFileChecked("class " + constructorName + "{" + constructorText + "}", false, context);
GrTopLevelDefinition definition = file.getTopLevelDefinitions()[0];
GrTypeDefinition definition = file.getTypeDefinitions()[0];
if (!( definition != null && definition instanceof GrClassDefinition)) {
if (definition == null) {
throw new IncorrectOperationException("constructorName: " + constructorName + ", text: " + constructorText);
}
GrMethod[] methods = ((GrClassDefinition)definition).getCodeMethods();
GrMethod[] methods = definition.getCodeMethods();
if (methods.length != 1) {
throw new IncorrectOperationException("constructorName: " + constructorName + ", text: " + constructorText);
}
return methods[0];
}
@@ -47,7 +47,6 @@ import org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefini
import org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinitionBody;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMembersDeclaration;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod;
import org.jetbrains.plugins.groovy.lang.psi.api.toplevel.GrTopStatement;
import org.jetbrains.plugins.groovy.lang.psi.util.PsiUtil;
import java.util.ArrayList;
@@ -173,7 +172,7 @@ public class GroovyStatementMover extends StatementUpDownMover {
}
}
private boolean shouldDigInside(GrTopStatement statement) {
private boolean shouldDigInside(GroovyPsiElement statement) {
if (stmtLevel && (statement instanceof GrMethod || statement instanceof GrTypeDefinition)) {
return false;
}
@@ -183,9 +182,9 @@ public class GroovyStatementMover extends StatementUpDownMover {
return true;
}
private void addChildRanges(GrTopStatement[] statements) {
private void addChildRanges(GroovyPsiElement[] statements) {
for (int i = 0; i < statements.length; i++) {
GrTopStatement statement = statements[i];
GroovyPsiElement statement = statements[i];
if (nlsAfter(statement)) {
final LineRange range = getLineRange(statement);
if ((i == 0 || isStatement(statements[i-1])) && isStatement(statement)) {