use of org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinitionBody in project intellij-community by JetBrains.
the class GroovyTypeDefinitionBodySelectioner method select.
@Override
public List<TextRange> select(PsiElement e, CharSequence editorText, int cursorOffset, Editor editor) {
List<TextRange> result = super.select(e, editorText, cursorOffset, editor);
if (e instanceof GrTypeDefinitionBody) {
GrTypeDefinitionBody block = ((GrTypeDefinitionBody) e);
int startOffset = findOpeningBrace(block);
int endOffset = findClosingBrace(block, startOffset);
TextRange range = new TextRange(startOffset, endOffset);
result.addAll(expandToWholeLine(editorText, range));
}
return result;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinitionBody in project intellij-community by JetBrains.
the class GrIntroduceFieldProcessor method getAnchorForDeclaration.
@Nullable
private static PsiElement getAnchorForDeclaration(@NotNull GrTypeDefinition targetClass) {
final GrTypeDefinitionBody body = targetClass.getBody();
if (body == null)
return null;
PsiElement anchor = body.getLBrace();
final GrMembersDeclaration[] declarations = targetClass.getMemberDeclarations();
for (GrMembersDeclaration declaration : declarations) {
if (declaration instanceof GrVariableDeclaration)
anchor = declaration;
if (!(declaration instanceof GrVariableDeclaration))
return anchor;
}
return anchor;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinitionBody in project intellij-community by JetBrains.
the class ReplaceAbstractClassInstanceByMapIntention method processIntention.
@Override
protected void processIntention(@NotNull PsiElement psiElement, @NotNull Project project, Editor editor) throws IncorrectOperationException {
PsiDocumentManager.getInstance(project).commitAllDocuments();
GrCodeReferenceElement ref = (GrCodeReferenceElement) psiElement;
final GrAnonymousClassDefinition anonymous = (GrAnonymousClassDefinition) ref.getParent();
final GrNewExpression newExpr = (GrNewExpression) anonymous.getParent();
final PsiElement resolved = ref.resolve();
// && ((PsiClass)resolved).isInterface();
assert resolved instanceof PsiClass;
GrTypeDefinitionBody body = anonymous.getBody();
assert body != null;
List<Pair<PsiMethod, GrOpenBlock>> methods = new ArrayList<>();
for (GrMethod method : body.getMethods()) {
methods.add(new Pair<>(method, method.getBlock()));
}
final PsiClass iface = (PsiClass) resolved;
final Collection<CandidateInfo> collection = OverrideImplementExploreUtil.getMethodsToOverrideImplement(anonymous, true);
for (CandidateInfo info : collection) {
methods.add(new Pair<>((PsiMethod) info.getElement(), null));
}
StringBuilder buffer = new StringBuilder();
if (methods.size() == 1) {
final Pair<PsiMethod, GrOpenBlock> pair = methods.get(0);
appendClosureTextByMethod(pair.getFirst(), buffer, pair.getSecond(), newExpr);
if (!GroovyConfigUtils.getInstance().isVersionAtLeast(psiElement, GroovyConfigUtils.GROOVY2_2)) {
buffer.append(" as ").append(iface.getQualifiedName());
}
} else {
buffer.append("[");
buffer.append("\n");
for (Pair<PsiMethod, GrOpenBlock> pair : methods) {
final PsiMethod method = pair.getFirst();
final GrOpenBlock block = pair.getSecond();
buffer.append(method.getName()).append(": ");
appendClosureTextByMethod(method, buffer, block, newExpr);
buffer.append(",\n");
}
if (!methods.isEmpty()) {
buffer.delete(buffer.length() - 2, buffer.length());
buffer.append('\n');
}
buffer.append("]");
buffer.append(" as ").append(iface.getQualifiedName());
}
createAndAdjustNewExpression(project, newExpr, buffer);
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinitionBody in project intellij-community by JetBrains.
the class GroovyFoldingBuilder method isSingleHighLevelClassBody.
private static boolean isSingleHighLevelClassBody(PsiElement element) {
if (!(element instanceof GrTypeDefinitionBody))
return false;
final PsiElement parent = element.getParent();
if (!(parent instanceof GrTypeDefinition))
return false;
final GrTypeDefinition clazz = (GrTypeDefinition) parent;
if (clazz.isAnonymous() || clazz.getContainingClass() != null)
return false;
final PsiFile file = element.getContainingFile();
return file instanceof GroovyFile && ((GroovyFile) file).getClasses().length == 1;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinitionBody in project intellij-community by JetBrains.
the class GrTypeDefinitionImpl method add.
@Override
public PsiElement add(@NotNull PsiElement psiElement) throws IncorrectOperationException {
final GrTypeDefinitionBody body = getBody();
if (body == null)
throw new IncorrectOperationException("Class must have body");
final PsiElement lBrace = body.getLBrace();
if (lBrace == null)
throw new IncorrectOperationException("No left brace");
PsiMember member = getAnyMember(psiElement);
PsiElement anchor = member != null ? getDefaultAnchor(body, member) : null;
if (anchor == null) {
anchor = lBrace.getNextSibling();
}
if (anchor != null) {
ASTNode node = anchor.getNode();
assert node != null;
if (GroovyTokenTypes.mSEMI.equals(node.getElementType())) {
anchor = anchor.getNextSibling();
}
if (psiElement instanceof GrField) {
//add field with modifiers which are in its parent
int i = ArrayUtilRt.find(((GrVariableDeclaration) psiElement.getParent()).getVariables(), psiElement);
psiElement = body.addBefore(psiElement.getParent(), anchor);
GrVariable[] vars = ((GrVariableDeclaration) psiElement).getVariables();
for (int j = 0; j < vars.length; j++) {
if (i != j)
vars[i].delete();
}
psiElement = vars[i];
} else {
psiElement = body.addBefore(psiElement, anchor);
}
} else {
psiElement = body.add(psiElement);
}
return psiElement;
}
Aggregations