Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update openTextDocument API #1126

Merged
merged 3 commits into from
Jul 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -88,6 +88,7 @@ import {
CodeEditorDidVisibleEvent,
RegisterEditorComponentEvent,
} from './types';
import { UntitledDocumentIdCounter } from './untitled-resource';

@Injectable()
export class WorkbenchEditorServiceImpl extends WithEventBus implements WorkbenchEditorService {
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
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