use of org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap in project intellij-community by JetBrains.
the class GroovyLiteralSelectioner 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 GrListOrMap)
return result;
int startOffset = -1;
int endOffset = -1;
final String text = e.getText();
final int stringOffset = e.getTextOffset();
final IElementType elementType = e.getNode().getElementType();
if (elementType == GroovyTokenTypes.mGSTRING_CONTENT || elementType == GroovyTokenTypes.mREGEX_CONTENT || elementType == GroovyTokenTypes.mDOLLAR_SLASH_REGEX_CONTENT) {
int cur;
int index = -1;
while (true) {
cur = text.indexOf('\n', index + 1);
if (cur < 0 || cur + stringOffset > cursorOffset)
break;
index = cur;
}
if (index >= 0) {
startOffset = stringOffset + index + 1;
}
index = text.indexOf('\n', cursorOffset - stringOffset);
if (index >= 0) {
endOffset = stringOffset + index + 1;
}
}
if (startOffset >= 0 && endOffset >= 0) {
result.add(new TextRange(startOffset, endOffset));
}
final String content = GrStringUtil.removeQuotes(text);
final int offset = stringOffset + text.indexOf(content);
result.add(new TextRange(offset, offset + content.length()));
return result;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap in project intellij-community by JetBrains.
the class GrSplitDeclarationIntention method getElementPredicate.
@NotNull
@Override
protected PsiElementPredicate getElementPredicate() {
return new PsiElementPredicate() {
@Override
public boolean satisfiedBy(PsiElement element) {
if (element instanceof GrVariableDeclaration) {
GrVariableDeclaration decl = (GrVariableDeclaration) element;
GrVariable[] variables = decl.getVariables();
if (variables.length > 1 && PsiUtil.isLocalVariable(variables[0])) {
if (!decl.isTuple() || decl.getTupleInitializer() instanceof GrListOrMap) {
myText = GroovyIntentionsBundle.message("split.into.separate.declaration");
} else {
myText = GroovyIntentionsBundle.message("split.into.declaration.and.assignment");
}
return true;
} else if (variables.length == 1 && PsiUtil.isLocalVariable(variables[0]) && variables[0].getInitializerGroovy() != null) {
myText = GroovyIntentionsBundle.message("split.into.declaration.and.assignment");
return true;
}
}
return false;
}
};
}
use of org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap in project intellij-community by JetBrains.
the class ExpressionGenerator method visitSafeCastExpression.
@Override
public void visitSafeCastExpression(@NotNull GrSafeCastExpression typeCastExpression) {
final GrExpression operand = (GrExpression) PsiUtil.skipParenthesesIfSensibly(typeCastExpression.getOperand(), false);
final GrTypeElement typeElement = typeCastExpression.getCastTypeElement();
if (operand instanceof GrListOrMap && ((GrListOrMap) operand).isMap() && typeElement != null) {
AnonymousFromMapGenerator.writeAnonymousMap((GrListOrMap) operand, typeElement, builder, context);
return;
}
final PsiType type = typeElement.getType();
if (operand instanceof GrListOrMap && !((GrListOrMap) operand).isMap() && type instanceof PsiArrayType) {
builder.append("new ");
final GrExpression[] initializers = ((GrListOrMap) operand).getInitializers();
if (initializers.length == 0) {
TypeWriter.writeTypeForNew(builder, ((PsiArrayType) type).getComponentType(), typeCastExpression);
builder.append("[0]");
} else {
TypeWriter.writeTypeForNew(builder, type, typeCastExpression);
builder.append('{');
for (GrExpression initializer : initializers) {
initializer.accept(this);
builder.append(", ");
}
if (initializers.length > 0) {
builder.delete(builder.length() - 2, builder.length());
//builder.removeFromTheEnd(2);
}
builder.append('}');
}
return;
}
final GroovyResolveResult resolveResult = PsiImplUtil.extractUniqueResult(typeCastExpression.multiResolve(false));
final PsiElement resolved = resolveResult.getElement();
if (resolved instanceof PsiMethod) {
final GrExpression typeParam;
try {
typeParam = factory.createExpressionFromText(typeElement.getText(), typeCastExpression);
} catch (IncorrectOperationException e) {
generateCast(typeElement, operand);
return;
}
invokeMethodOn(((PsiMethod) resolved), operand, new GrExpression[] { typeParam }, GrNamedArgument.EMPTY_ARRAY, GrClosableBlock.EMPTY_ARRAY, resolveResult.getSubstitutor(), typeCastExpression);
} else {
generateCast(typeElement, operand);
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap in project intellij-community by JetBrains.
the class CodeBlockGenerator method writeTupleDeclaration.
private void writeTupleDeclaration(GrVariableDeclaration variableDeclaration, StringBuilder builder, ExpressionContext expressionContext) {
GrVariable[] variables = variableDeclaration.getVariables();
final GrExpression tupleInitializer = variableDeclaration.getTupleInitializer();
if (tupleInitializer instanceof GrListOrMap) {
for (GrVariable variable : variables) {
GenerationUtil.writeVariableSeparately(variable, builder, expressionContext);
builder.append(";\n");
}
} else if (tupleInitializer != null) {
GroovyResolveResult iteratorMethodResult = GenerationUtil.resolveMethod(tupleInitializer, "iterator", GrExpression.EMPTY_ARRAY, GrNamedArgument.EMPTY_ARRAY, GrClosableBlock.EMPTY_ARRAY, variableDeclaration);
final PsiType iteratorType = inferIteratorType(iteratorMethodResult, tupleInitializer);
final String iteratorName = genIteratorVar(variableDeclaration, builder, expressionContext, tupleInitializer, iteratorType, iteratorMethodResult);
final GrModifierList modifierList = variableDeclaration.getModifierList();
PsiType iterableTypeParameter = PsiUtil.extractIterableTypeParameter(iteratorType, false);
for (final GrVariable v : variables) {
ModifierListGenerator.writeModifiers(builder, modifierList);
final PsiType type = context.typeProvider.getVarType(v);
TypeWriter.writeType(builder, type, variableDeclaration);
builder.append(' ').append(v.getName());
builder.append(" = ");
GenerationUtil.wrapInCastIfNeeded(builder, type, iterableTypeParameter, tupleInitializer, expressionContext, new StatementWriter() {
@Override
public void writeStatement(StringBuilder builder, ExpressionContext context) {
builder.append(iteratorName).append(".hasNext() ? ").append(iteratorName).append(".next() : null");
}
});
builder.append(";\n");
}
} else {
GenerationUtil.writeSimpleVarDeclaration(variableDeclaration, builder, expressionContext);
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap in project intellij-community by JetBrains.
the class GroovyBlock method getChildAttributes.
@Override
@NotNull
public ChildAttributes getChildAttributes(final int newChildIndex) {
ASTNode astNode = getNode();
final PsiElement psiParent = astNode.getPsi();
if (psiParent instanceof GroovyFileBase) {
return new ChildAttributes(Indent.getNoneIndent(), null);
}
if (psiParent instanceof GrSwitchStatement) {
List<Block> subBlocks = getSubBlocks();
if (newChildIndex > 0) {
Block block = subBlocks.get(newChildIndex - 1);
if (block instanceof GroovyBlock) {
PsiElement anchorPsi = ((GroovyBlock) block).getNode().getPsi();
if (anchorPsi instanceof GrCaseSection) {
for (GrStatement statement : ((GrCaseSection) anchorPsi).getStatements()) {
if (statement instanceof GrBreakStatement || statement instanceof GrContinueStatement || statement instanceof GrReturnStatement || statement instanceof GrThrowStatement) {
final Indent indent = GroovyIndentProcessor.getSwitchCaseIndent(myContext.getSettings());
return new ChildAttributes(indent, null);
}
}
int indentSize = myContext.getSettings().getIndentOptions().INDENT_SIZE;
final int spaces = myContext.getSettings().INDENT_CASE_FROM_SWITCH ? 2 * indentSize : indentSize;
return new ChildAttributes(Indent.getSpaceIndent(spaces), null);
}
}
}
}
if (psiParent instanceof GrCaseLabel) {
return new ChildAttributes(GroovyIndentProcessor.getSwitchCaseIndent(getContext().getSettings()), null);
}
if (psiParent instanceof GrCaseSection) {
return getSwitchIndent((GrCaseSection) psiParent, newChildIndex);
}
if (TokenSets.BLOCK_SET.contains(astNode.getElementType()) || GroovyElementTypes.SWITCH_STATEMENT.equals(astNode.getElementType())) {
return new ChildAttributes(Indent.getNormalIndent(), null);
}
if (GroovyElementTypes.CASE_SECTION.equals(astNode.getElementType())) {
return new ChildAttributes(Indent.getNormalIndent(), null);
}
if (psiParent instanceof GrBinaryExpression || psiParent instanceof GrConditionalExpression || psiParent instanceof GrCommandArgumentList || psiParent instanceof GrArgumentList || psiParent instanceof GrParameterList || psiParent instanceof GrListOrMap || psiParent instanceof GrAnnotationArgumentList || psiParent instanceof GrVariable || psiParent instanceof GrAssignmentExpression) {
return new ChildAttributes(Indent.getContinuationWithoutFirstIndent(), null);
}
if (psiParent instanceof GrDocComment || psiParent instanceof GrDocTag) {
return new ChildAttributes(Indent.getSpaceIndent(GroovyIndentProcessor.GDOC_COMMENT_INDENT), null);
}
if (psiParent instanceof GrIfStatement || psiParent instanceof GrLoopStatement) {
return new ChildAttributes(Indent.getNormalIndent(), null);
}
if (psiParent instanceof GrLabeledStatement && newChildIndex == 2) {
final Indent indent = getContext().getGroovySettings().INDENT_LABEL_BLOCKS ? Indent.getLabelIndent() : Indent.getNoneIndent();
return new ChildAttributes(indent, null);
}
return new ChildAttributes(Indent.getNoneIndent(), null);
}
Aggregations