From 6c86474bdba02e60248f558617e25894323f6f56 Mon Sep 17 00:00:00 2001 From: Matthias BUSSONNIER Date: Sat, 12 Nov 2011 15:10:54 +0100 Subject: [PATCH] Improve tooltip tringgering,make it configurable As until now, when pressing tab and a white space was preceding the cursor The completion was triggerd with the whole namespace in it. Now if a whitespace or an opening bracket is just befor the cursor it will try to display a tooltip. The logic to find what object_info_request is send have been sightly changed to try to match the expression just before the last unmached openig bracket before the cursor (without considering what is after the cursor). example (_|_ represent the cursor): >>> his_|_ # completion >>> hist(_|_ # tooltip on hist >>> hist(rand(20),bins=range(_|_ #tooltip on range >>> hist(rand(20),bins=range(10), _|_ # tooltip on hist (whitespace before cursor) >>> hist(rand(20),bins=range(10),_|_ # completion as we dont care of what is after the cursor: >>> hist(rand(5000), bins=50, _|_orientaion='horizontal') # and tab, equivalent to >>> hist(rand(5000), bins=50, _|_ # onte the space again >>> hist(_|_rand(5000), bins=50, orientaion='horizontal') # and tab, equivalent to >>> hist(_|_ the 4 give tooltip on hist note that you can get tooltip on things that aren't function by appending a '(' like >>> matplotlib( Which is kinda weird... so we might want to bound another shortcut for tooltip, but which matches without bracket... additionnaly I have added a "Config" pannel in the left pannel with a checkbox bind to wether or not activate this functionnality Note, (rebase and edited commit, might not work perfetly xwithout the following ones) --- .../html/notebook/static/css/notebook.css | 4 +++ .../frontend/html/notebook/static/js/cell.js | 1 - .../html/notebook/static/js/codecell.js | 9 ++++--- .../html/notebook/static/js/notebook.js | 26 ++++++++++++++++--- .../html/notebook/static/js/notebookmain.js | 1 + .../html/notebook/templates/notebook.html | 14 ++++++++++ 6 files changed, 46 insertions(+), 9 deletions(-) diff --git a/IPython/frontend/html/notebook/static/css/notebook.css b/IPython/frontend/html/notebook/static/css/notebook.css index 9585d459d..0a3470106 100644 --- a/IPython/frontend/html/notebook/static/css/notebook.css +++ b/IPython/frontend/html/notebook/static/css/notebook.css @@ -115,6 +115,10 @@ span.section_row_buttons a { float: right; } +#tooltipontab_span { + float: right; +} + .checkbox_label { font-size: 85%; float: right; diff --git a/IPython/frontend/html/notebook/static/js/cell.js b/IPython/frontend/html/notebook/static/js/cell.js index 2d86c6265..b186bd8c3 100644 --- a/IPython/frontend/html/notebook/static/js/cell.js +++ b/IPython/frontend/html/notebook/static/js/cell.js @@ -85,7 +85,6 @@ var IPython = (function (IPython) { } }; - // Subclasses must implement create_element. Cell.prototype.create_element = function () {}; diff --git a/IPython/frontend/html/notebook/static/js/codecell.js b/IPython/frontend/html/notebook/static/js/codecell.js index 6170df573..1d4f17785 100644 --- a/IPython/frontend/html/notebook/static/js/codecell.js +++ b/IPython/frontend/html/notebook/static/js/codecell.js @@ -65,7 +65,8 @@ var IPython = (function (IPython) { // value is used to determine if CodeMirror should ignore the event: // true = ignore, false = don't ignore. tooltip_wait_time = 2000; - tooltip_on_tab = true; + + tooltip_on_tab = this.notebook.tooltip_on_tab; var that = this; // whatever key is pressed, first, cancel the tooltip request before @@ -79,9 +80,9 @@ var IPython = (function (IPython) { // Always ignore shift-enter in CodeMirror as we handle it. return true; }else if (event.keyCode === 53 && event.type === 'keydown' && tooltip_wait_time >= 0) { - // Pressing '(' , request tooltip + // Pressing '(' , request tooltip, don't forget to reappend it var cursor = editor.getCursor(); - var pre_cursor = editor.getRange({line:cursor.line,ch:0},cursor).trim(); + var pre_cursor = editor.getRange({line:cursor.line,ch:0},cursor).trim()+'('; CodeCell.prototype.request_tooltip_after_time(pre_cursor,tooltip_wait_time,that); } else if (event.keyCode === 9 && event.type == 'keydown') { // Tab completion. @@ -92,7 +93,7 @@ var IPython = (function (IPython) { // Don't autocomplete if the part of the line before the cursor // is empty. In this case, let CodeMirror handle indentation. return false; - } else if (pre_cursor.substr(-1) === "(" && tooltip_on_tab ) { + } else if ((pre_cursor.substr(-1) === "("|| pre_cursor.substr(-1) === " ") && tooltip_on_tab ) { CodeCell.prototype.request_tooltip_after_time(pre_cursor,0,that); } else { pre_cursor.trim(); diff --git a/IPython/frontend/html/notebook/static/js/notebook.js b/IPython/frontend/html/notebook/static/js/notebook.js index 1c5d07995..b6c36e11e 100644 --- a/IPython/frontend/html/notebook/static/js/notebook.js +++ b/IPython/frontend/html/notebook/static/js/notebook.js @@ -27,6 +27,7 @@ var IPython = (function (IPython) { this.style(); this.create_elements(); this.bind_events(); + this.set_tooltipontab(true); }; @@ -621,6 +622,11 @@ var IPython = (function (IPython) { }; + Notebook.prototype.set_tooltipontab = function (state) { + console.log("change tooltip on tab to : "+state); + this.tooltip_on_tab = state; + }; + Notebook.prototype.set_autoindent = function (state) { var cells = this.cells(); len = cells.length; @@ -883,10 +889,22 @@ var IPython = (function (IPython) { Notebook.prototype.request_tool_tip = function (cell,func) { - //remove ending '(' if any - //there should be a way to do it in the regexp - if(func.substr(-1) === '('){func=func.substr(0, func.length-1);} - // regexp to select last part of expression + // Feel free to shorten this logic if you are better + // than me in regEx + // basicaly you shoul be able to get xxx.xxx.xxx from + // something(range(10), kwarg=smth) ; xxx.xxx.xxx( firstarg, rand(234,23), kwarg1=2, + // remove everything between matchin bracket (need to iterate) + matchBracket = /\([^\(\)]+\)/g; + oldfunc = func; + func = func.replace(matchBracket,""); + while( oldfunc != func ) + { + oldfunc = func; + func = func.replace(matchBracket,""); + } + // remove everythin after last open bracket + endBracket = /\([^\(]*$/g; + func = func.replace(endBracket,""); var re = /[a-zA-Z._]+$/g; var msg_id = this.kernel.object_info_request(re.exec(func)); this.msg_cell_map[msg_id] = cell.cell_id; diff --git a/IPython/frontend/html/notebook/static/js/notebookmain.js b/IPython/frontend/html/notebook/static/js/notebookmain.js index 11c0db61e..78e4c054c 100644 --- a/IPython/frontend/html/notebook/static/js/notebookmain.js +++ b/IPython/frontend/html/notebook/static/js/notebookmain.js @@ -51,6 +51,7 @@ $(document).ready(function () { IPython.quick_help.element.addClass('hidden'); // shortcuts are disabled in read_only $('button#new_notebook').addClass('hidden'); $('div#cell_section').addClass('hidden'); + $('div#config_section').addClass('hidden'); $('div#kernel_section').addClass('hidden'); $('span#login_widget').removeClass('hidden'); // left panel starts collapsed, but the collapse must happen after diff --git a/IPython/frontend/html/notebook/templates/notebook.html b/IPython/frontend/html/notebook/templates/notebook.html index e2fdc3bde..299a76f56 100644 --- a/IPython/frontend/html/notebook/templates/notebook.html +++ b/IPython/frontend/html/notebook/templates/notebook.html @@ -251,6 +251,20 @@ +
+
+

Config

+
+
+
+ + + + Tooltip on tab: +
+
+
+