Open books in VS Code notebooks if the setting is enabled. (#22031)

This commit is contained in:
Cory Rivera
2023-02-27 11:54:35 -08:00
committed by GitHub
parent dbd1c1b5b3
commit 73f00b63ce
3 changed files with 28 additions and 8 deletions

View File

@@ -19,6 +19,9 @@
"supported": true "supported": true
} }
}, },
"enabledApiProposals": [
"notebookEditor"
],
"contributes": { "contributes": {
"configuration": { "configuration": {
"type": "object", "type": "object",

View File

@@ -395,13 +395,23 @@ export class BookTreeViewProvider implements vscode.TreeDataProvider<BookTreeIte
} }
} }
private get useVSCodeNotebooks(): boolean {
let workbench = vscode.workspace.getConfiguration('workbench');
return workbench?.get<boolean>('useVSCodeNotebooks') && workbench?.get<boolean>('enablePreviewFeatures');
}
async openNotebook(resource: string): Promise<void> { async openNotebook(resource: string): Promise<void> {
try { try {
await vscode.commands.executeCommand(constants.BuiltInCommands.SetContext, constants.unsavedBooksContextKey, false); await vscode.commands.executeCommand(constants.BuiltInCommands.SetContext, constants.unsavedBooksContextKey, false);
if (this._openAsUntitled) { if (this._openAsUntitled) {
await this.openNotebookAsUntitled(resource); await this.openNotebookAsUntitled(resource);
} else { } else {
await azdata.nb.showNotebookDocument(vscode.Uri.file(resource)); if (this.useVSCodeNotebooks) {
let doc = await vscode.workspace.openNotebookDocument(vscode.Uri.file(resource));
await vscode.window.showNotebookDocument(doc);
} else {
await azdata.nb.showNotebookDocument(vscode.Uri.file(resource));
}
// let us keep a list of already visited notebooks so that we do not trust them again, potentially // let us keep a list of already visited notebooks so that we do not trust them again, potentially
// overriding user changes // overriding user changes
let normalizedResource = path.normalize(resource); let normalizedResource = path.normalize(resource);
@@ -532,13 +542,19 @@ export class BookTreeViewProvider implements vscode.TreeDataProvider<BookTreeIte
async openNotebookAsUntitled(resource: string): Promise<void> { async openNotebookAsUntitled(resource: string): Promise<void> {
try { try {
await vscode.commands.executeCommand(constants.BuiltInCommands.SetContext, constants.unsavedBooksContextKey, true); await vscode.commands.executeCommand(constants.BuiltInCommands.SetContext, constants.unsavedBooksContextKey, true);
let untitledFileName: vscode.Uri = this.getUntitledNotebookUri(resource); let untitledFileUri: vscode.Uri = this.getUntitledNotebookUri(resource);
let document: vscode.TextDocument = await vscode.workspace.openTextDocument(resource);
await azdata.nb.showNotebookDocument(untitledFileName, { if (this.useVSCodeNotebooks) {
connectionProfile: null, let doc = await vscode.workspace.openNotebookDocument(untitledFileUri);
initialContent: document.getText(), await vscode.window.showNotebookDocument(doc);
initialDirtyState: false } else {
}); let document: vscode.TextDocument = await vscode.workspace.openTextDocument(resource);
await azdata.nb.showNotebookDocument(untitledFileUri, {
connectionProfile: null,
initialContent: document.getText(),
initialDirtyState: false
});
}
} catch (e) { } catch (e) {
void vscode.window.showErrorMessage(loc.openUntitledNotebookError(resource, e instanceof Error ? e.message : e)); void vscode.window.showErrorMessage(loc.openUntitledNotebookError(resource, e instanceof Error ? e.message : e));
} }

View File

@@ -6,4 +6,5 @@
/// <reference path='../../../../src/sql/azdata.d.ts'/> /// <reference path='../../../../src/sql/azdata.d.ts'/>
/// <reference path='../../../../src/sql/azdata.proposed.d.ts'/> /// <reference path='../../../../src/sql/azdata.proposed.d.ts'/>
/// <reference path='../../../../src/vscode-dts/vscode.d.ts'/> /// <reference path='../../../../src/vscode-dts/vscode.d.ts'/>
/// <reference path='../../../../src/vscode-dts/vscode.proposed.notebookEditor.d.ts'/>
/// <reference types='@types/node'/> /// <reference types='@types/node'/>