Improve file sorting for C and C++ projects

The C and C++ languages use header files for defining an API. When
reviewing changes, it is much more friendly to take a look at the API
contracts first, and only then show the actual implementation.

Unfortunately, the default alphabetical order sorts .cpp before .h,
which means that users who use the [ and ] keys for file navigation
are first shown the implementation, and only then the "big picture"
API changes.

The conventions for file extensions vary; it's common to have
.h/.hpp/.hxx and .c/.cpp/.cxx in any combination. For example, most of
the Qt and KDE (sub)projects use .h and .cpp.

Things are a bit more complicated by use of the PIMPL idiom in these
projects where it's desirable to have Foo.h (public header), Foo_p.h
(private implementation header) and Foo.cpp (implementation) to be
shown in this order. That's out of scope for now, though.

Change-Id: Id3bab23f4458320764a0dcd88c32c1a1d1b06075
Inspired-By: Martin Gräßlin <mgraesslin@kde.org>
This commit is contained in:
Jan Kundrát
2015-01-28 19:03:59 +01:00
parent 5b4c65e0ab
commit bb32b9aa30

View File

@@ -43,6 +43,20 @@ public class FileInfo extends JavaScriptObject {
} else if (Patch.COMMIT_MSG.equals(b.path())) {
return 1;
}
// Look at file suffixes to check if it makes sense to use a different order
int s1 = a.path().lastIndexOf('.');
int s2 = b.path().lastIndexOf('.');
if (s1 > 0 && s2 > 0 &&
a.path().substring(0, s1).equals(b.path().substring(0, s2))) {
String suffixA = a.path().substring(s1);
String suffixB = b.path().substring(s2);
// C++ and C: give priority to header files (.h/.hpp/...)
if (suffixA.indexOf(".h") == 0) {
return -1;
} else if (suffixB.indexOf(".h") == 0) {
return 1;
}
}
return a.path().compareTo(b.path());
}
});