use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration in project intellij-community by JetBrains.
the class GroovyBlockGenerator method calculateAlignments.
private void calculateAlignments(List<ASTNode> children, boolean classLevel) {
List<GrStatement> currentGroup = null;
boolean spock = true;
for (ASTNode child : children) {
PsiElement psi = child.getPsi();
if (psi instanceof GrLabeledStatement) {
alignGroup(currentGroup, spock, classLevel);
currentGroup = ContainerUtil.newArrayList();
spock = true;
} else if (currentGroup != null && spock && isTablePart(psi)) {
currentGroup.add((GrStatement) psi);
} else if (psi instanceof GrVariableDeclaration) {
GrVariable[] variables = ((GrVariableDeclaration) psi).getVariables();
if (variables.length > 0) {
if (!classLevel || currentGroup == null || fieldGroupEnded(psi) || spock) {
alignGroup(currentGroup, spock, classLevel);
currentGroup = ContainerUtil.newArrayList();
spock = false;
}
currentGroup.add((GrStatement) psi);
}
} else {
if (shouldSkip(classLevel, psi))
continue;
alignGroup(currentGroup, spock, classLevel);
currentGroup = null;
}
}
if (currentGroup != null) {
alignGroup(currentGroup, spock, classLevel);
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration in project intellij-community by JetBrains.
the class GrVariableJoinLinesHandler method tryJoinStatements.
@Override
public int tryJoinStatements(@NotNull GrStatement first, @NotNull GrStatement second) {
if (first instanceof GrVariableDeclaration && !((GrVariableDeclaration) first).isTuple() && second instanceof GrAssignmentExpression) {
final GrExpression lvalue = ((GrAssignmentExpression) second).getLValue();
final GrExpression rValue = ((GrAssignmentExpression) second).getRValue();
if (lvalue instanceof GrReferenceExpression && rValue != null) {
final PsiElement resolved = ((GrReferenceExpression) lvalue).resolve();
if (ArrayUtil.contains(resolved, ((GrVariableDeclaration) first).getVariables())) {
assert resolved instanceof GrVariable;
if (((GrVariable) resolved).getInitializerGroovy() == null) {
((GrVariable) resolved).setInitializerGroovy(rValue);
second.delete();
GrExpression newInitializer = ((GrVariable) resolved).getInitializerGroovy();
assert newInitializer != null;
return newInitializer.getTextRange().getEndOffset();
}
}
}
}
return CANNOT_JOIN;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration in project intellij-community by JetBrains.
the class GrIntroduceVariableHandler method addVariable.
private static GrVariable addVariable(@NotNull GrIntroduceContext context, @NotNull GroovyIntroduceVariableSettings settings) {
GrStatement anchor = findAnchor(context, settings.replaceAllOccurrences());
PsiElement parent = anchor.getParent();
assert parent instanceof GrStatementOwner;
GrVariableDeclaration generated = generateDeclaration(context, settings);
GrStatement declaration = ((GrStatementOwner) parent).addStatementBefore(generated, anchor);
declaration = (GrStatement) JavaCodeStyleManager.getInstance(context.getProject()).shortenClassReferences(declaration);
return ((GrVariableDeclaration) declaration).getVariables()[0];
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration in project intellij-community by JetBrains.
the class GrIntroduceVariableHandler method generateDeclaration.
@NotNull
private static GrVariableDeclaration generateDeclaration(@NotNull GrIntroduceContext context, @NotNull GroovyIntroduceVariableSettings settings) {
final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(context.getProject());
final String[] modifiers = settings.isDeclareFinal() ? new String[] { PsiModifier.FINAL } : null;
final GrVariableDeclaration declaration = factory.createVariableDeclaration(modifiers, "foo", settings.getSelectedType(), settings.getName());
generateInitializer(context, declaration.getVariables()[0]);
return declaration;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration in project intellij-community by JetBrains.
the class GrSetStrongTypeIntention method getOrCreateTypeElement.
@NotNull
private static TypeInfo getOrCreateTypeElement(@NotNull PsiElement parent, @NotNull PsiElement elementToBuildTemplateOn) {
GrModifierList modifierList = getModifierList(parent);
if (modifierList != null && modifierList.hasModifierProperty(GrModifier.DEF) && modifierList.getModifiers().length == 1) {
PsiElement modifier = modifierList.getModifier(GrModifier.DEF);
LOG.assertTrue(modifier != null);
int modifierOffset = modifier.getTextRange().getEndOffset() - elementToBuildTemplateOn.getTextRange().getStartOffset();
return new TypeInfo(modifier, modifierOffset);
} else {
int nameElementOffset;
final PsiClassType typeToUse = TypesUtil.createType("Abc", parent);
if (elementToBuildTemplateOn instanceof GrVariableDeclaration) {
GrVariableDeclaration decl = (GrVariableDeclaration) elementToBuildTemplateOn;
decl.setType(typeToUse);
nameElementOffset = decl.getModifierList().getTextRange().getEndOffset() - elementToBuildTemplateOn.getTextRange().getStartOffset();
} else {
GrVariable var = (GrVariable) parent;
var.setType(typeToUse);
nameElementOffset = var.getNameIdentifierGroovy().getTextRange().getStartOffset() - elementToBuildTemplateOn.getTextRange().getStartOffset();
}
return new TypeInfo(getTypeElement(parent), nameElementOffset);
}
}
Aggregations