use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration in project intellij-community by JetBrains.
the class GrSplitDeclarationIntention method processIntention.
@Override
protected void processIntention(@NotNull PsiElement element, @NotNull Project project, Editor editor) throws IncorrectOperationException {
if (!(element instanceof GrVariableDeclaration))
return;
GrVariableDeclaration declaration = (GrVariableDeclaration) element;
GrVariable[] variables = declaration.getVariables();
if (variables.length == 1) {
processSingleVar(project, declaration, variables[0]);
} else if (variables.length > 1) {
if (!declaration.isTuple() || declaration.getTupleInitializer() instanceof GrListOrMap) {
processMultipleVars(project, declaration);
} else {
processTuple(project, declaration);
}
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration in project intellij-community by JetBrains.
the class GrSplitDeclarationIntention method createVarDeclaration.
private static GrStatement createVarDeclaration(Project project, GrVariable variable, String modifiers, boolean isTuple) {
StringBuilder builder = new StringBuilder();
builder.append(modifiers).append(' ');
GrTypeElement typeElement = variable.getTypeElementGroovy();
if (typeElement != null) {
builder.append(typeElement.getText()).append(' ');
}
builder.append(variable.getName());
GrExpression initializer = variable.getInitializerGroovy();
if (initializer != null) {
builder.append('=').append(initializer.getText());
}
GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(project);
GrVariableDeclaration decl = (GrVariableDeclaration) factory.createStatementFromText(builder);
if (isTuple && (variable.getDeclaredType() != null || decl.getModifierList().getModifiers().length > 1)) {
decl.getModifierList().setModifierProperty(GrModifier.DEF, false);
}
return decl;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration in project intellij-community by JetBrains.
the class ConvertMethodToClosureIntention method execute.
private static void execute(final GrMethod method, final Collection<GrReferenceExpression> usagesToConvert) {
ApplicationManager.getApplication().runWriteAction(() -> {
GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(method.getProject());
StringBuilder builder = new StringBuilder(method.getTextLength());
String modifiers = method.getModifierList().getText();
if (modifiers.trim().isEmpty()) {
modifiers = GrModifier.DEF;
}
builder.append(modifiers).append(' ');
builder.append(method.getName()).append("={");
builder.append(method.getParameterList().getText()).append(" ->");
final GrOpenBlock block = method.getBlock();
builder.append(block.getText().substring(1));
final GrVariableDeclaration variableDeclaration = GroovyPsiElementFactory.getInstance(method.getProject()).createFieldDeclarationFromText(builder.toString());
method.replace(variableDeclaration);
for (GrReferenceExpression element : usagesToConvert) {
final PsiElement qualifier = element.getQualifier();
final StringBuilder text = new StringBuilder(qualifier.getText());
element.setQualifier(null);
text.append('.').append(element.getText());
element.replace(factory.createExpressionFromText(text.toString()));
}
});
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration in project intellij-community by JetBrains.
the class GrCreateFieldForParameterIntention method performRefactoring.
@Override
protected void performRefactoring(Project project, PsiClass targetClass, PsiMethod method, PsiParameter myParameter, PsiType type, String fieldName, boolean methodStatic, boolean isFinal) {
GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(project);
if (targetClass.findFieldByName(fieldName, false) == null) {
String[] modifiers = getModifiers(methodStatic, isFinal);
GrVariableDeclaration fieldDeclaration = factory.createFieldDeclaration(modifiers, fieldName, null, type);
GrVariableDeclaration inserted = (GrVariableDeclaration) targetClass.add(fieldDeclaration);
JavaCodeStyleManager.getInstance(project).shortenClassReferences(inserted);
}
GrOpenBlock block = ((GrMethod) method).getBlock();
if (block == null)
return;
GrAssignmentExpression assignment = createAssignment(targetClass, myParameter, fieldName, methodStatic, factory);
GrStatement anchor = getAnchor(block);
GrStatement statement = block.addStatementBefore(assignment, anchor);
JavaCodeStyleManager.getInstance(project).shortenClassReferences(statement);
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration in project intellij-community by JetBrains.
the class GroovyBlockGenerator method alignVariableDeclarations.
private void alignVariableDeclarations(List<GrStatement> group, boolean classLevel) {
AlignmentProvider.Aligner typeElement = myAlignmentProvider.createAligner(true);
AlignmentProvider.Aligner varName = myAlignmentProvider.createAligner(true);
AlignmentProvider.Aligner eq = myAlignmentProvider.createAligner(true);
for (GrStatement statement : group) {
GrVariableDeclaration varDeclaration = (GrVariableDeclaration) statement;
GrVariable[] variables = varDeclaration.getVariables();
for (GrVariable variable : variables) {
varName.append(variable.getNameIdentifierGroovy());
}
if (classLevel && myContext.getSettings().ALIGN_GROUP_FIELD_DECLARATIONS) {
typeElement.append(varDeclaration.getTypeElementGroovy());
ASTNode current_eq = variables[variables.length - 1].getNode().findChildByType(GroovyTokenTypes.mASSIGN);
if (current_eq != null) {
eq.append(current_eq.getPsi());
}
}
}
}
Aggregations