use of org.jetbrains.plugins.groovy.lang.psi.api.toplevel.GrTopStatement in project intellij-community by JetBrains.
the class GrPackageInspection method getElementToHighlight.
@Nullable
private static PsiElement getElementToHighlight(GroovyFile file) {
GrPackageDefinition packageDefinition = file.getPackageDefinition();
if (packageDefinition != null)
return packageDefinition;
PsiClass[] classes = file.getClasses();
for (PsiClass aClass : classes) {
if (!(aClass instanceof SyntheticElement) && aClass instanceof GrTypeDefinition) {
return ((GrTypeDefinition) aClass).getNameIdentifierGroovy();
}
}
GrTopStatement[] statements = file.getTopStatements();
if (statements.length > 0) {
GrTopStatement first = statements[0];
if (first instanceof GrNamedElement)
return ((GrNamedElement) first).getNameIdentifierGroovy();
return first;
}
return null;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.toplevel.GrTopStatement in project intellij-community by JetBrains.
the class GroovyShellLanguageConsoleView method processCode.
protected void processCode() {
GroovyShellCodeFragment groovyFile = getGroovyFile();
for (GrTopStatement statement : groovyFile.getTopStatements()) {
if (statement instanceof GrImportStatement) {
groovyFile.addImportsFromString(importToString((GrImportStatement) statement));
} else if (statement instanceof GrMethod) {
groovyFile.addVariable(((GrMethod) statement).getName(), generateClosure((GrMethod) statement));
} else if (statement instanceof GrAssignmentExpression) {
GrAssignmentExpression assignment = (GrAssignmentExpression) statement;
GrExpression left = assignment.getLValue();
if (left instanceof GrReferenceExpression && !((GrReferenceExpression) left).isQualified()) {
groovyFile.addVariable(((GrReferenceExpression) left).getReferenceName(), assignment.getRValue());
}
} else if (statement instanceof GrTypeDefinition) {
groovyFile.addTypeDefinition(prepareTypeDefinition((GrTypeDefinition) statement));
}
}
PsiType scriptType = groovyFile.getInferredScriptReturnType();
if (scriptType != null) {
groovyFile.addVariable("_", scriptType);
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.toplevel.GrTopStatement in project intellij-community by JetBrains.
the class GroovyPsiElementFactoryImpl method createModifierFromText.
@NotNull
@Override
public PsiElement createModifierFromText(@NotNull String name) {
final GroovyFileBase file = createGroovyFileChecked(name + " foo() {}");
final GrTopStatement[] definitions = file.getTopStatements();
if (definitions.length != 1)
throw new IncorrectOperationException(name);
return definitions[0].getFirstChild().getFirstChild();
}
use of org.jetbrains.plugins.groovy.lang.psi.api.toplevel.GrTopStatement in project intellij-community by JetBrains.
the class GroovyPsiElementFactoryImpl method createReferenceExpressionFromText.
@NotNull
@Override
public GrReferenceExpression createReferenceExpressionFromText(@NotNull String idText, PsiElement context) {
GroovyFile file = createGroovyFileChecked(idText, false, context);
GrTopStatement[] statements = file.getTopStatements();
if (statements.length != 1)
throw new IncorrectOperationException("refText: " + idText);
if (!(statements[0] instanceof GrReferenceExpression))
throw new IncorrectOperationException("refText: " + idText);
return (GrReferenceExpression) statements[0];
}
use of org.jetbrains.plugins.groovy.lang.psi.api.toplevel.GrTopStatement in project intellij-community by JetBrains.
the class GroovyPsiElementFactoryImpl method createTypeElement.
@Override
@NotNull
public GrTypeElement createTypeElement(@NotNull String typeText, @Nullable final PsiElement context) throws IncorrectOperationException {
final GroovyFile file = createGroovyFileChecked("def " + typeText + " someVar", false, context);
GrTopStatement[] topStatements = file.getTopStatements();
if (topStatements == null || topStatements.length == 0)
throw new IncorrectOperationException("can't create type element from:" + typeText);
GrTopStatement statement = topStatements[0];
if (!(statement instanceof GrVariableDeclaration))
throw new IncorrectOperationException("can't create type element from:" + typeText);
GrVariableDeclaration decl = (GrVariableDeclaration) statement;
final GrTypeElement element = decl.getTypeElementGroovy();
if (element == null)
throw new IncorrectOperationException("can't create type element from:" + typeText);
return element;
}
Aggregations