OLD | NEW |
---|---|
(Empty) | |
1 /** | |
2 * @constructor | |
3 */ | |
4 WebInspector.ConsolePrompt = function() | |
5 { | |
6 this._addCompletionsFromHistory = true; | |
7 this._history = new WebInspector.HistoryManager(); | |
8 } | |
9 | |
10 /** | |
11 * @return {!Promise} | |
12 */ | |
13 WebInspector.ConsolePrompt.create = function() | |
14 { | |
15 var prompt = new WebInspector.ConsolePrompt(); | |
16 return self.runtime.extension(WebInspector.TextEditorFactory).instance().the n(innerCreate); | |
17 | |
18 /** | |
19 * @param {!WebInspector.TextEditorFactory} factory | |
20 */ | |
21 function innerCreate(factory) | |
22 { | |
23 prompt.setEditor(factory.createEditor({ | |
dgozman
2016/09/12 21:38:56
Let's pass editor/factory in constructor instead.
einbinder
2016/09/13 01:07:59
Done.
| |
24 lineNumbers: false, | |
25 lineWrapping: true, | |
26 bracketMatchingFlag: "consoleBracketMatching", | |
27 mimeType: "javascript", | |
28 autoHeight: true | |
29 })); | |
30 return prompt; | |
31 } | |
32 } | |
33 WebInspector.ConsolePrompt.prototype = { | |
34 | |
35 /** | |
36 * @return {!WebInspector.HistoryManager} | |
37 */ | |
38 history: function() | |
39 { | |
40 return this._history; | |
41 }, | |
42 | |
43 /** | |
44 * @param {!WebInspector.TextEditor} editor | |
45 */ | |
46 setEditor: function(editor) | |
47 { | |
48 this._editor = editor; | |
49 this._editor.configureAutocomplete({ | |
50 substituteRangeCallback: this._substituteRange.bind(this), | |
51 suggestionsCallback: this._wordsWithPrefix.bind(this), | |
52 captureEnter: true | |
53 }) | |
54 this.addKeyDownHandler(this._editorKeyDown.bind(this)); | |
55 this.element = this._editor.widget().element; | |
56 }, | |
57 | |
58 /** | |
59 * @param {!Element} element | |
60 * @return {!Element} | |
61 */ | |
62 attach: function(element) | |
dgozman
2016/09/12 21:38:56
Let's maybe make ConsolePrompt a widget itself?
einbinder
2016/09/13 01:07:59
Done.
einbinder
2016/09/13 01:07:59
Done.
| |
63 { | |
64 this._editor.widget().show(element); | |
65 return this._editor.widget().element; | |
66 }, | |
67 | |
68 /** | |
69 * @param {function(!KeyboardEvent)} handler | |
70 */ | |
71 addKeyDownHandler: function(handler) | |
dgozman
2016/09/12 21:38:56
Is this used outside?
einbinder
2016/09/13 01:07:59
Removed.
| |
72 { | |
73 this._editor.addKeyDownHandler(handler); | |
74 }, | |
75 | |
76 clearAutoComplete: function() | |
dgozman
2016/09/12 21:38:57
|clearAutocomplete| to match editor.
einbinder
2016/09/13 01:07:59
Done.
| |
77 { | |
78 this._editor.clearAutocomplete(); | |
79 }, | |
80 | |
81 /** | |
82 * @return {boolean} | |
83 */ | |
84 isCaretInsidePrompt: function() | |
85 { | |
86 return this.hasFocus(); | |
87 }, | |
88 | |
89 /** | |
90 * @return {boolean} | |
91 */ | |
92 isCaretAtEndOfPrompt: function() | |
93 { | |
94 return this._editor.selection().collapseToEnd().compareTo(this._editor.f ullRange().collapseToEnd()) === 0; | |
95 }, | |
96 | |
97 /** | |
98 * @return {boolean} | |
99 */ | |
100 isCaretOnLastLine: function() | |
101 { | |
102 return this._editor.selection().endLine === this._editor.fullRange().end Line; | |
103 }, | |
104 | |
105 /** | |
106 * @return {boolean} | |
107 */ | |
108 isCaretOnFirstLine: function() | |
109 { | |
110 return (this._editor.selection().endLine === 0); | |
dgozman
2016/09/12 21:38:56
nit: extra ()
einbinder
2016/09/13 01:07:59
Done.
| |
111 }, | |
112 | |
113 moveCaretToEndOfPrompt: function() | |
114 { | |
115 this._editor.setSelection(WebInspector.TextRange.createFromLocation(Infi nity,Infinity)); | |
116 }, | |
117 | |
118 moveCaretToEndOfFirstLine: function() | |
119 { | |
120 this._editor.setSelection(WebInspector.TextRange.createFromLocation(0,In finity)); | |
121 }, | |
122 | |
123 /** | |
124 * @param {string} text | |
125 */ | |
126 setText: function(text){ | |
dgozman
2016/09/12 21:38:56
{ on next line
einbinder
2016/09/13 01:07:59
Done.
| |
127 this._editor.setText(text); | |
128 }, | |
129 | |
130 /** | |
131 * @return {string} | |
132 */ | |
133 text: function() | |
134 { | |
135 return this._editor.text(); | |
136 }, | |
137 | |
138 setAddCompletionsFromHistory: function(value) | |
dgozman
2016/09/12 21:38:57
annotate please.
einbinder
2016/09/13 01:07:59
Done.
| |
139 { | |
140 this._addCompletionsFromHistory = value; | |
141 }, | |
142 | |
143 /** | |
144 * @param {!KeyboardEvent} event | |
145 */ | |
146 _editorKeyDown: function(event) | |
147 { | |
148 var newText; | |
149 var isPrevious; | |
150 | |
151 switch (event.keyCode) { | |
152 case WebInspector.KeyboardShortcut.Keys.Up.code: | |
153 if (!this.isCaretOnFirstLine()) | |
154 break; | |
155 newText = this._history.previous(this.text()); | |
156 isPrevious = true; | |
157 break; | |
158 case WebInspector.KeyboardShortcut.Keys.Down.code: | |
159 if (!this.isCaretOnLastLine()) | |
160 break; | |
161 newText = this._history.next(); | |
162 break; | |
163 case WebInspector.KeyboardShortcut.Keys.P.code: // Ctrl+P = Previous | |
164 if (WebInspector.isMac() && event.ctrlKey && !event.metaKey && !even t.altKey && !event.shiftKey) { | |
165 newText = this._history.previous(this.text()); | |
166 isPrevious = true; | |
167 } | |
168 break; | |
169 case WebInspector.KeyboardShortcut.Keys.N.code: // Ctrl+N = Next | |
170 if (WebInspector.isMac() && event.ctrlKey && !event.metaKey && !even t.altKey && !event.shiftKey) | |
171 newText = this._history.next(); | |
172 break; | |
173 } | |
174 | |
175 if (newText !== undefined) { | |
176 event.consume(true); | |
177 this.setText(newText); | |
178 | |
179 if (isPrevious) | |
180 this.moveCaretToEndOfFirstLine(); | |
181 else | |
182 this.moveCaretToEndOfPrompt(); | |
183 return; | |
dgozman
2016/09/12 21:38:56
No need to return.
einbinder
2016/09/13 01:07:59
Done.
| |
184 } | |
185 }, | |
186 | |
187 /** | |
188 * @param {string} prefix | |
189 * @return {!WebInspector.SuggestBox.Suggestions} | |
190 */ | |
191 historyCompletions: function(prefix) | |
dgozman
2016/09/12 21:38:56
Is this used outside?
einbinder
2016/09/13 01:07:59
Doesn't appear to be anymore. Fixed.
| |
192 { | |
193 if (!this._addCompletionsFromHistory || !this.isCaretAtEndOfPrompt()) | |
194 return []; | |
195 var result = []; | |
196 var text = this.text(); | |
197 var set = new Set(); | |
198 var data = this._history.historyData(); | |
199 for (var i = data.length - 1; i >= 0 && result.length < 50; --i) { | |
200 var item = data[i]; | |
201 if (!item.startsWith(text)) | |
202 continue; | |
203 if (set.has(item)) | |
204 continue; | |
205 set.add(item); | |
206 result.push({title: item.substring(text.length - prefix.length), cla ssName: "additional"}); | |
207 } | |
208 return result; | |
209 }, | |
210 | |
211 focus: function() | |
212 { | |
213 this._editor.focus(); | |
214 }, | |
215 | |
216 /** | |
217 * @return {boolean} | |
218 */ | |
219 hasFocus: function() | |
dgozman
2016/09/12 21:38:56
Is this used outside?
einbinder
2016/09/13 01:07:59
Removed.
| |
220 { | |
221 return this._editor.hasFocus(); | |
222 }, | |
223 | |
224 /** | |
225 * @param {number} lineNumber | |
226 * @param {number} columnNumber | |
227 * @return {?WebInspector.TextRange} | |
228 */ | |
229 _substituteRange: function(lineNumber, columnNumber) | |
230 { | |
231 var lineText = this._editor.line(lineNumber); | |
232 var index; | |
233 for (index = lineText.length - 1; index >= 0; index--) { | |
234 if (" =:[({;,!+-*/&|^<>.".indexOf(lineText.charAt(index)) !== -1) | |
235 break; | |
236 } | |
237 return new WebInspector.TextRange(lineNumber, index + 1, lineNumber, col umnNumber); | |
238 }, | |
239 | |
240 /** | |
241 * @param {!WebInspector.TextRange} prefixRange | |
242 * @param {!WebInspector.TextRange} substituteRange | |
243 * @return {!Promise.<!Array.<{title: string, className: (string|undefined)} >>} | |
244 */ | |
245 _wordsWithPrefix: function(prefixRange, substituteRange) | |
246 { | |
247 var resolve; | |
248 var promise = new Promise(fufill => resolve = fufill); | |
249 var prefix = this._editor.text(prefixRange); | |
250 var before = this._editor.text(new WebInspector.TextRange(0,0,prefixRang e.startLine,prefixRange.startColumn)); | |
251 var historyWords = this.historyCompletions(prefix); | |
252 WebInspector.ExecutionContextSelector.completionsForTextInCurrentContext (before, prefix, false, innerWordsWithPrefix); | |
253 return promise; | |
254 /** | |
255 * @param {!Array.<string>} words | |
256 */ | |
257 function innerWordsWithPrefix(words) | |
258 { | |
259 resolve(words.map(item => ({title:item})).concat(historyWords)); | |
260 } | |
261 } | |
262 } | |
OLD | NEW |