Commit 7360ae32 authored by Erik Luo's avatar Erik Luo Committed by Commit Bot

DevTools: do not show autocomplete suggestions that already exist

Completions in the middle of the text will not show SuggestBox
if one of the suggestions already exists in the editor.

Diff GIF: https://imgur.com/a/1Ry9Ser

Bug: none
Change-Id: I3bd27fa58b6a36c963e08f10f90e4747882f77e3
Reviewed-on: https://chromium-review.googlesource.com/1050503Reviewed-by: default avatarDmitry Gozman <dgozman@chromium.org>
Reviewed-by: default avatarJoel Einbinder <einbinder@chromium.org>
Commit-Queue: Erik Luo <luoe@chromium.org>
Cr-Commit-Position: refs/heads/master@{#559306}
parent e79fcd1d
Tests that console correctly finds suggestions in complicated cases. Tests that console correctly finds suggestions in complicated cases.
Checking 'window.|foo'
Found: _foo
Checking 'window._|foo'
Not Found: _foo
Checking 'window._|foo' forcefully
Found: _foo
Checking 'window.do' Checking 'window.do'
Found: document Found: document
...@@ -128,6 +137,14 @@ Checking 'I|mag' ...@@ -128,6 +137,14 @@ Checking 'I|mag'
Found: Image Found: Image
Found: Infinity Found: Infinity
Checking 'I|mage'
Not Found: Image
Not Found: Infinity
Checking 'I|mage' forcefully
Found: Image
Found: Infinity
Checking 'var x = (do|);' Checking 'var x = (do|);'
Found: document Found: document
......
...@@ -19,6 +19,7 @@ ...@@ -19,6 +19,7 @@
function shouldNotFindThisFunction() { } function shouldNotFindThisFunction() { }
function shouldFindThisFunction() { } function shouldFindThisFunction() { }
window["should not find this"] = true; window["should not find this"] = true;
window._foo = true;
var myMap = new Map([['first', 1], ['second', 2], ['third', 3], ['shouldNotFindThis', 4]]); var myMap = new Map([['first', 1], ['second', 2], ['third', 3], ['shouldNotFindThis', 4]]);
var complicatedObject = { var complicatedObject = {
'foo-bar': true, 'foo-bar': true,
...@@ -104,6 +105,9 @@ ...@@ -104,6 +105,9 @@
sequential([ sequential([
() => ConsoleTestRunner.waitUntilConsoleEditorLoaded().then( () => ConsoleTestRunner.waitUntilConsoleEditorLoaded().then(
e => consoleEditor = e), e => consoleEditor = e),
() => testCompletions('window.|foo', ['_foo']),
() => testCompletions('window._|foo', ['_foo'], false),
() => testCompletions('window._|foo', ['_foo'], true),
() => testCompletions('window.do', ['document']), () => testCompletions('window.do', ['document']),
() => testCompletions('win', ['window']), () => testCompletions('win', ['window']),
() => testCompletions('window["doc', ['"document"]']), () => testCompletions('window["doc', ['"document"]']),
...@@ -150,6 +154,8 @@ ...@@ -150,6 +154,8 @@
() => testCompletions('document[ [win', ['window']), () => testCompletions('document[ [win', ['window']),
() => testCompletions('document[ [ win', ['window']), () => testCompletions('document[ [ win', ['window']),
() => testCompletions('I|mag', ['Image', 'Infinity']), () => testCompletions('I|mag', ['Image', 'Infinity']),
() => testCompletions('I|mage', ['Image', 'Infinity'], false),
() => testCompletions('I|mage', ['Image', 'Infinity'], true),
() => testCompletions('var x = (do|);', ['document']), () => testCompletions('var x = (do|);', ['document']),
() => testCompletions('complicatedObject["foo', ['"foo-bar"]']), () => testCompletions('complicatedObject["foo', ['"foo-bar"]']),
() => testCompletions('complicatedObject["foo-', ['"foo-bar"]']), () => testCompletions('complicatedObject["foo-', ['"foo-bar"]']),
......
...@@ -356,7 +356,7 @@ Console.ConsolePrompt = class extends UI.Widget { ...@@ -356,7 +356,7 @@ Console.ConsolePrompt = class extends UI.Widget {
* @param {boolean=} force * @param {boolean=} force
* @return {!Promise<!UI.SuggestBox.Suggestions>} * @return {!Promise<!UI.SuggestBox.Suggestions>}
*/ */
_wordsWithQuery(queryRange, substituteRange, force) { async _wordsWithQuery(queryRange, substituteRange, force) {
const query = this._editor.text(queryRange); const query = this._editor.text(queryRange);
const before = this._editor.text(new TextUtils.TextRange(0, 0, queryRange.startLine, queryRange.startColumn)); const before = this._editor.text(new TextUtils.TextRange(0, 0, queryRange.startLine, queryRange.startColumn));
const historyWords = this._historyCompletions(query, force); const historyWords = this._historyCompletions(query, force);
...@@ -369,10 +369,15 @@ Console.ConsolePrompt = class extends UI.Widget { ...@@ -369,10 +369,15 @@ Console.ConsolePrompt = class extends UI.Widget {
if (!trimmedBefore.endsWith('.')) if (!trimmedBefore.endsWith('.'))
excludedTokens.add('js-property'); excludedTokens.add('js-property');
if (excludedTokens.has(token.type)) if (excludedTokens.has(token.type))
return Promise.resolve(historyWords); return historyWords;
} }
return ObjectUI.javaScriptAutocomplete.completionsForTextInCurrentContext(before, query, force) const words = await ObjectUI.javaScriptAutocomplete.completionsForTextInCurrentContext(before, query, force);
.then(words => words.concat(historyWords)); if (!force && !this._isCaretAtEndOfPrompt()) {
const queryAndAfter = this._editor.line(queryRange.startLine).substring(queryRange.startColumn);
if (queryAndAfter && words.some(word => queryAndAfter.startsWith(word.text) && query.length !== word.text.length))
return [];
}
return words.concat(historyWords);
} }
/** /**
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment