Skip to content

Commit 2cf5346

Browse files
committed
Fix arrowing up and down to save code too (#8574)
1 parent 5fbe29e commit 2cf5346

File tree

3 files changed

+19
-0
lines changed

3 files changed

+19
-0
lines changed

news/2 Fixes/8491.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Arrowing up and down through cells can lose code that was just typed.

src/datascience-ui/interactive-common/mainStateController.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,18 @@ export class MainStateController implements IMessageHandler {
561561
}
562562
}
563563

564+
public updateCode = (cellId: string, code: string) => {
565+
const index = this.pendingState.cellVMs.findIndex(c => c.cell.id === cellId);
566+
if (index > 0) {
567+
const cellVMs = [...this.pendingState.cellVMs];
568+
const current = this.pendingState.cellVMs[index];
569+
const newCell = { ...current, inputBlockText: code, cell: { ...current.cell, data: { ...current.cell.data, source: code } } };
570+
// tslint:disable-next-line: no-any
571+
cellVMs[index] = (newCell as any); // This is because IMessageCell doesn't fit in here. But message cells can't change type
572+
this.setState({ cellVMs });
573+
}
574+
}
575+
564576
public changeCellType = (cellId: string, newType: 'code' | 'markdown') => {
565577
const index = this.pendingState.cellVMs.findIndex(c => c.cell.id === cellId);
566578
if (index >= 0 && this.pendingState.cellVMs[index].cell.data.cell_type !== newType) {

src/datascience-ui/native-editor/nativeCell.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,9 @@ export class NativeCell extends React.Component<INativeCellProps> {
392392
const prevCellId = this.getPrevCellId();
393393
if (prevCellId) {
394394
e.stopPropagation();
395+
if (e.editorInfo) {
396+
this.props.stateController.updateCode(this.getCell().id, e.editorInfo.contents);
397+
}
395398
this.moveSelection(prevCellId, this.isFocused(), CursorPos.Bottom);
396399
}
397400

@@ -403,6 +406,9 @@ export class NativeCell extends React.Component<INativeCellProps> {
403406

404407
if (nextCellId) {
405408
e.stopPropagation();
409+
if (e.editorInfo) {
410+
this.props.stateController.updateCode(this.getCell().id, e.editorInfo.contents);
411+
}
406412
this.moveSelection(nextCellId, this.isFocused(), CursorPos.Top);
407413
}
408414

0 commit comments

Comments
 (0)