##// END OF EJS Templates
Merge pull request #3793 from jhamrick/master...
Matthias Bussonnier -
r11719:4c80e0df merge
parent child Browse files
Show More
@@ -1,441 +1,443 b''
1 1 //----------------------------------------------------------------------------
2 2 // Copyright (C) 2008-2011 The IPython Development Team
3 3 //
4 4 // Distributed under the terms of the BSD License. The full license is in
5 5 // the file COPYING, distributed as part of this software.
6 6 //----------------------------------------------------------------------------
7 7
8 8 //============================================================================
9 9 // CodeCell
10 10 //============================================================================
11 11 /**
12 12 * An extendable module that provide base functionnality to create cell for notebook.
13 13 * @module IPython
14 14 * @namespace IPython
15 15 * @submodule CodeCell
16 16 */
17 17
18 18
19 19 /* local util for codemirror */
20 20 var posEq = function(a, b) {return a.line == b.line && a.ch == b.ch;}
21 21
22 22 /**
23 23 *
24 24 * function to delete until previous non blanking space character
25 25 * or first multiple of 4 tabstop.
26 26 * @private
27 27 */
28 28 CodeMirror.commands.delSpaceToPrevTabStop = function(cm){
29 29 var from = cm.getCursor(true), to = cm.getCursor(false), sel = !posEq(from, to);
30 30 if (!posEq(from, to)) {cm.replaceRange("", from, to); return}
31 31 var cur = cm.getCursor(), line = cm.getLine(cur.line);
32 32 var tabsize = cm.getOption('tabSize');
33 33 var chToPrevTabStop = cur.ch-(Math.ceil(cur.ch/tabsize)-1)*tabsize;
34 34 var from = {ch:cur.ch-chToPrevTabStop,line:cur.line}
35 35 var select = cm.getRange(from,cur)
36 36 if( select.match(/^\ +$/) != null){
37 37 cm.replaceRange("",from,cur)
38 38 } else {
39 39 cm.deleteH(-1,"char")
40 40 }
41 41 };
42 42
43 43
44 44 var IPython = (function (IPython) {
45 45 "use strict";
46 46
47 47 var utils = IPython.utils;
48 48 var key = IPython.utils.keycodes;
49 49
50 50 /**
51 51 * A Cell conceived to write code.
52 52 *
53 53 * The kernel doesn't have to be set at creation time, in that case
54 54 * it will be null and set_kernel has to be called later.
55 55 * @class CodeCell
56 56 * @extends IPython.Cell
57 57 *
58 58 * @constructor
59 59 * @param {Object|null} kernel
60 60 * @param {object|undefined} [options]
61 61 * @param [options.cm_config] {object} config to pass to CodeMirror
62 62 */
63 63 var CodeCell = function (kernel, options) {
64 64 this.kernel = kernel || null;
65 65 this.code_mirror = null;
66 66 this.input_prompt_number = null;
67 67 this.collapsed = false;
68 68 this.default_mode = 'ipython';
69 69 this.cell_type = "code";
70 70
71 71
72 72 var cm_overwrite_options = {
73 73 onKeyEvent: $.proxy(this.handle_codemirror_keyevent,this)
74 74 };
75 75
76 76 options = this.mergeopt(CodeCell, options, {cm_config:cm_overwrite_options});
77 77
78 78 IPython.Cell.apply(this,[options]);
79 79
80 80 var that = this;
81 81 this.element.focusout(
82 82 function() { that.auto_highlight(); }
83 83 );
84 84 };
85 85
86 86 CodeCell.options_default = {
87 87 cm_config : {
88 88 extraKeys: {
89 89 "Tab" : "indentMore",
90 90 "Shift-Tab" : "indentLess",
91 91 "Backspace" : "delSpaceToPrevTabStop",
92 92 "Cmd-/" : "toggleComment",
93 93 "Ctrl-/" : "toggleComment"
94 94 },
95 95 mode: 'ipython',
96 96 theme: 'ipython',
97 97 matchBrackets: true
98 98 }
99 99 };
100 100
101 101
102 102 CodeCell.prototype = new IPython.Cell();
103 103
104 104 /**
105 105 * @method auto_highlight
106 106 */
107 107 CodeCell.prototype.auto_highlight = function () {
108 108 this._auto_highlight(IPython.config.cell_magic_highlight)
109 109 };
110 110
111 111 /** @method create_element */
112 112 CodeCell.prototype.create_element = function () {
113 113 IPython.Cell.prototype.create_element.apply(this, arguments);
114 114
115 115 var cell = $('<div></div>').addClass('cell border-box-sizing code_cell');
116 116 cell.attr('tabindex','2');
117 117
118 118 this.celltoolbar = new IPython.CellToolbar(this);
119 119
120 120 var input = $('<div></div>').addClass('input');
121 121 var vbox = $('<div/>').addClass('vbox box-flex1')
122 122 input.append($('<div/>').addClass('prompt input_prompt'));
123 123 vbox.append(this.celltoolbar.element);
124 124 var input_area = $('<div/>').addClass('input_area');
125 125 this.code_mirror = CodeMirror(input_area.get(0), this.cm_config);
126 126 $(this.code_mirror.getInputField()).attr("spellcheck", "false");
127 127 vbox.append(input_area);
128 128 input.append(vbox);
129 129 var output = $('<div></div>');
130 130 cell.append(input).append(output);
131 131 this.element = cell;
132 132 this.output_area = new IPython.OutputArea(output, true);
133 133
134 134 // construct a completer only if class exist
135 135 // otherwise no print view
136 136 if (IPython.Completer !== undefined)
137 137 {
138 138 this.completer = new IPython.Completer(this);
139 139 }
140 140 };
141 141
142 142 /**
143 143 * This method gets called in CodeMirror's onKeyDown/onKeyPress
144 144 * handlers and is used to provide custom key handling. Its return
145 145 * value is used to determine if CodeMirror should ignore the event:
146 146 * true = ignore, false = don't ignore.
147 147 * @method handle_codemirror_keyevent
148 148 */
149 149 CodeCell.prototype.handle_codemirror_keyevent = function (editor, event) {
150 150
151 151 var that = this;
152 152 // whatever key is pressed, first, cancel the tooltip request before
153 153 // they are sent, and remove tooltip if any, except for tab again
154 154 if (event.type === 'keydown' && event.which != key.TAB ) {
155 155 IPython.tooltip.remove_and_cancel_tooltip();
156 156 };
157 157
158 158 var cur = editor.getCursor();
159 159 if (event.keyCode === key.ENTER){
160 160 this.auto_highlight();
161 161 }
162 162
163 163 if (event.keyCode === key.ENTER && (event.shiftKey || event.ctrlKey)) {
164 164 // Always ignore shift-enter in CodeMirror as we handle it.
165 165 return true;
166 166 } else if (event.which === 40 && event.type === 'keypress' && IPython.tooltip.time_before_tooltip >= 0) {
167 167 // triger on keypress (!) otherwise inconsistent event.which depending on plateform
168 168 // browser and keyboard layout !
169 169 // Pressing '(' , request tooltip, don't forget to reappend it
170 IPython.tooltip.pending(that);
170 // The second argument says to hide the tooltip if the docstring
171 // is actually empty
172 IPython.tooltip.pending(that, true);
171 173 } else if (event.which === key.UPARROW && event.type === 'keydown') {
172 174 // If we are not at the top, let CM handle the up arrow and
173 175 // prevent the global keydown handler from handling it.
174 176 if (!that.at_top()) {
175 177 event.stop();
176 178 return false;
177 179 } else {
178 180 return true;
179 181 };
180 182 } else if (event.which === key.ESC) {
181 183 IPython.tooltip.remove_and_cancel_tooltip(true);
182 184 return true;
183 185 } else if (event.which === key.DOWNARROW && event.type === 'keydown') {
184 186 // If we are not at the bottom, let CM handle the down arrow and
185 187 // prevent the global keydown handler from handling it.
186 188 if (!that.at_bottom()) {
187 189 event.stop();
188 190 return false;
189 191 } else {
190 192 return true;
191 193 };
192 194 } else if (event.keyCode === key.TAB && event.type == 'keydown' && event.shiftKey) {
193 195 if (editor.somethingSelected()){
194 196 var anchor = editor.getCursor("anchor");
195 197 var head = editor.getCursor("head");
196 198 if( anchor.line != head.line){
197 199 return false;
198 200 }
199 201 }
200 202 IPython.tooltip.request(that);
201 203 event.stop();
202 204 return true;
203 205 } else if (event.keyCode === key.TAB && event.type == 'keydown') {
204 206 // Tab completion.
205 207 //Do not trim here because of tooltip
206 208 if (editor.somethingSelected()){return false}
207 209 var pre_cursor = editor.getRange({line:cur.line,ch:0},cur);
208 210 if (pre_cursor.trim() === "") {
209 211 // Don't autocomplete if the part of the line before the cursor
210 212 // is empty. In this case, let CodeMirror handle indentation.
211 213 return false;
212 214 } else if ((pre_cursor.substr(-1) === "("|| pre_cursor.substr(-1) === " ") && IPython.config.tooltip_on_tab ) {
213 215 IPython.tooltip.request(that);
214 216 // Prevent the event from bubbling up.
215 217 event.stop();
216 218 // Prevent CodeMirror from handling the tab.
217 219 return true;
218 220 } else {
219 221 event.stop();
220 222 this.completer.startCompletion();
221 223 return true;
222 224 };
223 225 } else {
224 226 // keypress/keyup also trigger on TAB press, and we don't want to
225 227 // use those to disable tab completion.
226 228 return false;
227 229 };
228 230 return false;
229 231 };
230 232
231 233
232 234 // Kernel related calls.
233 235
234 236 CodeCell.prototype.set_kernel = function (kernel) {
235 237 this.kernel = kernel;
236 238 }
237 239
238 240 /**
239 241 * Execute current code cell to the kernel
240 242 * @method execute
241 243 */
242 244 CodeCell.prototype.execute = function () {
243 245 this.output_area.clear_output(true, true, true);
244 246 this.set_input_prompt('*');
245 247 this.element.addClass("running");
246 248 var callbacks = {
247 249 'execute_reply': $.proxy(this._handle_execute_reply, this),
248 250 'output': $.proxy(this.output_area.handle_output, this.output_area),
249 251 'clear_output': $.proxy(this.output_area.handle_clear_output, this.output_area),
250 252 'set_next_input': $.proxy(this._handle_set_next_input, this),
251 253 'input_request': $.proxy(this._handle_input_request, this)
252 254 };
253 255 var msg_id = this.kernel.execute(this.get_text(), callbacks, {silent: false});
254 256 };
255 257
256 258 /**
257 259 * @method _handle_execute_reply
258 260 * @private
259 261 */
260 262 CodeCell.prototype._handle_execute_reply = function (content) {
261 263 this.set_input_prompt(content.execution_count);
262 264 this.element.removeClass("running");
263 265 $([IPython.events]).trigger('set_dirty.Notebook', {value: true});
264 266 }
265 267
266 268 /**
267 269 * @method _handle_set_next_input
268 270 * @private
269 271 */
270 272 CodeCell.prototype._handle_set_next_input = function (text) {
271 273 var data = {'cell': this, 'text': text}
272 274 $([IPython.events]).trigger('set_next_input.Notebook', data);
273 275 }
274 276
275 277 /**
276 278 * @method _handle_input_request
277 279 * @private
278 280 */
279 281 CodeCell.prototype._handle_input_request = function (content) {
280 282 this.output_area.append_raw_input(content);
281 283 }
282 284
283 285
284 286 // Basic cell manipulation.
285 287
286 288 CodeCell.prototype.select = function () {
287 289 IPython.Cell.prototype.select.apply(this);
288 290 this.code_mirror.refresh();
289 291 this.code_mirror.focus();
290 292 this.auto_highlight();
291 293 // We used to need an additional refresh() after the focus, but
292 294 // it appears that this has been fixed in CM. This bug would show
293 295 // up on FF when a newly loaded markdown cell was edited.
294 296 };
295 297
296 298
297 299 CodeCell.prototype.select_all = function () {
298 300 var start = {line: 0, ch: 0};
299 301 var nlines = this.code_mirror.lineCount();
300 302 var last_line = this.code_mirror.getLine(nlines-1);
301 303 var end = {line: nlines-1, ch: last_line.length};
302 304 this.code_mirror.setSelection(start, end);
303 305 };
304 306
305 307
306 308 CodeCell.prototype.collapse = function () {
307 309 this.collapsed = true;
308 310 this.output_area.collapse();
309 311 };
310 312
311 313
312 314 CodeCell.prototype.expand = function () {
313 315 this.collapsed = false;
314 316 this.output_area.expand();
315 317 };
316 318
317 319
318 320 CodeCell.prototype.toggle_output = function () {
319 321 this.collapsed = Boolean(1 - this.collapsed);
320 322 this.output_area.toggle_output();
321 323 };
322 324
323 325
324 326 CodeCell.prototype.toggle_output_scroll = function () {
325 327 this.output_area.toggle_scroll();
326 328 };
327 329
328 330
329 331 CodeCell.input_prompt_classical = function (prompt_value, lines_number) {
330 332 var ns = prompt_value || "&nbsp;";
331 333 return 'In&nbsp;[' + ns + ']:'
332 334 };
333 335
334 336 CodeCell.input_prompt_continuation = function (prompt_value, lines_number) {
335 337 var html = [CodeCell.input_prompt_classical(prompt_value, lines_number)];
336 338 for(var i=1; i < lines_number; i++){html.push(['...:'])};
337 339 return html.join('</br>')
338 340 };
339 341
340 342 CodeCell.input_prompt_function = CodeCell.input_prompt_classical;
341 343
342 344
343 345 CodeCell.prototype.set_input_prompt = function (number) {
344 346 var nline = 1
345 347 if( this.code_mirror != undefined) {
346 348 nline = this.code_mirror.lineCount();
347 349 }
348 350 this.input_prompt_number = number;
349 351 var prompt_html = CodeCell.input_prompt_function(this.input_prompt_number, nline);
350 352 this.element.find('div.input_prompt').html(prompt_html);
351 353 };
352 354
353 355
354 356 CodeCell.prototype.clear_input = function () {
355 357 this.code_mirror.setValue('');
356 358 };
357 359
358 360
359 361 CodeCell.prototype.get_text = function () {
360 362 return this.code_mirror.getValue();
361 363 };
362 364
363 365
364 366 CodeCell.prototype.set_text = function (code) {
365 367 return this.code_mirror.setValue(code);
366 368 };
367 369
368 370
369 371 CodeCell.prototype.at_top = function () {
370 372 var cursor = this.code_mirror.getCursor();
371 373 if (cursor.line === 0 && cursor.ch === 0) {
372 374 return true;
373 375 } else {
374 376 return false;
375 377 }
376 378 };
377 379
378 380
379 381 CodeCell.prototype.at_bottom = function () {
380 382 var cursor = this.code_mirror.getCursor();
381 383 if (cursor.line === (this.code_mirror.lineCount()-1) && cursor.ch === this.code_mirror.getLine(cursor.line).length) {
382 384 return true;
383 385 } else {
384 386 return false;
385 387 }
386 388 };
387 389
388 390
389 391 CodeCell.prototype.clear_output = function (stdout, stderr, other) {
390 392 this.output_area.clear_output(stdout, stderr, other);
391 393 };
392 394
393 395
394 396 // JSON serialization
395 397
396 398 CodeCell.prototype.fromJSON = function (data) {
397 399 IPython.Cell.prototype.fromJSON.apply(this, arguments);
398 400 if (data.cell_type === 'code') {
399 401 if (data.input !== undefined) {
400 402 this.set_text(data.input);
401 403 // make this value the starting point, so that we can only undo
402 404 // to this state, instead of a blank cell
403 405 this.code_mirror.clearHistory();
404 406 this.auto_highlight();
405 407 }
406 408 if (data.prompt_number !== undefined) {
407 409 this.set_input_prompt(data.prompt_number);
408 410 } else {
409 411 this.set_input_prompt();
410 412 };
411 413 this.output_area.fromJSON(data.outputs);
412 414 if (data.collapsed !== undefined) {
413 415 if (data.collapsed) {
414 416 this.collapse();
415 417 } else {
416 418 this.expand();
417 419 };
418 420 };
419 421 };
420 422 };
421 423
422 424
423 425 CodeCell.prototype.toJSON = function () {
424 426 var data = IPython.Cell.prototype.toJSON.apply(this);
425 427 data.input = this.get_text();
426 428 data.cell_type = 'code';
427 429 if (this.input_prompt_number) {
428 430 data.prompt_number = this.input_prompt_number;
429 431 };
430 432 var outputs = this.output_area.toJSON();
431 433 data.outputs = outputs;
432 434 data.language = 'python';
433 435 data.collapsed = this.collapsed;
434 436 return data;
435 437 };
436 438
437 439
438 440 IPython.CodeCell = CodeCell;
439 441
440 442 return IPython;
441 443 }(IPython));
@@ -1,355 +1,363 b''
1 1 //----------------------------------------------------------------------------
2 2 // Copyright (C) 2008-2011 The IPython Development Team
3 3 //
4 4 // Distributed under the terms of the BSD License. The full license is in
5 5 // the file COPYING, distributed as part of this software.
6 6 //----------------------------------------------------------------------------
7 7 //============================================================================
8 8 // Tooltip
9 9 //============================================================================
10 10 //
11 11 // you can set the autocall time by setting `IPython.tooltip.time_before_tooltip` in ms
12 12 //
13 13 // you can configure the differents action of pressing tab several times in a row by
14 14 // setting/appending different fonction in the array
15 15 // IPython.tooltip.tabs_functions
16 16 //
17 17 // eg :
18 18 // IPython.tooltip.tabs_functions[4] = function (){console.log('this is the action of the 4th tab pressing')}
19 19 //
20 20 var IPython = (function (IPython) {
21 21 "use strict";
22 22
23 23 var utils = IPython.utils;
24 24
25 25 // tooltip constructor
26 26 var Tooltip = function () {
27 27 var that = this;
28 28 this.time_before_tooltip = 1200;
29 29
30 30 // handle to html
31 31 this.tooltip = $('#tooltip');
32 32 this._hidden = true;
33 33
34 34 // variable for consecutive call
35 35 this._old_cell = null;
36 36 this._old_request = null;
37 37 this._consecutive_counter = 0;
38 38
39 39 // 'sticky ?'
40 40 this._sticky = false;
41 41
42 // display tooltip if the docstring is empty?
43 this._hide_if_no_docstring = false;
44
42 45 // contain the button in the upper right corner
43 46 this.buttons = $('<div/>').addClass('tooltipbuttons');
44 47
45 48 // will contain the docstring
46 49 this.text = $('<div/>').addClass('tooltiptext').addClass('smalltooltip');
47 50
48 51 // build the buttons menu on the upper right
49 52 // expand the tooltip to see more
50 53 var expandlink = $('<a/>').attr('href', "#").addClass("ui-corner-all") //rounded corner
51 54 .attr('role', "button").attr('id', 'expanbutton').attr('title', 'Grow the tooltip vertically (press tab 2 times)').click(function () {
52 55 that.expand()
53 56 }).append(
54 57 $('<span/>').text('Expand').addClass('ui-icon').addClass('ui-icon-plus'));
55 58
56 59 // open in pager
57 60 var morelink = $('<a/>').attr('href', "#").attr('role', "button").addClass('ui-button').attr('title', 'show the current docstring in pager (press tab 4 times)');
58 61 var morespan = $('<span/>').text('Open in Pager').addClass('ui-icon').addClass('ui-icon-arrowstop-l-n');
59 62 morelink.append(morespan);
60 63 morelink.click(function () {
61 64 that.showInPager(that._old_cell);
62 65 });
63 66
64 67 // close the tooltip
65 68 var closelink = $('<a/>').attr('href', "#").attr('role', "button").addClass('ui-button');
66 69 var closespan = $('<span/>').text('Close').addClass('ui-icon').addClass('ui-icon-close');
67 70 closelink.append(closespan);
68 71 closelink.click(function () {
69 72 that.remove_and_cancel_tooltip(true);
70 73 });
71 74
72 75 this._clocklink = $('<a/>').attr('href', "#");
73 76 this._clocklink.attr('role', "button");
74 77 this._clocklink.addClass('ui-button');
75 78 this._clocklink.attr('title', 'Tootip is not dismissed while typing for 10 seconds');
76 79 var clockspan = $('<span/>').text('Close');
77 80 clockspan.addClass('ui-icon');
78 81 clockspan.addClass('ui-icon-clock');
79 82 this._clocklink.append(clockspan);
80 83 this._clocklink.click(function () {
81 84 that.cancel_stick();
82 85 });
83 86
84 87
85 88
86 89
87 90 //construct the tooltip
88 91 // add in the reverse order you want them to appear
89 92 this.buttons.append(closelink);
90 93 this.buttons.append(expandlink);
91 94 this.buttons.append(morelink);
92 95 this.buttons.append(this._clocklink);
93 96 this._clocklink.hide();
94 97
95 98
96 99 // we need a phony element to make the small arrow
97 100 // of the tooltip in css
98 101 // we will move the arrow later
99 102 this.arrow = $('<div/>').addClass('pretooltiparrow');
100 103 this.tooltip.append(this.buttons);
101 104 this.tooltip.append(this.arrow);
102 105 this.tooltip.append(this.text);
103 106
104 107 // function that will be called if you press tab 1, 2, 3... times in a row
105 108 this.tabs_functions = [function (cell, text) {
106 109 that._request_tooltip(cell, text);
107 110 }, function () {
108 111 that.expand();
109 112 }, function () {
110 113 that.stick();
111 114 }, function (cell) {
112 115 that.cancel_stick();
113 116 that.showInPager(cell);
114 117 }];
115 118 // call after all the tabs function above have bee call to clean their effects
116 119 // if necessary
117 120 this.reset_tabs_function = function (cell, text) {
118 121 this._old_cell = (cell) ? cell : null;
119 122 this._old_request = (text) ? text : null;
120 123 this._consecutive_counter = 0;
121 124 }
122 125 };
123 126
124 127 Tooltip.prototype.showInPager = function (cell) {
125 128 // reexecute last call in pager by appending ? to show back in pager
126 129 var that = this;
127 130 var empty = function () {};
128 131 cell.kernel.execute(
129 132 that.name + '?', {
130 133 'execute_reply': empty,
131 134 'output': empty,
132 135 'clear_output': empty,
133 136 'cell': cell
134 137 }, {
135 138 'silent': false
136 139 });
137 140 this.remove_and_cancel_tooltip();
138 141 }
139 142
140 143 // grow the tooltip verticaly
141 144 Tooltip.prototype.expand = function () {
142 145 this.text.removeClass('smalltooltip');
143 146 this.text.addClass('bigtooltip');
144 147 $('#expanbutton').hide('slow');
145 148 }
146 149
147 150 // deal with all the logic of hiding the tooltip
148 151 // and reset it's status
149 152 Tooltip.prototype._hide = function () {
150 153 this.tooltip.fadeOut('fast');
151 154 $('#expanbutton').show('slow');
152 155 this.text.removeClass('bigtooltip');
153 156 this.text.addClass('smalltooltip');
154 157 // keep scroll top to be sure to always see the first line
155 158 this.text.scrollTop(0);
156 159 this._hidden = true;
157 160 this.code_mirror = null;
158 161 }
159 162
160 163 Tooltip.prototype.remove_and_cancel_tooltip = function (force) {
161 164 // note that we don't handle closing directly inside the calltip
162 165 // as in the completer, because it is not focusable, so won't
163 166 // get the event.
164 167 if (this._sticky == false || force == true) {
165 168 this.cancel_stick();
166 169 this._hide();
167 170 }
168 171 this.cancel_pending();
169 172 this.reset_tabs_function();
170 173 }
171 174
172 175 // cancel autocall done after '(' for example.
173 176 Tooltip.prototype.cancel_pending = function () {
174 177 if (this._tooltip_timeout != null) {
175 178 clearTimeout(this._tooltip_timeout);
176 179 this._tooltip_timeout = null;
177 180 }
178 181 }
179 182
180 183 // will trigger tooltip after timeout
181 Tooltip.prototype.pending = function (cell) {
184 Tooltip.prototype.pending = function (cell, hide_if_no_docstring) {
182 185 var that = this;
183 186 this._tooltip_timeout = setTimeout(function () {
184 that.request(cell)
187 that.request(cell, hide_if_no_docstring)
185 188 }, that.time_before_tooltip);
186 189 }
187 190
188 191 Tooltip.prototype._request_tooltip = function (cell, func) {
189 192 // use internally just to make the request to the kernel
190 193 // Feel free to shorten this logic if you are better
191 194 // than me in regEx
192 195 // basicaly you shoul be able to get xxx.xxx.xxx from
193 196 // something(range(10), kwarg=smth) ; xxx.xxx.xxx( firstarg, rand(234,23), kwarg1=2,
194 197 // remove everything between matchin bracket (need to iterate)
195 198 var matchBracket = /\([^\(\)]+\)/g;
196 199 var endBracket = /\([^\(]*$/g;
197 200 var oldfunc = func;
198 201
199 202 func = func.replace(matchBracket, "");
200 203 while (oldfunc != func) {
201 204 oldfunc = func;
202 205 func = func.replace(matchBracket, "");
203 206 }
204 207 // remove everything after last open bracket
205 208 func = func.replace(endBracket, "");
206 209
207 210 var re = /[a-z_][0-9a-z._]+$/gi; // casse insensitive
208 211 var callbacks = {
209 212 'object_info_reply': $.proxy(this._show, this)
210 213 }
211 214 var msg_id = cell.kernel.object_info_request(re.exec(func), callbacks);
212 215 }
213 216
214 217 // make an imediate completion request
215 Tooltip.prototype.request = function (cell) {
218 Tooltip.prototype.request = function (cell, hide_if_no_docstring) {
216 219 // request(codecell)
217 220 // Deal with extracting the text from the cell and counting
218 221 // call in a row
219 222 this.cancel_pending();
220 223 var editor = cell.code_mirror;
221 224 var cursor = editor.getCursor();
222 225 var text = editor.getRange({
223 226 line: cursor.line,
224 227 ch: 0
225 228 }, cursor).trim();
226 229
230 this._hide_if_no_docstring = hide_if_no_docstring;
231
227 232 if(editor.somethingSelected()){
228 233 text = editor.getSelection();
229 234 }
230 235
231 236 // need a permanent handel to code_mirror for future auto recall
232 237 this.code_mirror = editor;
233 238
234 239 // now we treat the different number of keypress
235 240 // first if same cell, same text, increment counter by 1
236 241 if (this._old_cell == cell && this._old_request == text && this._hidden == false) {
237 242 this._consecutive_counter++;
238 243 } else {
239 244 // else reset
240 245 this.cancel_stick();
241 246 this.reset_tabs_function (cell, text);
242 247 }
243 248
244 249 // don't do anything if line beggin with '(' or is empty
245 250 if (text === "" || text === "(") {
246 251 return;
247 252 }
248 253
249 254 this.tabs_functions[this._consecutive_counter](cell, text);
250 255
251 256 // then if we are at the end of list function, reset
252 257 if (this._consecutive_counter == this.tabs_functions.length) this.reset_tabs_function (cell, text);
253 258
254 259 return;
255 260 }
256 261
257 262 // cancel the option of having the tooltip to stick
258 263 Tooltip.prototype.cancel_stick = function () {
259 264 clearTimeout(this._stick_timeout);
260 265 this._stick_timeout = null;
261 266 this._clocklink.hide('slow');
262 267 this._sticky = false;
263 268 }
264 269
265 270 // put the tooltip in a sicky state for 10 seconds
266 271 // it won't be removed by remove_and_cancell() unless you called with
267 272 // the first parameter set to true.
268 273 // remove_and_cancell_tooltip(true)
269 274 Tooltip.prototype.stick = function (time) {
270 275 time = (time != undefined) ? time : 10;
271 276 var that = this;
272 277 this._sticky = true;
273 278 this._clocklink.show('slow');
274 279 this._stick_timeout = setTimeout(function () {
275 280 that._sticky = false;
276 281 that._clocklink.hide('slow');
277 282 }, time * 1000);
278 283 }
279 284
280 285 // should be called with the kernel reply to actually show the tooltip
281 286 Tooltip.prototype._show = function (reply) {
282 287 // move the bubble if it is not hidden
283 288 // otherwise fade it
284 289 this.name = reply.name;
285 290
286 291 // do some math to have the tooltip arrow on more or less on left or right
287 292 // width of the editor
288 293 var w = $(this.code_mirror.getScrollerElement()).width();
289 294 // ofset of the editor
290 295 var o = $(this.code_mirror.getScrollerElement()).offset();
291 296
292 297 // whatever anchor/head order but arrow at mid x selection
293 298 var anchor = this.code_mirror.cursorCoords(false);
294 299 var head = this.code_mirror.cursorCoords(true);
295 300 var xinit = (head.left+anchor.left)/2;
296 301 var xinter = o.left + (xinit - o.left) / w * (w - 450);
297 302 var posarrowleft = xinit - xinter;
298 303
299 304 if (this._hidden == false) {
300 305 this.tooltip.animate({
301 306 'left': xinter - 30 + 'px',
302 307 'top': (head.bottom + 10) + 'px'
303 308 });
304 309 } else {
305 310 this.tooltip.css({
306 311 'left': xinter - 30 + 'px'
307 312 });
308 313 this.tooltip.css({
309 314 'top': (head.bottom + 10) + 'px'
310 315 });
311 316 }
312 317 this.arrow.animate({
313 318 'left': posarrowleft + 'px'
314 319 });
315 this.tooltip.fadeIn('fast');
316 this._hidden = false;
317 320
318 321 // build docstring
319 322 var defstring = reply.call_def;
320 323 if (defstring == null) {
321 324 defstring = reply.init_definition;
322 325 }
323 326 if (defstring == null) {
324 327 defstring = reply.definition;
325 328 }
326 329
327 330 var docstring = reply.call_docstring;
328 331 if (docstring == null) {
329 332 docstring = reply.init_docstring;
330 333 }
331 334 if (docstring == null) {
332 335 docstring = reply.docstring;
333 336 }
334 if (docstring == null) {
337
338 if (docstring == null && this._hide_if_no_docstring) {
339 return;
340 } else {
335 341 docstring = "<empty docstring>";
336 342 }
337 343
344 this.tooltip.fadeIn('fast');
345 this._hidden = false;
338 346 this.text.children().remove();
339 347
340 348 var pre = $('<pre/>').html(utils.fixConsole(docstring));
341 349 if (defstring) {
342 350 var defstring_html = $('<pre/>').html(utils.fixConsole(defstring));
343 351 this.text.append(defstring_html);
344 352 }
345 353 this.text.append(pre);
346 354 // keep scroll top to be sure to always see the first line
347 355 this.text.scrollTop(0);
348 356 }
349 357
350 358
351 359 IPython.Tooltip = Tooltip;
352 360
353 361 return IPython;
354 362
355 363 }(IPython));
General Comments 0
You need to be logged in to leave comments. Login now