diff --git a/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/patch/ApplyPatchDifferentiatedDialog.java b/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/patch/ApplyPatchDifferentiatedDialog.java index e32434c67177..afa5384b8557 100644 --- a/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/patch/ApplyPatchDifferentiatedDialog.java +++ b/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/patch/ApplyPatchDifferentiatedDialog.java @@ -19,6 +19,7 @@ import com.intellij.icons.AllIcons; import com.intellij.ide.util.PropertiesComponent; import com.intellij.openapi.Disposable; import com.intellij.openapi.actionSystem.*; +import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.diff.impl.patch.PatchReader; import com.intellij.openapi.diff.impl.patch.PatchSyntaxException; import com.intellij.openapi.diff.impl.patch.PatchVirtualFileReader; @@ -35,12 +36,12 @@ import com.intellij.openapi.ui.popup.JBPopupFactory; import com.intellij.openapi.ui.popup.PopupStep; import com.intellij.openapi.ui.popup.util.BaseListPopupStep; import com.intellij.openapi.util.*; +import com.intellij.openapi.util.io.FileUtil; import com.intellij.openapi.util.text.StringUtil; import com.intellij.openapi.vcs.ObjectsConvertor; import com.intellij.openapi.vcs.VcsBundle; import com.intellij.openapi.vcs.changes.Change; import com.intellij.openapi.vcs.changes.ChangeListManager; -import com.intellij.openapi.vcs.changes.CommitContext; import com.intellij.openapi.vcs.changes.LocalChangeList; import com.intellij.openapi.vcs.changes.actions.DiffRequestPresentable; import com.intellij.openapi.vcs.changes.actions.ShowDiffAction; @@ -52,6 +53,7 @@ import com.intellij.ui.SimpleColoredComponent; import com.intellij.ui.SimpleTextAttributes; import com.intellij.util.Alarm; import com.intellij.util.NullableConsumer; +import com.intellij.util.containers.ContainerUtil; import com.intellij.util.containers.Convertor; import com.intellij.util.containers.MultiMap; import com.intellij.util.ui.UIUtil; @@ -78,7 +80,6 @@ public class ApplyPatchDifferentiatedDialog extends DialogWrapper { private final MyChangeTreeList myChangesTreeList; private JComponent myCenterPanel; - private JComponent mySouthPanel; private final Project myProject; private final AtomicReference myRecentPathFileChange; @@ -93,9 +94,8 @@ public class ApplyPatchDifferentiatedDialog extends DialogWrapper { private boolean myContainBasedChanges; private JLabel myPatchFileLabel; private PatchReader myReader; - private CommitContext myCommitContext; private VirtualFileAdapter myListener; - private boolean myCanChangePatchFile; + private final boolean myCanChangePatchFile; private String myHelpId = "reference.dialogs.vcs.patch.apply"; public ApplyPatchDifferentiatedDialog(final Project project, final ApplyPatchExecutor callback, final List executors, @@ -103,14 +103,22 @@ public class ApplyPatchDifferentiatedDialog extends DialogWrapper { this(project, callback, executors, applyPatchMode, patchFile, null, null); } - public ApplyPatchDifferentiatedDialog(final Project project, final ApplyPatchExecutor callback, final List executors, - @NotNull final ApplyPatchMode applyPatchMode, @NotNull final List patches, @Nullable final LocalChangeList defaultList) { + public ApplyPatchDifferentiatedDialog(final Project project, + final ApplyPatchExecutor callback, + final List executors, + @NotNull final ApplyPatchMode applyPatchMode, + @NotNull final List patches, + @Nullable final LocalChangeList defaultList) { this(project, callback, executors, applyPatchMode, null, patches, defaultList); } - private ApplyPatchDifferentiatedDialog(final Project project, final ApplyPatchExecutor callback, final List executors, - @NotNull final ApplyPatchMode applyPatchMode, @Nullable final VirtualFile patchFile, @Nullable final List patches, - @Nullable final LocalChangeList defaultList) { + private ApplyPatchDifferentiatedDialog(final Project project, + final ApplyPatchExecutor callback, + final List executors, + @NotNull final ApplyPatchMode applyPatchMode, + @Nullable final VirtualFile patchFile, + @Nullable final List patches, + @Nullable final LocalChangeList defaultList) { super(project, true); myCallback = callback; myExecutors = executors; @@ -124,22 +132,23 @@ public class ApplyPatchDifferentiatedDialog extends DialogWrapper { myPatches = new LinkedList(); myRecentPathFileChange = new AtomicReference(); myChangesTreeList = new MyChangeTreeList(project, Collections.emptyList(), - new Runnable() { - public void run() { - final NamedTrinity includedTrinity = new NamedTrinity(); - final Collection includedChanges = myChangesTreeList.getIncludedChanges(); - final Set> set = new HashSet>(); - for (FilePatchInProgress.PatchChange change : includedChanges) { - final TextFilePatch patch = change.getPatchInProgress().getPatch(); - final Couple pair = Couple.of(patch.getBeforeName(), patch.getAfterName()); - if (set.contains(pair)) continue; - set.add(pair); - acceptChange(includedTrinity, change); - } - myInfoCalculator.setIncluded(includedTrinity); - myCommitLegendPanel.update(); - } - }, new MyChangeNodeDecorator()); + new Runnable() { + public void run() { + final NamedTrinity includedTrinity = new NamedTrinity(); + final Collection includedChanges = + myChangesTreeList.getIncludedChanges(); + final Set> set = new HashSet>(); + for (FilePatchInProgress.PatchChange change : includedChanges) { + final TextFilePatch patch = change.getPatchInProgress().getPatch(); + final Couple pair = Couple.of(patch.getBeforeName(), patch.getAfterName()); + if (set.contains(pair)) continue; + set.add(pair); + acceptChange(includedTrinity, change); + } + myInfoCalculator.setIncluded(includedTrinity); + myCommitLegendPanel.update(); + } + }, new MyChangeNodeDecorator()); myChangesTreeList.setDoubleClickHandler(new Runnable() { @Override public void run() { @@ -183,7 +192,8 @@ public class ApplyPatchDifferentiatedDialog extends DialogWrapper { if (patchFile != null && patchFile.isValid()) { init(patchFile); - } else if (patches != null) { + } + else if (patches != null) { init(patches, defaultList); } @@ -195,7 +205,7 @@ public class ApplyPatchDifferentiatedDialog extends DialogWrapper { @Override public void contentsChanged(@NotNull VirtualFileEvent event) { if (myRecentPathFileChange.get() != null && myRecentPathFileChange.get().getVf() != null && - myRecentPathFileChange.get().getVf().equals(event.getFile())) { + event.getFile().equals(myRecentPathFileChange.get().getVf())) { queueRequest(); } } @@ -216,17 +226,16 @@ public class ApplyPatchDifferentiatedDialog extends DialogWrapper { myLoadQueue.queue(myUpdater); } - private void init(List patches, final LocalChangeList localChangeList) { - final List matchedPathes = new MatchPatchPaths(myProject).execute(patches); - - SwingUtilities.invokeLater(new Runnable() { + private void init(@NotNull List patches, final LocalChangeList localChangeList) { + final List matchedPatches = new MatchPatchPaths(myProject).execute(patches); + ApplicationManager.getApplication().invokeLater(new Runnable() { public void run() { if (localChangeList != null) { myChangeListChooser.setDefaultSelection(localChangeList); } myPatches.clear(); - myPatches.addAll(matchedPathes); + myPatches.addAll(matchedPatches); updateTree(true); } }); @@ -273,7 +282,9 @@ public class ApplyPatchDifferentiatedDialog extends DialogWrapper { patchGroups.putValue(patchInProgress.getBase(), patchInProgress); } final LocalChangeList selected = getSelectedChangeList(); - executor.apply(patchGroups, selected, myRecentPathFileChange.get() == null ? null : myRecentPathFileChange.get().getVf().getName(), + FilePresentation presentation = myRecentPathFileChange.get(); + VirtualFile vf = presentation != null ? presentation.getVf() : null; + executor.apply(patchGroups, selected, vf == null ? null : vf.getName(), myReader == null ? null : myReader.getAdditionalInfo(ApplyPatchDefaultExecutor.pathsFromGroups(patchGroups))); } @@ -292,7 +303,7 @@ public class ApplyPatchDifferentiatedDialog extends DialogWrapper { myRecentPathFileChange.set(new FilePresentation(myPatchFile.getText())); } - public void init(final VirtualFile patchFile) { + private void init(final VirtualFile patchFile) { myPatchFile.setText(patchFile.getPresentableUrl()); myRecentPathFileChange.set(new FilePresentation(patchFile)); queueRequest(); @@ -306,7 +317,7 @@ public class ApplyPatchDifferentiatedDialog extends DialogWrapper { public void run() { final FilePresentation filePresentation = myRecentPathFileChange.get(); if ((filePresentation == null) || (filePresentation.getVf() == null)) { - SwingUtilities.invokeLater(myReset); + ApplicationManager.getApplication().invokeLater(myReset); return; } final VirtualFile file = filePresentation.getVf(); @@ -314,14 +325,15 @@ public class ApplyPatchDifferentiatedDialog extends DialogWrapper { final PatchReader patchReader = loadPatches(filePresentation); if (patchReader == null) return; - final List matchedPathes = patchReader == null ? Collections.emptyList() : - new MatchPatchPaths(myProject).execute(patchReader.getPatches()); + List textPatches = patchReader.getPatches(); + final List matchedPatches = + textPatches != null ? new MatchPatchPaths(myProject).execute(textPatches) : ContainerUtil.emptyList(); - SwingUtilities.invokeLater(new Runnable() { + ApplicationManager.getApplication().invokeLater(new Runnable() { public void run() { myChangeListChooser.setDefaultName(file.getNameWithoutExtension().replace('_', ' ').trim()); myPatches.clear(); - myPatches.addAll(matchedPathes); + myPatches.addAll(matchedPatches); myReader = patchReader; updateTree(true); paintBusy(false); @@ -331,10 +343,11 @@ public class ApplyPatchDifferentiatedDialog extends DialogWrapper { } @Nullable - private PatchReader loadPatches(final FilePresentation filePresentation) { + private static PatchReader loadPatches(final FilePresentation filePresentation) { final VirtualFile patchFile = filePresentation.getVf(); + if (patchFile == null) return null; patchFile.refresh(false, false); - if (! patchFile.isValid()) { + if (!patchFile.isValid()) { return null; } @@ -375,11 +388,11 @@ public class ApplyPatchDifferentiatedDialog extends DialogWrapper { return myVf; } final VirtualFile file = LocalFileSystem.getInstance().refreshAndFindFileByPath(myPath); - return (file != null) && (! file.isDirectory()) ? file : null; + return (file != null) && (!file.isDirectory()) ? file : null; } } - public void reset() { + private void reset() { myPatches.clear(); myChangesTreeList.setChangesToDisplay(Collections.emptyList()); myChangesTreeList.repaint(); @@ -399,7 +412,7 @@ public class ApplyPatchDifferentiatedDialog extends DialogWrapper { myCenterPanel.add(myPatchFileLabel, gb); gb.fill = GridBagConstraints.HORIZONTAL; - ++ gb.gridy; + ++gb.gridy; myCenterPanel.add(myPatchFile, gb); final DefaultActionGroup group = new DefaultActionGroup(); @@ -425,22 +438,22 @@ public class ApplyPatchDifferentiatedDialog extends DialogWrapper { } final ActionToolbar toolbar = ActionManager.getInstance().createActionToolbar("APPLY_PATCH", group, true); - ++ gb.gridy; + ++gb.gridy; gb.fill = GridBagConstraints.HORIZONTAL; myCenterPanel.add(toolbar.getComponent(), gb); - ++ gb.gridy; + ++gb.gridy; gb.weighty = 1; gb.fill = GridBagConstraints.BOTH; myCenterPanel.add(myChangesTreeList, gb); - ++ gb.gridy; + ++gb.gridy; gb.weighty = 0; gb.fill = GridBagConstraints.NONE; gb.insets.bottom = UIUtil.DEFAULT_VGAP; myCenterPanel.add(myCommitLegendPanel.getComponent(), gb); - ++ gb.gridy; + ++gb.gridy; gb.fill = GridBagConstraints.HORIZONTAL; myCenterPanel.add(myChangeListChooser, gb); } @@ -450,8 +463,9 @@ public class ApplyPatchDifferentiatedDialog extends DialogWrapper { private void paintBusy(final boolean requestPut) { if (requestPut) { myChangesTreeList.setPaintBusy(true); - } else { - myChangesTreeList.setPaintBusy(! myLoadQueue.isEmpty()); + } + else { + myChangesTreeList.setPaintBusy(!myLoadQueue.isEmpty()); } } @@ -479,7 +493,7 @@ public class ApplyPatchDifferentiatedDialog extends DialogWrapper { final List under = node.getAllChangesUnder(); return ObjectsConvertor.convert(under, new Convertor() { public FilePatchInProgress.PatchChange convert(Change o) { - return (FilePatchInProgress.PatchChange) o; + return (FilePatchInProgress.PatchChange)o; } }); } @@ -488,7 +502,7 @@ public class ApplyPatchDifferentiatedDialog extends DialogWrapper { protected FilePatchInProgress.PatchChange getLeadSelectedObject(ChangesBrowserNode node) { final Object o = node.getUserObject(); if (o instanceof FilePatchInProgress.PatchChange) { - return (FilePatchInProgress.PatchChange) o; + return (FilePatchInProgress.PatchChange)o; } return null; } @@ -511,7 +525,8 @@ public class ApplyPatchDifferentiatedDialog extends DialogWrapper { final List autoBases = patch.getAutoBasesCopy(); if (autoBases.isEmpty() || (autoBases.size() == 1 && autoBases.get(0).equals(patch.getBase()))) { myNewBaseSelector.run(); - } else { + } + else { autoBases.add(null); final MapPopup step = new MapPopup(autoBases, myNewBaseSelector); JBPopupFactory.getInstance().createListPopup(step).showCenteredInCurrentWindow(myProject); @@ -526,13 +541,14 @@ public class ApplyPatchDifferentiatedDialog extends DialogWrapper { } } - private boolean sameBase(final List selectedChanges) { + private static boolean sameBase(final List selectedChanges) { VirtualFile base = null; for (FilePatchInProgress.PatchChange change : selectedChanges) { final VirtualFile changeBase = change.getPatchInProgress().getBase(); if (base == null) { base = changeBase; - } else if (! base.equals(changeBase)) { + } + else if (!base.equals(changeBase)) { return false; } } @@ -550,7 +566,7 @@ public class ApplyPatchDifferentiatedDialog extends DialogWrapper { myChangesTreeList.expandAll(); } myChangesTreeList.repaint(); - if ((! doInitCheck) && patchesToSelect != null) { + if ((!doInitCheck) && patchesToSelect != null) { final List toSelect = new ArrayList(patchesToSelect.size()); for (FilePatchInProgress.PatchChange change : changes) { if (patchesToSelect.contains(change.getPatchInProgress())) { @@ -571,20 +587,22 @@ public class ApplyPatchDifferentiatedDialog extends DialogWrapper { private List getAllChanges() { return ObjectsConvertor.convert(myPatches, - new Convertor() { - public FilePatchInProgress.PatchChange convert(FilePatchInProgress o) { - return o.getChange(); - } - }); + new Convertor() { + public FilePatchInProgress.PatchChange convert(FilePatchInProgress o) { + return o.getChange(); + } + }); } - private void acceptChange(final NamedTrinity trinity, final FilePatchInProgress.PatchChange change) { + private static void acceptChange(final NamedTrinity trinity, final FilePatchInProgress.PatchChange change) { final FilePatchInProgress patchInProgress = change.getPatchInProgress(); if (FilePatchStatus.ADDED.equals(patchInProgress.getStatus())) { trinity.plusAdded(); - } else if (FilePatchStatus.DELETED.equals(patchInProgress.getStatus())) { + } + else if (FilePatchStatus.DELETED.equals(patchInProgress.getStatus())) { trinity.plusDeleted(); - } else { + } + else { trinity.plusModified(); } } @@ -603,7 +621,8 @@ public class ApplyPatchDifferentiatedDialog extends DialogWrapper { included.add(change); } } - } else { + } + else { // todo maybe written pretty final Collection includedNow = myChangesTreeList.getIncludedChanges(); final Set toBeIncluded = new HashSet(); @@ -645,7 +664,7 @@ public class ApplyPatchDifferentiatedDialog extends DialogWrapper { } } - private List changes2patches(final List selectedChanges) { + private static List changes2patches(final List selectedChanges) { return ObjectsConvertor.convert(selectedChanges, new Convertor() { public FilePatchInProgress convert(FilePatchInProgress.PatchChange o) { return o.getPatchInProgress(); @@ -701,22 +720,16 @@ public class ApplyPatchDifferentiatedDialog extends DialogWrapper { myDeleted = 0; } - public NamedTrinity(int added, int modified, int deleted) { - myAdded = added; - myModified = modified; - myDeleted = deleted; - } - public void plusAdded() { - ++ myAdded; + ++myAdded; } public void plusModified() { - ++ myModified; + ++myModified; } public void plusDeleted() { - ++ myDeleted; + ++myDeleted; } public int getAdded() { @@ -777,13 +790,13 @@ public class ApplyPatchDifferentiatedDialog extends DialogWrapper { private static class MyChangeNodeDecorator implements ChangeNodeDecorator { public void decorate(Change change, SimpleColoredComponent component, boolean isShowFlatten) { if (change instanceof FilePatchInProgress.PatchChange) { - final FilePatchInProgress.PatchChange patchChange = (FilePatchInProgress.PatchChange) change; - if (! isShowFlatten) { - // add change subpath + final FilePatchInProgress.PatchChange patchChange = (FilePatchInProgress.PatchChange)change; + if (!isShowFlatten) { + // add change sub path final TextFilePatch filePatch = patchChange.getPatchInProgress().getPatch(); final String patchPath = filePatch.getAfterName() == null ? filePatch.getBeforeName() : filePatch.getAfterName(); component.append(" "); - component.append("["+ patchPath + "]", SimpleTextAttributes.GRAY_ATTRIBUTES); + component.append("[" + patchPath + "]", SimpleTextAttributes.GRAY_ATTRIBUTES); } if (patchChange.getPatchInProgress().getCurrentStrip() > 0) { component.append(" stripped " + patchChange.getPatchInProgress().getCurrentStrip(), SimpleTextAttributes.GRAY_ITALIC_ATTRIBUTES); @@ -791,9 +804,11 @@ public class ApplyPatchDifferentiatedDialog extends DialogWrapper { final String text; if (FilePatchStatus.ADDED.equals(patchChange.getPatchInProgress().getStatus())) { text = "(Added)"; - } else if (FilePatchStatus.DELETED.equals(patchChange.getPatchInProgress().getStatus())) { + } + else if (FilePatchStatus.DELETED.equals(patchChange.getPatchInProgress().getStatus())) { text = "(Deleted)"; - } else { + } + else { text = "(Modified)"; } component.append(" "); @@ -803,7 +818,7 @@ public class ApplyPatchDifferentiatedDialog extends DialogWrapper { public List> stressPartsOfFileName(final Change change, final String parentPath) { if (change instanceof FilePatchInProgress.PatchChange) { - final FilePatchInProgress.PatchChange patchChange = (FilePatchInProgress.PatchChange) change; + final FilePatchInProgress.PatchChange patchChange = (FilePatchInProgress.PatchChange)change; final String basePath = patchChange.getPatchInProgress().getBase().getPath(); final String basePathCorrected = basePath.trim().replace('/', File.separatorChar); if (parentPath.startsWith(basePathCorrected)) { @@ -818,7 +833,7 @@ public class ApplyPatchDifferentiatedDialog extends DialogWrapper { } } - public Collection getIncluded() { + private Collection getIncluded() { return ObjectsConvertor.convert(myChangesTreeList.getIncludedChanges(), new Convertor() { public FilePatchInProgress convert(FilePatchInProgress.PatchChange o) { @@ -827,7 +842,7 @@ public class ApplyPatchDifferentiatedDialog extends DialogWrapper { }); } - public LocalChangeList getSelectedChangeList() { + private LocalChangeList getSelectedChangeList() { return myChangeListChooser.getSelectedList(myProject); } @@ -864,7 +879,7 @@ public class ApplyPatchDifferentiatedDialog extends DialogWrapper { @Override public void actionPerformed(AnActionEvent e) { - if (! isEnabled()) return; + if (!isEnabled()) return; final List selectedChanges = myChangesTreeList.getSelectedChanges(); for (FilePatchInProgress.PatchChange change : selectedChanges) { change.getPatchInProgress().down(); @@ -876,7 +891,7 @@ public class ApplyPatchDifferentiatedDialog extends DialogWrapper { final List selectedChanges = myChangesTreeList.getSelectedChanges(); if (selectedChanges.isEmpty()) return false; for (FilePatchInProgress.PatchChange change : selectedChanges) { - if (! change.getPatchInProgress().canDown()) return false; + if (!change.getPatchInProgress().canDown()) return false; } return true; } @@ -894,7 +909,7 @@ public class ApplyPatchDifferentiatedDialog extends DialogWrapper { @Override public void actionPerformed(AnActionEvent e) { - if (! isEnabled()) return; + if (!isEnabled()) return; final List selectedChanges = myChangesTreeList.getSelectedChanges(); for (FilePatchInProgress.PatchChange change : selectedChanges) { change.getPatchInProgress().up(); @@ -906,7 +921,7 @@ public class ApplyPatchDifferentiatedDialog extends DialogWrapper { final List selectedChanges = myChangesTreeList.getSelectedChanges(); if (selectedChanges.isEmpty()) return false; for (FilePatchInProgress.PatchChange change : selectedChanges) { - if (! change.getPatchInProgress().canUp()) return false; + if (!change.getPatchInProgress().canUp()) return false; } return true; } @@ -929,13 +944,14 @@ public class ApplyPatchDifferentiatedDialog extends DialogWrapper { private class MyShowDiff extends AnAction { private final MyChangeComparator myMyChangeComparator; + private MyShowDiff() { super("Show Diff", "Show Diff", AllIcons.Actions.Diff); myMyChangeComparator = new MyChangeComparator(); } public void update(AnActionEvent e) { - e.getPresentation().setEnabled((! myPatches.isEmpty()) && myContainBasedChanges); + e.getPresentation().setEnabled((!myPatches.isEmpty()) && myContainBasedChanges); } public void actionPerformed(AnActionEvent e) { @@ -944,21 +960,21 @@ public class ApplyPatchDifferentiatedDialog extends DialogWrapper { private void showDiff() { if (ChangeListManager.getInstance(myProject).isFreezedWithNotification(null)) return; - if (myPatches.isEmpty() || (! myContainBasedChanges)) return; + if (myPatches.isEmpty() || (!myContainBasedChanges)) return; final List changes = getAllChanges(); Collections.sort(changes, myMyChangeComparator); List selectedChanges = myChangesTreeList.getSelectedChanges(); int selectedIdx = 0; - final ArrayList diffRequestPresentables = new ArrayList(changes.size()); + final ArrayList diffRequestPresentableList = new ArrayList(changes.size()); if (selectedChanges.isEmpty()) { selectedChanges = changes; } - if (! selectedChanges.isEmpty()) { + if (!selectedChanges.isEmpty()) { final FilePatchInProgress.PatchChange c = selectedChanges.get(0); for (FilePatchInProgress.PatchChange change : changes) { final FilePatchInProgress patchInProgress = change.getPatchInProgress(); - if (! patchInProgress.baseExistsOrAdded()) continue; + if (!patchInProgress.baseExistsOrAdded()) continue; final TextFilePatch patch = patchInProgress.getPatch(); final String path = patch.getBeforeName() == null ? patch.getAfterName() : patch.getBeforeName(); final DiffRequestPresentable diffRequestPresentable = @@ -969,15 +985,15 @@ public class ApplyPatchDifferentiatedDialog extends DialogWrapper { } }); if (diffRequestPresentable != null) { - diffRequestPresentables.add(diffRequestPresentable); + diffRequestPresentableList.add(diffRequestPresentable); } if (change.equals(c)) { - selectedIdx = diffRequestPresentables.size() - 1; + selectedIdx = diffRequestPresentableList.size() - 1; } } } - if (diffRequestPresentables.isEmpty()) return; - ShowDiffAction.showDiffImpl(myProject, diffRequestPresentables, selectedIdx, new ShowDiffUIContext(false)); + if (diffRequestPresentableList.isEmpty()) return; + ShowDiffAction.showDiffImpl(myProject, diffRequestPresentableList, selectedIdx, new ShowDiffUIContext(false)); } } @@ -986,7 +1002,7 @@ public class ApplyPatchDifferentiatedDialog extends DialogWrapper { if (PropertiesComponent.getInstance(myProject).isTrueValue("ChangesBrowser.SHOW_FLATTEN")) { return o1.getPatchInProgress().getIoCurrentBase().getName().compareTo(o2.getPatchInProgress().getIoCurrentBase().getName()); } - return o1.getPatchInProgress().getIoCurrentBase().compareTo(o2.getPatchInProgress().getIoCurrentBase()); + return FileUtil.compareFiles(o1.getPatchInProgress().getIoCurrentBase(), o2.getPatchInProgress().getIoCurrentBase()); } } } diff --git a/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/patch/MatchPatchPaths.java b/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/patch/MatchPatchPaths.java index f8de6c33641f..9d269ccd4d73 100644 --- a/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/patch/MatchPatchPaths.java +++ b/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/patch/MatchPatchPaths.java @@ -23,6 +23,7 @@ import com.intellij.openapi.util.Computable; import com.intellij.openapi.util.Pair; import com.intellij.openapi.vcs.ObjectsConvertor; import com.intellij.openapi.vfs.VfsUtil; +import com.intellij.openapi.vfs.VfsUtilCore; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.util.containers.Convertor; import com.intellij.util.containers.MultiMap; @@ -35,14 +36,8 @@ import java.util.Collection; import java.util.Iterator; import java.util.List; -/** - * Created with IntelliJ IDEA. - * User: Irina.Chernushina - * Date: 10/25/12 - * Time: 5:51 PM - */ public class MatchPatchPaths { - private final int ourBigFileBound = 100000; + private static final int BIG_FILE_BOUND = 100000; private final Project myProject; private final VirtualFile myBaseDir; @@ -51,7 +46,7 @@ public class MatchPatchPaths { myBaseDir = myProject.getBaseDir(); } - public List execute(final List list) { + public List execute(@NotNull final List list) { final PatchBaseDirectoryDetector directoryDetector = PatchBaseDirectoryDetector.getInstance(myProject); final List candidates = new ArrayList(list.size()); @@ -75,7 +70,7 @@ public class MatchPatchPaths { for (TextFilePatch patch : newOrWithoutMatches) { final String[] strings = patch.getAfterName().replace('\\', '/').split("/"); Pair best = null; - for (int i = strings.length - 2; i >= 0; -- i) { + for (int i = strings.length - 2; i >= 0; --i) { final String name = strings[i]; final Collection files = findFilesFromIndex(directoryDetector, name); if (! files.isEmpty()) { @@ -98,16 +93,16 @@ public class MatchPatchPaths { patchInProgress.up(); } result.putValue(best.getFirst(), patchInProgress); - } else { + } + else { final FilePatchInProgress patchInProgress = new FilePatchInProgress(patch, null, myBaseDir); result.putValue(myBaseDir, patchInProgress); } } } - private void selectByContext(List candidates, MultiMap result) { - for (Iterator iterator = candidates.iterator(); iterator.hasNext(); ) { - final PatchAndVariants candidate = iterator.next(); + private static void selectByContext(List candidates, MultiMap result) { + for (final PatchAndVariants candidate : candidates) { int maxLines = -100; FilePatchInProgress best = null; for (FilePatchInProgress variant : candidate.getVariants()) { @@ -121,14 +116,15 @@ public class MatchPatchPaths { } } - private void filterExactMatches(List candidates, MultiMap result) { + private static void filterExactMatches(List candidates, MultiMap result) { for (Iterator iterator = candidates.iterator(); iterator.hasNext(); ) { final PatchAndVariants candidate = iterator.next(); if (candidate.getVariants().size() == 1) { final FilePatchInProgress oneCandidate = candidate.getVariants().get(0); result.putValue(oneCandidate.getBase(), oneCandidate); iterator.remove(); - } else { + } + else { final List exact = new ArrayList(candidate.getVariants().size()); for (FilePatchInProgress patch : candidate.getVariants()) { if (patch.getCurrentStrip() == 0) { @@ -139,7 +135,8 @@ public class MatchPatchPaths { final FilePatchInProgress patchInProgress = exact.get(0); putSelected(result, candidate.getVariants(), patchInProgress); iterator.remove(); - } else if (! exact.isEmpty()) { + } + else if (!exact.isEmpty()) { candidate.getVariants().retainAll(exact); } } @@ -165,7 +162,8 @@ public class MatchPatchPaths { } if (files.isEmpty()) { newOrWithoutMatches.add(patch); - } else { + } + else { final List variants = ObjectsConvertor.convert(files, new Convertor() { @Override public FilePatchInProgress convert(VirtualFile o) { @@ -174,8 +172,9 @@ public class MatchPatchPaths { }, ObjectsConvertor.NOT_NULL); if (variants.isEmpty()) { newOrWithoutMatches.add(patch); // just to be sure - } else { - candidates.add(new PatchAndVariants(patch, variants)); + } + else { + candidates.add(new PatchAndVariants(variants)); } } } @@ -190,9 +189,9 @@ public class MatchPatchPaths { }); } - private void putSelected(MultiMap result, - final List variants, - FilePatchInProgress patchInProgress) { + private static void putSelected(MultiMap result, + final List variants, + FilePatchInProgress patchInProgress) { patchInProgress.setAutoBases(ObjectsConvertor.convert(variants, new Convertor() { @Override public VirtualFile convert(FilePatchInProgress o) { @@ -202,16 +201,17 @@ public class MatchPatchPaths { result.putValue(patchInProgress.getBase(), patchInProgress); } - private int getMatchingLines(final FilePatchInProgress patch) { + private static int getMatchingLines(final FilePatchInProgress patch) { final VirtualFile base = patch.getCurrentBase(); if (base == null) return -1; String text; try { - if (base.getLength() > ourBigFileBound) { + if (base.getLength() > BIG_FILE_BOUND) { // partially - text = VfsUtil.loadText(base, ourBigFileBound); - } else { - text = VfsUtil.loadText(base); + text = VfsUtilCore.loadText(base, BIG_FILE_BOUND); + } + else { + text = VfsUtilCore.loadText(base); } } catch (IOException e) { @@ -221,38 +221,30 @@ public class MatchPatchPaths { } private static class PatchAndVariants { - private final TextFilePatch myPatch; private final List myVariants; - private PatchAndVariants(TextFilePatch patch, List variants) { - myPatch = patch; + private PatchAndVariants(List variants) { myVariants = variants; } - public TextFilePatch getPatch() { - return myPatch; - } - public List getVariants() { return myVariants; } } - private Pair compareNames(final String beforeName, final VirtualFile file) { + private static Pair compareNames(final String beforeName, final VirtualFile file) { if (beforeName == null) return null; final String[] parts = beforeName.replace('\\', '/').split("/"); return compareNamesImpl(parts, file.getParent(), parts.length - 2); } - private Pair compareNamesImpl(String[] parts, VirtualFile parent, int idx) { - VirtualFile previous = parent; + private static Pair compareNamesImpl(String[] parts, VirtualFile parent, int idx) { while ((parent != null) && (idx >= 0)) { - if (! parent.getName().equals(parts[idx])) { + if (!parent.getName().equals(parts[idx])) { return new Pair(parent, idx + 1); } - previous = parent; parent = parent.getParent(); - -- idx; + --idx; } return new Pair(parent, idx + 1); }