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

Remove hardcoding of languages #12341

Merged
merged 3 commits into from
Jun 15, 2020
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
25 changes: 1 addition & 24 deletions src/client/datascience/notebook/helpers/cellUpdateHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
NotebookCellOutputsChangeEvent,
NotebookCellsChangeEvent
} from '../../../common/application/types';
import { MARKDOWN_LANGUAGE, PYTHON_LANGUAGE } from '../../../common/constants';
import { MARKDOWN_LANGUAGE } from '../../../common/constants';
import { traceError } from '../../../logging';
import { INotebookModel } from '../../types';
import { findMappedNotebookCellModel } from './cellMappers';
Expand Down Expand Up @@ -74,16 +74,6 @@ function clearCellOutput(change: NotebookCellOutputsChangeEvent, model: INoteboo

function changeCellLanguage(change: NotebookCellLanguageChangeEvent, model: INotebookModel) {
const cellModel = findMappedNotebookCellModel(change.cell, model.cells);

// VSC fires event if changing cell language from markdown to markdown.
// https://github.com/microsoft/vscode/issues/98836
if (
(change.language === PYTHON_LANGUAGE && cellModel.data.cell_type === 'code') ||
(change.language === MARKDOWN_LANGUAGE && cellModel.data.cell_type === 'markdown')
) {
return;
}

const cellData = createCellFrom(cellModel.data, change.language === MARKDOWN_LANGUAGE ? 'markdown' : 'code');
// tslint:disable-next-line: no-any
change.cell.outputs = createVSCCellOutputsFromOutputs(cellData.outputs as any);
Expand All @@ -97,19 +87,6 @@ function changeCellLanguage(change: NotebookCellLanguageChangeEvent, model: INot
}

function handleChangesToCells(change: NotebookCellsChangeEvent, model: INotebookModel) {
// For some reason VSC fires a change even when opening a document.
// Ignore this https://github.com/microsoft/vscode/issues/98841
if (
change.changes.length === 1 &&
change.changes[0].deletedCount === 0 &&
change.changes[0].start === 0 &&
change.changes[0].items.length === model.cells.length &&
change.document.cells.length === model.cells.length
) {
// This is an event fired when a document is opened, we can safely ignore this.
return;
}

if (isCellMoveChange(change)) {
handleCellMove(change, model);
} else if (isCellDelete(change)) {
Expand Down
22 changes: 13 additions & 9 deletions src/client/datascience/notebook/helpers/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
'use strict';

import { nbformat } from '@jupyterlab/coreutils';
import * as assert from 'assert';
import * as uuid from 'uuid/v4';
import type {
CellDisplayOutput,
Expand Down Expand Up @@ -43,13 +42,14 @@ interface IBaseCellVSCodeMetadata {
*/
export function notebookModelToVSCNotebookData(model: INotebookModel): NotebookData {
const cells = model.cells
.map(createVSCNotebookCellDataFromCell)
.map(createVSCNotebookCellDataFromCell.bind(undefined, model))
.filter((item) => !!item)
.map((item) => item!);

const defaultLangauge = getDefaultCodeLanguage(model);
return {
cells,
languages: [PYTHON_LANGUAGE],
languages: [defaultLangauge],
metadata: {
cellEditable: true,
cellRunnable: true,
Expand Down Expand Up @@ -86,10 +86,7 @@ export function createCellFromVSCNotebookCell(vscCell: NotebookCell, model: INot
state: CellState.init
};
}
assert.equal(vscCell.language, PYTHON_LANGUAGE, 'Cannot create a non Python cell');
return {
// tslint:disable-next-line: no-suspicious-comment
// TODO: #12068 Translate output into nbformat.IOutput.
data: createCodeCell([vscCell.document.getText()], []),
file: model.file.toString(),
id: uuid(),
Expand Down Expand Up @@ -130,15 +127,22 @@ export function updateVSCNotebookCellMetadata(cellMetadata: NotebookCellMetadata
});
}

export function createVSCNotebookCellDataFromCell(cell: ICell): NotebookCellData | undefined {
export function getDefaultCodeLanguage(model: INotebookModel) {
return model.metadata?.language_info?.name &&
model.metadata?.language_info?.name.toLowerCase() !== PYTHON_LANGUAGE.toLowerCase()
? model.metadata?.language_info?.name
: PYTHON_LANGUAGE;
}

export function createVSCNotebookCellDataFromCell(model: INotebookModel, cell: ICell): NotebookCellData | undefined {
if (cell.data.cell_type !== 'code' && cell.data.cell_type !== 'markdown') {
traceError(`Conversion of Cell into VS Code NotebookCell not supported ${cell.data.cell_type}`);
return;
}

// tslint:disable-next-line: no-any
const outputs = createVSCCellOutputsFromOutputs(cell.data.outputs as any);

const defaultCodeLanguage = getDefaultCodeLanguage(model);
// If we have an execution count & no errors, then success state.
// If we have an execution count & errors, then error state.
// Else idle state.
Expand All @@ -160,7 +164,7 @@ export function createVSCNotebookCellDataFromCell(cell: ICell): NotebookCellData
const notebookCellData: NotebookCellData = {
cellKind:
cell.data.cell_type === 'code' ? vscodeNotebookEnums.CellKind.Code : vscodeNotebookEnums.CellKind.Markdown,
language: cell.data.cell_type === 'code' ? PYTHON_LANGUAGE : MARKDOWN_LANGUAGE,
language: cell.data.cell_type === 'code' ? defaultCodeLanguage : MARKDOWN_LANGUAGE,
metadata: {
editable: true,
executionOrder: typeof cell.data.execution_count === 'number' ? cell.data.execution_count : undefined,
Expand Down
5 changes: 3 additions & 2 deletions src/client/datascience/notebook/notebookEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import { CellKind, ConfigurationTarget, Event, EventEmitter, Uri, WebviewPanel } from 'vscode';
import type { NotebookDocument } from 'vscode-proposed';
import { IApplicationShell, ICommandManager, IVSCodeNotebook } from '../../common/application/types';
import { PYTHON_LANGUAGE } from '../../common/constants';
import { IConfigurationService } from '../../common/types';
import { DataScience } from '../../common/utils/localize';
import { noop } from '../../common/utils/misc';
Expand All @@ -22,6 +21,7 @@ import {
InterruptResult,
IStatusProvider
} from '../types';
import { getDefaultCodeLanguage } from './helpers/helpers';
import { INotebookExecutionService } from './types';

export class NotebookEditor implements INotebookEditor {
Expand Down Expand Up @@ -112,9 +112,10 @@ export class NotebookEditor implements INotebookEditor {
if (!this.vscodeNotebook.activeNotebookEditor) {
return;
}
const defaultLanguage = getDefaultCodeLanguage(this.model);
this.vscodeNotebook.activeNotebookEditor.edit((editor) => {
const totalLength = this.document.cells.length;
editor.insert(this.document.cells.length, '', PYTHON_LANGUAGE, CellKind.Code, [], undefined);
editor.insert(this.document.cells.length, '', defaultLanguage, CellKind.Code, [], undefined);
for (let i = totalLength - 1; i >= 0; i = i - 1) {
editor.delete(i);
}
Expand Down
76 changes: 76 additions & 0 deletions src/test/datascience/notebook/contentProvider.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,82 @@ suite('Data Science - NativeNotebook ContentProvider', () => {
}
]);
});

test('Return notebook with csharp language', async () => {
const model: Partial<INotebookModel> = {
metadata: {
language_info: {
name: 'csharp'
},
orig_nbformat: 5
},
cells: [
{
data: {
cell_type: 'code',
execution_count: 10,
hasExecutionOrder: true,
outputs: [],
source: 'Console.WriteLine("1")',
metadata: {}
},
file: 'a.ipynb',
id: 'MyCellId1',
line: 0,
state: CellState.init
},
{
data: {
cell_type: 'markdown',
hasExecutionOrder: false,
source: '# HEAD',
metadata: {}
},
file: 'a.ipynb',
id: 'MyCellId2',
line: 0,
state: CellState.init
}
]
};
when(storageProvider.load(anything())).thenResolve((model as unknown) as INotebookModel);

const notebook = await contentProvider.openNotebook(fileUri);

assert.isOk(notebook);
assert.deepEqual(notebook.languages, ['csharp']);
// ignore metadata we add.
notebook.cells.forEach((cell) => delete cell.metadata.custom);

assert.deepEqual(notebook.cells, [
{
cellKind: (vscodeNotebookEnums as any).CellKind.Code,
language: 'csharp',
outputs: [],
source: 'Console.WriteLine("1")',
metadata: {
editable: true,
executionOrder: 10,
hasExecutionOrder: true,
runState: (vscodeNotebookEnums as any).NotebookCellRunState.Success,
runnable: true
}
},
{
cellKind: (vscodeNotebookEnums as any).CellKind.Markdown,
language: MARKDOWN_LANGUAGE,
outputs: [],
source: '# HEAD',
metadata: {
editable: true,
executionOrder: undefined,
hasExecutionOrder: false,
runState: (vscodeNotebookEnums as any).NotebookCellRunState.Idle,
runnable: false
}
}
]);
});
test('Verify mime types and order', () => {
// https://github.com/microsoft/vscode-python/issues/11880
});
Expand Down