Skip to content

Deprecate duplicate functions from utils. #427

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 15, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 4 additions & 32 deletions notebook/static/base/js/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions notebook/static/notebook/js/completer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: [],
Expand Down Expand Up @@ -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) {
Expand All @@ -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],
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion notebook/static/notebook/js/tooltip.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down