IDEA-97632, IDEA-64572 Correctly sort changes in tree view + test

In the tree view subfolders are displayed above files.
This commit is contained in:
Kirill Likhodedov
2012-12-17 19:41:46 +04:00
parent d0c50e3c66
commit 0d422deb6c
3 changed files with 95 additions and 0 deletions
@@ -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<Change> {
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;
@@ -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);
}
}
+1
View File
@@ -4,6 +4,7 @@
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/testSrc" isTestSource="true" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />