Search in sources :

Example 1 with Replacer

use of com.intellij.structuralsearch.plugin.replace.impl.Replacer 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 2 with Replacer

use of com.intellij.structuralsearch.plugin.replace.impl.Replacer in project intellij-community by JetBrains.

the class SSBasedInspection method createQuickFix.

private static LocalQuickFix createQuickFix(final Project project, final MatchResult matchResult, final Configuration configuration) {
    if (!(configuration instanceof ReplaceConfiguration))
        return null;
    ReplaceConfiguration replaceConfiguration = (ReplaceConfiguration) configuration;
    final Replacer replacer = new Replacer(project, replaceConfiguration.getOptions());
    final ReplacementInfo replacementInfo = replacer.buildReplacement(matchResult);
    return new LocalQuickFix() {

        @Override
        @NotNull
        public String getName() {
            return SSRBundle.message("SSRInspection.replace.with", replacementInfo.getReplacement());
        }

        @Override
        public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
            PsiElement element = descriptor.getPsiElement();
            if (element != null) {
                replacer.replace(replacementInfo);
            }
        }

        @Override
        @NotNull
        public String getFamilyName() {
            return SSRBundle.message("SSRInspection.family.name");
        }
    };
}
Also used : Project(com.intellij.openapi.project.Project) Replacer(com.intellij.structuralsearch.plugin.replace.impl.Replacer) ReplacementInfo(com.intellij.structuralsearch.plugin.replace.ReplacementInfo) ReplaceConfiguration(com.intellij.structuralsearch.plugin.replace.ui.ReplaceConfiguration) NotNull(org.jetbrains.annotations.NotNull) PsiElement(com.intellij.psi.PsiElement)

Example 3 with Replacer

use of com.intellij.structuralsearch.plugin.replace.impl.Replacer in project intellij-community by JetBrains.

the class StructuralReplaceTestCase method setUp.

@Override
protected void setUp() throws Exception {
    super.setUp();
    StructuralSearchUtil.ourUseUniversalMatchingAlgorithm = false;
    LanguageLevelProjectExtension.getInstance(getProject()).setLanguageLevel(LanguageLevel.JDK_1_4);
    options = new ReplaceOptions();
    options.setMatchOptions(new MatchOptions());
    replacer = new Replacer(getProject(), null);
}
Also used : ReplaceOptions(com.intellij.structuralsearch.plugin.replace.ReplaceOptions) Replacer(com.intellij.structuralsearch.plugin.replace.impl.Replacer)

Aggregations

Replacer (com.intellij.structuralsearch.plugin.replace.impl.Replacer)3 Project (com.intellij.openapi.project.Project)2 ReplaceOptions (com.intellij.structuralsearch.plugin.replace.ReplaceOptions)2 NotNull (org.jetbrains.annotations.NotNull)2 PsiElement (com.intellij.psi.PsiElement)1 PsiExpression (com.intellij.psi.PsiExpression)1 MatchOptions (com.intellij.structuralsearch.MatchOptions)1 ReplacementInfo (com.intellij.structuralsearch.plugin.replace.ReplacementInfo)1 ReplaceConfiguration (com.intellij.structuralsearch.plugin.replace.ui.ReplaceConfiguration)1