Title: [208248] trunk/Source/WebInspectorUI
Revision
208248
Author
commit-qu...@webkit.org
Date
2016-11-01 14:56:11 -0700 (Tue, 01 Nov 2016)

Log Message

REGRESSION (r191419): Web Inspector: Autocomplete is broken
https://bugs.webkit.org/show_bug.cgi?id=150493

Patch by Devin Rousso <dcrousso+web...@gmail.com> on 2016-11-01
Reviewed by Timothy Hatcher.

Fixed CodeMirror.undo() logic rolled out by r191539 <https://webkit.org/b/150537>

Added calls to CodeMirror.changeGeneration(true) which tells the history stack to "close"
the current event, preventing it from being consolidated with new changes.  If the user
typed fast enough, non-completion changes (like adding ":" in CSS) would get merged with
completion changes, causing calls to CodeMirror.undo() to undo those changes as well.  To
prevent this, a boolean variable is set to true whenever a completion "activity" is in
progress, defined by a sequence of completion related events.  When this "activity" first
starts, call CodeMirror.changeGeneration(true) to freeze the current history.

See original bug for more information <https://webkit.org/b/147720>.

* UserInterface/Controllers/CodeMirrorCompletionController.js:
(WebInspector.CodeMirrorCompletionController):
(WebInspector.CodeMirrorCompletionController.prototype.updateCompletions):
(WebInspector.CodeMirrorCompletionController.prototype.isCompletionChange):
(WebInspector.CodeMirrorCompletionController.prototype.hideCompletions):
(WebInspector.CodeMirrorCompletionController.prototype.close):
(WebInspector.CodeMirrorCompletionController.prototype.completionSuggestionsSelectedCompletion):
(WebInspector.CodeMirrorCompletionController.prototype._applyCompletionHint.update):
(WebInspector.CodeMirrorCompletionController.prototype._applyCompletionHint):
(WebInspector.CodeMirrorCompletionController.prototype._commitCompletionHint.update):
(WebInspector.CodeMirrorCompletionController.prototype._commitCompletionHint):
(WebInspector.CodeMirrorCompletionController.prototype._removeCompletionHint.update):
(WebInspector.CodeMirrorCompletionController.prototype._removeCompletionHint):
(WebInspector.CodeMirrorCompletionController.prototype._completeAtCurrentPosition):
(WebInspector.CodeMirrorCompletionController.prototype._handleBeforeChange):
(WebInspector.CodeMirrorCompletionController.prototype._createCompletionHintMarker): Deleted.
(WebInspector.CodeMirrorCompletionController.prototype._removeLastChangeFromHistory): Deleted.
(WebInspector.CodeMirrorCompletionController.prototype._removeCompletionHint.clearMarker): Deleted.

Modified Paths

Diff

Modified: trunk/Source/WebInspectorUI/ChangeLog (208247 => 208248)


--- trunk/Source/WebInspectorUI/ChangeLog	2016-11-01 21:49:40 UTC (rev 208247)
+++ trunk/Source/WebInspectorUI/ChangeLog	2016-11-01 21:56:11 UTC (rev 208248)
@@ -1,5 +1,43 @@
 2016-11-01  Devin Rousso  <dcrousso+web...@gmail.com>
 
+        REGRESSION (r191419): Web Inspector: Autocomplete is broken
+        https://bugs.webkit.org/show_bug.cgi?id=150493
+
+        Reviewed by Timothy Hatcher.
+
+        Fixed CodeMirror.undo() logic rolled out by r191539 <https://webkit.org/b/150537>
+
+        Added calls to CodeMirror.changeGeneration(true) which tells the history stack to "close"
+        the current event, preventing it from being consolidated with new changes.  If the user
+        typed fast enough, non-completion changes (like adding ":" in CSS) would get merged with
+        completion changes, causing calls to CodeMirror.undo() to undo those changes as well.  To
+        prevent this, a boolean variable is set to true whenever a completion "activity" is in
+        progress, defined by a sequence of completion related events.  When this "activity" first
+        starts, call CodeMirror.changeGeneration(true) to freeze the current history.
+
+        See original bug for more information <https://webkit.org/b/147720>.
+
+        * UserInterface/Controllers/CodeMirrorCompletionController.js:
+        (WebInspector.CodeMirrorCompletionController):
+        (WebInspector.CodeMirrorCompletionController.prototype.updateCompletions):
+        (WebInspector.CodeMirrorCompletionController.prototype.isCompletionChange):
+        (WebInspector.CodeMirrorCompletionController.prototype.hideCompletions):
+        (WebInspector.CodeMirrorCompletionController.prototype.close):
+        (WebInspector.CodeMirrorCompletionController.prototype.completionSuggestionsSelectedCompletion):
+        (WebInspector.CodeMirrorCompletionController.prototype._applyCompletionHint.update):
+        (WebInspector.CodeMirrorCompletionController.prototype._applyCompletionHint):
+        (WebInspector.CodeMirrorCompletionController.prototype._commitCompletionHint.update):
+        (WebInspector.CodeMirrorCompletionController.prototype._commitCompletionHint):
+        (WebInspector.CodeMirrorCompletionController.prototype._removeCompletionHint.update):
+        (WebInspector.CodeMirrorCompletionController.prototype._removeCompletionHint):
+        (WebInspector.CodeMirrorCompletionController.prototype._completeAtCurrentPosition):
+        (WebInspector.CodeMirrorCompletionController.prototype._handleBeforeChange):
+        (WebInspector.CodeMirrorCompletionController.prototype._createCompletionHintMarker): Deleted.
+        (WebInspector.CodeMirrorCompletionController.prototype._removeLastChangeFromHistory): Deleted.
+        (WebInspector.CodeMirrorCompletionController.prototype._removeCompletionHint.clearMarker): Deleted.
+
+2016-11-01  Devin Rousso  <dcrousso+web...@gmail.com>
+
         Web Inspector: Replace sublists inside DOM-related model objects with WI.Collection
         https://bugs.webkit.org/show_bug.cgi?id=164098
 

Modified: trunk/Source/WebInspectorUI/UserInterface/Controllers/CodeMirrorCompletionController.js (208247 => 208248)


--- trunk/Source/WebInspectorUI/UserInterface/Controllers/CodeMirrorCompletionController.js	2016-11-01 21:49:40 UTC (rev 208247)
+++ trunk/Source/WebInspectorUI/UserInterface/Controllers/CodeMirrorCompletionController.js	2016-11-01 21:56:11 UTC (rev 208248)
@@ -58,6 +58,7 @@
             "Cmd-Y": this._handleHideKey.bind(this)
         };
 
+        this._handleBeforeChangeListener = this._handleBeforeChange.bind(this);
         this._handleChangeListener = this._handleChange.bind(this);
         this._handleCursorActivityListener = this._handleCursorActivity.bind(this);
         this._handleHideActionListener = this._handleHideAction.bind(this);
@@ -64,6 +65,7 @@
 
         this._codeMirror.addKeyMap(this._keyMap);
 
+        this._codeMirror.on("beforeChange", this._handleBeforeChangeListener);
         this._codeMirror.on("change", this._handleChangeListener);
         this._codeMirror.on("cursorActivity", this._handleCursorActivityListener);
         this._codeMirror.on("blur", this._handleHideActionListener);
@@ -70,6 +72,12 @@
         this._codeMirror.on("scroll", this._handleHideActionListener);
 
         this._updatePromise = null;
+
+        this._currentCompletion = null;
+        this._ignoreChange = false;
+        this._ignoreNextCursorActivity = false;
+        this._ignoreNextUndo = false;
+        this._inCompletionActivity = false;
     }
 
     // Public
@@ -86,8 +94,10 @@
 
     updateCompletions(completions, implicitSuffix)
     {
-        if (isNaN(this._startOffset) || isNaN(this._endOffset) || isNaN(this._lineNumber))
+        if (isNaN(this._startOffset) || isNaN(this._endOffset) || isNaN(this._lineNumber)) {
+            this._inCompletionActivity = false;
             return;
+        }
 
         if (!completions || !completions.length) {
             this.hideCompletions();
@@ -133,7 +143,7 @@
 
     isCompletionChange(change)
     {
-        return this._ignoreChange || change.origin === WebInspector.CodeMirrorCompletionController.CompletionOrigin || change.origin === WebInspector.CodeMirrorCompletionController.DeleteCompletionOrigin;
+        return this._ignoreChange || this._ignoreNextUndo || change.origin === WebInspector.CodeMirrorCompletionController.CompletionOrigin || change.origin === WebInspector.CodeMirrorCompletionController.DeleteCompletionOrigin;
     }
 
     isShowingCompletions()
@@ -160,8 +170,9 @@
         this._implicitSuffix = "";
         this._forced = false;
 
-        delete this._currentCompletion;
-        delete this._ignoreNextCursorActivity;
+        this._currentCompletion = null;
+        this._ignoreNextCursorActivity = false;
+        this._inCompletionActivity = false;
 
         this._resolveUpdatePromise(WebInspector.CodeMirrorCompletionController.UpdatePromise.NoCompletionsFound);
     }
@@ -170,6 +181,7 @@
     {
         this._codeMirror.removeKeyMap(this._keyMap);
 
+        this._codeMirror.off("beforeChange", this._handleBeforeChangeListener);
         this._codeMirror.off("change", this._handleChangeListener);
         this._codeMirror.off("cursorActivity", this._handleCursorActivityListener);
         this._codeMirror.off("blur", this._handleHideActionListener);
@@ -191,6 +203,8 @@
 
     completionSuggestionsSelectedCompletion(suggestionsView, completionText)
     {
+        this._ignoreNextUndo = true;
+
         this._applyCompletionHint(completionText);
     }
 
@@ -245,15 +259,6 @@
         this._notifyCompletionsHiddenIfNeededTimeout = setTimeout(notify.bind(this), WebInspector.CodeMirrorCompletionController.CompletionsHiddenDelay);
     }
 
-    _createCompletionHintMarker(position, text)
-    {
-        var container = document.createElement("span");
-        container.classList.add(WebInspector.CodeMirrorCompletionController.CompletionHintStyleClassName);
-        container.textContent = text;
-
-        this._completionHintMarker = this._codeMirror.setUniqueBookmark(position, {widget: container, insertLeft: true});
-    }
-
     _applyCompletionHint(completionText)
     {
         console.assert(completionText);
@@ -262,17 +267,26 @@
 
         function update()
         {
+            if (!this._inCompletionActivity) {
+                this._inCompletionActivity = true;
+                this._codeMirror.changeGeneration(true);
+            }
+
             this._currentCompletion = completionText;
 
-            this._removeCompletionHint(true, true);
+            this._removeCompletionHint(true);
 
-            var replacementText = this._currentReplacementText;
+            let replacementText = this._currentReplacementText;
 
-            var from = {line: this._lineNumber, ch: this._startOffset};
-            var cursor = {line: this._lineNumber, ch: this._endOffset};
-            var currentText = this._codeMirror.getRange(from, cursor);
+            let from = {line: this._lineNumber, ch: this._startOffset};
+            let cursor = {line: this._lineNumber, ch: this._endOffset};
+            let to = {line: this._lineNumber, ch: this._startOffset + replacementText.length};
 
-            this._createCompletionHintMarker(cursor, replacementText.replace(currentText, ""));
+            this._codeMirror.replaceRange(replacementText, from, cursor, WebInspector.CodeMirrorCompletionController.CompletionOrigin);
+
+            this._codeMirror.setCursor(cursor);
+            if (cursor.ch !== to.ch)
+                this._completionHintMarker = this._codeMirror.markText(cursor, to, {className: WebInspector.CodeMirrorCompletionController.CompletionHintStyleClassName});
         }
 
         this._ignoreChange = true;
@@ -280,7 +294,7 @@
 
         this._codeMirror.operation(update.bind(this));
 
-        delete this._ignoreChange;
+        this._ignoreChange = false;
     }
 
     _commitCompletionHint()
@@ -287,7 +301,7 @@
     {
         function update()
         {
-            this._removeCompletionHint(true, true);
+            this._removeCompletionHint(true);
 
             var replacementText = this._currentReplacementText;
 
@@ -302,8 +316,6 @@
 
             this._codeMirror.replaceRange(replacementText, from, cursor, WebInspector.CodeMirrorCompletionController.CompletionOrigin);
 
-            // Don't call _removeLastChangeFromHistory here to allow the committed completion to be undone.
-
             this._codeMirror.setCursor(to);
 
             this.hideCompletions();
@@ -314,71 +326,35 @@
 
         this._codeMirror.operation(update.bind(this));
 
-        delete this._ignoreChange;
+        this._ignoreChange = false;
     }
 
-    _removeLastChangeFromHistory()
+    _removeCompletionHint(nonatomic)
     {
-        var history = this._codeMirror.getHistory();
-
-        // We don't expect a undone history. But if there is one clear it. If could lead to undefined behavior.
-        console.assert(!history.undone.length);
-        history.undone = [];
-
-        // Pop the last item from the done history.
-        console.assert(history.done.length);
-        history.done.pop();
-
-        this._codeMirror.setHistory(history);
-    }
-
-    _removeCompletionHint(nonatomic, dontRestorePrefix)
-    {
         if (!this._completionHintMarker)
             return;
 
         this._notifyCompletionsHiddenSoon();
 
-        function clearMarker(marker)
+        function update()
         {
-            if (!marker)
-                return;
+            this._codeMirror.undo();
 
-            var range = marker.find();
+            let range = this._completionHintMarker.find();
             if (range)
-                marker.clear();
+                this._completionHintMarker.clear();
 
-            return null;
+            this._completionHintMarker = null;
         }
 
-        function update()
-        {
-            this._completionHintMarker = clearMarker(this._completionHintMarker);
+        this._ignoreChange = true;
 
-            if (dontRestorePrefix)
-                return;
-
-            console.assert(!isNaN(this._startOffset));
-            console.assert(!isNaN(this._endOffset));
-            console.assert(!isNaN(this._lineNumber));
-
-            var from = {line: this._lineNumber, ch: this._startOffset};
-            var to = {line: this._lineNumber, ch: this._endOffset};
-
-            this._codeMirror.replaceRange(this._prefix, from, to, WebInspector.CodeMirrorCompletionController.DeleteCompletionOrigin);
-            this._removeLastChangeFromHistory();
-        }
-
-        if (nonatomic) {
+        if (nonatomic)
             update.call(this);
-            return;
-        }
+        else
+            this._codeMirror.operation(update.bind(this));
 
-        this._ignoreChange = true;
-
-        this._codeMirror.operation(update.bind(this));
-
-        delete this._ignoreChange;
+        this._ignoreChange = false;
     }
 
     _scanStringForExpression(modeName, string, startOffset, direction, allowMiddleAndEmpty, includeStopCharacter, ignoreInitialUnmatchedOpenBracket, stopCharactersRegex)
@@ -462,7 +438,7 @@
             return;
         }
 
-        this._removeCompletionHint(true, true);
+        this._removeCompletionHint(true);
 
         var cursor = this._codeMirror.getCursor();
         var token = this._codeMirror.getTokenAt(cursor);
@@ -807,6 +783,17 @@
         this._applyCompletionHint(this._currentCompletion);
     }
 
+    _handleBeforeChange(codeMirror, change)
+    {
+        if (this.isCompletionChange(change))
+            return;
+
+        this._ignoreNextCursorActivity = true;
+
+        if (this.isShowingCompletions())
+            this.hideCompletions();
+    }
+
     _handleChange(codeMirror, change)
     {
         if (this.isCompletionChange(change))
@@ -814,6 +801,11 @@
 
         this._ignoreNextCursorActivity = true;
 
+        if (this._ignoreNextUndo && change.origin === "undo") {
+            this._ignoreNextUndo = false;
+            return;
+        }
+
         if (!change.origin || change.origin.charAt(0) !== "+") {
             this.hideCompletions();
             return;
@@ -832,7 +824,7 @@
             return;
 
         if (this._ignoreNextCursorActivity) {
-            delete this._ignoreNextCursorActivity;
+            this._ignoreNextCursorActivity = false;
             return;
         }
 
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to