Skip to content
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

Fix: split_cell doesn't always split cell #6017

Merged
merged 3 commits into from
Mar 25, 2021
Merged
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
44 changes: 38 additions & 6 deletions notebook/static/notebook/js/cell.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ define([
'services/config',
], function($, utils, i18n, CodeMirror, cm_match, cm_closeb, cm_comment, configmod) {
"use strict";

function is_single_cursor(dict1, dict2) {
return ((dict1.line == dict2.line) && (dict1.ch == dict2.ch));
};

var overlayHack = CodeMirror.scrollbarModel.native.prototype.overlayHack;

Expand Down Expand Up @@ -598,24 +602,44 @@ define([
* @method get_split_text
**/
Cell.prototype.get_split_text = function () {
var start = {line:0, ch:0};
var last_line_num = this.code_mirror.lineCount()-1;
var last_line_len = this.code_mirror.getLine(last_line_num).length;
var end = {line:last_line_num, ch:last_line_len};

var flag_empty_cell = is_single_cursor(start, end);
var flag_first_position = false;
var flag_last_position = false;
var flag_all_select = false;

var ranges = this.code_mirror.listSelections();

var cursors = [{line: 0, ch: 0}];
var cursors = [start];

for (var i = 0; i < ranges.length; i++) {
// append both to handle selections
if (ranges[i].head.sticky == 'before') {
// ranges[i].head.sticky is null if ctrl-a select
if ((ranges[i].head.sticky == 'before') || (ranges[i].head.sticky === null )) {
cursors.push(ranges[i].anchor);
cursors.push(ranges[i].head);
if (is_single_cursor(ranges[i].anchor, start) &&
is_single_cursor(ranges[i].head, end)) {
flag_all_select = true;
}
} else {
cursors.push(ranges[i].head);
cursors.push(ranges[i].anchor);
if (is_single_cursor(ranges[i].head, start) &&
is_single_cursor(ranges[i].anchor, end)) {
flag_all_select = true;
}
}
// single cursor at beginning or end of cell
if (is_single_cursor(ranges[i].head, ranges[i].anchor)) {
if (is_single_cursor(ranges[i].head, start)) flag_first_position = true;
if (is_single_cursor(ranges[i].head, end)) flag_last_position = true;
}
}

var last_line_num = this.code_mirror.lineCount()-1;
var last_line_len = this.code_mirror.getLine(last_line_num).length;
var end = {line:last_line_num, ch:last_line_len};
cursors.push(end);

// Cursors is now sorted, but likely has duplicates due to anchor and head being the same for cursors
Expand All @@ -630,11 +654,19 @@ define([

// Split text
var text_list = [];
// Split single cursors at first position
if (flag_empty_cell || flag_first_position) text_list.push('');
for (var i = 1; i < locations.length; i++) {
var text = this.code_mirror.getRange(locations[i-1], locations[i]);
text = text.replace(/^\n+/, '').replace(/\n+$/, ''); // removes newlines at beginning and end
text_list.push(text);
}
// Split single cursors at last position
if (flag_last_position) text_list.push('');
// Duplicate cell if full cell is selected
if ((text_list.length == 1) && flag_all_select && !flag_empty_cell) {
text_list = text_list.concat(text_list);
}
return text_list;
};

Expand Down