private void addReplaceActions(final ReplaceContext replaceContext) { final Runnable replaceRunnable = new Runnable() { @Override public void run() { replaceUsagesUnderCommand(replaceContext, replaceContext.getUsageView().getUsages()); } }; replaceContext .getUsageView() .addButtonToLowerPane(replaceRunnable, FindBundle.message("find.replace.all.action")); final Runnable replaceSelectedRunnable = new Runnable() { @Override public void run() { replaceUsagesUnderCommand( replaceContext, replaceContext.getUsageView().getSelectedUsages()); } }; replaceContext .getUsageView() .addButtonToLowerPane( replaceSelectedRunnable, FindBundle.message("find.replace.selected.action")); }
private void addReplaceActions(final ReplaceContext replaceContext) { final Runnable replaceRunnable = new Runnable() { @Override public void run() { final UsageView usageView = replaceContext.getUsageView(); final boolean success = doReplace(replaceContext, usageView.getUsages()); closeUsageViewIfEmpty(usageView, success); } }; replaceContext .getUsageView() .addButtonToLowerPane(replaceRunnable, FindBundle.message("find.replace.all.action")); final Runnable replaceSelectedRunnable = new Runnable() { @Override public void run() { doReplaceSelected(replaceContext); } }; replaceContext .getUsageView() .addButtonToLowerPane( replaceSelectedRunnable, FindBundle.message("find.replace.selected.action")); }
private void doReplaceSelected(final ReplaceContext replaceContext) { final Set<Usage> selectedUsages = replaceContext.getUsageView().getSelectedUsages(); if (selectedUsages == null) { return; } Set<VirtualFile> readOnlyFiles = null; for (final Usage usage : selectedUsages) { final VirtualFile file = ((UsageInFile) usage).getFile(); if (file != null && !file.isWritable()) { if (readOnlyFiles == null) readOnlyFiles = new HashSet<VirtualFile>(); readOnlyFiles.add(file); } } if (readOnlyFiles != null) { ReadonlyStatusHandler.getInstance(myProject) .ensureFilesWritable(VfsUtilCore.toVirtualFileArray(readOnlyFiles)); } if (hasReadOnlyUsages(selectedUsages)) { int result = Messages.showOkCancelDialog( replaceContext.getUsageView().getComponent(), FindBundle.message("find.replace.occurrences.in.read.only.files.prompt"), FindBundle.message("find.replace.occurrences.in.read.only.files.title"), Messages.getWarningIcon()); if (result != 0) { return; } } CommandProcessor.getInstance() .executeCommand( myProject, new Runnable() { @Override public void run() { final boolean success = doReplace(replaceContext, selectedUsages); final UsageView usageView = replaceContext.getUsageView(); if (closeUsageViewIfEmpty(usageView, success)) return; usageView.getComponent().requestFocus(); } }, FindBundle.message("find.replace.command"), null); }
private boolean ensureUsagesWritable( ReplaceContext replaceContext, Collection<Usage> selectedUsages) { Set<VirtualFile> readOnlyFiles = null; for (final Usage usage : selectedUsages) { final VirtualFile file = ((UsageInFile) usage).getFile(); if (file != null && !file.isWritable()) { if (readOnlyFiles == null) readOnlyFiles = new HashSet<VirtualFile>(); readOnlyFiles.add(file); } } if (readOnlyFiles != null) { ReadonlyStatusHandler.getInstance(myProject) .ensureFilesWritable(VfsUtilCore.toVirtualFileArray(readOnlyFiles)); } if (hasReadOnlyUsages(selectedUsages)) { int result = Messages.showOkCancelDialog( replaceContext.getUsageView().getComponent(), FindBundle.message("find.replace.occurrences.in.read.only.files.prompt"), FindBundle.message("find.replace.occurrences.in.read.only.files.title"), Messages.getWarningIcon()); if (result != Messages.OK) { return false; } } return true; }
private boolean doReplace(Usage usage, ReplaceContext replaceContext) { try { doReplace(usage, replaceContext.getFindModel(), replaceContext.getExcludedSet(), false); replaceContext.getUsageView().removeUsage(usage); } catch (FindManager.MalformedReplacementStringException e) { markAsMalformedReplacement(replaceContext, usage); return false; } return true; }
private boolean doReplace(final ReplaceContext replaceContext, Collection<Usage> usages) { boolean success = true; int replacedCount = 0; for (final Usage usage : usages) { try { doReplace(usage, replaceContext.getFindModel(), replaceContext.getExcludedSet(), false); replaceContext.getUsageView().removeUsage(usage); replacedCount++; } catch (FindManager.MalformedReplacementStringException e) { markAsMalformedReplacement(replaceContext, usage); success = false; } } reportNumberReplacedOccurrences(myProject, replacedCount); return success; }
private boolean replaceUsages( @NotNull ReplaceContext replaceContext, @NotNull Collection<Usage> usages) { if (!ensureUsagesWritable(replaceContext, usages)) { return true; } int replacedCount = 0; boolean success = true; for (final Usage usage : usages) { try { if (replaceUsage( usage, replaceContext.getFindModel(), replaceContext.getExcludedSetCached(), false)) { replacedCount++; } } catch (FindManager.MalformedReplacementStringException e) { markAsMalformedReplacement(replaceContext, usage); success = false; } } replaceContext.getUsageView().removeUsagesBulk(usages); reportNumberReplacedOccurrences(myProject, replacedCount); return success; }
private static void markAsMalformedReplacement(ReplaceContext replaceContext, Usage usage) { replaceContext.getUsageView().excludeUsages(new Usage[] {usage}); }
private void replaceWithPrompt(final ReplaceContext replaceContext) { final List<Usage> _usages = replaceContext.getUsageView().getSortedUsages(); if (hasReadOnlyUsages(_usages)) { WindowManager.getInstance() .getStatusBar(myProject) .setInfo(FindBundle.message("find.replace.occurrences.found.in.read.only.files.status")); return; } final Usage[] usages = _usages.toArray(new Usage[_usages.size()]); // usageView.expandAll(); for (int i = 0; i < usages.length; ++i) { final Usage usage = usages[i]; final UsageInfo usageInfo = ((UsageInfo2UsageAdapter) usage).getUsageInfo(); final PsiElement elt = usageInfo.getElement(); if (elt == null) continue; final PsiFile psiFile = elt.getContainingFile(); if (!psiFile.isWritable()) continue; Runnable selectOnEditorRunnable = new Runnable() { @Override public void run() { final VirtualFile virtualFile = psiFile.getVirtualFile(); if (virtualFile != null && ApplicationManager.getApplication() .runReadAction( new Computable<Boolean>() { @Override public Boolean compute() { return virtualFile.isValid() ? Boolean.TRUE : Boolean.FALSE; } }) .booleanValue()) { if (usage.isValid()) { usage.highlightInEditor(); replaceContext.getUsageView().selectUsages(new Usage[] {usage}); } } } }; CommandProcessor.getInstance() .executeCommand( myProject, selectOnEditorRunnable, FindBundle.message("find.replace.select.on.editor.command"), null); String title = FindBundle.message("find.replace.found.usage.title", i + 1, usages.length); int result; try { replaceUsage( usage, replaceContext.getFindModel(), replaceContext.getExcludedSetCached(), true); result = FindManager.getInstance(myProject) .showPromptDialog(replaceContext.getFindModel(), title); } catch (FindManager.MalformedReplacementStringException e) { markAsMalformedReplacement(replaceContext, usage); result = FindManager.getInstance(myProject) .showMalformedReplacementPrompt(replaceContext.getFindModel(), title, e); } if (result == FindManager.PromptResult.CANCEL) { return; } if (result == FindManager.PromptResult.SKIP) { continue; } final int currentNumber = i; if (result == FindManager.PromptResult.OK) { final Ref<Boolean> success = Ref.create(); Runnable runnable = new Runnable() { @Override public void run() { success.set(replaceUsageAndRemoveFromView(usage, replaceContext)); } }; CommandProcessor.getInstance() .executeCommand(myProject, runnable, FindBundle.message("find.replace.command"), null); if (closeUsageViewIfEmpty(replaceContext.getUsageView(), success.get())) { return; } } if (result == FindManager.PromptResult.ALL_IN_THIS_FILE) { final int[] nextNumber = new int[1]; Runnable runnable = new Runnable() { @Override public void run() { int j = currentNumber; boolean success = true; for (; j < usages.length; j++) { final Usage usage = usages[j]; final UsageInfo usageInfo = ((UsageInfo2UsageAdapter) usage).getUsageInfo(); final PsiElement elt = usageInfo.getElement(); if (elt == null) continue; PsiFile otherPsiFile = elt.getContainingFile(); if (!otherPsiFile.equals(psiFile)) { break; } if (!replaceUsageAndRemoveFromView(usage, replaceContext)) { success = false; } } closeUsageViewIfEmpty(replaceContext.getUsageView(), success); nextNumber[0] = j; } }; CommandProcessor.getInstance() .executeCommand(myProject, runnable, FindBundle.message("find.replace.command"), null); //noinspection AssignmentToForLoopParameter i = nextNumber[0] - 1; } if (result == FindManager.PromptResult.ALL_FILES) { CommandProcessor.getInstance() .executeCommand( myProject, new Runnable() { @Override public void run() { final boolean success = replaceUsages(replaceContext, _usages); closeUsageViewIfEmpty(replaceContext.getUsageView(), success); } }, FindBundle.message("find.replace.command"), null); break; } } }