From 4feb92019690ab78a9cd330307de652ca7601f37 Mon Sep 17 00:00:00 2001 From: pfeldman Date: Tue, 12 Jan 2016 12:46:41 -0800 Subject: [PATCH] DevTools: include history items in the console suggest box. Review URL: https://codereview.chromium.org/1569353003 Cr-Commit-Position: refs/heads/master@{#368978} --- front_end/ui/SuggestBox.js | 7 +++-- front_end/ui/TextPrompt.js | 58 +++++++++++++++++++++++++++++++------- 2 files changed, 52 insertions(+), 13 deletions(-) diff --git a/front_end/ui/SuggestBox.js b/front_end/ui/SuggestBox.js index 171fcd072b..a6e2903d94 100644 --- a/front_end/ui/SuggestBox.js +++ b/front_end/ui/SuggestBox.js @@ -178,7 +178,7 @@ WebInspector.SuggestBox.prototype = { if (!this.visible() || !this._selectedElement) return false; - var suggestion = this._selectedElement.textContent; + var suggestion = this._selectedElement.__fullValue; if (!suggestion) return false; @@ -247,10 +247,11 @@ WebInspector.SuggestBox.prototype = { element.tabIndex = -1; if (prefix && prefix.length && !text.indexOf(prefix)) { element.createChild("span", "prefix").textContent = prefix; - element.createChild("span", "suffix").textContent = text.substring(prefix.length); + element.createChild("span", "suffix").textContent = text.substring(prefix.length).trimEnd(50); } else { - element.createChild("span", "suffix").textContent = text; + element.createChild("span", "suffix").textContent = text.trimEnd(50); } + element.__fullValue = text; element.createChild("span", "spacer"); element.addEventListener("mousedown", this._onItemMouseDown.bind(this), false); return element; diff --git a/front_end/ui/TextPrompt.js b/front_end/ui/TextPrompt.js index e25660b414..f882cfd0c4 100644 --- a/front_end/ui/TextPrompt.js +++ b/front_end/ui/TextPrompt.js @@ -412,7 +412,7 @@ WebInspector.TextPrompt.prototype = { var wordPrefixRange = selectionRange.startContainer.rangeOfWord(selectionRange.startOffset, this._completionStopCharacters, this._element, "backward"); this._waitingForCompletions = true; - this._loadCompletions(/** @type {!Element} */ (this._proxyElement), this.text(), selectionRange.startOffset, wordPrefixRange, force || false, this._completionsReady.bind(this, selection, wordPrefixRange, !!reverse)); + this._loadCompletions(/** @type {!Element} */ (this._proxyElement), this.text(), selectionRange.startOffset, wordPrefixRange, force || false, this._completionsReady.bind(this, selection, wordPrefixRange, !!reverse, !!force)); }, disableDefaultSuggestionForEmptyInput: function() @@ -466,19 +466,42 @@ WebInspector.TextPrompt.prototype = { return document.createRange(); }, + /** + * @param {string} prefix + * @return {!Array.} + */ + additionalCompletions: function(prefix) + { + return []; + }, + /** * @param {!Selection} selection * @param {!Range} originalWordPrefixRange * @param {boolean} reverse + * @param {boolean} force * @param {!Array.} completions * @param {number=} selectedIndex */ - _completionsReady: function(selection, originalWordPrefixRange, reverse, completions, selectedIndex) + _completionsReady: function(selection, originalWordPrefixRange, reverse, force, completions, selectedIndex) { + var prefix = originalWordPrefixRange.toString(); + if (prefix || force) { + if (prefix) + completions = completions.concat(this.additionalCompletions(prefix)); + else + completions = this.additionalCompletions(prefix).concat(completions); + } + + // Filter out dupes. + var store = new Set(); + completions = completions.filter(item => !store.has(item) && !!store.add(item)); + if (!this._waitingForCompletions || !completions.length) { this.hideSuggestBox(); return; } + delete this._waitingForCompletions; var selectionRange = selection.getRangeAt(0); @@ -487,7 +510,7 @@ WebInspector.TextPrompt.prototype = { fullWordRange.setStart(originalWordPrefixRange.startContainer, originalWordPrefixRange.startOffset); fullWordRange.setEnd(selectionRange.endContainer, selectionRange.endOffset); - if (originalWordPrefixRange.toString() + selectionRange.toString() !== fullWordRange.toString()) + if (prefix + selectionRange.toString() !== fullWordRange.toString()) return; selectedIndex = (this._disableDefaultSuggestionForEmptyInput && !this.text()) ? -1 : (selectedIndex || 0); @@ -555,15 +578,10 @@ WebInspector.TextPrompt.prototype = { /** * @param {string} completionText * @param {boolean=} isIntermediateSuggestion - * @param {!Range=} originalPrefixRange */ - _applySuggestion: function(completionText, isIntermediateSuggestion, originalPrefixRange) + _applySuggestion: function(completionText, isIntermediateSuggestion) { - var wordPrefixLength; - if (originalPrefixRange) - wordPrefixLength = originalPrefixRange.toString().length; - else - wordPrefixLength = this._userEnteredText ? this._userEnteredText.length : 0; + var wordPrefixLength = this._userEnteredText ? this._userEnteredText.length : 0; this._userEnteredRange.deleteContents(); this._element.normalize(); @@ -803,6 +821,26 @@ WebInspector.TextPromptWithHistory.prototype = { return this._data; }, + /** + * @override + * @param {string} prefix + * @return {!Array.} + */ + additionalCompletions: function(prefix) + { + if (!this.isCaretAtEndOfPrompt()) + return []; + var result = []; + var text = this.text(); + for (var i = this._data.length - 1; i >= 0 && result.length < 50; --i) { + var item = this._data[i]; + if (!item.startsWith(text)) + continue; + result.push(item.substring(text.length - prefix.length)); + } + return result; + }, + /** * @param {!Array.} data */