Create untitled notebook names in core code rather than in the notebook extension (#21111)

This commit is contained in:
Cory Rivera
2022-11-04 19:31:55 -07:00
committed by GitHub
parent a618ef983a
commit facd317b4d
9 changed files with 63 additions and 94 deletions

View File

@@ -28,6 +28,7 @@ import { ITextFileService } from 'vs/workbench/services/textfile/common/textfile
import { NotebookEditor } from 'sql/workbench/contrib/notebook/browser/notebookEditor';
import { extHostNamedCustomer, IExtHostContext } from 'vs/workbench/services/extensions/common/extHostCustomers';
import { SqlExtHostContext, SqlMainContext } from 'vs/workbench/api/common/extHost.protocol';
import { IUntitledTextEditorService } from 'vs/workbench/services/untitled/common/untitledTextEditorService';
class MainThreadNotebookEditor extends Disposable {
private _contentChangedEmitter = new Emitter<NotebookContentChange>();
@@ -321,7 +322,8 @@ export class MainThreadNotebookDocumentsAndEditors extends Disposable implements
@IInstantiationService private _instantiationService: IInstantiationService,
@INotebookService private readonly _notebookService: INotebookService,
@IFileService private readonly _fileService: IFileService,
@ITextFileService private readonly _textFileService: ITextFileService
@ITextFileService private readonly _textFileService: ITextFileService,
@IUntitledTextEditorService private readonly _untitledEditorService: IUntitledTextEditorService,
) {
super();
if (extHostContext) {
@@ -343,15 +345,47 @@ export class MainThreadNotebookDocumentsAndEditors extends Disposable implements
}
}
/**
* Attempts to create a new notebook with the specified provider and contents. Used for VS Code extension compatibility.
*/
async $tryCreateNotebookDocument(providerId: string, contents?: azdata.nb.INotebookContents): Promise<UriComponents> {
let input = await this._notebookService.createNotebookInputFromContents(providerId, contents);
return input.resource;
}
$tryShowNotebookDocument(resource: UriComponents, options: INotebookShowOptions): Promise<string> {
// Append a numbered suffix if an untitled notebook is already open with the same path
if (resource.scheme === Schemas.untitled) {
if (!resource.path || this.untitledEditorTitleExists(resource.path)) {
resource.path = this.createPrefixedNotebookFilePath(resource.path);
}
}
return Promise.resolve(this.doOpenEditor(resource, options));
}
private untitledEditorTitleExists(filePath: string): boolean {
return !!this._untitledEditorService.get(URI.from({ scheme: Schemas.untitled, path: filePath }));
}
private createPrefixedNotebookFilePath(prefix?: string): string {
if (!prefix) {
prefix = 'Notebook';
}
let prefixFileName = (counter: number): string => {
return `${prefix}-${counter}`;
};
let counter = 1;
// Get document name and check if it exists
let filePath = prefixFileName(counter);
while (this.untitledEditorTitleExists(filePath)) {
counter++;
filePath = prefixFileName(counter);
}
return filePath;
}
$trySetTrusted(uriComponent: UriComponents, isTrusted: boolean): Promise<boolean> {
let uri = URI.revive(uriComponent);
return this._notebookService.setTrusted(uri, isTrusted);