Apply special file sorting to gr-comment-list

Files in the file list are sorted specially so that header files appear
before their corresponding source files. This change applies this same
sorting algorithm to the comment list.

In order to achieve this, the special file sort function was exposed
publicly -- removed from gr-rest-api-interface and put in a new file,
gr-file-path-behavior.

Bug: Issue 4586
Change-Id: I31079dcef0c3c2421b2561cedf69f68aaf653af4
This commit is contained in:
Kasper Nilsson
2016-09-23 11:59:03 -07:00
parent 6c0b685cd7
commit 028d382a5c
7 changed files with 92 additions and 50 deletions

View File

@@ -0,0 +1,61 @@
<!--
Copyright (C) 2016 The Android Open Source Project
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.
-->
<script>
(function(window) {
'use strict';
/** @polymerBehavior Gerrit.PathListBehavior */
var PathListBehavior = {
specialFilePathCompare: function(a, b) {
var COMMIT_MESSAGE_PATH = '/COMMIT_MSG';
// The commit message always goes first.
if (a === COMMIT_MESSAGE_PATH) {
return -1;
}
if (b === COMMIT_MESSAGE_PATH) {
return 1;
}
var aLastDotIndex = a.lastIndexOf('.');
var aExt = a.substr(aLastDotIndex + 1);
var aFile = a.substr(0, aLastDotIndex);
var bLastDotIndex = b.lastIndexOf('.');
var bExt = b.substr(bLastDotIndex + 1);
var bFile = b.substr(0, bLastDotIndex);
// Sort header files above others with the same base name.
var headerExts = ['h', 'hxx', 'hpp'];
if (aFile.length > 0 && aFile === bFile) {
if (headerExts.indexOf(aExt) !== -1 &&
headerExts.indexOf(bExt) !== -1) {
return a.localeCompare(b);
}
if (headerExts.indexOf(aExt) !== -1) {
return -1;
}
if (headerExts.indexOf(bExt) !== -1) {
return 1;
}
}
return aFile.localeCompare(bFile) || a.localeCompare(b);
},
};
window.Gerrit = window.Gerrit || {};
window.Gerrit.PathListBehavior = PathListBehavior;
})(window);
</script>

View File

@@ -14,6 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
-->
<link rel="import" href="../../../behaviors/gr-path-list-behavior.html">
<link rel="import" href="../../../bower_components/polymer/polymer.html">
<link rel="import" href="../../shared/gr-linked-text/gr-linked-text.html">

View File

@@ -16,6 +16,7 @@
Polymer({
is: 'gr-comment-list',
behaviors: [Gerrit.PathListBehavior],
properties: {
changeNum: Number,
@@ -25,7 +26,8 @@
},
_computeFilesFromComments: function(comments) {
return Object.keys(comments || {}).sort();
var arr = Object.keys(comments || {});
return arr.sort(this.specialFilePathCompare);
},
_computeFileDiffURL: function(file, changeNum, patchNum) {
@@ -56,6 +58,6 @@
return 'PS' + comment.patch_set + ', ';
}
return '';
}
},
});
})();

View File

@@ -36,9 +36,21 @@ limitations under the License.
element = fixture('basic');
});
test('_computeFilesFromComments', function() {
var comments = {'file_b.html': [], 'file_c.css': [], 'file_a.js': []};
var expected = ['file_a.js', 'file_b.html', 'file_c.css'];
test('_computeFilesFromComments w/ special file path sorting', function() {
var comments = {
'file_b.html': [],
'file_c.css': [],
'file_a.js': [],
'test.cc': [],
'test.h': [],
};
var expected = [
'file_a.js',
'file_b.html',
'file_c.css',
'test.h',
'test.cc'
];
var actual = element._computeFilesFromComments(comments);
assert.deepEqual(actual, expected);

View File

@@ -14,6 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
-->
<link rel="import" href="../../../behaviors/gr-path-list-behavior.html">
<link rel="import" href="../../../bower_components/polymer/polymer.html">
<script src="../../../bower_components/es6-promise/dist/es6-promise.min.js"></script>
<script src="../../../bower_components/fetch/fetch.js"></script>

View File

@@ -67,6 +67,7 @@
Polymer({
is: 'gr-rest-api-interface',
behaviors: [Gerrit.PathListBehavior],
/**
* Fired when an server error occurs.
@@ -395,13 +396,12 @@
getChangeFilePathsAsSpeciallySortedArray: function(changeNum, patchRange) {
return this.getChangeFiles(changeNum, patchRange).then(function(files) {
return Object.keys(files).sort(this._specialFilePathCompare.bind(this));
return Object.keys(files).sort(this.specialFilePathCompare);
}.bind(this));
},
_normalizeChangeFilesResponse: function(response) {
var paths = Object.keys(response).sort(
this._specialFilePathCompare.bind(this));
var paths = Object.keys(response).sort(this.specialFilePathCompare);
var files = [];
for (var i = 0; i < paths.length; i++) {
var info = response[paths[i]];
@@ -413,41 +413,6 @@
return files;
},
_specialFilePathCompare: function(a, b) {
var COMMIT_MESSAGE_PATH = '/COMMIT_MSG';
// The commit message always goes first.
if (a === COMMIT_MESSAGE_PATH) {
return -1;
}
if (b === COMMIT_MESSAGE_PATH) {
return 1;
}
var aLastDotIndex = a.lastIndexOf('.');
var aExt = a.substr(aLastDotIndex + 1);
var aFile = a.substr(0, aLastDotIndex);
var bLastDotIndex = b.lastIndexOf('.');
var bExt = b.substr(bLastDotIndex + 1);
var bFile = b.substr(0, bLastDotIndex);
// Sort header files above others with the same base name.
var headerExts = ['h', 'hxx', 'hpp'];
if (aFile.length > 0 && aFile === bFile) {
if (headerExts.indexOf(aExt) !== -1 &&
headerExts.indexOf(bExt) !== -1) {
return a.localeCompare(b);
}
if (headerExts.indexOf(aExt) !== -1) {
return -1;
}
if (headerExts.indexOf(bExt) !== -1) {
return 1;
}
}
return aFile.localeCompare(bFile) || a.localeCompare(b);
},
getChangeRevisionActions: function(changeNum, patchNum) {
return this.fetchJSON(
this.getChangeActionURL(changeNum, patchNum, '/actions')).then(

View File

@@ -215,27 +215,27 @@ limitations under the License.
test('special file path sorting', function() {
assert.deepEqual(
['.b', '/COMMIT_MSG', '.a', 'file'].sort(
element._specialFilePathCompare),
element.specialFilePathCompare),
['/COMMIT_MSG', '.a', '.b', 'file']);
assert.deepEqual(
['.b', '/COMMIT_MSG', 'foo/bar/baz.cc', 'foo/bar/baz.h'].sort(
element._specialFilePathCompare),
element.specialFilePathCompare),
['/COMMIT_MSG', '.b', 'foo/bar/baz.h', 'foo/bar/baz.cc']);
assert.deepEqual(
['.b', '/COMMIT_MSG', 'foo/bar/baz.cc', 'foo/bar/baz.hpp'].sort(
element._specialFilePathCompare),
element.specialFilePathCompare),
['/COMMIT_MSG', '.b', 'foo/bar/baz.hpp', 'foo/bar/baz.cc']);
assert.deepEqual(
['.b', '/COMMIT_MSG', 'foo/bar/baz.cc', 'foo/bar/baz.hxx'].sort(
element._specialFilePathCompare),
element.specialFilePathCompare),
['/COMMIT_MSG', '.b', 'foo/bar/baz.hxx', 'foo/bar/baz.cc']);
assert.deepEqual(
['foo/bar.h', 'foo/bar.hxx', 'foo/bar.hpp'].sort(
element._specialFilePathCompare),
element.specialFilePathCompare),
['foo/bar.h', 'foo/bar.hpp', 'foo/bar.hxx']);
// Regression test for Issue 4448.
@@ -245,7 +245,7 @@ limitations under the License.
'minidump/minidump_thread_writer.cc',
'minidump/minidump_thread_writer.h',
]
.sort(element._specialFilePathCompare),
.sort(element.specialFilePathCompare),
[
'minidump/minidump_memory_writer.h',
'minidump/minidump_memory_writer.cc',
@@ -258,7 +258,7 @@ limitations under the License.
'task_test.go',
'task.go',
]
.sort(element._specialFilePathCompare),
.sort(element.specialFilePathCompare),
[
'task.go',
'task_test.go',