Skip to content

Commit 6f385f1

Browse files
authored
updated logic for cell language changes (#1323)
* updated logic for cell language changes * fix metadata errors and keep reopening on close * updated code
1 parent 3174511 commit 6f385f1

File tree

6 files changed

+55
-86
lines changed

6 files changed

+55
-86
lines changed

src/dotnet-interactive-vscode/common/vscode/commands.ts

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -180,20 +180,7 @@ export function registerFileCommands(context: vscode.ExtensionContext, clientMap
180180
context.subscriptions.push(vscode.commands.registerCommand('dotnet-interactive.newNotebookIpynb', async () => {
181181
// note, new .ipynb notebooks are currently affected by this bug: https://github.com/microsoft/vscode/issues/121974
182182

183-
const jupyterExtension = vscode.extensions.getExtension<IJupyterExtensionApi>("ms-toolsai.jupyter");
184-
185-
if (!vscode.extensions.getExtension("ms-toolsai.jupyter")?.isActive) {
186-
await vscode.extensions.getExtension("ms-toolsai.jupyter")?.activate();
187-
}
188-
189-
const jupyterExtensionExports = jupyterExtension?.exports;
190-
191-
if (jupyterExtensionExports) {
192-
await jupyterExtensionExports.createBlankNotebook({ defaultCellLanguage: "dotnet-interactive.csharp" });
193-
}
194-
else {
195-
await newNotebook('.ipynb');
196-
}
183+
await newNotebook('.ipynb');
197184

198185
selectDotNetInteractiveKernel();
199186
}));
@@ -239,7 +226,28 @@ export function registerFileCommands(context: vscode.ExtensionContext, clientMap
239226
const viewType = extension === '.dib' || extension === '.dotnet-interactive'
240227
? 'dotnet-interactive'
241228
: 'jupyter-notebook';
242-
await vscode.commands.executeCommand('vscode.openWith', uri, viewType);
229+
230+
if (viewType === 'jupyter-notebook') {
231+
await openNewNotebookWithJupyterEXtension();
232+
} else {
233+
await vscode.commands.executeCommand('vscode.openWith', uri, viewType);
234+
}
235+
}
236+
237+
async function openNewNotebookWithJupyterEXtension() {
238+
const jupyterExtension = vscode.extensions.getExtension<IJupyterExtensionApi>('ms-toolsai.jupyter');
239+
240+
if (jupyterExtension) {
241+
if (!jupyterExtension?.isActive) {
242+
await jupyterExtension?.activate();
243+
}
244+
245+
const jupyterExtensionExports = jupyterExtension?.exports;
246+
247+
if (jupyterExtensionExports) {
248+
await jupyterExtensionExports.createBlankNotebook({ defaultCellLanguage: 'dotnet-interactive.csharp' });
249+
}
250+
}
243251
}
244252

245253
context.subscriptions.push(vscode.commands.registerCommand('dotnet-interactive.saveAsNotebook', async () => {

src/dotnet-interactive-vscode/common/vscode/extension.ts

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import { OutputChannelAdapter } from './OutputChannelAdapter';
1919
import * as versionSpecificFunctions from '../../versionSpecificFunctions';
2020

2121
import { isInsidersBuild } from './vscodeUtilities';
22+
import { getDotNetMetadata } from '../ipynbUtilities';
2223

2324
export const KernelId = 'dotnet-interactive';
2425

@@ -185,18 +186,22 @@ async function updateNotebookCellLanguageInMetadata(candidateNotebookCellDocumen
185186
isDotnetInteractiveLanguage(candidateNotebookCellDocument.languageId)) {
186187
const cell = versionSpecificFunctions.getCells(notebook).find(c => c.document === candidateNotebookCellDocument);
187188
if (cell && cell.kind === vscode.NotebookCellKind.Code) {
188-
const newMetadata = cell.metadata.with({
189-
custom: {
190-
metadata: {
191-
dotnet_interactive: {
192-
language: getSimpleLanguage(candidateNotebookCellDocument.languageId)
189+
const cellLanguage = getSimpleLanguage(candidateNotebookCellDocument.languageId);
190+
const dotnetMetadata = getDotNetMetadata(cell.metadata);
191+
if (dotnetMetadata.language !== cellLanguage) {
192+
const newMetadata = cell.metadata.with({
193+
custom: {
194+
metadata: {
195+
dotnet_interactive: {
196+
language: cellLanguage
197+
}
193198
}
194199
}
195-
}
196-
});
197-
const edit = new vscode.WorkspaceEdit();
198-
edit.replaceNotebookCellMetadata(notebook.uri, cell.index, newMetadata);
199-
await vscode.workspace.applyEdit(edit);
200+
});
201+
const edit = new vscode.WorkspaceEdit();
202+
edit.replaceNotebookCellMetadata(notebook.uri, cell.index, newMetadata);
203+
await vscode.workspace.applyEdit(edit);
204+
}
200205
}
201206
}
202207
}

src/dotnet-interactive-vscode/insiders/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@
118118
},
119119
"dotnet-interactive.minimumInteractiveToolVersion": {
120120
"type": "string",
121-
"default": "1.0.222802",
121+
"default": "1.0.225503",
122122
"description": "The minimum required version of the .NET Interactive tool."
123123
}
124124
}

src/dotnet-interactive-vscode/insiders/src/notebookControllers.ts

Lines changed: 7 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { getCellLanguage, getDotNetMetadata, getLanguageInfoMetadata, isDotNetNo
1515
import { reshapeOutputValueForVsCode } from './common/interfaces/utilities';
1616
import { selectDotNetInteractiveKernel } from './common/vscode/commands';
1717

18-
const executionTasks: Map<vscode.Uri, vscode.NotebookCellExecutionTask> = new Map();
18+
const executionTasks: Map<string, vscode.NotebookCellExecutionTask> = new Map();
1919

2020
const viewType = 'dotnet-interactive';
2121
const jupyterViewType = 'jupyter-notebook';
@@ -97,12 +97,12 @@ export class DotNetNotebookKernel {
9797
private async executeCell(cell: vscode.NotebookCell, controller: vscode.NotebookController): Promise<void> {
9898
const executionTask = controller.createNotebookCellExecutionTask(cell);
9999
if (executionTask) {
100-
executionTasks.set(cell.document.uri, executionTask);
100+
executionTasks.set(cell.document.uri.toString(), executionTask);
101101
try {
102102
executionTask.start({
103103
startTime: Date.now(),
104104
});
105-
setCellLockState(cell, true);
105+
106106
executionTask.clearOutput(cell.index);
107107
const client = await this.clientMapper.getOrAddClient(cell.notebook.uri);
108108
executionTask.token.onCancellationRequested(() => {
@@ -175,22 +175,17 @@ export async function updateCellLanguages(document: vscode.NotebookDocument): Pr
175175
await vscode.workspace.applyEdit(edit);
176176
}
177177

178-
function setCellLockState(cell: vscode.NotebookCell, locked: boolean) {
179-
const edit = new vscode.WorkspaceEdit();
180-
edit.replaceNotebookCellMetadata(cell.notebook.uri, cell.index, cell.metadata.with({ editable: !locked }));
181-
return vscode.workspace.applyEdit(edit);
182-
}
183-
184178
async function updateCellOutputs(executionTask: vscode.NotebookCellExecutionTask, cell: vscode.NotebookCell, outputs: Array<vscodeLike.NotebookCellOutput>): Promise<void> {
185179
const reshapedOutputs = outputs.map(o => new vscode.NotebookCellOutput(o.outputs.map(oi => generateVsCodeNotebookCellOutputItem(oi.mime, oi.value))));
186180
await executionTask.replaceOutput(reshapedOutputs, cell.index);
187181
}
188182

189183
export function endExecution(cell: vscode.NotebookCell, success: boolean) {
190-
setCellLockState(cell, false);
191-
const executionTask = executionTasks.get(cell.document.uri);
184+
185+
const key = cell.document.uri.toString();
186+
const executionTask = executionTasks.get(key);
192187
if (executionTask) {
193-
executionTasks.delete(cell.document.uri);
188+
executionTasks.delete(key);
194189
executionTask.end({
195190
success,
196191
endTime: Date.now(),
@@ -206,24 +201,7 @@ function generateVsCodeNotebookCellOutputItem(mimeType: string, value: unknown):
206201
async function updateDocumentKernelspecMetadata(document: vscode.NotebookDocument): Promise<void> {
207202
const edit = new vscode.WorkspaceEdit();
208203
const documentKernelMetadata = withDotNetKernelMetadata(document.metadata);
209-
210-
// workaround for https://github.com/microsoft/vscode/issues/115912; capture all cell data so we can re-apply it at the end
211-
const cellData: Array<vscode.NotebookCellData> = versionSpecificFunctions.getCells(document).map(c => {
212-
return new vscode.NotebookCellData(
213-
c.kind,
214-
c.document.getText(),
215-
c.document.languageId,
216-
c.outputs.concat(), // can't pass through a readonly property, so we have to make it a regular array
217-
c.metadata,
218-
);
219-
});
220-
221204
edit.replaceNotebookMetadata(document.uri, documentKernelMetadata);
222-
223-
// this is the re-application for the workaround mentioned above
224-
const range = new vscode.NotebookRange(0, document.cellCount);
225-
edit.replaceNotebookCells(document.uri, range, cellData);
226-
227205
await vscode.workspace.applyEdit(edit);
228206
}
229207

src/dotnet-interactive-vscode/stable/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@
118118
},
119119
"dotnet-interactive.minimumInteractiveToolVersion": {
120120
"type": "string",
121-
"default": "1.0.222802",
121+
"default": "1.0.225503",
122122
"description": "The minimum required version of the .NET Interactive tool."
123123
}
124124
}
@@ -398,4 +398,4 @@
398398
"node-fetch": "^2.6.1",
399399
"uuid": "^8.3.2"
400400
}
401-
}
401+
}

src/dotnet-interactive-vscode/stable/src/notebookControllers.ts

Lines changed: 7 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { getCellLanguage, getDotNetMetadata, getLanguageInfoMetadata, isDotNetNo
1515
import { reshapeOutputValueForVsCode } from './common/interfaces/utilities';
1616
import { selectDotNetInteractiveKernel } from './common/vscode/commands';
1717

18-
const executionTasks: Map<vscode.Uri, vscode.NotebookCellExecutionTask> = new Map();
18+
const executionTasks: Map<string, vscode.NotebookCellExecutionTask> = new Map();
1919

2020
const viewType = 'dotnet-interactive';
2121
const jupyterViewType = 'jupyter-notebook';
@@ -97,12 +97,12 @@ export class DotNetNotebookKernel {
9797
private async executeCell(cell: vscode.NotebookCell, controller: vscode.NotebookController): Promise<void> {
9898
const executionTask = controller.createNotebookCellExecutionTask(cell);
9999
if (executionTask) {
100-
executionTasks.set(cell.document.uri, executionTask);
100+
executionTasks.set(cell.document.uri.toString(), executionTask);
101101
try {
102102
executionTask.start({
103103
startTime: Date.now(),
104104
});
105-
setCellLockState(cell, true);
105+
106106
executionTask.clearOutput(cell.index);
107107
const client = await this.clientMapper.getOrAddClient(cell.notebook.uri);
108108
executionTask.token.onCancellationRequested(() => {
@@ -175,22 +175,17 @@ export async function updateCellLanguages(document: vscode.NotebookDocument): Pr
175175
await vscode.workspace.applyEdit(edit);
176176
}
177177

178-
function setCellLockState(cell: vscode.NotebookCell, locked: boolean) {
179-
const edit = new vscode.WorkspaceEdit();
180-
edit.replaceNotebookCellMetadata(cell.notebook.uri, cell.index, cell.metadata.with({ editable: !locked }));
181-
return vscode.workspace.applyEdit(edit);
182-
}
183-
184178
async function updateCellOutputs(executionTask: vscode.NotebookCellExecutionTask, cell: vscode.NotebookCell, outputs: Array<vscodeLike.NotebookCellOutput>): Promise<void> {
185179
const reshapedOutputs = outputs.map(o => new vscode.NotebookCellOutput(o.outputs.map(oi => generateVsCodeNotebookCellOutputItem(oi.mime, oi.value))));
186180
await executionTask.replaceOutput(reshapedOutputs, cell.index);
187181
}
188182

189183
export function endExecution(cell: vscode.NotebookCell, success: boolean) {
190-
setCellLockState(cell, false);
191-
const executionTask = executionTasks.get(cell.document.uri);
184+
185+
const key = cell.document.uri.toString();
186+
const executionTask = executionTasks.get(key);
192187
if (executionTask) {
193-
executionTasks.delete(cell.document.uri);
188+
executionTasks.delete(key);
194189
executionTask.end({
195190
success,
196191
endTime: Date.now(),
@@ -206,24 +201,7 @@ function generateVsCodeNotebookCellOutputItem(mimeType: string, value: unknown):
206201
async function updateDocumentKernelspecMetadata(document: vscode.NotebookDocument): Promise<void> {
207202
const edit = new vscode.WorkspaceEdit();
208203
const documentKernelMetadata = withDotNetKernelMetadata(document.metadata);
209-
210-
// workaround for https://github.com/microsoft/vscode/issues/115912; capture all cell data so we can re-apply it at the end
211-
const cellData: Array<vscode.NotebookCellData> = versionSpecificFunctions.getCells(document).map(c => {
212-
return new vscode.NotebookCellData(
213-
c.kind,
214-
c.document.getText(),
215-
c.document.languageId,
216-
c.outputs.concat(), // can't pass through a readonly property, so we have to make it a regular array
217-
c.metadata,
218-
);
219-
});
220-
221204
edit.replaceNotebookMetadata(document.uri, documentKernelMetadata);
222-
223-
// this is the re-application for the workaround mentioned above
224-
const range = new vscode.NotebookRange(0, document.cellCount);
225-
edit.replaceNotebookCells(document.uri, range, cellData);
226-
227205
await vscode.workspace.applyEdit(edit);
228206
}
229207

0 commit comments

Comments
 (0)