Title: [130140] trunk/Source/WebCore
Revision
130140
Author
commit-qu...@webkit.org
Date
2012-10-02 01:59:39 -0700 (Tue, 02 Oct 2012)

Log Message

Web Inspector: CSS property names autocomplete: Suggest most used rather than alphabeticaly first
https://bugs.webkit.org/show_bug.cgi?id=96763

Patch by Nikita Vasilyev <m...@elv1s.ru> on 2012-10-02
Reviewed by Alexander Pavlov.

Implement selection of non-first item in WebInspector.TextPrompt.SuggestBox.

* inspector/front-end/CSSCompletions.js:
(WebInspector.CSSCompletions.Weight): Collect most used CSS property names. Rarely used properties are not presented.
(WebInspector.CSSCompletions.prototype.firstStartsWith): Remove unused function.
(WebInspector.CSSCompletions.prototype.mostUsedOf):
* inspector/front-end/StylesSidebarPane.js:
* inspector/front-end/TextPrompt.js:
(WebInspector.TextPrompt.prototype._completionsReady):
(WebInspector.TextPrompt.prototype.pageDownKeyPressed):
(WebInspector.TextPrompt.SuggestBox): Introduce _length and _selectedIndex to remove unnecessary DOM traversals.
    Simplify canShowForSingleItem logic.
(WebInspector.TextPrompt.SuggestBox.prototype._selectClosest): _onPreviousItem and _onNextItem had some logic
    duplication so I replaced them with this method.
(WebInspector.TextPrompt.SuggestBox.prototype.updateSuggestions):
(WebInspector.TextPrompt.SuggestBox.prototype._updateItems):
(WebInspector.TextPrompt.SuggestBox.prototype._selectItem):
(WebInspector.TextPrompt.SuggestBox.prototype._completionsReady):
(WebInspector.TextPrompt.SuggestBox.prototype.upKeyPressed):
(WebInspector.TextPrompt.SuggestBox.prototype.downKeyPressed):
(WebInspector.TextPrompt.SuggestBox.prototype.pageUpKeyPressed):
(WebInspector.TextPrompt.SuggestBox.prototype.pageDownKeyPressed):

Modified Paths

Diff

Modified: trunk/Source/WebCore/ChangeLog (130139 => 130140)


--- trunk/Source/WebCore/ChangeLog	2012-10-02 08:36:49 UTC (rev 130139)
+++ trunk/Source/WebCore/ChangeLog	2012-10-02 08:59:39 UTC (rev 130140)
@@ -1,3 +1,33 @@
+2012-10-02  Nikita Vasilyev  <m...@elv1s.ru>
+
+        Web Inspector: CSS property names autocomplete: Suggest most used rather than alphabeticaly first
+        https://bugs.webkit.org/show_bug.cgi?id=96763
+
+        Reviewed by Alexander Pavlov.
+
+        Implement selection of non-first item in WebInspector.TextPrompt.SuggestBox.
+
+        * inspector/front-end/CSSCompletions.js:
+        (WebInspector.CSSCompletions.Weight): Collect most used CSS property names. Rarely used properties are not presented.
+        (WebInspector.CSSCompletions.prototype.firstStartsWith): Remove unused function.
+        (WebInspector.CSSCompletions.prototype.mostUsedOf):
+        * inspector/front-end/StylesSidebarPane.js:
+        * inspector/front-end/TextPrompt.js:
+        (WebInspector.TextPrompt.prototype._completionsReady):
+        (WebInspector.TextPrompt.prototype.pageDownKeyPressed):
+        (WebInspector.TextPrompt.SuggestBox): Introduce _length and _selectedIndex to remove unnecessary DOM traversals.
+            Simplify canShowForSingleItem logic.
+        (WebInspector.TextPrompt.SuggestBox.prototype._selectClosest): _onPreviousItem and _onNextItem had some logic 
+            duplication so I replaced them with this method.
+        (WebInspector.TextPrompt.SuggestBox.prototype.updateSuggestions):
+        (WebInspector.TextPrompt.SuggestBox.prototype._updateItems):
+        (WebInspector.TextPrompt.SuggestBox.prototype._selectItem):
+        (WebInspector.TextPrompt.SuggestBox.prototype._completionsReady):
+        (WebInspector.TextPrompt.SuggestBox.prototype.upKeyPressed):
+        (WebInspector.TextPrompt.SuggestBox.prototype.downKeyPressed):
+        (WebInspector.TextPrompt.SuggestBox.prototype.pageUpKeyPressed):
+        (WebInspector.TextPrompt.SuggestBox.prototype.pageDownKeyPressed):
+
 2012-10-02  Keishi Hattori  <kei...@webkit.org>
 
         Web Inspector: Modifications in a shadow tree don't update the Elements panel.

Modified: trunk/Source/WebCore/inspector/front-end/CSSCompletions.js (130139 => 130140)


--- trunk/Source/WebCore/inspector/front-end/CSSCompletions.js	2012-10-02 08:36:49 UTC (rev 130139)
+++ trunk/Source/WebCore/inspector/front-end/CSSCompletions.js	2012-10-02 08:59:39 UTC (rev 130140)
@@ -90,6 +90,126 @@
     return WebInspector.CSSCompletions._cssPropertiesMetainfoKeySet;
 }
 
+// Weight of CSS properties based their usage on few popular websites https://gist.github.com/3751436
+WebInspector.CSSCompletions.Weight = {
+    "-webkit-animation": 1,
+    "-webkit-animation-duration": 1,
+    "-webkit-animation-iteration-count": 1,
+    "-webkit-animation-name": 1,
+    "-webkit-animation-timing-function": 1,
+    "-webkit-appearance": 1,
+    "-webkit-background-clip": 2,
+    "-webkit-border-horizontal-spacing": 1,
+    "-webkit-border-vertical-spacing": 1,
+    "-webkit-box-shadow": 24,
+    "-webkit-font-smoothing": 2,
+    "-webkit-transform": 1,
+    "-webkit-transition": 8,
+    "-webkit-transition-delay": 7,
+    "-webkit-transition-duration": 7,
+    "-webkit-transition-property": 7,
+    "-webkit-transition-timing-function": 6,
+    "-webkit-user-select": 1,
+    "background": 222,
+    "background-attachment": 144,
+    "background-clip": 143,
+    "background-color": 222,
+    "background-image": 201,
+    "background-origin": 142,
+    "background-size": 25,
+    "border": 121,
+    "border-bottom": 121,
+    "border-bottom-color": 121,
+    "border-bottom-left-radius": 50,
+    "border-bottom-right-radius": 50,
+    "border-bottom-style": 114,
+    "border-bottom-width": 120,
+    "border-collapse": 3,
+    "border-left": 95,
+    "border-left-color": 95,
+    "border-left-style": 89,
+    "border-left-width": 94,
+    "border-radius": 50,
+    "border-right": 93,
+    "border-right-color": 93,
+    "border-right-style": 88,
+    "border-right-width": 93,
+    "border-top": 111,
+    "border-top-color": 111,
+    "border-top-left-radius": 49,
+    "border-top-right-radius": 49,
+    "border-top-style": 104,
+    "border-top-width": 109,
+    "bottom": 16,
+    "box-shadow": 25,
+    "box-sizing": 2,
+    "clear": 23,
+    "color": 237,
+    "cursor": 34,
+    "direction": 4,
+    "display": 210,
+    "fill": 2,
+    "filter": 1,
+    "float": 105,
+    "font": 174,
+    "font-family": 25,
+    "font-size": 174,
+    "font-style": 9,
+    "font-weight": 89,
+    "height": 161,
+    "left": 54,
+    "letter-spacing": 3,
+    "line-height": 75,
+    "list-style": 17,
+    "list-style-image": 8,
+    "list-style-position": 8,
+    "list-style-type": 17,
+    "margin": 241,
+    "margin-bottom": 226,
+    "margin-left": 225,
+    "margin-right": 213,
+    "margin-top": 241,
+    "max-height": 5,
+    "max-width": 11,
+    "min-height": 9,
+    "min-width": 6,
+    "opacity": 24,
+    "outline": 10,
+    "outline-color": 10,
+    "outline-style": 10,
+    "outline-width": 10,
+    "overflow": 57,
+    "overflow-x": 56,
+    "overflow-y": 57,
+    "padding": 216,
+    "padding-bottom": 208,
+    "padding-left": 216,
+    "padding-right": 206,
+    "padding-top": 216,
+    "position": 136,
+    "resize": 1,
+    "right": 29,
+    "stroke": 1,
+    "stroke-width": 1,
+    "table-layout": 1,
+    "text-align": 66,
+    "text-decoration": 53,
+    "text-indent": 9,
+    "text-overflow": 8,
+    "text-shadow": 19,
+    "text-transform": 5,
+    "top": 71,
+    "unicode-bidi": 1,
+    "vertical-align": 37,
+    "visibility": 11,
+    "white-space": 24,
+    "width": 255,
+    "word-wrap": 6,
+    "z-index": 32,
+    "zoom": 10
+};
+
+
 WebInspector.CSSCompletions.prototype = {
     startsWith: function(prefix)
     {
@@ -103,10 +223,22 @@
         return results;
     },
 
-    firstStartsWith: function(prefix)
+    /**
+     * @param {Array.<string>} properties
+     * @return {number}
+     */
+    mostUsedOf: function(properties)
     {
-        var foundIndex = this._firstIndexOfPrefix(prefix);
-        return (foundIndex === -1 ? "" : this._values[foundIndex]);
+        var maxWeight = 0;
+        var index = 0;
+        for (var i = 0; i < properties.length; i++) {
+            var weight = WebInspector.CSSCompletions.Weight[properties[i]];
+            if (weight > maxWeight) {
+                maxWeight = weight;
+                index = i;
+            }
+        }
+        return index;
     },
 
     _firstIndexOfPrefix: function(prefix)

Modified: trunk/Source/WebCore/inspector/front-end/StylesSidebarPane.js (130139 => 130140)


--- trunk/Source/WebCore/inspector/front-end/StylesSidebarPane.js	2012-10-02 08:36:49 UTC (rev 130139)
+++ trunk/Source/WebCore/inspector/front-end/StylesSidebarPane.js	2012-10-02 08:59:39 UTC (rev 130140)
@@ -2596,7 +2596,8 @@
             return;
 
         var results = this._cssCompletions.startsWith(prefix);
-        completionsReadyCallback(results);
+        var selectedIndex = this._cssCompletions.mostUsedOf(results);
+        completionsReadyCallback(results, selectedIndex);
     }
 }
 

Modified: trunk/Source/WebCore/inspector/front-end/TextPrompt.js (130139 => 130140)


--- trunk/Source/WebCore/inspector/front-end/TextPrompt.js	2012-10-02 08:36:49 UTC (rev 130139)
+++ trunk/Source/WebCore/inspector/front-end/TextPrompt.js	2012-10-02 08:59:39 UTC (rev 130140)
@@ -433,8 +433,9 @@
      * @param {Range} originalWordPrefixRange
      * @param {boolean} reverse
      * @param {Array.<string>=} completions
+     * @param {number=} selectedIndex
      */
-    _completionsReady: function(selection, auto, originalWordPrefixRange, reverse, completions)
+    _completionsReady: function(selection, auto, originalWordPrefixRange, reverse, completions, selectedIndex)
     {
         if (!this._waitingForCompletions || !completions || !completions.length) {
             this.hideSuggestBox();
@@ -451,29 +452,31 @@
         if (originalWordPrefixRange.toString() + selectionRange.toString() != fullWordRange.toString())
             return;
 
+        selectedIndex = selectedIndex || 0;
+
         this._userEnteredRange = fullWordRange;
         this._userEnteredText = fullWordRange.toString();
 
         if (this._suggestBox)
-            this._suggestBox.updateSuggestions(this._boxForAnchorAtStart(selection, fullWordRange), completions, !this.isCaretAtEndOfPrompt());
+            this._suggestBox.updateSuggestions(this._boxForAnchorAtStart(selection, fullWordRange), completions, selectedIndex, !this.isCaretAtEndOfPrompt());
 
         var wordPrefixLength = originalWordPrefixRange.toString().length;
 
         if (auto) {
-            var completionText = completions[0];
+            var completionText = completions[selectedIndex];
             var commonPrefix = this._buildCommonPrefix(completions, wordPrefixLength);
 
             this._commonPrefix = commonPrefix;
         } else {
             if (completions.length === 1) {
-                var completionText = completions[0];
+                var completionText = completions[selectedIndex];
                 wordPrefixLength = completionText.length;
             } else {
                 var commonPrefix = this._buildCommonPrefix(completions, wordPrefixLength);
                 wordPrefixLength = commonPrefix.length;
 
                 if (selection.isCollapsed)
-                    var completionText = completions[0];
+                    var completionText = completions[selectedIndex];
                 else {
                     var currentText = fullWordRange.toString();
 
@@ -485,7 +488,7 @@
 
                     var nextIndex = foundIndex + (reverse ? -1 : 1);
                     if (foundIndex === null || nextIndex >= completions.length)
-                        var completionText = completions[0];
+                        var completionText = completions[selectedIndex];
                     else if (nextIndex < 0)
                         var completionText = completions[completions.length - 1];
                     else
@@ -749,7 +752,7 @@
             return this._suggestBox.pageDownKeyPressed(event);
 
         return false;
-    },
+    }
 }
 
 WebInspector.TextPrompt.prototype.__proto__ = WebInspector.Object.prototype;
@@ -929,6 +932,8 @@
 {
     this._textPrompt = textPrompt;
     this._inputElement = inputElement;
+    this._length = 0;
+    this._selectedIndex = -1;
     this._selectedElement = null;
     this._boundOnScroll = this._onscrollresize.bind(this, true);
     this._boundOnResize = this._onscrollresize.bind(this, false);
@@ -1072,76 +1077,40 @@
         return true;
     },
 
-    _onNextItem: function(event, isPageScroll)
+    /**
+     * @param {number} shift
+     * @param {boolean=} isCircular
+     * @return {boolean} is changed
+     */
+    _selectClosest: function(shift, isCircular)
     {
-        var children = this.contentElement.childNodes;
-        if (!children.length)
+        if (!this._length)
             return false;
 
-        if (!this._selectedElement)
-            this._selectedElement = this.contentElement.firstChild;
-        else {
-            if (!isPageScroll)
-                this._selectedElement = this._selectedElement.nextSibling || this.contentElement.firstChild;
-            else {
-                var candidate = this._selectedElement;
+        var index = this._selectedIndex + shift;
 
-                for (var itemsLeft = this._rowCountPerViewport; itemsLeft; --itemsLeft) {
-                    if (candidate.nextSibling)
-                        candidate = candidate.nextSibling;
-                    else
-                        break;
-                }
+        if (isCircular)
+            index = (this._length + index) % this._length;
+        else
+            index = Number.constrain(index, 0, this._length - 1);
 
-                this._selectedElement = candidate;
-            }
-        }
-        this._updateSelection();
+        this._selectItem(index);
         this._applySuggestion(undefined, true);
         return true;
     },
 
-    _onPreviousItem: function(event, isPageScroll)
-    {
-        var children = this.contentElement.childNodes;
-        if (!children.length)
-            return false;
-
-        if (!this._selectedElement)
-            this._selectedElement = this.contentElement.lastChild;
-        else {
-            if (!isPageScroll)
-                this._selectedElement = this._selectedElement.previousSibling || this.contentElement.lastChild;
-            else {
-                var candidate = this._selectedElement;
-
-                for (var itemsLeft = this._rowCountPerViewport; itemsLeft; --itemsLeft) {
-                    if (candidate.previousSibling)
-                        candidate = candidate.previousSibling;
-                    else
-                        break;
-                }
-
-                this._selectedElement = candidate;
-            }
-        }
-        this._updateSelection();
-        this._applySuggestion(undefined, true);
-        return true;
-    },
-
     /**
      * @param {AnchorBox} anchorBox
      * @param {Array.<string>=} completions
      * @param {boolean=} canShowForSingleItem
      */
-    updateSuggestions: function(anchorBox, completions, canShowForSingleItem)
+    updateSuggestions: function(anchorBox, completions, selectedIndex, canShowForSingleItem)
     {
         if (this._suggestTimeout) {
             clearTimeout(this._suggestTimeout);
             delete this._suggestTimeout;
         }
-        this._completionsReady(anchorBox, completions, canShowForSingleItem);
+        this._completionsReady(anchorBox, completions, selectedIndex, canShowForSingleItem);
     },
 
     _onItemMouseDown: function(text, event)
@@ -1169,10 +1138,12 @@
     },
 
     /**
-     * @param {boolean=} canShowForSingleItem
+     * @param {Array.<string>=} items
+     * @param {number} selectedIndex
      */
-    _updateItems: function(items, canShowForSingleItem)
+    _updateItems: function(items, selectedIndex)
     {
+        this._length = items.length;
         this.contentElement.removeChildren();
 
         var userEnteredText = this._textPrompt._userEnteredText;
@@ -1182,21 +1153,23 @@
             this.contentElement.appendChild(currentItemElement);
         }
 
-        this._selectedElement = canShowForSingleItem ? this.contentElement.firstChild : null;
-        this._updateSelection();
+        this._selectedElement = null;
+        this._selectItem(selectedIndex);
     },
 
-    _updateSelection: function()
+    /**
+     * @param {number} index
+     */
+    _selectItem: function(index)
     {
-        // FIXME: might want some optimization if becomes a bottleneck.
-        for (var child = this.contentElement.firstChild; child; child = child.nextSibling) {
-            if (child !== this._selectedElement)
-                child.removeStyleClass("selected");
-        }
-        if (this._selectedElement) {
-            this._selectedElement.addStyleClass("selected");
-            this._selectedElement.scrollIntoViewIfNeeded(false);
-        }
+        if (this._selectedElement)
+            this._selectedElement.classList.remove("selected");
+
+        this._selectedIndex = index;
+        this._selectedElement = this.contentElement.children[index];
+        this._selectedElement.classList.add("selected");
+
+        this._selectedElement.scrollIntoViewIfNeeded(false);
     },
 
     /**
@@ -1228,10 +1201,10 @@
      * @param {Array.<string>=} completions
      * @param {boolean=} canShowForSingleItem
      */
-    _completionsReady: function(anchorBox, completions, canShowForSingleItem)
+    _completionsReady: function(anchorBox, completions, selectedIndex, canShowForSingleItem)
     {
         if (this._canShowBox(completions, canShowForSingleItem)) {
-            this._updateItems(completions, canShowForSingleItem);
+            this._updateItems(completions, selectedIndex);
             this._updateBoxPosition(anchorBox);
             if (!this.visible)
                 this._bodyElement.appendChild(this._element);
@@ -1242,22 +1215,22 @@
 
     upKeyPressed: function(event)
     {
-        return this._onPreviousItem(event, false);
+        return this._selectClosest(-1, true);
     },
 
     downKeyPressed: function(event)
     {
-        return this._onNextItem(event, false);
+        return this._selectClosest(1, true);
     },
 
     pageUpKeyPressed: function(event)
     {
-        return this._onPreviousItem(event, true);
+        return this._selectClosest(-this._rowCountPerViewport, false);
     },
 
     pageDownKeyPressed: function(event)
     {
-        return this._onNextItem(event, true);
+        return this._selectClosest(this._rowCountPerViewport, false);
     },
 
     enterKeyPressed: function(event)
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
http://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to