Preserve previous code cell's language when creating a new code cell from an existing context. (#18646)

This commit is contained in:
Cory Rivera
2022-03-06 21:35:57 -08:00
committed by GitHub
parent 5d0f0afdc6
commit eccb77aca3
6 changed files with 41 additions and 11 deletions

View File

@@ -75,7 +75,7 @@ export class SplitCellAction extends CellActionBase {
doRun(context: CellContext): Promise<void> {
let model = context.model;
let index = model.cells.findIndex((cell) => cell.id === context.cell.id);
context.model?.splitCell(context.cell.cellType, this.notebookService, index);
context.model?.splitCell(context.cell.cellType, this.notebookService, index, context.cell.metadata?.language);
return Promise.resolve();
}
public setListener(context: CellContext) {
@@ -246,7 +246,7 @@ export class AddCellFromContextAction extends CellActionBase {
if (index !== undefined && this.isAfter) {
index += 1;
}
model.addCell(this.cellType, index);
model.addCell(this.cellType, index, context.cell.metadata?.language);
} catch (error) {
let message = getErrorMessage(error);

View File

@@ -74,7 +74,7 @@ export class AddCellAction extends Action {
}
}
if (context?.model) {
context.model.addCell(this.cellType, index);
context.model.addCell(this.cellType, index, context.cell.metadata?.language);
context.model.sendNotebookTelemetryActionEvent(TelemetryKeys.NbTelemetryAction.AddCell, { cell_type: this.cellType });
}
} else {

View File

@@ -1006,6 +1006,33 @@ suite('notebook model', function (): void {
assert.strictEqual(model.languageInfo.name, 'fake', 'Notebook language info is not set properly');
});
test('Should add new cell with provided cell language', async function () {
let mockContentManager = TypeMoq.Mock.ofType(NotebookEditorContentLoader);
mockContentManager.setup(c => c.loadContent()).returns(() => Promise.resolve(expectedNotebookContent));
defaultModelOptions.contentLoader = mockContentManager.object;
let model = new NotebookModel(defaultModelOptions, undefined, logService, undefined, new NullAdsTelemetryService(), queryConnectionService.object, configurationService, undoRedoService);
await model.loadContents();
const newLanguage = 'CustomCellLanguage';
let cell = model.addCell(CellTypes.Code, undefined, newLanguage);
assert.strictEqual(model.cells.length, expectedNotebookContent.cells.length + 1, 'New cell was not added to list of cells.');
assert.strictEqual(cell.language, newLanguage);
});
test('Should add new cell with default language if none is provided', async function () {
let mockContentManager = TypeMoq.Mock.ofType(NotebookEditorContentLoader);
mockContentManager.setup(c => c.loadContent()).returns(() => Promise.resolve(expectedNotebookContent));
defaultModelOptions.contentLoader = mockContentManager.object;
let model = new NotebookModel(defaultModelOptions, undefined, logService, undefined, new NullAdsTelemetryService(), queryConnectionService.object, configurationService, undoRedoService);
await model.loadContents();
let cell = model.addCell(CellTypes.Code);
assert.strictEqual(model.cells.length, expectedNotebookContent.cells.length + 1, 'New cell was not added to list of cells.');
assert.strictEqual(cell.language, expectedNotebookContent.metadata.language_info.name);
});
async function loadModelAndStartClientSession(notebookContent: nb.INotebookContents): Promise<NotebookModel> {
let mockContentManager = TypeMoq.Mock.ofType(NotebookEditorContentLoader);
mockContentManager.setup(c => c.loadContent()).returns(() => Promise.resolve(notebookContent));

View File

@@ -122,7 +122,7 @@ export class NotebookModelStub implements INotebookModel {
getMetaValue(key: string) {
throw new Error('Method not implemented.');
}
addCell(cellType: CellType, index?: number): void {
addCell(cellType: CellType, index?: number, language?: string): void {
throw new Error('Method not implemented.');
}
moveCell(cellModel: ICellModel, direction: MoveDirection): void {