use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrString in project intellij-community by JetBrains.
the class ExpressionGenerator method visitGStringExpression.
@Override
public void visitGStringExpression(@NotNull GrString gstring) {
final String newExprText = ConvertGStringToStringIntention.convertGStringLiteralToStringLiteral(gstring);
final GrExpression newExpr = factory.createExpressionFromText(newExprText, gstring);
newExpr.accept(this);
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrString in project intellij-community by JetBrains.
the class GrBreakStringOnLineBreaksIntention method invokeImpl.
private static String invokeImpl(PsiElement element) {
final String text = element.getText();
final String quote = GrStringUtil.getStartQuote(text);
if (!("'".equals(quote) || "\"".equals(quote)))
return text;
if (!text.contains("\\n"))
return text;
String value = GrStringUtil.removeQuotes(text);
StringBuilder buffer = new StringBuilder();
if (element instanceof GrString) {
processGString(element, quote, value, buffer);
} else {
processSimpleString(quote, value, buffer);
}
final String result = buffer.toString();
if (result.endsWith("+\n\"\""))
return result.substring(0, result.length() - 4);
return result;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrString in project android by JetBrains.
the class GradleEditorValueExtractor method visitGStringExpression.
@Override
public void visitGStringExpression(GrString expression) {
GroovyPsiElement[] parts = expression.getAllContentParts();
boolean registeredAssignment = false;
for (GroovyPsiElement part : parts) {
if (part instanceof GrStringContent) {
if (!myInterestedInReferencesOnly && !registeredAssignment && !part.getTextRange().isEmpty()) {
registeredAssignment = true;
String text = expression.getText();
TextRange range = expression.getTextRange();
if (text.startsWith("'") || text.startsWith("\"")) {
text = text.substring(1);
range = TextRange.create(range.getStartOffset() + 1, range.getEndOffset());
}
if (text.endsWith("'") || text.endsWith("\"")) {
text = text.substring(0, text.length() - 1);
range = TextRange.create(range.getStartOffset(), range.getEndOffset() - 1);
}
myContext.addCachedValue(text, range);
}
} else if (part instanceof GrStringInjection) {
GrExpression injectedExpression = ((GrStringInjection) part).getExpression();
if (injectedExpression != null) {
injectedExpression.accept(myVisitor);
}
}
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrString in project intellij-community by JetBrains.
the class GroovyFoldingBuilder method addFoldingForStrings.
private static void addFoldingForStrings(List<FoldingDescriptor> descriptors, ASTNode node) {
if (!isMultiLineStringLiteral(node))
return;
if (!node.getElementType().equals(GroovyElementTypes.GSTRING) && !node.getElementType().equals(GroovyElementTypes.REGEX)) {
descriptors.add(new FoldingDescriptor(node, node.getTextRange()));
return;
}
final GrString grString = (GrString) node.getPsi();
if (grString == null)
return;
final GrStringInjection[] injections = grString.getInjections();
if (injections.length == 0) {
descriptors.add(new FoldingDescriptor(node, node.getTextRange()));
return;
}
final String start_quote = GrStringUtil.getStartQuote(node.getText());
final String end_quote = GrStringUtil.getEndQuote(node.getText());
final FoldingGroup group = FoldingGroup.newGroup("GString");
final TextRange nodeRange = node.getTextRange();
int startOffset = nodeRange.getStartOffset();
GrStringInjection injection = injections[0];
TextRange injectionRange = injection.getTextRange();
if (startOffset + 1 < injectionRange.getStartOffset()) {
descriptors.add(new NamedFoldingDescriptor(node, startOffset, injectionRange.getStartOffset(), group, start_quote));
}
final String placeholder = " ";
startOffset = injectionRange.getEndOffset();
for (int i = 1; i < injections.length; i++) {
injection = injections[i];
injectionRange = injection.getTextRange();
final int endOffset = injectionRange.getStartOffset();
if (endOffset - startOffset >= 2) {
descriptors.add(new NamedFoldingDescriptor(injection.getNode().getTreePrev(), startOffset, endOffset, group, placeholder));
}
startOffset = injectionRange.getEndOffset();
}
if (startOffset + 1 < nodeRange.getEndOffset()) {
descriptors.add(new NamedFoldingDescriptor(node.getLastChildNode(), startOffset, nodeRange.getEndOffset(), group, end_quote));
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrString in project intellij-community by JetBrains.
the class PsiImplUtil method replaceExpression.
@Nullable
public static GrExpression replaceExpression(GrExpression oldExpr, GrExpression newExpr, boolean removeUnnecessaryParentheses) {
PsiElement oldParent = oldExpr.getParent();
if (oldParent == null)
throw new PsiInvalidElementAccessException(oldExpr);
if (!(oldExpr instanceof GrApplicationStatement)) {
newExpr = ApplicationStatementUtil.convertToMethodCallExpression(newExpr);
}
// Remove unnecessary parentheses
if (removeUnnecessaryParentheses && oldParent instanceof GrParenthesizedExpression && !(oldParent.getParent() instanceof GrArgumentLabel)) {
return ((GrExpression) oldParent).replaceWithExpression(newExpr, true);
}
//regexes cannot be after identifier , try to replace it with simple string
if (getRegexAtTheBeginning(newExpr) != null && isAfterIdentifier(oldExpr)) {
final PsiElement copy = newExpr.copy();
final GrLiteral regex = getRegexAtTheBeginning(copy);
LOG.assertTrue(regex != null);
final GrLiteral stringLiteral = GrStringUtil.createStringFromRegex(regex);
if (regex == copy) {
return oldExpr.replaceWithExpression(stringLiteral, removeUnnecessaryParentheses);
} else {
regex.replace(stringLiteral);
return oldExpr.replaceWithExpression((GrExpression) copy, removeUnnecessaryParentheses);
}
}
GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(oldExpr.getProject());
if (oldParent instanceof GrStringInjection) {
if (newExpr instanceof GrString || newExpr instanceof GrLiteral && ((GrLiteral) newExpr).getValue() instanceof String) {
return GrStringUtil.replaceStringInjectionByLiteral((GrStringInjection) oldParent, (GrLiteral) newExpr);
} else {
GrClosableBlock block = factory.createClosureFromText("{foo}");
oldParent.getNode().replaceChild(oldExpr.getNode(), block.getNode());
GrStatement[] statements = block.getStatements();
return ((GrExpression) statements[0]).replaceWithExpression(newExpr, removeUnnecessaryParentheses);
}
}
if (PsiTreeUtil.getParentOfType(oldExpr, GrStringInjection.class, false, GrCodeBlock.class) != null) {
final GrStringInjection stringInjection = PsiTreeUtil.getParentOfType(oldExpr, GrStringInjection.class);
GrStringUtil.wrapInjection(stringInjection);
assert stringInjection != null;
final PsiElement replaced = oldExpr.replaceWithExpression(newExpr, removeUnnecessaryParentheses);
return (GrExpression) replaced;
}
//check priorities
if (oldParent instanceof GrExpression && !(oldParent instanceof GrParenthesizedExpression)) {
GrExpression addedParenth = checkPrecedence(newExpr, oldExpr) ? parenthesize(newExpr) : newExpr;
if (newExpr != addedParenth) {
return oldExpr.replaceWithExpression(addedParenth, removeUnnecessaryParentheses);
}
}
//we should add the expression in arg list
if (oldExpr instanceof GrClosableBlock && !(newExpr instanceof GrClosableBlock) && oldParent instanceof GrMethodCallExpression && ArrayUtil.contains(oldExpr, ((GrMethodCallExpression) oldParent).getClosureArguments())) {
return ((GrMethodCallExpression) oldParent).replaceClosureArgument((GrClosableBlock) oldExpr, newExpr);
}
newExpr = (GrExpression) oldExpr.replace(newExpr);
// to find target parenthesised expression.
if (newExpr instanceof GrParenthesizedExpression && isFirstChild(newExpr)) {
int parentCount = 0;
PsiElement element = oldParent;
while (element != null && !(element instanceof GrCommandArgumentList)) {
if (element instanceof GrCodeBlock || element instanceof GrParenthesizedExpression)
break;
if (element instanceof PsiFile)
break;
final PsiElement parent = element.getParent();
if (parent == null)
break;
if (!isFirstChild(element))
break;
element = parent;
parentCount++;
}
if (element instanceof GrCommandArgumentList) {
final GrCommandArgumentList commandArgList = (GrCommandArgumentList) element;
final PsiElement parent = commandArgList.getParent();
LOG.assertTrue(parent instanceof GrApplicationStatement);
final GrMethodCall methodCall = factory.createMethodCallByAppCall((GrApplicationStatement) parent);
final GrMethodCall newCall = (GrMethodCall) parent.replace(methodCall);
PsiElement result = newCall.getArgumentList().getAllArguments()[0];
for (int i = 0; i < parentCount; i++) {
result = PsiUtil.skipWhitespacesAndComments(result.getFirstChild(), true);
}
LOG.assertTrue(result instanceof GrParenthesizedExpression);
return (GrExpression) result;
}
}
return newExpr;
}
Aggregations