Skip to content

Commit 57d063c

Browse files
authored
Fix: split_cell doesn't always split cell (#6017)
* Split cells if a single cursor is positioned at the beginning or end of a cell * Duplicate cell if full cell is selected * fix null replace
1 parent c719e27 commit 57d063c

File tree

1 file changed

+38
-6
lines changed

1 file changed

+38
-6
lines changed

notebook/static/notebook/js/cell.js

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ define([
2020
'services/config',
2121
], function($, utils, i18n, CodeMirror, cm_match, cm_closeb, cm_comment, configmod) {
2222
"use strict";
23+
24+
function is_single_cursor(dict1, dict2) {
25+
return ((dict1.line == dict2.line) && (dict1.ch == dict2.ch));
26+
};
2327

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

@@ -598,24 +602,44 @@ define([
598602
* @method get_split_text
599603
**/
600604
Cell.prototype.get_split_text = function () {
605+
var start = {line:0, ch:0};
606+
var last_line_num = this.code_mirror.lineCount()-1;
607+
var last_line_len = this.code_mirror.getLine(last_line_num).length;
608+
var end = {line:last_line_num, ch:last_line_len};
609+
610+
var flag_empty_cell = is_single_cursor(start, end);
611+
var flag_first_position = false;
612+
var flag_last_position = false;
613+
var flag_all_select = false;
614+
601615
var ranges = this.code_mirror.listSelections();
602616

603-
var cursors = [{line: 0, ch: 0}];
617+
var cursors = [start];
604618

605619
for (var i = 0; i < ranges.length; i++) {
606620
// append both to handle selections
607-
if (ranges[i].head.sticky == 'before') {
621+
// ranges[i].head.sticky is null if ctrl-a select
622+
if ((ranges[i].head.sticky == 'before') || (ranges[i].head.sticky === null )) {
608623
cursors.push(ranges[i].anchor);
609624
cursors.push(ranges[i].head);
625+
if (is_single_cursor(ranges[i].anchor, start) &&
626+
is_single_cursor(ranges[i].head, end)) {
627+
flag_all_select = true;
628+
}
610629
} else {
611630
cursors.push(ranges[i].head);
612631
cursors.push(ranges[i].anchor);
632+
if (is_single_cursor(ranges[i].head, start) &&
633+
is_single_cursor(ranges[i].anchor, end)) {
634+
flag_all_select = true;
635+
}
636+
}
637+
// single cursor at beginning or end of cell
638+
if (is_single_cursor(ranges[i].head, ranges[i].anchor)) {
639+
if (is_single_cursor(ranges[i].head, start)) flag_first_position = true;
640+
if (is_single_cursor(ranges[i].head, end)) flag_last_position = true;
613641
}
614642
}
615-
616-
var last_line_num = this.code_mirror.lineCount()-1;
617-
var last_line_len = this.code_mirror.getLine(last_line_num).length;
618-
var end = {line:last_line_num, ch:last_line_len};
619643
cursors.push(end);
620644

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

631655
// Split text
632656
var text_list = [];
657+
// Split single cursors at first position
658+
if (flag_empty_cell || flag_first_position) text_list.push('');
633659
for (var i = 1; i < locations.length; i++) {
634660
var text = this.code_mirror.getRange(locations[i-1], locations[i]);
635661
text = text.replace(/^\n+/, '').replace(/\n+$/, ''); // removes newlines at beginning and end
636662
text_list.push(text);
637663
}
664+
// Split single cursors at last position
665+
if (flag_last_position) text_list.push('');
666+
// Duplicate cell if full cell is selected
667+
if ((text_list.length == 1) && flag_all_select && !flag_empty_cell) {
668+
text_list = text_list.concat(text_list);
669+
}
638670
return text_list;
639671
};
640672

0 commit comments

Comments
 (0)