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");
}
};
}
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);
}
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);
}
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);
}
}
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();
}
}
}
Aggregations