use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrStringInjection in project intellij-community by JetBrains.
the class ConvertGStringToStringIntention method convertGStringLiteralToStringLiteral.
public static String convertGStringLiteralToStringLiteral(GrLiteral literal) {
PsiElement child = literal.getFirstChild();
if (child == null)
return literal.getText();
String text;
ArrayList<String> list = new ArrayList<>();
PsiElement prevSibling = null;
PsiElement nextSibling;
do {
text = child.getText();
nextSibling = child.getNextSibling();
if (child instanceof GrStringInjection) {
if (((GrStringInjection) child).getClosableBlock() != null) {
text = prepareClosableBlock(((GrStringInjection) child).getClosableBlock());
} else if (((GrStringInjection) child).getExpression() != null) {
text = prepareExpression(((GrStringInjection) child).getExpression());
} else {
text = child.getText();
}
} else {
text = prepareText(text, prevSibling == null, nextSibling == null, nextSibling instanceof GrClosableBlock || nextSibling instanceof GrReferenceExpression);
}
if (text != null) {
list.add(text);
}
prevSibling = child;
child = child.getNextSibling();
} while (child != null);
StringBuilder builder = new StringBuilder(literal.getTextLength() * 2);
if (list.isEmpty())
return "''";
builder.append(list.get(0));
for (int i = 1; i < list.size(); i++) {
builder.append(" + ").append(list.get(i));
}
return builder.toString();
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrStringInjection in project intellij-community by JetBrains.
the class ConvertStringToMultilineIntention method prepareNewLiteralText.
private static StringBuilder prepareNewLiteralText(List<GrLiteral> literals) {
String quote = !containsInjections(literals) && literals.get(0).getText().startsWith("'") ? "'''" : "\"\"\"";
final StringBuilder buffer = new StringBuilder();
buffer.append(quote);
for (GrLiteral literal : literals) {
if (literal instanceof GrLiteralImpl) {
appendSimpleStringValue(literal, buffer, quote);
} else {
final GrStringImpl gstring = (GrStringImpl) literal;
for (PsiElement child : gstring.getAllContentParts()) {
if (child instanceof GrStringContent) {
appendSimpleStringValue(child, buffer, "\"\"\"");
} else if (child instanceof GrStringInjection) {
buffer.append(child.getText());
}
}
}
}
buffer.append(quote);
return buffer;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrStringInjection in project intellij-community by JetBrains.
the class GrIntroduceHandlerBase method expressionIsIncorrect.
public static boolean expressionIsIncorrect(@Nullable GrExpression expression, boolean acceptVoidCalls) {
if (expression instanceof GrParenthesizedExpression)
return true;
if (PsiUtil.isSuperReference(expression))
return true;
if (expression instanceof GrAssignmentExpression)
return true;
if (expression instanceof GrReferenceExpression && expression.getParent() instanceof GrCall) {
final GroovyResolveResult resolveResult = ((GrReferenceExpression) expression).advancedResolve();
final PsiElement resolved = resolveResult.getElement();
return resolved instanceof PsiMethod && !resolveResult.isInvokedOnProperty() || resolved instanceof PsiClass;
}
if (expression instanceof GrReferenceExpression && expression.getParent() instanceof GrReferenceExpression) {
return !PsiUtil.isThisReference(expression) && ((GrReferenceExpression) expression).resolve() instanceof PsiClass;
}
if (expression instanceof GrClosableBlock && expression.getParent() instanceof GrStringInjection)
return true;
if (!acceptVoidCalls && expression instanceof GrMethodCall && PsiType.VOID.equals(expression.getType()))
return true;
return false;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrStringInjection in project intellij-community by JetBrains.
the class GrIntroduceFieldDialog method canBeInitializedOutsideBlock.
private static boolean canBeInitializedOutsideBlock(@NotNull GrIntroduceContext context, @NotNull PsiClass clazz) {
final StringPartInfo part = context.getStringPart();
GrExpression expression = context.getExpression();
if (expression != null) {
expression = (GrExpression) PsiUtil.skipParentheses(expression, false);
if (expression == null)
return false;
if (expression instanceof GrReferenceExpression) {
final PsiElement resolved = ((GrReferenceExpression) expression).resolve();
if (PsiUtil.isLocalVariable(resolved)) {
expression = ((GrVariable) resolved).getInitializerGroovy();
if (expression == null)
return false;
}
}
ExpressionChecker visitor = new ExpressionChecker(clazz, expression);
expression.accept(visitor);
return visitor.isResult();
}
if (part != null) {
for (GrStringInjection injection : part.getInjections()) {
GroovyPsiElement scope = injection.getExpression() != null ? injection.getExpression() : injection.getClosableBlock();
assert scope != null;
ExpressionChecker visitor = new ExpressionChecker(clazz, scope);
scope.accept(visitor);
if (!visitor.isResult()) {
return visitor.isResult();
}
}
return true;
} else {
return false;
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrStringInjection in project intellij-community by JetBrains.
the class GroovySpacingProcessor method visitClosure.
@Override
public void visitClosure(@NotNull GrClosableBlock closure) {
ASTNode rBraceAtTheEnd = GeeseUtil.getClosureRBraceAtTheEnd(myChild1);
if (myGroovySettings.USE_FLYING_GEESE_BRACES && myType2 == GroovyTokenTypes.mRCURLY && rBraceAtTheEnd != null) {
String text = rBraceAtTheEnd.getTreeParent().getText();
if (text.indexOf('\n') < 0) {
/* the case:
foo {
bar {print x}<we are here>}
*/
myResult = Spacing.createSpacing(1, 1, 1, false, 1);
} else {
myResult = Spacing.createSpacing(0, 0, 0, true, 100, 0);
}
} else if (myType1 == GroovyTokenTypes.mLCURLY && myType2 == GroovyTokenTypes.mRCURLY) {
//empty closure
createSpaceInCode(false);
} else if (myType1 == GroovyTokenTypes.mLCURLY && myType2 != GroovyElementTypes.PARAMETERS_LIST && myType2 != GroovyTokenTypes.mCLOSABLE_BLOCK_OP || myType2 == GroovyTokenTypes.mRCURLY) {
//spaces between statements
boolean spacesWithinBraces = closure.getParent() instanceof GrStringInjection ? myGroovySettings.SPACE_WITHIN_GSTRING_INJECTION_BRACES : mySettings.SPACE_WITHIN_BRACES;
createDependentLFSpacing(true, spacesWithinBraces, closure.getTextRange());
} else if (myType1 == GroovyTokenTypes.mCLOSABLE_BLOCK_OP) {
myResult = GroovySpacingProcessorBasic.createDependentSpacingForClosure(mySettings, myGroovySettings, closure, true);
} else if (myType1 == GroovyTokenTypes.mLCURLY && (myType2 == GroovyElementTypes.PARAMETERS_LIST || myType2 == GroovyTokenTypes.mCLOSABLE_BLOCK_OP)) {
boolean spacesWithinBraces = closure.getParent() instanceof GrStringInjection ? myGroovySettings.SPACE_WITHIN_GSTRING_INJECTION_BRACES : mySettings.SPACE_WITHIN_BRACES;
createSpaceInCode(spacesWithinBraces);
}
}
Aggregations