Skip to content

Commit

Permalink
feat: support global untitled id
Browse files Browse the repository at this point in the history
  • Loading branch information
guqiankun.gqk committed Jul 6, 2022
1 parent 0a42568 commit de01ec6
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
9 changes: 9 additions & 0 deletions packages/editor/src/browser/untitled-resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@ import { AskSaveResult, IResource, IResourceProvider, WorkbenchEditorService } f

import { IEditorDocumentModelService, IEditorDocumentModelContentProvider } from './doc-model/types';

@Injectable()
export class UntitledDocumentIdCounter {
private _id = 1;

get id() {
return this._id++;
}
}

@Injectable()
export class UntitledSchemeDocumentProvider implements IEditorDocumentModelContentProvider {
@Autowired(IEditorDocumentModelService)
Expand Down
6 changes: 4 additions & 2 deletions packages/editor/src/browser/workbench-editor.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import {
Disposable,
makeRandomHexString,
} from '@opensumi/ide-core-common';
import { UntitledDocumentIdCounter } from '@opensumi/ide-editor/lib/browser/untitled-resource';
import { IMessageService } from '@opensumi/ide-overlay';
import * as monaco from '@opensumi/monaco-editor-core/esm/vs/editor/editor.api';

Expand Down Expand Up @@ -138,7 +139,8 @@ export class WorkbenchEditorServiceImpl extends WithEventBus implements Workbenc
@Autowired(IEditorDocumentModelService)
protected documentModelManager: IEditorDocumentModelService;

private untitledIndex = 1;
@Autowired()
private untitledIndex: UntitledDocumentIdCounter;

private untitledCloseIndex: number[] = [];

Expand Down Expand Up @@ -484,7 +486,7 @@ export class WorkbenchEditorServiceImpl extends WithEventBus implements Workbenc

private createUntitledURI() {
// 优先从已删除的 index 中获取
const index = this.untitledCloseIndex.shift() || this.untitledIndex++;
const index = this.untitledCloseIndex.shift() || this.untitledIndex.id;
return new URI().withScheme(Schemes.untitled).withQuery(`name=Untitled-${index}&index=${index}`);
}

Expand Down
8 changes: 5 additions & 3 deletions packages/extension/src/browser/vscode/api/main.thread.doc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
EditorDocumentModelOptionChangedEvent,
EditorDocumentModelWillSaveEvent,
} from '@opensumi/ide-editor/lib/browser';
import { UntitledDocumentIdCounter } from '@opensumi/ide-editor/lib/browser/untitled-resource';
import { IFileServiceClient } from '@opensumi/ide-file-service';

import { ExtHostAPIIdentifier, IMainThreadDocumentsShape, IExtensionHostDocService } from '../../../common/vscode';
Expand Down Expand Up @@ -69,10 +70,11 @@ class ExtensionEditorDocumentProvider implements IEditorDocumentModelContentProv

@Injectable({ multiple: true })
export class MainThreadExtensionDocumentData extends WithEventBus implements IMainThreadDocumentsShape {
private tempDocIdCount = 0;

private readonly proxy: IExtensionHostDocService;

@Autowired()
private tempDocIdCount: UntitledDocumentIdCounter;

@Autowired(IEditorDocumentModelService)
protected docManager: IEditorDocumentModelService;

Expand Down Expand Up @@ -224,7 +226,7 @@ export class MainThreadExtensionDocumentData extends WithEventBus implements IMa
}
const { language, content } = options;
const docRef = await this.docManager.createModelReference(
new URI(`${Schemes.untitled}://temp/Untitled-` + this.tempDocIdCount++),
new URI(`${Schemes.untitled}://temp/Untitled-` + this.tempDocIdCount.id),
'ext-create-document',
);
if (language) {
Expand Down

0 comments on commit de01ec6

Please sign in to comment.