Chromium Code Reviews| 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 /** | 5 /** |
| 6 * @constructor | 6 * @constructor |
| 7 * @extends {WebInspector.Widget} | 7 * @extends {WebInspector.Widget} |
| 8 */ | 8 */ |
| 9 WebInspector.ConsolePrompt = function() | 9 WebInspector.ConsolePrompt = function() |
| 10 { | 10 { |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 61 | 61 |
| 62 clearAutocomplete: function() | 62 clearAutocomplete: function() |
| 63 { | 63 { |
| 64 if (this._editor) | 64 if (this._editor) |
| 65 this._editor.clearAutocomplete(); | 65 this._editor.clearAutocomplete(); |
| 66 }, | 66 }, |
| 67 | 67 |
| 68 /** | 68 /** |
| 69 * @return {boolean} | 69 * @return {boolean} |
| 70 */ | 70 */ |
| 71 isCaretInsidePrompt: function() | 71 _isCaretAtEndOfPrompt: function() |
| 72 { | |
| 73 return this.hasFocus(); | |
| 74 }, | |
| 75 | |
| 76 /** | |
| 77 * @return {boolean} | |
| 78 */ | |
| 79 isCaretAtEndOfPrompt: function() | |
| 80 { | 72 { |
| 81 return !!this._editor && this._editor.selection().collapseToEnd().equal( this._editor.fullRange().collapseToEnd()); | 73 return !!this._editor && this._editor.selection().collapseToEnd().equal( this._editor.fullRange().collapseToEnd()); |
| 82 }, | 74 }, |
| 83 | 75 |
| 84 /** | |
|
einbinder
2016/09/22 22:38:14
All these helper methods get to go away!
| |
| 85 * @return {boolean} | |
| 86 */ | |
| 87 isCaretOnLastLine: function() | |
| 88 { | |
| 89 return !!this._editor && this._editor.selection().endLine === this._edit or.fullRange().endLine; | |
| 90 }, | |
| 91 | |
| 92 /** | |
| 93 * @return {boolean} | |
| 94 */ | |
| 95 isCaretOnFirstLine: function() | |
| 96 { | |
| 97 return !!this._editor && this._editor.selection().endLine === 0; | |
| 98 }, | |
| 99 | |
| 100 moveCaretToEndOfPrompt: function() | 76 moveCaretToEndOfPrompt: function() |
| 101 { | 77 { |
| 102 if (this._editor) | 78 if (this._editor) |
| 103 this._editor.setSelection(WebInspector.TextRange.createFromLocation( Infinity,Infinity)); | 79 this._editor.setSelection(WebInspector.TextRange.createFromLocation( Infinity,Infinity)); |
| 104 }, | 80 }, |
| 105 | 81 |
| 106 moveCaretToEndOfFirstLine: function() | |
| 107 { | |
| 108 if (this._editor) | |
| 109 this._editor.setSelection(WebInspector.TextRange.createFromLocation( 0,Infinity)); | |
| 110 }, | |
| 111 | |
| 112 /** | 82 /** |
| 113 * @param {string} text | 83 * @param {string} text |
| 114 */ | 84 */ |
| 115 setText: function(text) | 85 setText: function(text) |
| 116 { | 86 { |
| 117 if (this._editor) | 87 if (this._editor) |
| 118 this._editor.setText(text); | 88 this._editor.setText(text); |
| 119 else | 89 else |
| 120 this._initialText = text; | 90 this._initialText = text; |
| 121 }, | 91 }, |
| 122 | 92 |
| 123 /** | 93 /** |
| 124 * @return {string} | 94 * @return {string} |
| 125 */ | 95 */ |
| 126 text: function() | 96 text: function() |
| 127 { | 97 { |
| 128 return this._editor ? this._editor.text() : this._initialText; | 98 return this._editor ? this._editor.text() : this._initialText; |
| 129 }, | 99 }, |
| 130 | 100 |
| 131 newlineAndIndent: function() | |
| 132 { | |
| 133 this._editor.newlineAndIndent(); | |
| 134 }, | |
| 135 | |
| 136 /** | 101 /** |
| 137 * @param {boolean} value | 102 * @param {boolean} value |
| 138 */ | 103 */ |
| 139 setAddCompletionsFromHistory: function(value) | 104 setAddCompletionsFromHistory: function(value) |
| 140 { | 105 { |
| 141 this._addCompletionsFromHistory = value; | 106 this._addCompletionsFromHistory = value; |
| 142 }, | 107 }, |
| 143 | 108 |
| 144 /** | 109 /** |
| 145 * @param {!Event} event | 110 * @param {!Event} event |
| 146 */ | 111 */ |
| 147 _editorKeyDown: function(event) | 112 _editorKeyDown: function(event) |
| 148 { | 113 { |
| 149 var keyboardEvent = /** @type {!KeyboardEvent} */ (event); | 114 var keyboardEvent = /** @type {!KeyboardEvent} */ (event); |
| 150 var newText; | 115 var newText; |
| 151 var isPrevious; | 116 var isPrevious; |
| 152 | 117 |
| 153 switch (keyboardEvent.keyCode) { | 118 switch (keyboardEvent.keyCode) { |
| 154 case WebInspector.KeyboardShortcut.Keys.Up.code: | 119 case WebInspector.KeyboardShortcut.Keys.Up.code: |
| 155 if (!this.isCaretOnFirstLine()) | 120 if (this._editor.selection().endLine > 0) |
| 156 break; | 121 break; |
| 157 newText = this._history.previous(this.text()); | 122 newText = this._history.previous(this.text()); |
| 158 isPrevious = true; | 123 isPrevious = true; |
| 159 break; | 124 break; |
| 160 case WebInspector.KeyboardShortcut.Keys.Down.code: | 125 case WebInspector.KeyboardShortcut.Keys.Down.code: |
| 161 if (!this.isCaretOnLastLine()) | 126 if (this._editor.selection().endLine < this._editor.fullRange().endL ine) |
| 162 break; | 127 break; |
| 163 newText = this._history.next(); | 128 newText = this._history.next(); |
| 164 break; | 129 break; |
| 165 case WebInspector.KeyboardShortcut.Keys.P.code: // Ctrl+P = Previous | 130 case WebInspector.KeyboardShortcut.Keys.P.code: // Ctrl+P = Previous |
| 166 if (WebInspector.isMac() && keyboardEvent.ctrlKey && !keyboardEvent. metaKey && !keyboardEvent.altKey && !keyboardEvent.shiftKey) { | 131 if (WebInspector.isMac() && keyboardEvent.ctrlKey && !keyboardEvent. metaKey && !keyboardEvent.altKey && !keyboardEvent.shiftKey) { |
| 167 newText = this._history.previous(this.text()); | 132 newText = this._history.previous(this.text()); |
| 168 isPrevious = true; | 133 isPrevious = true; |
| 169 } | 134 } |
| 170 break; | 135 break; |
| 171 case WebInspector.KeyboardShortcut.Keys.N.code: // Ctrl+N = Next | 136 case WebInspector.KeyboardShortcut.Keys.N.code: // Ctrl+N = Next |
| 172 if (WebInspector.isMac() && keyboardEvent.ctrlKey && !keyboardEvent. metaKey && !keyboardEvent.altKey && !keyboardEvent.shiftKey) | 137 if (WebInspector.isMac() && keyboardEvent.ctrlKey && !keyboardEvent. metaKey && !keyboardEvent.altKey && !keyboardEvent.shiftKey) |
| 173 newText = this._history.next(); | 138 newText = this._history.next(); |
| 174 break; | 139 break; |
| 140 case WebInspector.KeyboardShortcut.Keys.Enter.code: | |
| 141 this._enterKeyPressed(keyboardEvent); | |
| 142 break; | |
| 175 } | 143 } |
| 176 | 144 |
| 177 if (newText === undefined) | 145 if (newText === undefined) |
| 178 return; | 146 return; |
| 179 keyboardEvent.consume(true); | 147 keyboardEvent.consume(true); |
| 180 this.setText(newText); | 148 this.setText(newText); |
| 181 | 149 |
| 182 if (isPrevious) | 150 if (isPrevious) |
| 183 this.moveCaretToEndOfFirstLine(); | 151 this._editor.setSelection(WebInspector.TextRange.createFromLocation( 0,Infinity)); |
| 184 else | 152 else |
| 185 this.moveCaretToEndOfPrompt(); | 153 this.moveCaretToEndOfPrompt(); |
| 186 }, | 154 }, |
| 187 | 155 |
| 188 /** | 156 /** |
| 157 * @param {!KeyboardEvent} event | |
| 158 */ | |
| 159 _enterKeyPressed: function(event) | |
| 160 { | |
| 161 if (event.altKey || event.ctrlKey || event.shiftKey) | |
| 162 return; | |
| 163 | |
| 164 event.consume(true); | |
| 165 | |
| 166 this.clearAutocomplete(); | |
| 167 | |
| 168 var str = this.text(); | |
| 169 if (!str.length) | |
| 170 return; | |
| 171 | |
| 172 var target = WebInspector.targetManager.mainTarget(); | |
| 173 var currentExecutionContext = WebInspector.context.flavor(WebInspector.E xecutionContext); | |
| 174 if (!this._isCaretAtEndOfPrompt() || !target || !currentExecutionContext ) { | |
| 175 this._appendCommand(str, true); | |
| 176 return; | |
| 177 } | |
| 178 target.runtimeModel.compileScript(str, "", false, currentExecutionContex t.id, compileCallback.bind(this)); | |
| 179 | |
| 180 /** | |
| 181 * @param {!RuntimeAgent.ScriptId=} scriptId | |
| 182 * @param {?RuntimeAgent.ExceptionDetails=} exceptionDetails | |
| 183 * @this {WebInspector.ConsolePrompt} | |
| 184 */ | |
| 185 function compileCallback(scriptId, exceptionDetails) | |
| 186 { | |
| 187 if (str !== this.text()) | |
| 188 return; | |
| 189 if (exceptionDetails && (exceptionDetails.exception.description === "SyntaxError: Unexpected end of input" | |
| 190 || exceptionDetails.exception.description === "SyntaxError: Unte rminated template literal")) { | |
| 191 this._editor.newlineAndIndent(); | |
| 192 this._enterProcessedForTest(); | |
| 193 return; | |
| 194 } | |
| 195 this._appendCommand(str, true); | |
| 196 this._enterProcessedForTest(); | |
| 197 } | |
| 198 }, | |
| 199 | |
| 200 /** | |
| 201 * @param {string} text | |
| 202 * @param {boolean} useCommandLineAPI | |
| 203 */ | |
| 204 _appendCommand: function(text, useCommandLineAPI) | |
| 205 { | |
| 206 this.setText(""); | |
| 207 var currentExecutionContext = WebInspector.context.flavor(WebInspector.E xecutionContext); | |
| 208 if (currentExecutionContext) { | |
| 209 WebInspector.ConsoleModel.evaluateCommandInConsole(currentExecutionC ontext, text, useCommandLineAPI); | |
| 210 if (WebInspector.inspectorView.currentPanel() && WebInspector.inspec torView.currentPanel().name === "console") | |
| 211 WebInspector.userMetrics.actionTaken(WebInspector.UserMetrics.Ac tion.CommandEvaluatedInConsolePanel); | |
| 212 } | |
| 213 }, | |
| 214 | |
| 215 _enterProcessedForTest: function() { }, | |
| 216 | |
| 217 /** | |
| 189 * @param {string} prefix | 218 * @param {string} prefix |
| 190 * @return {!WebInspector.SuggestBox.Suggestions} | 219 * @return {!WebInspector.SuggestBox.Suggestions} |
| 191 */ | 220 */ |
| 192 _historyCompletions: function(prefix) | 221 _historyCompletions: function(prefix) |
| 193 { | 222 { |
| 194 if (!this._addCompletionsFromHistory || !this.isCaretAtEndOfPrompt()) | 223 if (!this._addCompletionsFromHistory || !this._isCaretAtEndOfPrompt()) |
| 195 return []; | 224 return []; |
| 196 var result = []; | 225 var result = []; |
| 197 var text = this.text(); | 226 var text = this.text(); |
| 198 var set = new Set(); | 227 var set = new Set(); |
| 199 var data = this._history.historyData(); | 228 var data = this._history.historyData(); |
| 200 for (var i = data.length - 1; i >= 0 && result.length < 50; --i) { | 229 for (var i = data.length - 1; i >= 0 && result.length < 50; --i) { |
| 201 var item = data[i]; | 230 var item = data[i]; |
| 202 if (!item.startsWith(text)) | 231 if (!item.startsWith(text)) |
| 203 continue; | 232 continue; |
| 204 if (set.has(item)) | 233 if (set.has(item)) |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 257 function innerWordsWithPrefix(words) | 286 function innerWordsWithPrefix(words) |
| 258 { | 287 { |
| 259 fulfill(words.map(item => ({title:item})).concat(historyWords)); | 288 fulfill(words.map(item => ({title:item})).concat(historyWords)); |
| 260 } | 289 } |
| 261 }, | 290 }, |
| 262 | 291 |
| 263 _editorSetForTest: function() { }, | 292 _editorSetForTest: function() { }, |
| 264 | 293 |
| 265 __proto__: WebInspector.Widget.prototype | 294 __proto__: WebInspector.Widget.prototype |
| 266 } | 295 } |
| OLD | NEW |