mirror of
https://github.com/Microsoft/vscode
synced 2024-08-27 21:09:43 +00:00
fix #140673.
This commit is contained in:
parent
1bcad24aba
commit
157cba093c
|
@ -308,7 +308,9 @@ function createNotebookCellDataFromCodeCell(cell: nbformat.ICodeCell, cellLangua
|
|||
? { executionOrder: cell.execution_count as number }
|
||||
: {};
|
||||
|
||||
const cellData = new NotebookCellData(NotebookCellKind.Code, source, cellLanguage);
|
||||
const vscodeCustomMetadata = cell.metadata['vscode'] as { [key: string]: any } | undefined;
|
||||
const cellLanguageId = vscodeCustomMetadata && vscodeCustomMetadata.languageId && typeof vscodeCustomMetadata.languageId === 'string' ? vscodeCustomMetadata.languageId : cellLanguage;
|
||||
const cellData = new NotebookCellData(NotebookCellKind.Code, source, cellLanguageId);
|
||||
|
||||
cellData.outputs = outputs;
|
||||
cellData.metadata = { custom: getNotebookCellMetadata(cell) };
|
||||
|
|
|
@ -83,9 +83,11 @@ export class NotebookSerializer implements vscode.NotebookSerializer {
|
|||
|
||||
public serializeNotebookToString(data: vscode.NotebookData): string {
|
||||
const notebookContent = getNotebookMetadata(data);
|
||||
// use the preferred language from document metadata or the first cell language as the notebook preferred cell language
|
||||
const preferredCellLanguage = notebookContent.metadata?.language_info?.name ?? data.cells[0].languageId;
|
||||
|
||||
notebookContent.cells = data.cells
|
||||
.map(cell => createJupyterCellFromNotebookCell(cell))
|
||||
.map(cell => createJupyterCellFromNotebookCell(cell, preferredCellLanguage))
|
||||
.map(pruneCell);
|
||||
|
||||
const indentAmount = data.metadata && 'indentAmount' in data.metadata && typeof data.metadata.indentAmount === 'string' ?
|
||||
|
|
|
@ -17,7 +17,8 @@ enum CellOutputMimeTypes {
|
|||
}
|
||||
|
||||
export function createJupyterCellFromNotebookCell(
|
||||
vscCell: NotebookCellData
|
||||
vscCell: NotebookCellData,
|
||||
preferredLanguage: string | undefined
|
||||
): nbformat.IRawCell | nbformat.IMarkdownCell | nbformat.ICodeCell {
|
||||
let cell: nbformat.IRawCell | nbformat.IMarkdownCell | nbformat.ICodeCell;
|
||||
if (vscCell.kind === NotebookCellKind.Markup) {
|
||||
|
@ -25,7 +26,7 @@ export function createJupyterCellFromNotebookCell(
|
|||
} else if (vscCell.languageId === 'raw') {
|
||||
cell = createRawCellFromNotebookCell(vscCell);
|
||||
} else {
|
||||
cell = createCodeCellFromNotebookCell(vscCell);
|
||||
cell = createCodeCellFromNotebookCell(vscCell, preferredLanguage);
|
||||
}
|
||||
return cell;
|
||||
}
|
||||
|
@ -56,14 +57,27 @@ export function sortObjectPropertiesRecursively(obj: any): any {
|
|||
export function getCellMetadata(cell: NotebookCell | NotebookCellData) {
|
||||
return cell.metadata?.custom as CellMetadata | undefined;
|
||||
}
|
||||
function createCodeCellFromNotebookCell(cell: NotebookCellData): nbformat.ICodeCell {
|
||||
function createCodeCellFromNotebookCell(cell: NotebookCellData, preferredLanguage: string | undefined): nbformat.ICodeCell {
|
||||
const cellMetadata = getCellMetadata(cell);
|
||||
let metadata = cellMetadata?.metadata || {}; // This cannot be empty.
|
||||
if (cell.languageId !== preferredLanguage) {
|
||||
metadata = {
|
||||
...metadata,
|
||||
vscode: {
|
||||
languageId: cell.languageId
|
||||
}
|
||||
};
|
||||
} else {
|
||||
// cell current language is the same as the preferred cell language in the document, flush the vscode custom language id metadata
|
||||
metadata.vscode = undefined;
|
||||
}
|
||||
|
||||
const codeCell: any = {
|
||||
cell_type: 'code',
|
||||
execution_count: cell.executionSummary?.executionOrder ?? null,
|
||||
source: splitMultilineString(cell.value.replace(/\r\n/g, '\n')),
|
||||
outputs: (cell.outputs || []).map(translateCellDisplayOutput),
|
||||
metadata: cellMetadata?.metadata || {} // This cannot be empty.
|
||||
metadata: metadata
|
||||
};
|
||||
if (cellMetadata?.id) {
|
||||
codeCell.id = cellMetadata.id;
|
||||
|
|
Loading…
Reference in a new issue