Skip to content
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
1 change: 1 addition & 0 deletions news/2 Fixes/8399.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix cells being erased when saving and then changing focus to another cell.
55 changes: 25 additions & 30 deletions src/datascience-ui/interactive-common/mainStateController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -339,26 +339,21 @@ export class MainStateController implements IMessageHandler {
this.clearAllSilent();
}

public updateCellSource = (cellId: string) => {
const models = monacoEditor.editor.getModels();
const cvm = this.findCell(cellId);
if (cvm) {
const modelId = this.getMonacoId(cvm.cell.id);
if (modelId) {
const model = models.find(m => m.id === modelId);
if (model) {
cvm.cell.data.source = cvm.inputBlockText = model.getValue().replace(/\r/g, '');
}
}
}
}

public save = () => {
// We have to take the current value of each cell to make sure we have the correct text.
this.pendingState.cellVMs.forEach(c => this.updateCellSource(c.cell.id));
const newVMs = [...this.pendingState.cellVMs];
for (let i = 0; i < newVMs.length; i += 1) {
const text = this.getMonacoEditorContents(newVMs[i].cell.id);
if (text !== undefined) {
newVMs[i] = { ...newVMs[i], inputBlockText: text, cell: { ...newVMs[i].cell, data: { ...newVMs[i].cell.data, source: text } } };
}
}
this.setState({
cellVMs: newVMs
});

// Then send the save with the new state.
this.sendMessage(InteractiveWindowMessages.SaveAll, { cells: this.getNonEditCellVMs().map(cvm => cvm.cell) });
this.sendMessage(InteractiveWindowMessages.SaveAll, { cells: newVMs.map(cvm => cvm.cell) });
}

public showPlot = (imageHtml: string) => {
Expand Down Expand Up @@ -764,6 +759,20 @@ export class MainStateController implements IMessageHandler {
return this.pendingState;
}

public getMonacoEditorContents(cellId: string): string | undefined {
const index = this.findCellIndex(cellId);
if (index >= 0) {
// Get the model for the monaco editor
const monacoId = this.getMonacoId(cellId);
if (monacoId) {
const model = monacoEditor.editor.getModels().find(m => m.id === monacoId);
if (model) {
return model.getValue().replace(/\r/g, '');
}
}
}
}

// Adjust the visibility or collapsed state of a cell
protected alterCellVM(cellVM: ICellViewModel, visible: boolean, expanded: boolean): ICellViewModel {
if (cellVM.cell.data.cell_type === 'code') {
Expand Down Expand Up @@ -811,20 +820,6 @@ export class MainStateController implements IMessageHandler {
return cellVM;
}

protected getMonacoEditorContents(cellId: string): string | undefined {
const index = this.findCellIndex(cellId);
if (index >= 0) {
// Get the model for the monaco editor
const monacoId = this.getMonacoId(cellId);
if (monacoId) {
const model = monacoEditor.editor.getModels().find(m => m.id === monacoId);
if (model) {
return model.getValue().replace(/\r/g, '');
}
}
}
}

protected onCodeLostFocus(_cellId: string) {
// Default is do nothing.
}
Expand Down
6 changes: 3 additions & 3 deletions src/datascience-ui/native-editor/nativeCell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,7 @@ export class NativeCell extends React.Component<INativeCellProps> {
let content: string | undefined ;

// If inside editor, submit this code
if (possibleContents) {
if (possibleContents !== undefined) {
content = possibleContents;
} else {
// Outside editor, just use the cell
Expand Down Expand Up @@ -558,8 +558,8 @@ export class NativeCell extends React.Component<INativeCellProps> {
private renderMiddleToolbar = () => {
const cellId = this.props.cellVM.cell.id;
const runCell = () => {
this.props.stateController.updateCellSource(cellId);
this.runAndMove(concatMultilineStringInput(this.props.cellVM.cell.data.source));
const contents = this.props.stateController.getMonacoEditorContents(cellId);
this.runAndMove(contents);
this.props.stateController.sendCommand(NativeCommandType.Run, 'mouse');
};
const gatherCell = () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ export class NativeEditorStateController extends MainStateController {
if (index >= 0) {
// Get the model source from the monaco editor
const source = this.getMonacoEditorContents(cellId);
if (source) {
if (source !== undefined) {
const newVMs = [...this.getState().cellVMs];

// Update our state
Expand Down