Web Inspector: Improve Quick Open sorting algorithm
authorcommit-queue@webkit.org <commit-queue@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Mon, 24 Oct 2016 21:48:13 +0000 (21:48 +0000)
committercommit-queue@webkit.org <commit-queue@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Mon, 24 Oct 2016 21:48:13 +0000 (21:48 +0000)
https://bugs.webkit.org/show_bug.cgi?id=163705

Patch by Devin Rousso <dcrousso+webkit@gmail.com> on 2016-10-24
Reviewed by Joseph Pecoraro.

Source/WebInspectorUI:

* UserInterface/Models/ResourceQueryResult.js:
(WebInspector.ResourceQueryResult.prototype._calculateRank.getMultiplier):
(WebInspector.ResourceQueryResult.prototype._calculateRank):
Added logic to multiply the ranking increment/decrement based on whether the current match
is part of a sequence, whether that sequence began with a special character, and the length
of the current sequence.

LayoutTests:

* inspector/unit-tests/resource-query-controller-expected.txt:
* inspector/unit-tests/resource-query-controller.html:
Changed the ExecuteQueryGeneralRankings and ExecuteQueryPositionRankings tests to reflect
the new ranking logic.  Added ExecuteQueryConsecutiveRankings to test more realistic
scenarios and ensure better ranking.

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@207782 268f45cc-cd09-0410-ab3c-d52691b4dbfc

LayoutTests/ChangeLog
LayoutTests/inspector/unit-tests/resource-query-controller-expected.txt
LayoutTests/inspector/unit-tests/resource-query-controller.html
Source/WebInspectorUI/ChangeLog
Source/WebInspectorUI/UserInterface/Models/ResourceQueryResult.js

index f6658cd..9573281 100644 (file)
@@ -1,3 +1,16 @@
+2016-10-24  Devin Rousso  <dcrousso+webkit@gmail.com>
+
+        Web Inspector: Improve Quick Open sorting algorithm
+        https://bugs.webkit.org/show_bug.cgi?id=163705
+
+        Reviewed by Joseph Pecoraro.
+
+        * inspector/unit-tests/resource-query-controller-expected.txt:
+        * inspector/unit-tests/resource-query-controller.html:
+        Changed the ExecuteQueryGeneralRankings and ExecuteQueryPositionRankings tests to reflect
+        the new ranking logic.  Added ExecuteQueryConsecutiveRankings to test more realistic
+        scenarios and ensure better ranking.
+
 2016-10-24  Ryan Haddad  <ryanhaddad@apple.com>
 
         Rebaseline tests for ios-simulator after r207757.
index 2e67cdf..880eb31 100644 (file)
@@ -208,6 +208,9 @@ PASS: Results should be ranked by descending relevancy.
 -- Running test case: ExecuteQueryPositionRankings
 PASS: Results should be ranked by descending relevancy.
 
+-- Running test case: ExecuteQueryConsecutiveRankings
+PASS: Results should be ranked by descending relevancy.
+
 -- Running test case: GetMatchingTextRanges
 PASS: Result TextRanges should match the expected ranges.
 PASS: Result TextRanges should match the expected ranges.
index c877395..db3a3ab 100644 (file)
@@ -10,7 +10,7 @@ function test()
     suite.addTestCase({
         name: "FindSpecialCharacterIndices",
         description: "Should correctly find special filename characters.",
-        test: function() {
+        test() {
             let matcher = new WebInspector.ResourceQueryController;
             let tests = [
                 {
@@ -69,7 +69,7 @@ function test()
     suite.addTestCase({
         name: "ExecuteQueryAgainstNoResources",
         description: "Should return no results if no resources were added to the controller.",
-        test: function() {
+        test() {
             let matcher = new WebInspector.ResourceQueryController;
             let results = matcher.executeQuery("abcde");
             InspectorTest.expectThat(!results.length, "Should return no results.")
@@ -79,7 +79,7 @@ function test()
     suite.addTestCase({
         name: "ExecuteWhitespaceQueryOrEmptyQuery",
         description: "Empty queries and queries containing only whitespace should return no results.",
-        test: function() {
+        test() {
             const whitespaceCharacters = " \t\r\n";
             let matcher = new WebInspector.ResourceQueryController;
             matcher.addResource(new WebInspector.Resource("abcde"));
@@ -97,7 +97,7 @@ function test()
     suite.addTestCase({
         name: "ExecuteQueryContainingWhitespace",
         description: "Whitespace within a query should be ignored.",
-        test: function() {
+        test() {
             let matcher = new WebInspector.ResourceQueryController;
             matcher.addResource(new WebInspector.Resource("abcde"));
 
@@ -111,7 +111,7 @@ function test()
     suite.addTestCase({
         name: "ExecuteQueryMatchNone",
         description: "Should not find a match.",
-        test: function() {
+        test() {
             let matcher = new WebInspector.ResourceQueryController;
             let tests = [
                 {query: "abcde", filename: "abcd"},
@@ -131,7 +131,7 @@ function test()
     suite.addTestCase({
         name: "ExecuteQueryMatchesExpectedCharacters",
         description: "Should match the expected query characters.",
-        test: function() {
+        test() {
             let matcher = new WebInspector.ResourceQueryController;
             let tests = [
                 {
@@ -222,7 +222,7 @@ function test()
     suite.addTestCase({
         name: "ExecuteQueryShouldIgnoreCase",
         description: "Queries that differ only in case should return identical results.",
-        test: function() {
+        test() {
             let matcher = new WebInspector.ResourceQueryController;
             let tests = [
                 {
@@ -270,8 +270,8 @@ function test()
     suite.addTestCase({
         name: "ExecuteQueryGeneralRankings",
         description: "Check that query results are ranked by descending relevance.",
-        test: function() {
-            let filenames = ["AxBxCxDxEx", "AbCdE", "AbcDe", "AxbcDe", "AxbxcDe", "abcde", "xabcde"]; // Filenames in order of descending rank.
+        test() {
+            let filenames = ["AbCdE", "AbcDe", "abcde", "AxbcDe", "AxBxCxDxEx", "AxbxcDe", "xabcde"]; // Filenames in order of descending rank.
             let matcher = new WebInspector.ResourceQueryController;
             filenames.forEach((filename) => matcher.addResource(new WebInspector.Resource(filename)));
 
@@ -285,8 +285,8 @@ function test()
     suite.addTestCase({
         name: "ExecuteQueryPositionRankings",
         description: "Check that matches close to the beginning of the filename rank higher.",
-        test: function() {
-            let filenames = ["BxCxDx", "AxBxCxDx", "xxxAxxxBxxxCxxxD", "bcd", "abcd"]; // Filenames in order of descending rank.
+        test() {
+            let filenames = ["bcd", "BxCxDx", "AxBxCxDx", "abcd", "xxxAxxxBxxxCxxxD"]; // Filenames in order of descending rank.
             let matcher = new WebInspector.ResourceQueryController;
             filenames.forEach((filename) => matcher.addResource(new WebInspector.Resource(filename)));
 
@@ -298,9 +298,44 @@ function test()
     });
 
     suite.addTestCase({
+        name: "ExecuteQueryConsecutiveRankings",
+        description: "Check that consecutive matches rank higher.",
+        test() {
+            let matcher = new WebInspector.ResourceQueryController;
+            let tests = [
+                {
+                    filenames: ["Color.js", "HighlightColor.js", "CxxOxxLxxOxxR.js"],
+                    query: "color",
+                },
+                {
+                    filenames: ["ContentView.js", "CxxVxxIxxExxW.js"],
+                    query: "cview",
+                },
+                {
+                    filenames: ["HeapSnapshot.js", "HxxSxxHxxOxxT.js"],
+                    query: "hshot",
+                },
+            ];
+
+            let passed = true;
+            for (let {filenames, query} of tests) {
+                matcher.reset();
+                filenames.forEach((filename) => matcher.addResource(new WebInspector.Resource(filename)));
+
+                let results = matcher.executeQuery(query);
+                let resultFilenames = results.map((result) => result.resource.displayName);
+
+                passed = passed && Object.shallowEqual(resultFilenames, filenames);
+            }
+
+            InspectorTest.expectThat(passed, "Results should be ranked by descending relevancy.");
+        }
+    });
+
+    suite.addTestCase({
         name: "GetMatchingTextRanges",
         description: "Check that query result TextRanges are correct.",
-        test: function() {
+        test() {
             function textRange(start, end) {
                 return new WebInspector.TextRange(0, start, 0, end);
             }
index 7a2247c..239f695 100644 (file)
@@ -1,3 +1,17 @@
+2016-10-24  Devin Rousso  <dcrousso+webkit@gmail.com>
+
+        Web Inspector: Improve Quick Open sorting algorithm
+        https://bugs.webkit.org/show_bug.cgi?id=163705
+
+        Reviewed by Joseph Pecoraro.
+
+        * UserInterface/Models/ResourceQueryResult.js:
+        (WebInspector.ResourceQueryResult.prototype._calculateRank.getMultiplier):
+        (WebInspector.ResourceQueryResult.prototype._calculateRank):
+        Added logic to multiply the ranking increment/decrement based on whether the current match
+        is part of a sequence, whether that sequence began with a special character, and the length
+        of the current sequence.
+
 2016-10-19  Dean Jackson  <dino@apple.com>
 
         Support CSS Shapes Level 1 without a prefix
index eb5edbf..c2bf09d 100644 (file)
@@ -61,23 +61,44 @@ WebInspector.ResourceQueryResult = class QueryResult extends WebInspector.Object
 
     _calculateRank()
     {
-        const specialWeight = 50;
         const normalWeight = 10;
-        const consecutiveMatchWeight = 5;
+        const consecutiveWeight = 5;
+        const specialMultiplier = 5;
+
+        function getMultiplier(match) {
+            if (match.type === WebInspector.ResourceQueryMatch.Type.Special)
+                return specialMultiplier;
+
+            return 1;
+        }
 
         this._rank = 0;
 
         let previousMatch = null;
+        let consecutiveMatchStart = null;
         for (let match of this._matches) {
-            this._rank += match.type === WebInspector.ResourceQueryMatch.Type.Special ? specialWeight : normalWeight;
-            if (previousMatch && previousMatch.index === match.index - 1)
-                this._rank += consecutiveMatchWeight;
+            this._rank += normalWeight * getMultiplier(match);
+
+            let consecutive = previousMatch && previousMatch.index === match.index - 1;
+            if (consecutive) {
+                if (!consecutiveMatchStart)
+                    consecutiveMatchStart = previousMatch;
+
+                // If the first match in this consecutive series was a special character, give a
+                // bonus (more likely to match a specific word in the text).  Otherwise, multiply
+                // by the current length of the consecutive sequence (gives priority to fewer
+                // longer sequences instead of more short sequences).
+                this._rank += consecutiveWeight * getMultiplier(consecutiveMatchStart) * (match.index - consecutiveMatchStart.index);
+            } else if (consecutiveMatchStart)
+                consecutiveMatchStart = null;
 
             previousMatch = match;
 
-            // The match index is deducted from the total rank, so matches that
-            // occur closer to the beginning of the string are ranked higher.
-            this._rank -= match.index;
+            // The match index is deducted from the total rank, so matches that occur closer to
+            // the beginning of the string are ranked higher.  Increase the amount subtracted if
+            // the match is special, so as to favor matches towards the beginning of the string.
+            if (!consecutive)
+                this._rank -= match.index * getMultiplier(match);
         }
     }