Search in sources :

Example 1 with ReplacementInfo

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

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

the class ReplaceUsageViewContext method doReplace.

private void doReplace() {
    List<Usage> infos = myUsageView.getSortedUsages();
    List<ReplacementInfo> results = new ArrayList<>(infos.size());
    for (final Usage info : infos) {
        UsageInfo2UsageAdapter usage = (UsageInfo2UsageAdapter) info;
        if (isValid(usage)) {
            results.add(usage2ReplacementInfo.get(usage));
        }
    }
    getReplacer().replaceAll(results);
}
Also used : Usage(com.intellij.usages.Usage) UsageInfo2UsageAdapter(com.intellij.usages.UsageInfo2UsageAdapter) ArrayList(java.util.ArrayList) ReplacementInfo(com.intellij.structuralsearch.plugin.replace.ReplacementInfo)

Example 3 with ReplacementInfo

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

the class Replacer method replaceAll.

public void replaceAll(final List<ReplacementInfo> resultPtrList) {
    PsiElement lastAffectedElement = null;
    PsiElement currentAffectedElement;
    for (ReplacementInfo info : resultPtrList) {
        PsiElement element = info.getMatch(0);
        initContextAndHandler(element);
        if (replaceHandler != null) {
            replaceHandler.prepare(info);
        }
    }
    for (final ReplacementInfo aResultPtrList : resultPtrList) {
        currentAffectedElement = doReplace(aResultPtrList);
        if (currentAffectedElement != lastAffectedElement) {
            if (lastAffectedElement != null)
                reformatAndPostProcess(lastAffectedElement);
            lastAffectedElement = currentAffectedElement;
        }
    }
    reformatAndPostProcess(lastAffectedElement);
}
Also used : ReplacementInfo(com.intellij.structuralsearch.plugin.replace.ReplacementInfo)

Example 4 with ReplacementInfo

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

the class Replacer method testReplace.

public String testReplace(String in, String what, String by, ReplaceOptions options, boolean filePattern, boolean createPhysicalFile, FileType sourceFileType, Language sourceDialect) {
    this.options = options;
    final MatchOptions matchOptions = this.options.getMatchOptions();
    matchOptions.setSearchPattern(what);
    this.options.setReplacement(by);
    replacementBuilder = null;
    context = null;
    replaceHandler = null;
    matchOptions.clearVariableConstraints();
    MatcherImplUtil.transform(matchOptions);
    final StructuralSearchProfile profile = StructuralSearchUtil.getProfileByFileType(matchOptions.getFileType());
    assert profile != null;
    profile.checkSearchPattern(project, matchOptions);
    checkSupportedReplacementPattern(project, options);
    Matcher matcher = new Matcher(project);
    try {
        PsiElement firstElement, lastElement, parent;
        if (options.getMatchOptions().getScope() == null) {
            PsiElement[] elements = MatcherImplUtil.createTreeFromText(in, filePattern ? PatternTreeContext.File : PatternTreeContext.Block, sourceFileType, sourceDialect, null, project, createPhysicalFile);
            firstElement = elements[0];
            lastElement = elements[elements.length - 1];
            parent = firstElement.getParent();
            matchOptions.setScope(new LocalSearchScope(parent));
        } else {
            parent = ((LocalSearchScope) options.getMatchOptions().getScope()).getScope()[0];
            firstElement = parent.getFirstChild();
            lastElement = parent.getLastChild();
        }
        matchOptions.setResultIsContextMatch(true);
        CollectingMatchResultSink sink = new CollectingMatchResultSink();
        matcher.testFindMatches(sink, matchOptions);
        final List<ReplacementInfo> resultPtrList = new ArrayList<>();
        for (final MatchResult result : sink.getMatches()) {
            resultPtrList.add(buildReplacement(result));
        }
        sink.getMatches().clear();
        int startOffset = firstElement.getTextRange().getStartOffset();
        int endOffset = filePattern ? 0 : parent.getTextLength() - (lastElement.getTextRange().getEndOffset());
        // get nodes from text may contain
        PsiElement prevSibling = firstElement.getPrevSibling();
        if (prevSibling instanceof PsiWhiteSpace) {
            startOffset -= prevSibling.getTextLength() - 1;
        }
        PsiElement nextSibling = lastElement.getNextSibling();
        if (nextSibling instanceof PsiWhiteSpace) {
            endOffset -= nextSibling.getTextLength() - 1;
        }
        replaceAll(resultPtrList);
        String result = parent.getText();
        result = result.substring(startOffset);
        result = result.substring(0, result.length() - endOffset);
        return result;
    } catch (Exception e) {
        throw new IncorrectOperationException(e);
    } finally {
        options.getMatchOptions().setScope(null);
    }
}
Also used : LocalSearchScope(com.intellij.psi.search.LocalSearchScope) ReplacementInfo(com.intellij.structuralsearch.plugin.replace.ReplacementInfo) IncorrectOperationException(com.intellij.util.IncorrectOperationException) CollectingMatchResultSink(com.intellij.structuralsearch.plugin.util.CollectingMatchResultSink) IncorrectOperationException(com.intellij.util.IncorrectOperationException)

Example 5 with ReplacementInfo

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

the class ReplaceUsageViewContext method replaceOne.

private void replaceOne(UsageInfo2UsageAdapter info, boolean doConfirm) {
    ReplacementInfo replacementInfo = usage2ReplacementInfo.get(info);
    boolean approved;
    if (doConfirm) {
        ReplacementPreviewDialog wrapper = new ReplacementPreviewDialog(mySearchContext.getProject(), info.getUsageInfo(), replacementInfo.getReplacement());
        approved = wrapper.showAndGet();
    } else {
        approved = true;
    }
    if (approved) {
        ensureFileWritable(info);
        myUsageView.removeUsage(info);
        getReplacer().replace(replacementInfo);
        if (myUsageView.getUsagesCount() == 0) {
            myUsageView.close();
        }
    }
}
Also used : ReplacementInfo(com.intellij.structuralsearch.plugin.replace.ReplacementInfo)

Aggregations

ReplacementInfo (com.intellij.structuralsearch.plugin.replace.ReplacementInfo)5 Project (com.intellij.openapi.project.Project)1 PsiElement (com.intellij.psi.PsiElement)1 LocalSearchScope (com.intellij.psi.search.LocalSearchScope)1 Replacer (com.intellij.structuralsearch.plugin.replace.impl.Replacer)1 ReplaceConfiguration (com.intellij.structuralsearch.plugin.replace.ui.ReplaceConfiguration)1 CollectingMatchResultSink (com.intellij.structuralsearch.plugin.util.CollectingMatchResultSink)1 Usage (com.intellij.usages.Usage)1 UsageInfo2UsageAdapter (com.intellij.usages.UsageInfo2UsageAdapter)1 IncorrectOperationException (com.intellij.util.IncorrectOperationException)1 ArrayList (java.util.ArrayList)1 NotNull (org.jetbrains.annotations.NotNull)1