use of org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrCodeBlock in project intellij-community by JetBrains.
the class GroovyPlainEnterProcessor method doEnter.
@Override
public boolean doEnter(PsiElement psiElement, PsiFile file, @NotNull Editor editor, boolean modified) {
GrCodeBlock block = getControlStatementBlock(editor.getCaretModel().getOffset(), psiElement);
if (block != null) {
PsiElement firstElement = block.getFirstChild().getNextSibling();
final int offset = firstElement != null ? firstElement.getTextRange().getStartOffset() - 1 : block.getTextRange().getEndOffset();
editor.getCaretModel().moveToOffset(offset);
}
plainEnter(editor);
return true;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrCodeBlock in project intellij-community by JetBrains.
the class GroovySmartEnterProcessor method reformat.
@Override
protected void reformat(PsiElement atCaret) throws IncorrectOperationException {
PsiElement parent = atCaret.getParent();
if (parent instanceof GrCodeBlock) {
final GrCodeBlock block = (GrCodeBlock) parent;
if (block.getStatements().length > 0 && block.getStatements()[0] == atCaret) {
atCaret = block;
}
} else if (parent instanceof GrForStatement || parent instanceof GrSwitchStatement) {
atCaret = parent;
}
super.reformat(atCaret);
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrCodeBlock in project intellij-community by JetBrains.
the class GrMethodBodyFixer method apply.
@Override
public void apply(@NotNull Editor editor, @NotNull GroovySmartEnterProcessor processor, @NotNull PsiElement psiElement) {
if (!(psiElement instanceof GrMethod))
return;
GrMethod method = (GrMethod) psiElement;
final PsiClass aClass = method.getContainingClass();
if (aClass != null && aClass.isInterface() || method.hasModifierProperty(PsiModifier.ABSTRACT))
return;
final GrCodeBlock body = method.getBlock();
final Document doc = editor.getDocument();
if (body != null) {
// See IDEADEV-1093. This is quite hacky heuristic but it seem to be best we can do.
String bodyText = body.getText();
if (StringUtil.startsWithChar(bodyText, '{')) {
final GrStatement[] statements = body.getStatements();
if (statements.length > 0) {
// [todo]
// if (statements[0] instanceof PsiDeclarationStatement) {
// if (PsiTreeUtil.getDeepestLast(statements[0]) instanceof PsiErrorElement) {
// if (method.getContainingClass().getRBrace() == null) {
// doc.insertString(body.getTextRange().getStartOffset() + 1, "\n}");
// }
// }
// }
}
}
return;
}
int endOffset = method.getTextRange().getEndOffset();
if (StringUtil.endsWithChar(method.getText(), ';')) {
doc.deleteString(endOffset - 1, endOffset);
endOffset--;
}
doc.insertString(endOffset, "{\n}");
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrCodeBlock in project intellij-community by JetBrains.
the class GroovyMethodInliner method isOnExpressionOrReturnPlace.
/*
Method call is used as expression in some enclosing expression or
is method return result
*/
private static boolean isOnExpressionOrReturnPlace(@NotNull GrCallExpression call) {
PsiElement parent = call.getParent();
if (!(parent instanceof GrVariableDeclarationOwner)) {
return true;
}
// tail calls in methods and closures
GrVariableDeclarationOwner owner = (GrVariableDeclarationOwner) parent;
if (owner instanceof GrClosableBlock || owner instanceof GrOpenBlock && owner.getParent() instanceof GrMethod) {
GrStatement[] statements = ((GrCodeBlock) owner).getStatements();
assert statements.length > 0;
GrStatement last = statements[statements.length - 1];
if (last == call)
return true;
if (last instanceof GrReturnStatement && call == ((GrReturnStatement) last).getReturnValue()) {
return true;
}
}
return false;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrCodeBlock in project intellij-community by JetBrains.
the class AddMethodFix method doFix.
@Override
protected void doFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) throws IncorrectOperationException {
if (myClass.isInterface()) {
final GrMethod method = GroovyPsiElementFactory.getInstance(project).createMethodFromText("def " + myClass.getName() + " " + myMethodName + "();");
myClass.add(method);
} else {
String templName = JavaTemplateUtil.TEMPLATE_IMPLEMENTED_METHOD_BODY;
final FileTemplate template = FileTemplateManager.getInstance(project).getCodeTemplate(templName);
Properties properties = new Properties();
String returnType = generateTypeText(myClass);
properties.setProperty(FileTemplate.ATTRIBUTE_RETURN_TYPE, returnType);
properties.setProperty(FileTemplate.ATTRIBUTE_DEFAULT_RETURN_VALUE, PsiTypesUtil.getDefaultValueOfType(JavaPsiFacade.getElementFactory(project).createType(myClass)));
properties.setProperty(FileTemplate.ATTRIBUTE_CALL_SUPER, "");
properties.setProperty(FileTemplate.ATTRIBUTE_CLASS_NAME, myClass.getQualifiedName());
properties.setProperty(FileTemplate.ATTRIBUTE_SIMPLE_CLASS_NAME, myClass.getName());
properties.setProperty(FileTemplate.ATTRIBUTE_METHOD_NAME, myMethodName);
try {
String bodyText = StringUtil.replace(template.getText(properties), ";", "");
final GrCodeBlock newBody = GroovyPsiElementFactory.getInstance(project).createMethodBodyFromText("\n" + bodyText + "\n");
final GrMethod method = GroovyPsiElementFactory.getInstance(project).createMethodFromText("", myMethodName, returnType, ArrayUtil.EMPTY_STRING_ARRAY, myClass);
method.setBlock(newBody);
myClass.add(method);
} catch (IOException e) {
LOG.error(e);
}
}
}
Aggregations