Skip to content

Commit

Permalink
feat: update openTextDocument Api (#1126)
Browse files Browse the repository at this point in the history
* feat: update openTextDocument Api

* feat: support global untitled  id

* chore: fix ci
  • Loading branch information
AhkunTa authored Jul 6, 2022
1 parent 74990a7 commit ce748f5
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 15 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 @@ -90,6 +90,7 @@ import {
RegisterEditorComponentEvent,
AskSaveResult,
} from './types';
import { UntitledDocumentIdCounter } from './untitled-resource';

const MAX_CONFIRM_RESOURCES = 10;

Expand Down Expand Up @@ -151,7 +152,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 @@ -549,7 +551,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
17 changes: 11 additions & 6 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 @@ -218,16 +220,19 @@ export class MainThreadExtensionDocumentData extends WithEventBus implements IMa
});
}

async $tryCreateDocument(options: { content: string; language: string }): Promise<string> {
async $tryCreateDocument(options?: { content?: string; language?: string }): Promise<string> {
if (!options) {
options = {};
}
const { language, content } = options;
const docRef = await this.docManager.createModelReference(
new URI(`${Schemes.untitled}://temp/` + this.tempDocIdCount++),
new URI(`${Schemes.untitled}://temp/Untitled-` + this.tempDocIdCount.id),
'ext-create-document',
);
if (options.language) {
if (language) {
docRef.instance.languageId = language;
}
if (!isUndefinedOrNull(options.content)) {
if (!isUndefinedOrNull(content)) {
docRef.instance.updateContent(content);
}
return docRef.instance.uri.toString();
Expand Down
4 changes: 3 additions & 1 deletion packages/extension/src/common/vscode/doc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ export interface ExtensionDocumentDataManager extends IExtensionHostDocService {
getDocument(resource: Uri | string): vscode.TextDocument | undefined;
getDocumentData(resource: Uri | string): ExtHostDocumentData | undefined;
getAllDocument(): vscode.TextDocument[];
openTextDocument(path: Uri | string): Promise<vscode.TextDocument | undefined>;
openTextDocument(
uriOrFileNameOrOptions?: Uri | string | { language?: string; content?: string },
): Promise<vscode.TextDocument>;
registerTextDocumentContentProvider(scheme: string, provider: vscode.TextDocumentContentProvider): IDisposable;
onDidOpenTextDocument: Event<vscode.TextDocument>;
onDidCloseTextDocument: Event<vscode.TextDocument>;
Expand Down
17 changes: 11 additions & 6 deletions packages/extension/src/hosted/api/vscode/doc/doc-manager.host.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type vscode from 'vscode';

import { IRPCProtocol } from '@opensumi/ide-connection';
import { BinaryBuffer, CancellationTokenSource, Emitter, IDisposable, isUTF8 } from '@opensumi/ide-core-common';
import { BinaryBuffer, CancellationTokenSource, Emitter, IDisposable, isUTF8, URI } from '@opensumi/ide-core-common';

import {
ExtensionDocumentDataManager,
Expand Down Expand Up @@ -68,13 +68,18 @@ export class ExtensionDocumentDataManagerImpl implements ExtensionDocumentDataMa
return data ? data.document : undefined;
}

async openTextDocument(path: Uri | string) {
async openTextDocument(uriOrFileNameOrOptions?: Uri | string | { language?: string; content?: string }) {
let uri: Uri;

if (typeof path === 'string') {
uri = Uri.file(path);
const options = uriOrFileNameOrOptions as { language?: string; content?: string };

if (typeof uriOrFileNameOrOptions === 'string') {
uri = Uri.file(uriOrFileNameOrOptions);
} else if (URI.isUri(uriOrFileNameOrOptions)) {
uri = Uri.parse(uriOrFileNameOrOptions.toString());
} else if (!options || typeof options === 'object') {
uri = Uri.parse(await this._proxy.$tryCreateDocument(options));
} else {
uri = Uri.parse(path.toString());
throw new Error('illegal argument - uriOrFileNameOrOptions');
}

const doc = this._documents.get(uri.toString());
Expand Down

0 comments on commit ce748f5

Please sign in to comment.