Search in sources :

Example 26 with PsiExpression

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)));
}
Also used : Project(com.intellij.openapi.project.Project) PsiExpression(com.intellij.psi.PsiExpression) MatchOptions(com.intellij.structuralsearch.MatchOptions) ReplaceOptions(com.intellij.structuralsearch.plugin.replace.ReplaceOptions) Replacer(com.intellij.structuralsearch.plugin.replace.impl.Replacer) NotNull(org.jetbrains.annotations.NotNull)

Example 27 with PsiExpression

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();
}
Also used : PsiExpression(com.intellij.psi.PsiExpression) Matcher(java.util.regex.Matcher) NotNull(org.jetbrains.annotations.NotNull)

Example 28 with PsiExpression

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);
}
Also used : PsiExpression(com.intellij.psi.PsiExpression) PsiElementFactory(com.intellij.psi.PsiElementFactory) JBColor(com.intellij.ui.JBColor) PsiElement(com.intellij.psi.PsiElement)

Example 29 with PsiExpression

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);
    }
}
Also used : PsiExpression(com.intellij.psi.PsiExpression) PsiElementFactory(com.intellij.psi.PsiElementFactory) JBColor(com.intellij.ui.JBColor) Editor(com.intellij.openapi.editor.Editor) PsiElement(com.intellij.psi.PsiElement)

Example 30 with PsiExpression

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);
}
Also used : PsiExpression(com.intellij.psi.PsiExpression) PsiElementFactory(com.intellij.psi.PsiElementFactory) PsiElement(com.intellij.psi.PsiElement)

Aggregations

PsiExpression (com.intellij.psi.PsiExpression)72 PsiElement (com.intellij.psi.PsiElement)28 PsiReferenceExpression (com.intellij.psi.PsiReferenceExpression)13 PsiMethodCallExpression (com.intellij.psi.PsiMethodCallExpression)11 Nullable (org.jetbrains.annotations.Nullable)9 PsiReference (com.intellij.psi.PsiReference)8 PsiType (com.intellij.psi.PsiType)8 NonNls (org.jetbrains.annotations.NonNls)8 PsiClass (com.intellij.psi.PsiClass)7 PsiAssignmentExpression (com.intellij.psi.PsiAssignmentExpression)6 PsiExpressionStatement (com.intellij.psi.PsiExpressionStatement)6 PsiLocalVariable (com.intellij.psi.PsiLocalVariable)6 PsiMethod (com.intellij.psi.PsiMethod)6 PsiNewExpression (com.intellij.psi.PsiNewExpression)6 PsiPolyadicExpression (com.intellij.psi.PsiPolyadicExpression)6 PsiStatement (com.intellij.psi.PsiStatement)6 Project (com.intellij.openapi.project.Project)5 PsiBinaryExpression (com.intellij.psi.PsiBinaryExpression)5 PsiDeclarationStatement (com.intellij.psi.PsiDeclarationStatement)5 PsiElementFactory (com.intellij.psi.PsiElementFactory)5