use of org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GroovyScriptClass in project intellij-community by JetBrains.
the class GrIntroduceFieldHandlerBase method findPossibleScopes.
@NotNull
@Override
protected PsiClass[] findPossibleScopes(GrExpression expression, GrVariable variable, StringPartInfo partInfo, Editor editor) {
PsiElement place = getCurrentPlace(expression, variable, partInfo);
PsiClass aClass = PsiUtil.getContextClass(place);
if (aClass instanceof GroovyScriptClass) {
return new PsiClass[] { aClass };
} else {
List<PsiClass> result = ContainerUtil.newArrayList(aClass);
while (aClass != null) {
aClass = PsiTreeUtil.getParentOfType(aClass, PsiClass.class);
ContainerUtil.addIfNotNull(result, aClass);
}
return result.toArray(new PsiClass[result.size()]);
}
}
use of org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GroovyScriptClass in project intellij-community by JetBrains.
the class GrIntroduceFieldProcessor method initializeInMethod.
void initializeInMethod(@NotNull GrVariable field, @NotNull List<PsiElement> replaced) {
final PsiElement _scope = myContext.getScope();
final PsiElement scope = _scope instanceof GroovyScriptClass ? ((GroovyScriptClass) _scope).getContainingFile() : _scope;
final PsiElement place = replaced.get(0);
final GrMember member = GrIntroduceFieldHandler.getContainer(place, scope);
GrStatementOwner container = member instanceof GrMethod ? ((GrMethod) member).getBlock() : member instanceof GrClassInitializer ? ((GrClassInitializer) member).getBlock() : place.getContainingFile() instanceof GroovyFile ? ((GroovyFile) place.getContainingFile()) : null;
assert container != null;
final PsiElement anchor;
if (mySettings.removeLocalVar()) {
GrVariable variable = myLocalVariable;
anchor = PsiTreeUtil.getParentOfType(variable, GrStatement.class);
} else {
anchor = GrIntroduceHandlerBase.findAnchor(replaced.toArray(new PsiElement[replaced.size()]), container);
GrIntroduceHandlerBase.assertStatement(anchor, myContext.getScope());
}
initializeInMethodInner(field, container, (GrStatement) anchor, replaced.get(0));
}
use of org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GroovyScriptClass in project intellij-community by JetBrains.
the class GrIntroduceFieldProcessor method insertField.
@NotNull
protected GrVariableDeclaration insertField(@NotNull PsiClass targetClass) {
GrVariableDeclaration declaration = createField(targetClass);
if (targetClass instanceof GrEnumTypeDefinition) {
final GrEnumConstantList enumConstants = ((GrEnumTypeDefinition) targetClass).getEnumConstantList();
return (GrVariableDeclaration) targetClass.addAfter(declaration, enumConstants);
}
if (targetClass instanceof GrTypeDefinition) {
PsiElement anchor = getAnchorForDeclaration((GrTypeDefinition) targetClass);
return (GrVariableDeclaration) targetClass.addAfter(declaration, anchor);
} else {
assert targetClass instanceof GroovyScriptClass;
final GroovyFile file = ((GroovyScriptClass) targetClass).getContainingFile();
PsiElement[] elements = file.getMethods();
if (elements.length == 0)
elements = file.getStatements();
final PsiElement anchor = ArrayUtil.getFirstElement(elements);
return (GrVariableDeclaration) file.addBefore(declaration, anchor);
}
}
use of org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GroovyScriptClass in project intellij-community by JetBrains.
the class ClassItemGeneratorImpl method writeMethod.
@Override
public void writeMethod(StringBuilder builder, PsiMethod method) {
if (method == null)
return;
GenerationUtil.writeDocComment(builder, method, true);
String name = method.getName();
boolean isAbstract = method.hasModifierProperty(PsiModifier.ABSTRACT);
PsiModifierList modifierList = method.getModifierList();
final PsiClass containingClass = method.getContainingClass();
if (method.isConstructor() && containingClass != null && containingClass.isEnum()) {
ModifierListGenerator.writeModifiers(builder, modifierList, ModifierListGenerator.ENUM_CONSTRUCTOR_MODIFIERS);
} else {
ModifierListGenerator.writeModifiers(builder, modifierList);
}
if (method.hasTypeParameters()) {
GenerationUtil.writeTypeParameters(builder, method, classNameProvider);
builder.append(' ');
}
//append return type
if (!method.isConstructor()) {
PsiType retType = context.typeProvider.getReturnType(method);
TypeWriter.writeType(builder, retType, method, classNameProvider);
builder.append(' ');
}
builder.append(name);
if (method instanceof GroovyPsiElement) {
context.searchForLocalVarsToWrap((GroovyPsiElement) method);
}
GenerationUtil.writeParameterList(builder, method.getParameterList().getParameters(), classNameProvider, context);
if (method instanceof GrAnnotationMethod) {
GrAnnotationMemberValue defaultValue = ((GrAnnotationMethod) method).getDefaultValue();
if (defaultValue != null) {
builder.append("default ");
defaultValue.accept(new AnnotationGenerator(builder, context));
}
}
GenerationUtil.writeThrowsList(builder, method.getThrowsList(), getMethodExceptions(method), classNameProvider);
if (!isAbstract) {
/* ************ body ********* */
if (method instanceof GrMethod) {
if (method instanceof GrReflectedMethod && ((GrReflectedMethod) method).getSkippedParameters().length > 0) {
builder.append("{\n").append(generateDelegateCall((GrReflectedMethod) method)).append("\n}\n");
} else {
new CodeBlockGenerator(builder, context.extend()).generateMethodBody((GrMethod) method);
}
} else if (method instanceof GrAccessorMethod) {
writeAccessorBody(builder, method);
} else if (method instanceof LightMethodBuilder && containingClass instanceof GroovyScriptClass) {
if ("main".equals(method.getName())) {
writeMainScriptMethodBody(builder, method);
} else if ("run".equals(method.getName())) {
writeRunScriptMethodBody(builder, method);
}
} else {
builder.append("{//todo\n}");
}
} else {
builder.append(';');
}
}
use of org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GroovyScriptClass in project intellij-community by JetBrains.
the class ClassItemGeneratorImpl method collectMethods.
@Override
public Collection<PsiMethod> collectMethods(PsiClass typeDefinition) {
List<PsiMethod> result = ContainerUtil.filter(typeDefinition.getMethods(), m -> !GroovyObjectTransformationSupport.isGroovyObjectSupportMethod(m));
if (typeDefinition instanceof GroovyScriptClass) {
final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(context.project);
final String name = typeDefinition.getName();
GrTypeDefinition tempClass = factory.createTypeDefinition("class " + name + " extends groovy.lang.Script {\n" + " def " + name + "(groovy.lang.Binding binding){\n" + " super(binding);\n" + " }\n" + " def " + name + "(){\n" + " super();\n" + " }\n" + "}");
ContainerUtil.addAll(result, tempClass.getCodeConstructors());
}
return result;
}
Aggregations