use of org.jetbrains.plugins.groovy.lang.psi.GroovyFile in project intellij-community by JetBrains.
the class ConvertToJavaProcessor method performRefactoring.
//private static String
@Override
protected void performRefactoring(@NotNull UsageInfo[] usages) {
final GeneratorClassNameProvider classNameProvider = new GeneratorClassNameProvider();
ExpressionContext context = new ExpressionContext(myProject, myFiles);
final ClassGenerator classGenerator = new ClassGenerator(classNameProvider, new ClassItemGeneratorImpl(context));
for (GroovyFile file : myFiles) {
final PsiClass[] classes = file.getClasses();
StringBuilder builder = new StringBuilder();
boolean first = true;
for (PsiClass aClass : classes) {
classGenerator.writeTypeDefinition(builder, aClass, true, first);
first = false;
builder.append('\n');
}
final Document document = PsiDocumentManager.getInstance(myProject).getDocument(file);
LOG.assertTrue(document != null);
document.setText(builder.toString());
PsiDocumentManager.getInstance(myProject).commitDocument(document);
String fileName = getNewFileName(file);
PsiElement newFile;
try {
newFile = file.setName(fileName);
} catch (final IncorrectOperationException e) {
ApplicationManager.getApplication().invokeLater(() -> Messages.showMessageDialog(myProject, e.getMessage(), RefactoringBundle.message("error.title"), Messages.getErrorIcon()));
return;
}
doPostProcessing(newFile);
}
}
use of org.jetbrains.plugins.groovy.lang.psi.GroovyFile in project intellij-community by JetBrains.
the class ClosureGenerator method getOwner.
@NonNls
@NotNull
private CharSequence getOwner(@NotNull GrClosableBlock closure) {
final GroovyPsiElement context = PsiTreeUtil.getParentOfType(closure, GrMember.class, GroovyFile.class);
LOG.assertTrue(context != null);
final PsiClass contextClass;
if (context instanceof GroovyFile) {
contextClass = ((GroovyFile) context).getScriptClass();
} else if (context instanceof PsiClass) {
contextClass = (PsiClass) context;
} else if (context instanceof GrMember) {
if (((GrMember) context).hasModifierProperty(PsiModifier.STATIC)) {
//no context class
contextClass = null;
} else {
contextClass = ((GrMember) context).getContainingClass();
}
} else {
contextClass = null;
}
if (contextClass == null)
return "null";
final PsiElement implicitClass = GenerationUtil.getWrappingImplicitClass(closure);
if (implicitClass == null) {
return "this";
} else {
final StringBuilder buffer = new StringBuilder();
GenerationUtil.writeThisReference(contextClass, buffer, this.context);
return buffer;
}
}
use of org.jetbrains.plugins.groovy.lang.psi.GroovyFile in project intellij-community by JetBrains.
the class GroovyCompletionData method hasReturnValue.
private static boolean hasReturnValue(PsiElement context) {
GrControlFlowOwner flowOwner = ControlFlowUtils.findControlFlowOwner(context);
if (flowOwner instanceof GrClosableBlock)
return true;
if (flowOwner instanceof GroovyFile)
return true;
if (flowOwner == null)
return true;
PsiElement parent = flowOwner.getParent();
if (parent instanceof GrMethod) {
return !PsiType.VOID.equals(((GrMethod) parent).getReturnType());
} else if (parent instanceof GrClassInitializer) {
return false;
}
return true;
}
use of org.jetbrains.plugins.groovy.lang.psi.GroovyFile in project intellij-community by JetBrains.
the class GroovyCompletionData method isControlStructure.
private static boolean isControlStructure(PsiElement context) {
final int offset = context.getTextRange().getStartOffset();
PsiElement prevSibling = context.getPrevSibling();
if (context.getParent() instanceof GrReferenceElement && prevSibling != null && prevSibling.getNode() != null) {
ASTNode node = prevSibling.getNode();
return !TokenSets.DOTS.contains(node.getElementType());
}
if (GroovyCompletionUtil.isNewStatement(context, true)) {
final PsiElement leaf = GroovyCompletionUtil.getLeafByOffset(offset - 1, context);
if (leaf != null && (leaf.getParent() instanceof GrStatementOwner || leaf.getParent() instanceof GrLabeledStatement)) {
return true;
}
}
if (context.getParent() != null) {
PsiElement parent = context.getParent();
if (parent instanceof GrExpression && parent.getParent() instanceof GroovyFile) {
return true;
}
if (parent instanceof GrReferenceExpression) {
PsiElement superParent = parent.getParent();
if (superParent instanceof GrStatementOwner || superParent instanceof GrLabeledStatement || superParent instanceof GrControlStatement || superParent instanceof GrMethodCall) {
return true;
}
}
return false;
}
return false;
}
use of org.jetbrains.plugins.groovy.lang.psi.GroovyFile in project intellij-community by JetBrains.
the class GroovyCompletionData method suggestPrimitiveTypes.
private static boolean suggestPrimitiveTypes(PsiElement context) {
if (isInfixOperatorPosition(context))
return false;
if (isAfterForParameter(context))
return false;
final PsiElement parent = context.getParent();
if (parent == null)
return false;
PsiElement previous = PsiImplUtil.realPrevious(parent.getPrevSibling());
if (parent instanceof GrReferenceElement && parent.getParent() instanceof GrArgumentList) {
PsiElement prevSibling = context.getPrevSibling();
if (prevSibling != null && prevSibling.getNode() != null) {
if (!TokenSets.DOTS.contains(prevSibling.getNode().getElementType())) {
return true;
}
} else if (!(previous != null && GroovyTokenTypes.mAT.equals(previous.getNode().getElementType()))) {
return true;
}
}
if (GroovyCompletionUtil.isTupleVarNameWithoutTypeDeclared(context))
return true;
if (previous != null && GroovyTokenTypes.mAT.equals(previous.getNode().getElementType())) {
return false;
}
if (GroovyCompletionUtil.asSimpleVariable(context) || GroovyCompletionUtil.asTypedMethod(context) || GroovyCompletionUtil.asVariableInBlock(context) || asVariableAfterModifiers(context)) {
return true;
}
if ((parent instanceof GrParameter && ((GrParameter) parent).getTypeElementGroovy() == null) || parent instanceof GrReferenceElement && !(parent.getParent() instanceof GrImportStatement) && !(parent.getParent() instanceof GrPackageDefinition) && !(parent.getParent() instanceof GrArgumentList)) {
PsiElement prevSibling = context.getPrevSibling();
if (parent instanceof GrReferenceElement && prevSibling != null && prevSibling.getNode() != null) {
ASTNode node = prevSibling.getNode();
return !TokenSets.DOTS.contains(node.getElementType());
} else {
return true;
}
}
if (PsiImplUtil.realPrevious(parent.getPrevSibling()) instanceof GrModifierList) {
return true;
}
if (PsiImplUtil.realPrevious(context.getPrevSibling()) instanceof GrModifierList) {
return true;
}
return parent instanceof GrExpression && parent.getParent() instanceof GroovyFile && GroovyCompletionUtil.isNewStatement(context, false);
}
Aggregations