| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 /** | 4 /** |
| 5 * @unrestricted | 5 * @unrestricted |
| 6 */ | 6 */ |
| 7 WebInspector.ConsolePrompt = class extends WebInspector.Widget { | 7 Console.ConsolePrompt = class extends UI.Widget { |
| 8 constructor() { | 8 constructor() { |
| 9 super(); | 9 super(); |
| 10 this._addCompletionsFromHistory = true; | 10 this._addCompletionsFromHistory = true; |
| 11 this._history = new WebInspector.ConsoleHistoryManager(); | 11 this._history = new Console.ConsoleHistoryManager(); |
| 12 | 12 |
| 13 this._initialText = ''; | 13 this._initialText = ''; |
| 14 this._editor = null; | 14 this._editor = null; |
| 15 | 15 |
| 16 this.element.tabIndex = 0; | 16 this.element.tabIndex = 0; |
| 17 | 17 |
| 18 self.runtime.extension(WebInspector.TextEditorFactory).instance().then(gotFa
ctory.bind(this)); | 18 self.runtime.extension(UI.TextEditorFactory).instance().then(gotFactory.bind
(this)); |
| 19 | 19 |
| 20 /** | 20 /** |
| 21 * @param {!WebInspector.TextEditorFactory} factory | 21 * @param {!UI.TextEditorFactory} factory |
| 22 * @this {WebInspector.ConsolePrompt} | 22 * @this {Console.ConsolePrompt} |
| 23 */ | 23 */ |
| 24 function gotFactory(factory) { | 24 function gotFactory(factory) { |
| 25 this._editor = | 25 this._editor = |
| 26 factory.createEditor({lineNumbers: false, lineWrapping: true, mimeType
: 'javascript', autoHeight: true}); | 26 factory.createEditor({lineNumbers: false, lineWrapping: true, mimeType
: 'javascript', autoHeight: true}); |
| 27 | 27 |
| 28 this._editor.configureAutocomplete({ | 28 this._editor.configureAutocomplete({ |
| 29 substituteRangeCallback: this._substituteRange.bind(this), | 29 substituteRangeCallback: this._substituteRange.bind(this), |
| 30 suggestionsCallback: this._wordsWithQuery.bind(this), | 30 suggestionsCallback: this._wordsWithQuery.bind(this), |
| 31 captureEnter: true | 31 captureEnter: true |
| 32 }); | 32 }); |
| 33 this._editor.widget().element.addEventListener('keydown', this._editorKeyD
own.bind(this), true); | 33 this._editor.widget().element.addEventListener('keydown', this._editorKeyD
own.bind(this), true); |
| 34 this._editor.widget().show(this.element); | 34 this._editor.widget().show(this.element); |
| 35 | 35 |
| 36 this.setText(this._initialText); | 36 this.setText(this._initialText); |
| 37 delete this._initialText; | 37 delete this._initialText; |
| 38 if (this.hasFocus()) | 38 if (this.hasFocus()) |
| 39 this.focus(); | 39 this.focus(); |
| 40 this.element.tabIndex = -1; | 40 this.element.tabIndex = -1; |
| 41 | 41 |
| 42 this._editorSetForTest(); | 42 this._editorSetForTest(); |
| 43 } | 43 } |
| 44 } | 44 } |
| 45 | 45 |
| 46 /** | 46 /** |
| 47 * @return {!WebInspector.ConsoleHistoryManager} | 47 * @return {!Console.ConsoleHistoryManager} |
| 48 */ | 48 */ |
| 49 history() { | 49 history() { |
| 50 return this._history; | 50 return this._history; |
| 51 } | 51 } |
| 52 | 52 |
| 53 clearAutocomplete() { | 53 clearAutocomplete() { |
| 54 if (this._editor) | 54 if (this._editor) |
| 55 this._editor.clearAutocomplete(); | 55 this._editor.clearAutocomplete(); |
| 56 } | 56 } |
| 57 | 57 |
| 58 /** | 58 /** |
| 59 * @return {boolean} | 59 * @return {boolean} |
| 60 */ | 60 */ |
| 61 _isCaretAtEndOfPrompt() { | 61 _isCaretAtEndOfPrompt() { |
| 62 return !!this._editor && this._editor.selection().collapseToEnd().equal(this
._editor.fullRange().collapseToEnd()); | 62 return !!this._editor && this._editor.selection().collapseToEnd().equal(this
._editor.fullRange().collapseToEnd()); |
| 63 } | 63 } |
| 64 | 64 |
| 65 moveCaretToEndOfPrompt() { | 65 moveCaretToEndOfPrompt() { |
| 66 if (this._editor) | 66 if (this._editor) |
| 67 this._editor.setSelection(WebInspector.TextRange.createFromLocation(Infini
ty, Infinity)); | 67 this._editor.setSelection(Common.TextRange.createFromLocation(Infinity, In
finity)); |
| 68 } | 68 } |
| 69 | 69 |
| 70 /** | 70 /** |
| 71 * @param {string} text | 71 * @param {string} text |
| 72 */ | 72 */ |
| 73 setText(text) { | 73 setText(text) { |
| 74 if (this._editor) | 74 if (this._editor) |
| 75 this._editor.setText(text); | 75 this._editor.setText(text); |
| 76 else | 76 else |
| 77 this._initialText = text; | 77 this._initialText = text; |
| (...skipping 15 matching lines...) Expand all Loading... |
| 93 | 93 |
| 94 /** | 94 /** |
| 95 * @param {!Event} event | 95 * @param {!Event} event |
| 96 */ | 96 */ |
| 97 _editorKeyDown(event) { | 97 _editorKeyDown(event) { |
| 98 var keyboardEvent = /** @type {!KeyboardEvent} */ (event); | 98 var keyboardEvent = /** @type {!KeyboardEvent} */ (event); |
| 99 var newText; | 99 var newText; |
| 100 var isPrevious; | 100 var isPrevious; |
| 101 | 101 |
| 102 switch (keyboardEvent.keyCode) { | 102 switch (keyboardEvent.keyCode) { |
| 103 case WebInspector.KeyboardShortcut.Keys.Up.code: | 103 case UI.KeyboardShortcut.Keys.Up.code: |
| 104 if (this._editor.selection().endLine > 0) | 104 if (this._editor.selection().endLine > 0) |
| 105 break; | 105 break; |
| 106 newText = this._history.previous(this.text()); | 106 newText = this._history.previous(this.text()); |
| 107 isPrevious = true; | 107 isPrevious = true; |
| 108 break; | 108 break; |
| 109 case WebInspector.KeyboardShortcut.Keys.Down.code: | 109 case UI.KeyboardShortcut.Keys.Down.code: |
| 110 if (this._editor.selection().endLine < this._editor.fullRange().endLine) | 110 if (this._editor.selection().endLine < this._editor.fullRange().endLine) |
| 111 break; | 111 break; |
| 112 newText = this._history.next(); | 112 newText = this._history.next(); |
| 113 break; | 113 break; |
| 114 case WebInspector.KeyboardShortcut.Keys.P.code: // Ctrl+P = Previous | 114 case UI.KeyboardShortcut.Keys.P.code: // Ctrl+P = Previous |
| 115 if (WebInspector.isMac() && keyboardEvent.ctrlKey && !keyboardEvent.meta
Key && !keyboardEvent.altKey && | 115 if (Host.isMac() && keyboardEvent.ctrlKey && !keyboardEvent.metaKey && !
keyboardEvent.altKey && |
| 116 !keyboardEvent.shiftKey) { | 116 !keyboardEvent.shiftKey) { |
| 117 newText = this._history.previous(this.text()); | 117 newText = this._history.previous(this.text()); |
| 118 isPrevious = true; | 118 isPrevious = true; |
| 119 } | 119 } |
| 120 break; | 120 break; |
| 121 case WebInspector.KeyboardShortcut.Keys.N.code: // Ctrl+N = Next | 121 case UI.KeyboardShortcut.Keys.N.code: // Ctrl+N = Next |
| 122 if (WebInspector.isMac() && keyboardEvent.ctrlKey && !keyboardEvent.meta
Key && !keyboardEvent.altKey && | 122 if (Host.isMac() && keyboardEvent.ctrlKey && !keyboardEvent.metaKey && !
keyboardEvent.altKey && |
| 123 !keyboardEvent.shiftKey) | 123 !keyboardEvent.shiftKey) |
| 124 newText = this._history.next(); | 124 newText = this._history.next(); |
| 125 break; | 125 break; |
| 126 case WebInspector.KeyboardShortcut.Keys.Enter.code: | 126 case UI.KeyboardShortcut.Keys.Enter.code: |
| 127 this._enterKeyPressed(keyboardEvent); | 127 this._enterKeyPressed(keyboardEvent); |
| 128 break; | 128 break; |
| 129 } | 129 } |
| 130 | 130 |
| 131 if (newText === undefined) | 131 if (newText === undefined) |
| 132 return; | 132 return; |
| 133 keyboardEvent.consume(true); | 133 keyboardEvent.consume(true); |
| 134 this.setText(newText); | 134 this.setText(newText); |
| 135 | 135 |
| 136 if (isPrevious) | 136 if (isPrevious) |
| 137 this._editor.setSelection(WebInspector.TextRange.createFromLocation(0, Inf
inity)); | 137 this._editor.setSelection(Common.TextRange.createFromLocation(0, Infinity)
); |
| 138 else | 138 else |
| 139 this.moveCaretToEndOfPrompt(); | 139 this.moveCaretToEndOfPrompt(); |
| 140 } | 140 } |
| 141 | 141 |
| 142 /** | 142 /** |
| 143 * @param {!KeyboardEvent} event | 143 * @param {!KeyboardEvent} event |
| 144 */ | 144 */ |
| 145 _enterKeyPressed(event) { | 145 _enterKeyPressed(event) { |
| 146 if (event.altKey || event.ctrlKey || event.shiftKey) | 146 if (event.altKey || event.ctrlKey || event.shiftKey) |
| 147 return; | 147 return; |
| 148 | 148 |
| 149 event.consume(true); | 149 event.consume(true); |
| 150 | 150 |
| 151 this.clearAutocomplete(); | 151 this.clearAutocomplete(); |
| 152 | 152 |
| 153 var str = this.text(); | 153 var str = this.text(); |
| 154 if (!str.length) | 154 if (!str.length) |
| 155 return; | 155 return; |
| 156 | 156 |
| 157 var currentExecutionContext = WebInspector.context.flavor(WebInspector.Execu
tionContext); | 157 var currentExecutionContext = UI.context.flavor(SDK.ExecutionContext); |
| 158 if (!this._isCaretAtEndOfPrompt() || !currentExecutionContext) { | 158 if (!this._isCaretAtEndOfPrompt() || !currentExecutionContext) { |
| 159 this._appendCommand(str, true); | 159 this._appendCommand(str, true); |
| 160 return; | 160 return; |
| 161 } | 161 } |
| 162 currentExecutionContext.target().runtimeModel.compileScript( | 162 currentExecutionContext.target().runtimeModel.compileScript( |
| 163 str, '', false, currentExecutionContext.id, compileCallback.bind(this)); | 163 str, '', false, currentExecutionContext.id, compileCallback.bind(this)); |
| 164 | 164 |
| 165 /** | 165 /** |
| 166 * @param {!Protocol.Runtime.ScriptId=} scriptId | 166 * @param {!Protocol.Runtime.ScriptId=} scriptId |
| 167 * @param {?Protocol.Runtime.ExceptionDetails=} exceptionDetails | 167 * @param {?Protocol.Runtime.ExceptionDetails=} exceptionDetails |
| 168 * @this {WebInspector.ConsolePrompt} | 168 * @this {Console.ConsolePrompt} |
| 169 */ | 169 */ |
| 170 function compileCallback(scriptId, exceptionDetails) { | 170 function compileCallback(scriptId, exceptionDetails) { |
| 171 if (str !== this.text()) | 171 if (str !== this.text()) |
| 172 return; | 172 return; |
| 173 if (exceptionDetails && | 173 if (exceptionDetails && |
| 174 (exceptionDetails.exception.description.startsWith('SyntaxError: Unexp
ected end of input') || | 174 (exceptionDetails.exception.description.startsWith('SyntaxError: Unexp
ected end of input') || |
| 175 exceptionDetails.exception.description.startsWith('SyntaxError: Unter
minated template literal'))) { | 175 exceptionDetails.exception.description.startsWith('SyntaxError: Unter
minated template literal'))) { |
| 176 this._editor.newlineAndIndent(); | 176 this._editor.newlineAndIndent(); |
| 177 this._enterProcessedForTest(); | 177 this._enterProcessedForTest(); |
| 178 return; | 178 return; |
| 179 } | 179 } |
| 180 this._appendCommand(str, true); | 180 this._appendCommand(str, true); |
| 181 this._enterProcessedForTest(); | 181 this._enterProcessedForTest(); |
| 182 } | 182 } |
| 183 } | 183 } |
| 184 | 184 |
| 185 /** | 185 /** |
| 186 * @param {string} text | 186 * @param {string} text |
| 187 * @param {boolean} useCommandLineAPI | 187 * @param {boolean} useCommandLineAPI |
| 188 */ | 188 */ |
| 189 _appendCommand(text, useCommandLineAPI) { | 189 _appendCommand(text, useCommandLineAPI) { |
| 190 this.setText(''); | 190 this.setText(''); |
| 191 var currentExecutionContext = WebInspector.context.flavor(WebInspector.Execu
tionContext); | 191 var currentExecutionContext = UI.context.flavor(SDK.ExecutionContext); |
| 192 if (currentExecutionContext) { | 192 if (currentExecutionContext) { |
| 193 WebInspector.ConsoleModel.evaluateCommandInConsole(currentExecutionContext
, text, useCommandLineAPI); | 193 SDK.ConsoleModel.evaluateCommandInConsole(currentExecutionContext, text, u
seCommandLineAPI); |
| 194 if (WebInspector.ConsolePanel.instance().isShowing()) | 194 if (Console.ConsolePanel.instance().isShowing()) |
| 195 WebInspector.userMetrics.actionTaken(WebInspector.UserMetrics.Action.Com
mandEvaluatedInConsolePanel); | 195 Host.userMetrics.actionTaken(Host.UserMetrics.Action.CommandEvaluatedInC
onsolePanel); |
| 196 } | 196 } |
| 197 } | 197 } |
| 198 | 198 |
| 199 _enterProcessedForTest() { | 199 _enterProcessedForTest() { |
| 200 } | 200 } |
| 201 | 201 |
| 202 /** | 202 /** |
| 203 * @param {string} prefix | 203 * @param {string} prefix |
| 204 * @param {boolean=} force | 204 * @param {boolean=} force |
| 205 * @return {!WebInspector.SuggestBox.Suggestions} | 205 * @return {!UI.SuggestBox.Suggestions} |
| 206 */ | 206 */ |
| 207 _historyCompletions(prefix, force) { | 207 _historyCompletions(prefix, force) { |
| 208 if (!this._addCompletionsFromHistory || !this._isCaretAtEndOfPrompt() || (!p
refix && !force)) | 208 if (!this._addCompletionsFromHistory || !this._isCaretAtEndOfPrompt() || (!p
refix && !force)) |
| 209 return []; | 209 return []; |
| 210 var result = []; | 210 var result = []; |
| 211 var text = this.text(); | 211 var text = this.text(); |
| 212 var set = new Set(); | 212 var set = new Set(); |
| 213 var data = this._history.historyData(); | 213 var data = this._history.historyData(); |
| 214 for (var i = data.length - 1; i >= 0 && result.length < 50; --i) { | 214 for (var i = data.length - 1; i >= 0 && result.length < 50; --i) { |
| 215 var item = data[i]; | 215 var item = data[i]; |
| (...skipping 13 matching lines...) Expand all Loading... |
| 229 focus() { | 229 focus() { |
| 230 if (this._editor) | 230 if (this._editor) |
| 231 this._editor.widget().focus(); | 231 this._editor.widget().focus(); |
| 232 else | 232 else |
| 233 this.element.focus(); | 233 this.element.focus(); |
| 234 } | 234 } |
| 235 | 235 |
| 236 /** | 236 /** |
| 237 * @param {number} lineNumber | 237 * @param {number} lineNumber |
| 238 * @param {number} columnNumber | 238 * @param {number} columnNumber |
| 239 * @return {?WebInspector.TextRange} | 239 * @return {?Common.TextRange} |
| 240 */ | 240 */ |
| 241 _substituteRange(lineNumber, columnNumber) { | 241 _substituteRange(lineNumber, columnNumber) { |
| 242 var lineText = this._editor.line(lineNumber); | 242 var lineText = this._editor.line(lineNumber); |
| 243 var index; | 243 var index; |
| 244 for (index = lineText.length - 1; index >= 0; index--) { | 244 for (index = lineText.length - 1; index >= 0; index--) { |
| 245 if (' =:[({;,!+-*/&|^<>.'.indexOf(lineText.charAt(index)) !== -1) | 245 if (' =:[({;,!+-*/&|^<>.'.indexOf(lineText.charAt(index)) !== -1) |
| 246 break; | 246 break; |
| 247 } | 247 } |
| 248 return new WebInspector.TextRange(lineNumber, index + 1, lineNumber, columnN
umber); | 248 return new Common.TextRange(lineNumber, index + 1, lineNumber, columnNumber)
; |
| 249 } | 249 } |
| 250 | 250 |
| 251 /** | 251 /** |
| 252 * @param {!WebInspector.TextRange} queryRange | 252 * @param {!Common.TextRange} queryRange |
| 253 * @param {!WebInspector.TextRange} substituteRange | 253 * @param {!Common.TextRange} substituteRange |
| 254 * @param {boolean=} force | 254 * @param {boolean=} force |
| 255 * @param {string=} currentTokenType | 255 * @param {string=} currentTokenType |
| 256 * @return {!Promise<!WebInspector.SuggestBox.Suggestions>} | 256 * @return {!Promise<!UI.SuggestBox.Suggestions>} |
| 257 */ | 257 */ |
| 258 _wordsWithQuery(queryRange, substituteRange, force, currentTokenType) { | 258 _wordsWithQuery(queryRange, substituteRange, force, currentTokenType) { |
| 259 var query = this._editor.text(queryRange); | 259 var query = this._editor.text(queryRange); |
| 260 var before = this._editor.text(new WebInspector.TextRange(0, 0, queryRange.s
tartLine, queryRange.startColumn)); | 260 var before = this._editor.text(new Common.TextRange(0, 0, queryRange.startLi
ne, queryRange.startColumn)); |
| 261 var historyWords = this._historyCompletions(query, force); | 261 var historyWords = this._historyCompletions(query, force); |
| 262 | 262 |
| 263 var excludedTokens = new Set(['js-comment', 'js-string-2']); | 263 var excludedTokens = new Set(['js-comment', 'js-string-2']); |
| 264 if (!before.endsWith('[')) | 264 if (!before.endsWith('[')) |
| 265 excludedTokens.add('js-string'); | 265 excludedTokens.add('js-string'); |
| 266 if (excludedTokens.has(currentTokenType)) | 266 if (excludedTokens.has(currentTokenType)) |
| 267 return Promise.resolve(historyWords); | 267 return Promise.resolve(historyWords); |
| 268 | 268 |
| 269 return WebInspector.JavaScriptAutocomplete.completionsForTextInCurrentContex
t(before, query, force) | 269 return Components.JavaScriptAutocomplete.completionsForTextInCurrentContext(
before, query, force) |
| 270 .then(innerWordsWithQuery); | 270 .then(innerWordsWithQuery); |
| 271 /** | 271 /** |
| 272 * @param {!Array<string>} words | 272 * @param {!Array<string>} words |
| 273 * @return {!WebInspector.SuggestBox.Suggestions} | 273 * @return {!UI.SuggestBox.Suggestions} |
| 274 */ | 274 */ |
| 275 function innerWordsWithQuery(words) { | 275 function innerWordsWithQuery(words) { |
| 276 return words.map(item => ({title: item})).concat(historyWords); | 276 return words.map(item => ({title: item})).concat(historyWords); |
| 277 } | 277 } |
| 278 } | 278 } |
| 279 | 279 |
| 280 _editorSetForTest() { | 280 _editorSetForTest() { |
| 281 } | 281 } |
| 282 }; | 282 }; |
| 283 | 283 |
| 284 /** | 284 /** |
| 285 * @unrestricted | 285 * @unrestricted |
| 286 */ | 286 */ |
| 287 WebInspector.ConsoleHistoryManager = class { | 287 Console.ConsoleHistoryManager = class { |
| 288 constructor() { | 288 constructor() { |
| 289 /** | 289 /** |
| 290 * @type {!Array.<string>} | 290 * @type {!Array.<string>} |
| 291 */ | 291 */ |
| 292 this._data = []; | 292 this._data = []; |
| 293 | 293 |
| 294 /** | 294 /** |
| 295 * 1-based entry in the history stack. | 295 * 1-based entry in the history stack. |
| 296 * @type {number} | 296 * @type {number} |
| 297 */ | 297 */ |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 363 return this._currentHistoryItem(); | 363 return this._currentHistoryItem(); |
| 364 } | 364 } |
| 365 | 365 |
| 366 /** | 366 /** |
| 367 * @return {string|undefined} | 367 * @return {string|undefined} |
| 368 */ | 368 */ |
| 369 _currentHistoryItem() { | 369 _currentHistoryItem() { |
| 370 return this._data[this._data.length - this._historyOffset]; | 370 return this._data[this._data.length - this._historyOffset]; |
| 371 } | 371 } |
| 372 }; | 372 }; |
| OLD | NEW |