use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration in project intellij-community by JetBrains.
the class GroovyCompletionUtil method isTupleVarNameWithoutTypeDeclared.
public static boolean isTupleVarNameWithoutTypeDeclared(PsiElement position) {
PsiElement parent = position.getParent();
PsiElement pparent = parent.getParent();
return parent instanceof GrVariable && ((GrVariable) parent).getNameIdentifierGroovy() == position && ((GrVariable) parent).getTypeElementGroovy() == null && pparent instanceof GrVariableDeclaration && ((GrVariableDeclaration) pparent).isTuple();
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration in project intellij-community by JetBrains.
the class PsiImplUtil method removeVariable.
public static void removeVariable(GrVariable variable) {
final GrVariableDeclaration varDecl = (GrVariableDeclaration) variable.getParent();
final List<GrVariable> variables = Arrays.asList(varDecl.getVariables());
if (!variables.contains(variable)) {
throw new IllegalArgumentException();
}
final PsiElement parent = varDecl.getParent();
final ASTNode owner = parent.getNode();
if (variables.size() == 1 && owner != null) {
PsiElement next = varDecl.getNextSibling();
//noinspection ConstantConditions
while (next != null && next.getNode() != null && next.getNode().getElementType() == GroovyTokenTypes.mSEMI) {
PsiElement tmpNext = next.getNextSibling();
//noinspection ConstantConditions
next.delete();
next = tmpNext;
}
removeNewLineAfter(varDecl);
varDecl.delete();
return;
}
variable.delete();
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration in project intellij-community by JetBrains.
the class GrVariableBaseImpl method setInitializerGroovy.
@Override
public void setInitializerGroovy(GrExpression initializer) {
if (getParent() instanceof GrVariableDeclaration && ((GrVariableDeclaration) getParent()).isTuple()) {
throw new UnsupportedOperationException("don't invoke 'setInitializer()' for tuple declaration");
}
GrExpression oldInitializer = getInitializerGroovy();
if (initializer == null) {
if (oldInitializer != null) {
oldInitializer.delete();
PsiElement assign = findChildByType(GroovyTokenTypes.mASSIGN);
if (assign != null) {
assign.delete();
}
}
return;
}
if (oldInitializer != null) {
oldInitializer.replaceWithExpression(initializer, true);
} else {
getNode().addLeaf(TokenType.WHITE_SPACE, " ", null);
getNode().addLeaf(GroovyTokenTypes.mASSIGN, "=", null);
addAfter(initializer, getLastChild());
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration in project intellij-community by JetBrains.
the class GrVariableBaseImpl method getInitializerGroovy.
@Override
@Nullable
public GrExpression getInitializerGroovy() {
final PsiElement parent = getParent();
if (parent instanceof GrVariableDeclaration && ((GrVariableDeclaration) parent).isTuple()) {
final GrVariableDeclaration tuple = (GrVariableDeclaration) parent;
final GrExpression initializer = tuple.getTupleInitializer();
if (initializer instanceof GrListOrMap) {
final GrListOrMap listOrMap = (GrListOrMap) initializer;
final GrExpression[] initializers = listOrMap.getInitializers();
final int varNumber = ArrayUtil.indexOf(tuple.getVariables(), this);
if (initializers.length < varNumber + 1)
return null;
return initializers[varNumber];
}
}
return GroovyPsiElementImpl.findExpressionChild(this);
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration in project intellij-community by JetBrains.
the class GrSetStrongTypeIntention method processIntention.
@Override
protected void processIntention(@NotNull PsiElement element, @NotNull Project project, final Editor editor) throws IncorrectOperationException {
PsiElement parent = element.getParent();
PsiElement elementToBuildTemplate;
GrVariable[] variables;
if (parent instanceof GrVariable && parent.getParent() instanceof GrVariableDeclaration) {
variables = ((GrVariableDeclaration) parent.getParent()).getVariables();
elementToBuildTemplate = parent.getParent();
} else if (parent instanceof GrVariable && parent.getParent() instanceof GrForInClause) {
variables = new GrVariable[] { (GrVariable) parent };
elementToBuildTemplate = parent.getParent().getParent();
} else if (parent instanceof GrVariableDeclaration) {
variables = ((GrVariableDeclaration) parent).getVariables();
elementToBuildTemplate = parent;
} else if (parent instanceof GrParameter && parent.getParent() instanceof GrParameterList) {
variables = new GrVariable[] { (GrVariable) parent };
elementToBuildTemplate = parent.getParent().getParent();
} else if (parent instanceof GrVariable) {
variables = new GrVariable[] { ((GrVariable) parent) };
elementToBuildTemplate = parent;
} else {
return;
}
ArrayList<TypeConstraint> types = new ArrayList<>();
if (parent.getParent() instanceof GrForInClause) {
types.add(SupertypeConstraint.create(PsiUtil.extractIteratedType((GrForInClause) parent.getParent())));
} else {
for (GrVariable variable : variables) {
GrExpression initializer = variable.getInitializerGroovy();
if (initializer != null) {
PsiType type = initializer.getType();
if (type != null) {
types.add(SupertypeConstraint.create(type));
}
}
if (variable instanceof GrParameter) {
final PsiParameter parameter = (PsiParameter) variable;
final PsiType type = getClosureParameterType(parameter);
if (type != null) {
types.add(SupertypeConstraint.create(type));
}
}
}
}
final String originalText = elementToBuildTemplate.getText();
final TypeInfo typeInfo = getOrCreateTypeElement(parent, elementToBuildTemplate);
final PsiElement replaceElement = typeInfo.elementToReplace;
TypeConstraint[] constraints = types.toArray(new TypeConstraint[types.size()]);
ChooseTypeExpression chooseTypeExpression = new ChooseTypeExpression(constraints, element.getManager(), replaceElement.getResolveScope());
TemplateBuilderImpl builder = new TemplateBuilderImpl(elementToBuildTemplate);
builder.replaceElement(replaceElement, chooseTypeExpression);
final Document document = editor.getDocument();
final RangeMarker rangeMarker = document.createRangeMarker(elementToBuildTemplate.getTextRange());
rangeMarker.setGreedyToRight(true);
rangeMarker.setGreedyToLeft(true);
final PsiElement afterPostprocess = CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(elementToBuildTemplate);
final Template template = builder.buildTemplate();
TextRange range = afterPostprocess.getTextRange();
document.deleteString(range.getStartOffset(), range.getEndOffset());
TemplateManager templateManager = TemplateManager.getInstance(project);
templateManager.startTemplate(editor, template, new TemplateEditingAdapter() {
@Override
public void templateFinished(Template template, boolean brokenOff) {
if (brokenOff) {
ApplicationManager.getApplication().runWriteAction(() -> {
if (rangeMarker.isValid()) {
document.replaceString(rangeMarker.getStartOffset(), rangeMarker.getEndOffset(), originalText);
editor.getCaretModel().moveToOffset(rangeMarker.getStartOffset() + typeInfo.originalOffset);
}
});
}
}
});
}
Aggregations