use of com.intellij.psi.PsiExpression in project intellij-community by JetBrains.
the class TypeConversionDescriptor method replaceExpression.
@NotNull
public static PsiExpression replaceExpression(@NotNull PsiExpression expression, String stringToReplace, String replaceByString) {
Project project = expression.getProject();
final ReplaceOptions options = new ReplaceOptions();
final MatchOptions matchOptions = options.getMatchOptions();
matchOptions.setFileType(StdFileTypes.JAVA);
final Replacer replacer = new Replacer(project, null);
final String replacement = replacer.testReplace(expression.getText(), stringToReplace, replaceByString, options);
return (PsiExpression) JavaCodeStyleManager.getInstance(project).shortenClassReferences(expression.replace(JavaPsiFacade.getInstance(project).getElementFactory().createExpressionFromText(replacement, expression)));
}
use of com.intellij.psi.PsiExpression in project android by JetBrains.
the class InlinedResource method insertArguments.
@NotNull
private static String insertArguments(@NotNull PsiMethodCallExpression methodCallExpression, @NotNull String s) {
if (s.indexOf('%') == -1) {
return s;
}
final PsiExpression[] args = methodCallExpression.getArgumentList().getExpressions();
if (args.length == 0 || !args[0].isValid()) {
return s;
}
Matcher matcher = FORMAT.matcher(s);
int index = 0;
int prevIndex = 0;
int nextNumber = 1;
int start = 0;
StringBuilder sb = new StringBuilder(2 * s.length());
while (true) {
if (matcher.find(index)) {
if ("%".equals(matcher.group(6))) {
index = matcher.end();
continue;
}
int matchStart = matcher.start();
// Make sure this is not an escaped '%'
for (; prevIndex < matchStart; prevIndex++) {
char c = s.charAt(prevIndex);
if (c == '\\') {
prevIndex++;
}
}
if (prevIndex > matchStart) {
// We're in an escape, ignore this result
index = prevIndex;
continue;
}
index = matcher.end();
// Shouldn't throw a number format exception since we've already
// matched the pattern in the regexp
int number;
String numberString = matcher.group(1);
if (numberString != null) {
// Strip off trailing $
numberString = numberString.substring(0, numberString.length() - 1);
number = Integer.parseInt(numberString);
nextNumber = number + 1;
} else {
number = nextNumber++;
}
if (number > 0 && number < args.length) {
PsiExpression argExpression = args[number];
Object value = JavaConstantExpressionEvaluator.computeConstantExpression(argExpression, false);
if (value == null) {
value = args[number].getText();
}
for (int i = start; i < matchStart; i++) {
sb.append(s.charAt(i));
}
sb.append('{');
sb.append(value);
sb.append('}');
start = index;
}
} else {
for (int i = start, n = s.length(); i < n; i++) {
sb.append(s.charAt(i));
}
break;
}
}
return sb.toString();
}
use of com.intellij.psi.PsiExpression in project intellij-community by JetBrains.
the class ConvertToJBColorConstantQuickFix method applyFix.
@Override
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
final PsiElement element = descriptor.getPsiElement();
final PsiElementFactory factory = JavaPsiFacade.getInstance(project).getElementFactory();
final String jbColorConstant = String.format("%s.%s", JBColor.class.getName(), myConstantName);
final PsiExpression expression = factory.createExpressionFromText(jbColorConstant, element.getContext());
final PsiElement newElement = element.replace(expression);
JavaCodeStyleManager.getInstance(project).shortenClassReferences(newElement);
}
use of com.intellij.psi.PsiExpression in project intellij-community by JetBrains.
the class ConvertToJBColorQuickFix method applyFix.
@Override
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
final PsiElement element = descriptor.getPsiElement();
final PsiElementFactory factory = JavaPsiFacade.getInstance(project).getElementFactory();
final String newJBColor = String.format("new %s(%s, new java.awt.Color())", JBColor.class.getName(), element.getText());
final PsiExpression expression = factory.createExpressionFromText(newJBColor, element.getContext());
final PsiElement newElement = element.replace(expression);
final PsiElement el = JavaCodeStyleManager.getInstance(project).shortenClassReferences(newElement);
final int offset = el.getTextOffset() + el.getText().length() - 2;
final Editor editor = PsiUtilBase.findEditor(el);
if (editor != null) {
editor.getCaretModel().moveToOffset(offset);
}
}
use of com.intellij.psi.PsiExpression in project intellij-community by JetBrains.
the class ChangeToPairCreateQuickFix method applyFix.
@Override
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
PsiElement element = descriptor.getPsiElement();
if (element == null) {
return;
}
String text = element.getText();
String newText = "com.intellij.openapi.util.Pair.create(" + text.substring(text.indexOf('(') + 1);
PsiElementFactory factory = JavaPsiFacade.getInstance(project).getElementFactory();
PsiExpression expression = factory.createExpressionFromText(newText, element.getContext());
PsiElement newElement = element.replace(expression);
JavaCodeStyleManager.getInstance(project).shortenClassReferences(newElement);
}
Aggregations