From 0d422deb6c6422462740439c1bf7bb12a5b4cbb2 Mon Sep 17 00:00:00 2001 From: Kirill Likhodedov Date: Mon, 17 Dec 2012 19:40:22 +0400 Subject: [PATCH] IDEA-97632, IDEA-64572 Correctly sort changes in tree view + test In the tree view subfolders are displayed above files. --- .../vcs/changes/ui/ChangesComparator.java | 8 ++ .../vcs/changes/ui/ChangesComparatorTest.java | 86 +++++++++++++++++++ platform/vcs-impl/vcs-impl.iml | 1 + 3 files changed, 95 insertions(+) create mode 100644 platform/vcs-impl/testSrc/com/intellij/openapi/vcs/changes/ui/ChangesComparatorTest.java diff --git a/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/ui/ChangesComparator.java b/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/ui/ChangesComparator.java index 1a7bf9b55f51..955419970dc3 100644 --- a/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/ui/ChangesComparator.java +++ b/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/ui/ChangesComparator.java @@ -16,6 +16,7 @@ package com.intellij.openapi.vcs.changes.ui; import com.intellij.openapi.util.SystemInfo; +import com.intellij.openapi.util.io.FileUtil; import com.intellij.openapi.util.io.FileUtilRt; import com.intellij.openapi.util.text.StringUtil; import com.intellij.openapi.vcs.FilePath; @@ -51,6 +52,13 @@ public class ChangesComparator implements Comparator { final String parentPath1 = lastSlash1 >= 0 && !filePath1.isDirectory() ? path1.substring(0, lastSlash1) : path1; final int lastSlash2 = path2.lastIndexOf('/'); final String parentPath2 = lastSlash2 >= 0 && !filePath2.isDirectory() ? path2.substring(0, lastSlash2) : path2; + // subdirs precede files + if (FileUtil.isAncestor(parentPath2, parentPath1, true)) { + return -1; + } + else if (FileUtil.isAncestor(parentPath1, parentPath2, true)) { + return 1; + } final int compare = StringUtil.compare(parentPath1, parentPath2, !SystemInfo.isFileSystemCaseSensitive); if (compare != 0) { return compare; diff --git a/platform/vcs-impl/testSrc/com/intellij/openapi/vcs/changes/ui/ChangesComparatorTest.java b/platform/vcs-impl/testSrc/com/intellij/openapi/vcs/changes/ui/ChangesComparatorTest.java new file mode 100644 index 000000000000..e7e3321e694a --- /dev/null +++ b/platform/vcs-impl/testSrc/com/intellij/openapi/vcs/changes/ui/ChangesComparatorTest.java @@ -0,0 +1,86 @@ +/* + * Copyright 2000-2012 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.intellij.openapi.vcs.changes.ui; + +import com.intellij.openapi.vcs.FilePathImpl; +import com.intellij.openapi.vcs.changes.Change; +import com.intellij.openapi.vcs.changes.ContentRevision; +import com.intellij.openapi.vcs.history.VcsRevisionNumber; +import com.intellij.testFramework.vcs.MockContentRevision; +import org.jetbrains.annotations.NotNull; +import org.junit.Test; + +import java.io.File; + +import static junit.framework.Assert.assertEquals; + +/** + * Testing only tree-view, because the list view is obvious. + * + * @author Kirill Likhodedov + */ +public class ChangesComparatorTest { + + @Test + public void testInOneDirectory() throws Exception { + assertEquals(-1, compare("~/project/A.java", "~/project/B.java")); + assertEquals(1, compare("~/project/Z.java", "~/project/B.java")); + } + + @Test + public void testInDifferentDirs() throws Exception { + assertEquals(-1, compare("~/project/aaa/A.java", "~/project/bbb/B.java")); + assertEquals(-1, compare("~/project/aaa/B.java", "~/project/zzz/A.java")); + assertEquals(1, compare("~/project/zzz/A.java", "~/project/aaa/B.java")); + } + + @Test + public void testInSubDir() throws Exception { + // all folders precede plain files + assertEquals("Folders should precede plain files", -1, compare("~/project/dir/subdir/A.java", "~/project/dir/B.java")); + assertEquals("Folders should precede plain files", -1, compare("~/project/dir/A.java", "~/project/B.java")); + assertEquals("Folders should precede plain files", 1, compare("~/project/B.java", "~/project/dir/subdir/A.java")); + } + + @Test + public void testEqualPaths() throws Exception { + assertEquals("Equal paths should compare to 0", 0, compare("~/project/A.java", "~/project/A.java")); + assertEquals("Equal paths should compare to 0", 0, compare("~/project/aaa/A.java", "~/project/aaa/A.java")); + } + + private static int compare(String path1, String path2) throws Exception { + return compare(change(path1), change(path2)); + } + + private static int compare(Change c1, Change c2) { + int result = ChangesComparator.getInstance(false).compare(c1, c2); + if (result > 0) { + return 1; + } + if (result < 0) { + return -1; + } + return 0; + } + + @NotNull + private static Change change(@NotNull String path) throws Exception { + ContentRevision before = new MockContentRevision(new FilePathImpl(new File(path), false), VcsRevisionNumber.NULL); + ContentRevision after = new MockContentRevision(new FilePathImpl(new File(path), false), VcsRevisionNumber.NULL); + return new Change(before, after); + } + +} diff --git a/platform/vcs-impl/vcs-impl.iml b/platform/vcs-impl/vcs-impl.iml index 3a025bc97617..15884fde770c 100644 --- a/platform/vcs-impl/vcs-impl.iml +++ b/platform/vcs-impl/vcs-impl.iml @@ -4,6 +4,7 @@ +