use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrStringInjection in project intellij-community by JetBrains.
the class GroovySpacingProcessorBasic method createDependentSpacingForClosure.
@NotNull
static Spacing createDependentSpacingForClosure(@NotNull CommonCodeStyleSettings settings, @NotNull GroovyCodeStyleSettings groovySettings, @NotNull GrClosableBlock closure, final boolean forArrow) {
boolean spaceWithinBraces = closure.getParent() instanceof GrStringInjection ? groovySettings.SPACE_WITHIN_GSTRING_INJECTION_BRACES : settings.SPACE_WITHIN_BRACES;
GrStatement[] statements = closure.getStatements();
if (statements.length > 0) {
final PsiElement startElem = forArrow ? statements[0] : closure;
int start = startElem.getTextRange().getStartOffset();
int end = statements[statements.length - 1].getTextRange().getEndOffset();
TextRange range = new TextRange(start, end);
int minSpaces = spaceWithinBraces || forArrow ? 1 : 0;
int maxSpaces = spaceWithinBraces || forArrow ? 1 : 0;
return Spacing.createDependentLFSpacing(minSpaces, maxSpaces, range, settings.KEEP_LINE_BREAKS, settings.KEEP_BLANK_LINES_IN_CODE);
}
return spaceWithinBraces || forArrow ? COMMON_SPACING : NO_SPACING_WITH_NEWLINE;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrStringInjection 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.GrStringInjection 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;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrStringInjection in project intellij-community by JetBrains.
the class ClosureParameterEnhancer method inferType.
@Nullable
public static PsiType inferType(@NotNull GrClosableBlock closure, int index) {
PsiElement parent = closure.getParent();
if (parent instanceof GrStringInjection && index == 0) {
return TypesUtil.createTypeByFQClassName("java.io.StringWriter", closure);
}
if (parent instanceof GrArgumentList)
parent = parent.getParent();
if (!(parent instanceof GrMethodCall)) {
return null;
}
String methodName = findMethodName((GrMethodCall) parent);
GrExpression expression = ((GrMethodCall) parent).getInvokedExpression();
if (!(expression instanceof GrReferenceExpression))
return null;
GrExpression qualifier = ((GrReferenceExpression) expression).getQualifierExpression();
if (qualifier == null)
return null;
PsiType type = qualifier.getType();
if (type == null) {
return null;
}
final PsiParameter[] params = closure.getAllParameters();
if (params.length == 1 && simpleTypes.containsKey(methodName)) {
final String typeText = simpleTypes.get(methodName);
if (typeText.indexOf('<') < 0) {
return TypesUtil.createTypeByFQClassName(typeText, closure);
} else {
return JavaPsiFacade.getElementFactory(closure.getProject()).createTypeFromText(typeText, closure);
}
}
if (iterations.contains(methodName)) {
if (params.length == 1) {
return findTypeForIteration(qualifier, closure);
}
if (params.length == 2 && InheritanceUtil.isInheritor(type, JAVA_UTIL_MAP)) {
if (index == 0) {
return PsiUtil.substituteTypeParameter(type, JAVA_UTIL_MAP, 0, true);
}
return PsiUtil.substituteTypeParameter(type, JAVA_UTIL_MAP, 1, true);
}
} else if (GdkMethodUtil.isWithName(methodName) && params.length == 1) {
return type;
} else if (GdkMethodUtil.EACH_WITH_INDEX.equals(methodName)) {
PsiType res = findTypeForIteration(qualifier, closure);
if (params.length == 2 && res != null) {
if (index == 0) {
return res;
}
return TypesUtil.createTypeByFQClassName(JAVA_LANG_INTEGER, closure);
}
if (InheritanceUtil.isInheritor(type, JAVA_UTIL_MAP)) {
if (params.length == 2) {
if (index == 0) {
return getEntryForMap(type, closure.getProject(), closure.getResolveScope());
}
return TypesUtil.createTypeByFQClassName(JAVA_LANG_INTEGER, closure);
}
if (params.length == 3) {
if (index == 0) {
return PsiUtil.substituteTypeParameter(type, JAVA_UTIL_MAP, 0, true);
}
if (index == 1) {
return PsiUtil.substituteTypeParameter(type, JAVA_UTIL_MAP, 1, true);
}
return TypesUtil.createTypeByFQClassName(JAVA_LANG_INTEGER, closure);
}
}
} else if (GdkMethodUtil.INJECT.equals(methodName) && params.length == 2) {
if (index == 0) {
return TypesUtil.createTypeByFQClassName(JAVA_LANG_OBJECT, closure);
}
PsiType res = findTypeForIteration(qualifier, closure);
if (res != null) {
return res;
}
if (InheritanceUtil.isInheritor(type, JAVA_UTIL_MAP)) {
return getEntryForMap(type, closure.getProject(), closure.getResolveScope());
}
} else if (GdkMethodUtil.EACH_PERMUTATION.equals(methodName) && params.length == 1) {
final PsiType itemType = findTypeForIteration(qualifier, closure);
if (itemType != null) {
return JavaPsiFacade.getElementFactory(closure.getProject()).createTypeFromText(JAVA_UTIL_ARRAY_LIST + "<" + itemType.getCanonicalText() + ">", closure);
}
return TypesUtil.createTypeByFQClassName(JAVA_UTIL_ARRAY_LIST, closure);
} else if (GdkMethodUtil.WITH_DEFAULT.equals(methodName)) {
if (params.length == 1 && InheritanceUtil.isInheritor(type, JAVA_UTIL_MAP)) {
return PsiUtil.substituteTypeParameter(type, JAVA_UTIL_MAP, 0, true);
}
} else if (GdkMethodUtil.SORT.equals(methodName)) {
if (params.length < 3) {
return findTypeForIteration(qualifier, closure);
}
} else if (GdkMethodUtil.WITH_STREAM.equals(methodName)) {
final PsiMethod method = ((GrMethodCall) parent).resolveMethod();
if (method instanceof GrGdkMethod) {
return qualifier.getType();
} else if (method != null) {
final PsiParameter[] parameters = method.getParameterList().getParameters();
if (parameters.length > 0) {
return parameters[0].getType();
}
}
} else if (GdkMethodUtil.WITH_STREAMS.equals(methodName)) {
if (index == 0) {
return TypesUtil.createTypeByFQClassName("java.io.InputStream", closure);
} else if (index == 1)
return TypesUtil.createTypeByFQClassName("java.io.OutputStream", closure);
} else if (GdkMethodUtil.WITH_OBJECT_STREAMS.equals(methodName)) {
if (index == 0) {
return TypesUtil.createTypeByFQClassName("java.io.ObjectInputStream", closure);
} else if (index == 1)
return TypesUtil.createTypeByFQClassName("java.io.ObjectOutputStream", closure);
}
return null;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrStringInjection 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);
}
}
}
}
Aggregations