use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrString in project intellij-community by JetBrains.
the class GroovyLanguageInjectionSupport method getTopLevelInjectionTarget.
@NotNull
public static PsiElement getTopLevelInjectionTarget(@NotNull final PsiElement host) {
PsiElement target = host;
PsiElement parent = target.getParent();
for (; parent != null; target = parent, parent = target.getParent()) {
if (parent instanceof GrBinaryExpression)
continue;
if (parent instanceof GrString)
continue;
if (parent instanceof GrParenthesizedExpression)
continue;
if (parent instanceof GrConditionalExpression && ((GrConditionalExpression) parent).getCondition() != target)
continue;
if (parent instanceof GrAnnotationArrayInitializer)
continue;
if (parent instanceof GrListOrMap) {
parent = parent.getParent();
continue;
}
break;
}
return target;
}
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 GrConcatenation2InjectorAdapter method computeAnchorAndOperands.
@Override
protected Pair<PsiElement, PsiElement[]> computeAnchorAndOperands(@NotNull PsiElement context) {
PsiElement element = context;
PsiElement parent = context.getParent();
while (parent instanceof GrBinaryExpression && ((GrBinaryExpression) parent).getOperationTokenType() == GroovyTokenTypes.mPLUS || parent instanceof GrAssignmentExpression && ((GrAssignmentExpression) parent).getOperationToken() == GroovyTokenTypes.mPLUS_ASSIGN || parent instanceof GrConditionalExpression && ((GrConditionalExpression) parent).getCondition() != element || parent instanceof GrTypeCastExpression || parent instanceof GrSafeCastExpression || parent instanceof GrParenthesizedExpression || parent instanceof GrString) {
element = parent;
parent = parent.getParent();
}
PsiElement[] operands;
PsiElement anchor;
if (element instanceof GrBinaryExpression) {
operands = collectBinaryOperands(((GrBinaryExpression) element));
anchor = element;
} else if (element instanceof GrString) {
operands = collectGStringOperands((GrString) element);
anchor = element;
} else if (element instanceof GrAssignmentExpression) {
GrExpression rvalue = ((GrAssignmentExpression) element).getRValue();
operands = new PsiElement[] { rvalue == null ? element : rvalue };
anchor = element;
} else {
operands = new PsiElement[] { context };
anchor = context;
}
return Pair.create(anchor, operands);
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrString in project intellij-community by JetBrains.
the class ConvertToRegexIntention method processIntention.
@Override
protected void processIntention(@NotNull PsiElement element, @NotNull Project project, Editor editor) throws IncorrectOperationException {
if (!(element instanceof GrLiteral))
return;
StringBuilder buffer = new StringBuilder();
buffer.append("/");
if (GrStringUtil.isDollarSlashyString(((GrLiteral) element))) {
buffer.append(GrStringUtil.removeQuotes(element.getText()));
} else if (element instanceof GrLiteralImpl) {
Object value = ((GrLiteralImpl) element).getValue();
if (value instanceof String) {
GrStringUtil.escapeSymbolsForSlashyStrings(buffer, (String) value);
} else {
String rawText = GrStringUtil.removeQuotes(element.getText());
unescapeAndAppend(buffer, rawText);
}
} else if (element instanceof GrString) {
for (PsiElement part : ((GrString) element).getAllContentParts()) {
if (part instanceof GrStringContent) {
unescapeAndAppend(buffer, part.getText());
} else if (part instanceof GrStringInjection) {
buffer.append(part.getText());
}
}
}
buffer.append("/");
GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(project);
GrExpression regex = factory.createExpressionFromText(buffer);
//don't use replaceWithExpression since it can revert regex to string if regex brakes syntax
element.replace(regex);
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrString in project intellij-community by JetBrains.
the class RemoveUnnecessaryEscapeCharactersIntention method removeUnnecessaryEscapeSymbols.
private static String removeUnnecessaryEscapeSymbols(final GrLiteral literal) {
final String text = literal.getText();
final String quote = GrStringUtil.getStartQuote(text);
final String value = GrStringUtil.removeQuotes(text);
final StringBuilder buffer = new StringBuilder();
buffer.append(quote);
if (quote == "'") {
GrStringUtil.escapeAndUnescapeSymbols(value, "", "\"$", buffer);
} else if (quote == "'''") {
int position = buffer.length();
GrStringUtil.escapeAndUnescapeSymbols(value, "", "\"'$n", buffer);
GrStringUtil.fixAllTripleQuotes(buffer, position);
} else if (quote == "\"") {
if (literal instanceof GrString) {
final ASTNode node = literal.getNode();
for (ASTNode child = node.getFirstChildNode(); child != null; child = child.getTreeNext()) {
final IElementType type = child.getElementType();
if (type == GroovyTokenTypes.mGSTRING_BEGIN || type == GroovyTokenTypes.mGSTRING_END)
continue;
if (type == GroovyElementTypes.GSTRING_INJECTION) {
buffer.append(child.getText());
} else {
GrStringUtil.escapeAndUnescapeSymbols(child.getText(), "", "'", buffer);
}
}
} else {
GrStringUtil.escapeAndUnescapeSymbols(value, "", "'", buffer);
}
} else if (quote == "\"\"\"") {
if (literal instanceof GrString) {
final ASTNode node = literal.getNode();
for (ASTNode child = node.getFirstChildNode(); child != null; child = child.getTreeNext()) {
final IElementType type = child.getElementType();
if (type == GroovyTokenTypes.mGSTRING_BEGIN || type == GroovyTokenTypes.mGSTRING_END)
continue;
if (type == GroovyElementTypes.GSTRING_INJECTION) {
buffer.append(child.getText());
} else {
final int position = buffer.length();
GrStringUtil.escapeAndUnescapeSymbols(child.getText(), "", "\"'n", buffer);
GrStringUtil.fixAllTripleDoubleQuotes(buffer, position);
}
}
} else {
final int position = buffer.length();
GrStringUtil.escapeAndUnescapeSymbols(value, "", "\"'n", buffer);
GrStringUtil.fixAllTripleDoubleQuotes(buffer, position);
}
} else {
return text;
}
buffer.append(quote);
return buffer.toString();
}
Aggregations