diff --git a/notebook/static/base/js/utils.js b/notebook/static/base/js/utils.js index 78e7c09be9..b07c73e363 100644 --- a/notebook/static/base/js/utils.js +++ b/notebook/static/base/js/utils.js @@ -500,41 +500,13 @@ define([ }; var to_absolute_cursor_pos = function (cm, cursor) { - /** - * get the absolute cursor position from CodeMirror's col, ch - */ - if (!cursor) { - cursor = cm.getCursor(); - } - var cursor_pos = cursor.ch; - for (var i = 0; i < cursor.line; i++) { - cursor_pos += cm.getLine(i).length + 1; - } - return cursor_pos; + console.warn('`utils.to_absolute_cursor_pos(cm, pos)` is deprecated. Use `cm.indexFromPos(cursor)`'); + return cm.indexFromPos(cusrsor); }; var from_absolute_cursor_pos = function (cm, cursor_pos) { - /** - * turn absolute cursor position into CodeMirror col, ch cursor - */ - var i, line, next_line; - var offset = 0; - for (i = 0, next_line=cm.getLine(i); next_line !== undefined; i++, next_line=cm.getLine(i)) { - line = next_line; - if (offset + next_line.length < cursor_pos) { - offset += next_line.length + 1; - } else { - return { - line : i, - ch : cursor_pos - offset, - }; - } - } - // reached end, return endpoint - return { - line : i - 1, - ch : line.length - 1, - }; + console.warn('`utils.from_absolute_cursor_pos(cm, pos)` is deprecated. Use `cm.posFromIndex(index)`'); + return cm.posFromIndex(cursor_pos); }; // http://stackoverflow.com/questions/2400935/browser-detection-in-javascript diff --git a/notebook/static/notebook/js/completer.js b/notebook/static/notebook/js/completer.js index 47d5bef7b6..3ac2b3ed79 100644 --- a/notebook/static/notebook/js/completer.js +++ b/notebook/static/notebook/js/completer.js @@ -152,7 +152,7 @@ define([ // one kernel completion came back, finish_completing will be called with the results // we fork here and directly call finish completing if kernel is busy - var cursor_pos = utils.to_absolute_cursor_pos(this.editor, cur); + var cursor_pos = this.editor.indexFromPos(cur); if (this.skip_kernel_completion) { this.finish_completing({ content: { matches: [], @@ -181,7 +181,7 @@ define([ // adapted message spec replies don't have cursor position info, // interpret end=null as current position, // and negative start relative to that - end = utils.to_absolute_cursor_pos(this.editor, cur); + end = this.editor.indexFromPos(cur); if (start === null) { start = end; } else if (start < 0) { @@ -202,8 +202,8 @@ define([ // append the introspection result, in order, at at the beginning of // the table and compute the replacement range from current cursor // positon and matched_text length. - var from = utils.from_absolute_cursor_pos(this.editor, start); - var to = utils.from_absolute_cursor_pos(this.editor, end); + var from = this.editor.posFromIndex(start); + var to = this.editor.posFromIndex(end); for (i = matches.length - 1; i >= 0; --i) { filtered_results.unshift({ str: matches[i], @@ -274,7 +274,7 @@ define([ // After everything is on the page, compute the postion. // We put it above the code if it is too close to the bottom of the page. var pos = this.editor.cursorCoords( - utils.from_absolute_cursor_pos(this.editor, start) + this.editor.posFromIndex(start) ); var left = pos.left-3; var top; diff --git a/notebook/static/notebook/js/tooltip.js b/notebook/static/notebook/js/tooltip.js index c511628ada..d131200d84 100644 --- a/notebook/static/notebook/js/tooltip.js +++ b/notebook/static/notebook/js/tooltip.js @@ -201,7 +201,7 @@ define([ this.cancel_pending(); var editor = cell.code_mirror; var cursor = editor.getCursor(); - var cursor_pos = utils.to_absolute_cursor_pos(editor, cursor); + var cursor_pos = editor.indexFromPos(cursor); var text = cell.get_text(); this._hide_if_no_docstring = hide_if_no_docstring;