##// END OF EJS Templates
Merge pull request #20 from ivanov/5320-last-thing...
Jonathan Frederic -
r15971:ed37e8a9 merge
parent child Browse files
Show More
@@ -1,378 +1,369
1 1 // function completer.
2 2 //
3 3 // completer should be a class that takes an cell instance
4 4 var IPython = (function (IPython) {
5 5 // that will prevent us from misspelling
6 6 "use strict";
7 7
8 8 // easier key mapping
9 9 var keycodes = IPython.keyboard.keycodes;
10 10
11 11 function prepend_n_prc(str, n) {
12 12 for( var i =0 ; i< n ; i++){
13 13 str = '%'+str ;
14 14 }
15 15 return str;
16 16 }
17 17
18 18 function _existing_completion(item, completion_array){
19 19 for( var c in completion_array ) {
20 20 if(completion_array[c].trim().substr(-item.length) == item)
21 21 { return true; }
22 22 }
23 23 return false;
24 24 }
25 25
26 26 // what is the common start of all completions
27 27 function shared_start(B, drop_prct) {
28 28 if (B.length == 1) {
29 29 return B[0];
30 30 }
31 31 var A = [];
32 32 var common;
33 33 var min_lead_prct = 10;
34 34 for (var i = 0; i < B.length; i++) {
35 35 var str = B[i].str;
36 36 var localmin = 0;
37 37 if(drop_prct === true){
38 38 while ( str.substr(0, 1) == '%') {
39 39 localmin = localmin+1;
40 40 str = str.substring(1);
41 41 }
42 42 }
43 43 min_lead_prct = Math.min(min_lead_prct, localmin);
44 44 A.push(str);
45 45 }
46 46
47 47 if (A.length > 1) {
48 48 var tem1, tem2, s;
49 49 A = A.slice(0).sort();
50 50 tem1 = A[0];
51 51 s = tem1.length;
52 52 tem2 = A.pop();
53 53 while (s && tem2.indexOf(tem1) == -1) {
54 54 tem1 = tem1.substring(0, --s);
55 55 }
56 56 if (tem1 === "" || tem2.indexOf(tem1) !== 0) {
57 57 return {
58 58 str:prepend_n_prc('', min_lead_prct),
59 59 type: "computed",
60 60 from: B[0].from,
61 61 to: B[0].to
62 62 };
63 63 }
64 64 return {
65 65 str: prepend_n_prc(tem1, min_lead_prct),
66 66 type: "computed",
67 67 from: B[0].from,
68 68 to: B[0].to
69 69 };
70 70 }
71 71 return null;
72 72 }
73 73
74 74
75 75 var Completer = function (cell) {
76 76 this.cell = cell;
77 77 this.editor = cell.code_mirror;
78 78 var that = this;
79 79 $([IPython.events]).on('status_busy.Kernel', function () {
80 80 that.skip_kernel_completion = true;
81 81 });
82 82 $([IPython.events]).on('status_idle.Kernel', function () {
83 83 that.skip_kernel_completion = false;
84 84 });
85 85 };
86 86
87 87 Completer.prototype.startCompletion = function () {
88 88 // call for a 'first' completion, that will set the editor and do some
89 89 // special behavior like autopicking if only one completion available.
90 90 if (this.editor.somethingSelected()) return;
91 91 this.done = false;
92 92 // use to get focus back on opera
93 93 this.carry_on_completion(true);
94 94 };
95 95
96 96
97 97 // easy access for julia to monkeypatch
98 98 //
99 99 Completer.reinvoke_re = /[%0-9a-z._/\\:~-]/i;
100 100
101 101 Completer.prototype.reinvoke= function(pre_cursor, block, cursor){
102 102 return Completer.reinvoke_re.test(pre_cursor);
103 103 };
104 104
105 105 /**
106 106 *
107 107 * pass true as parameter if this is the first invocation of the completer
108 108 * this will prevent the completer to dissmiss itself if it is not on a
109 109 * word boundary like pressing tab after a space, and make it autopick the
110 110 * only choice if there is only one which prevent from popping the UI. as
111 111 * well as fast-forwarding the typing if all completion have a common
112 112 * shared start
113 113 **/
114 114 Completer.prototype.carry_on_completion = function (first_invocation) {
115 115 // Pass true as parameter if you want the completer to autopick when
116 116 // only one completion. This function is automatically reinvoked at
117 117 // each keystroke with first_invocation = false
118 118 var cur = this.editor.getCursor();
119 119 var line = this.editor.getLine(cur.line);
120 120 var pre_cursor = this.editor.getRange({
121 121 line: cur.line,
122 122 ch: cur.ch - 1
123 123 }, cur);
124 124
125 125 // we need to check that we are still on a word boundary
126 126 // because while typing the completer is still reinvoking itself
127 127 // so dismiss if we are on a "bad" caracter
128 128 if (!this.reinvoke(pre_cursor) && !first_invocation) {
129 129 this.close();
130 130 return;
131 131 }
132 132
133 133 this.autopick = false;
134 134 if (first_invocation) {
135 135 this.autopick = true;
136 136 }
137 137
138 138 // We want a single cursor position.
139 139 if (this.editor.somethingSelected()) {
140 140 return;
141 141 }
142 142
143 143 // one kernel completion came back, finish_completing will be called with the results
144 144 // we fork here and directly call finish completing if kernel is busy
145 145 if (this.skip_kernel_completion) {
146 146 this.finish_completing({
147 147 'matches': [],
148 148 matched_text: ""
149 149 });
150 150 } else {
151 151 this.cell.kernel.complete(line, cur.ch, $.proxy(this.finish_completing, this));
152 152 }
153 153 };
154 154
155 155 Completer.prototype.finish_completing = function (msg) {
156 156 // let's build a function that wrap all that stuff into what is needed
157 157 // for the new completer:
158 158 var content = msg.content;
159 159 var matched_text = content.matched_text;
160 160 var matches = content.matches;
161 161
162 162 var cur = this.editor.getCursor();
163 163 var results = CodeMirror.contextHint(this.editor);
164 164 var filtered_results = [];
165 165 //remove results from context completion
166 166 //that are already in kernel completion
167 167 for (var elm in results) {
168 168 if (!_existing_completion(results[elm].str, matches)) {
169 169 filtered_results.push(results[elm]);
170 170 }
171 171 }
172 172
173 173 // append the introspection result, in order, at at the beginning of
174 174 // the table and compute the replacement range from current cursor
175 175 // positon and matched_text length.
176 176 for (var i = matches.length - 1; i >= 0; --i) {
177 177 filtered_results.unshift({
178 178 str: matches[i],
179 179 type: "introspection",
180 180 from: {
181 181 line: cur.line,
182 182 ch: cur.ch - matched_text.length
183 183 },
184 184 to: {
185 185 line: cur.line,
186 186 ch: cur.ch
187 187 }
188 188 });
189 189 }
190 190
191 191 // one the 2 sources results have been merge, deal with it
192 192 this.raw_result = filtered_results;
193 193
194 194 // if empty result return
195 195 if (!this.raw_result || !this.raw_result.length) return;
196 196
197 197 // When there is only one completion, use it directly.
198 198 if (this.autopick && this.raw_result.length == 1) {
199 199 this.insert(this.raw_result[0]);
200 200 return;
201 201 }
202 202
203 203 if (this.raw_result.length == 1) {
204 204 // test if first and only completion totally matches
205 205 // what is typed, in this case dismiss
206 206 var str = this.raw_result[0].str;
207 207 var pre_cursor = this.editor.getRange({
208 208 line: cur.line,
209 209 ch: cur.ch - str.length
210 210 }, cur);
211 211 if (pre_cursor == str) {
212 212 this.close();
213 213 return;
214 214 }
215 215 }
216 216
217 217 if (!this.visible) {
218 console.log('add div');
219 218 this.complete = $('<div/>').addClass('completions');
220 219 this.complete.attr('id', 'complete');
221 220
222 221 // Currently webkit doesn't use the size attr correctly. See:
223 222 // https://code.google.com/p/chromium/issues/detail?id=4579
224 223 this.sel = $('<select/>')
225 224 .attr('tabindex', -1)
226 225 .attr('multiple', 'true');
227 226 this.complete.append(this.sel);
228 227 this.visible = true;
229 228 $('body').append(this.complete);
230 229
231 230 //build the container
232 231 var that = this;
233 232 this.sel.dblclick(function () {
234 233 that.pick();
235 234 });
236 235 this.sel.focus(function () {
237 236 that.editor.focus();
238 237 });
239 238 this._handle_keydown = function (cm, event) {
240 239 that.keydown(event);
241 240 };
242 241 this.editor.on('keydown', this._handle_keydown);
243 242 this._handle_keypress = function (cm, event) {
244 243 that.keypress(event);
245 244 };
246 245 this.editor.on('keypress', this._handle_keypress);
247 246 }
248 247 this.sel.attr('size', Math.min(10, this.raw_result.length));
249 248
250 249 // After everything is on the page, compute the postion.
251 250 // We put it above the code if it is too close to the bottom of the page.
252 251 cur.ch = cur.ch-matched_text.length;
253 252 var pos = this.editor.cursorCoords(cur);
254 253 var left = pos.left-3;
255 254 var top;
256 255 var cheight = this.complete.height();
257 256 var wheight = $(window).height();
258 257 if (pos.bottom+cheight+5 > wheight) {
259 258 top = pos.top-cheight-4;
260 259 } else {
261 260 top = pos.bottom+1;
262 261 }
263 262 this.complete.css('left', left + 'px');
264 263 this.complete.css('top', top + 'px');
265 264
266 265 // Clear and fill the list.
267 266 this.sel.text('');
268 267 this.build_gui_list(this.raw_result);
269 268 return true;
270 269 };
271 270
272 271 Completer.prototype.insert = function (completion) {
273 272 this.editor.replaceRange(completion.str, completion.from, completion.to);
274 273 };
275 274
276 275 Completer.prototype.build_gui_list = function (completions) {
277 276 for (var i = 0; i < completions.length; ++i) {
278 277 var opt = $('<option/>').text(completions[i].str).addClass(completions[i].type);
279 278 this.sel.append(opt);
280 279 }
281 280 this.sel.children().first().attr('selected', 'true');
282 281 this.sel.scrollTop(0);
283 282 };
284 283
285 284 Completer.prototype.close = function () {
286 285 this.done = true;
287 286 $('#complete').remove();
288 287 this.editor.off('keydown', this._handle_keydown);
289 288 this.editor.off('keypress', this._handle_keypress);
290 289 this.visible = false;
291 290 };
292 291
293 292 Completer.prototype.pick = function () {
294 293 this.insert(this.raw_result[this.sel[0].selectedIndex]);
295 294 this.close();
296 295 };
297 296
298 297 Completer.prototype.keydown = function (event) {
299 298 var code = event.keyCode;
300 299 var that = this;
301 300
302 301 // Enter
303 302 if (code == keycodes.enter) {
304 303 CodeMirror.e_stop(event);
305 304 this.pick();
306 305 // Escape or backspace
307 306 } else if (code == keycodes.esc || code == keycodes.backspace) {
308 307 CodeMirror.e_stop(event);
309 308 this.close();
310 309 } else if (code == keycodes.tab) {
311 310 //all the fastforwarding operation,
312 311 //Check that shared start is not null which can append with prefixed completion
313 312 // like %pylab , pylab have no shred start, and ff will result in py<tab><tab>
314 313 // to erase py
315 314 var sh = shared_start(this.raw_result, true);
316 315 if (sh) {
317 316 this.insert(sh);
318 317 }
319 318 this.close();
320 319 //reinvoke self
321 320 setTimeout(function () {
322 321 that.carry_on_completion();
323 322 }, 50);
324 323 } else if (code == keycodes.up || code == keycodes.down) {
325 324 // need to do that to be able to move the arrow
326 325 // when on the first or last line ofo a code cell
327 326 CodeMirror.e_stop(event);
328 327
329 328 var options = this.sel.find('option');
330 329 var index = this.sel[0].selectedIndex;
331 330 if (code == keycodes.up) {
332 331 index--;
333 332 }
334 333 if (code == keycodes.down) {
335 334 index++;
336 335 }
337 336 index = Math.min(Math.max(index, 0), options.length-1);
338 337 this.sel[0].selectedIndex = index;
339 338 } else if (code == keycodes.left || code == keycodes.right) {
340 339 this.close();
341 340 }
342 341 };
343 342
344 343 Completer.prototype.keypress = function (event) {
345 344 // FIXME: This is a band-aid.
346 345 // on keypress, trigger insertion of a single character.
347 346 // This simulates the old behavior of completion as you type,
348 347 // before events were disconnected and CodeMirror stopped
349 348 // receiving events while the completer is focused.
350 349
351 350 var that = this;
352 351 var code = event.keyCode;
353 352
354 353 // don't handle keypress if it's not a character (arrows on FF)
355 354 // or ENTER/TAB
356 355 if (event.charCode === 0 ||
357 356 code == keycodes.tab ||
358 357 code == keycodes.enter
359 358 ) return;
360 359
361 var cur = this.editor.getCursor();
362 var completion = {
363 str: String.fromCharCode(event.which),
364 type: "introspection",
365 from: cur,
366 to: cur,
367 };
368
369 360 this.close();
370 361 this.editor.focus();
371 362 setTimeout(function () {
372 363 that.carry_on_completion();
373 364 }, 50);
374 365 };
375 366 IPython.Completer = Completer;
376 367
377 368 return IPython;
378 369 }(IPython));
General Comments 0
You need to be logged in to leave comments. Login now