Skip to content

Commit

Permalink
openTextDocument shouldn't open a file in the editor
Browse files Browse the repository at this point in the history
Signed-off-by: Anatoliy Bazko <abazko@redhat.com>
  • Loading branch information
tolusha committed May 28, 2019
1 parent c184c9c commit 7dbd18d
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 18 deletions.
3 changes: 2 additions & 1 deletion packages/plugin-ext/src/api/plugin-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -850,7 +850,8 @@ export interface DocumentsExt {

export interface DocumentsMain {
$tryCreateDocument(options?: { language?: string; content?: string; }): Promise<UriComponents>;
$tryOpenDocument(uri: UriComponents, options?: TextDocumentShowOptions): Promise<void>;
$tryShowDocument(uri: UriComponents, options?: TextDocumentShowOptions): Promise<void>;
$tryOpenDocument(uri: UriComponents): Promise<boolean>;
$trySaveDocument(uri: UriComponents): Promise<boolean>;
$tryCloseDocument(uri: UriComponents): Promise<boolean>;
}
Expand Down
13 changes: 12 additions & 1 deletion packages/plugin-ext/src/main/browser/documents-main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export class DocumentsMainImpl implements DocumentsMain {
private toDispose = new DisposableCollection();
private modelToDispose = new Map<string, Disposable>();
private modelIsSynced = new Map<string, boolean>();
private modelService: EditorModelService;

protected saveTimeout = 1750;

Expand All @@ -47,6 +48,7 @@ export class DocumentsMainImpl implements DocumentsMain {
private openerService: OpenerService,
) {
this.proxy = rpc.getProxy(MAIN_RPC_CONTEXT.DOCUMENTS_EXT);
this.modelService = modelService;

this.toDispose.push(editorsAndDocuments.onDocumentAdd(documents => documents.forEach(this.onModelAdded, this)));
this.toDispose.push(editorsAndDocuments.onDocumentRemove(documents => documents.forEach(this.onModelRemoved, this)));
Expand Down Expand Up @@ -125,7 +127,7 @@ export class DocumentsMainImpl implements DocumentsMain {
return monaco.Uri.parse(resource.uri.toString());
}

async $tryOpenDocument(uri: UriComponents, options?: TextDocumentShowOptions): Promise<void> {
async $tryShowDocument(uri: UriComponents, options?: TextDocumentShowOptions): Promise<void> {
// Removing try-catch block here makes it not possible to handle errors.
// Following message is appeared in browser console
// - Uncaught (in promise) Error: Cannot read property 'message' of undefined.
Expand Down Expand Up @@ -189,6 +191,15 @@ export class DocumentsMainImpl implements DocumentsMain {
return false;
}

async $tryOpenDocument(uri: UriComponents): Promise<boolean> {
const model = await this.modelService.createModelReference(new URI(CodeURI.revive(uri)));
if (model) {
this.onModelAdded(model);
return true;
}
return false;
}

async $tryCloseDocument(uri: UriComponents): Promise<boolean> {
const widget = await this.editorManger.getByUri(new URI(CodeURI.revive(uri)));
if (widget) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { injectable, inject } from 'inversify';
import { MonacoTextModelService } from '@theia/monaco/lib/browser/monaco-text-model-service';
import { MonacoWorkspace } from '@theia/monaco/lib/browser/monaco-workspace';
import { Schemes } from '../../common/uri-components';
import URI from '@theia/core/lib/common/uri';

export const EditorModelService = Symbol('EditorModelService');
export interface EditorModelService {
Expand All @@ -31,6 +32,7 @@ export interface EditorModelService {
onModelSaved: Event<MonacoEditorModel>;

getModels(): MonacoEditorModel[];
createModelReference(uri: URI): Promise<MonacoEditorModel | undefined>
saveAll(includeUntitled?: boolean): Promise<boolean>;
}

Expand Down Expand Up @@ -108,4 +110,7 @@ export class EditorModelServiceImpl implements EditorModelService {
return results.reduce((a, b) => a && b, true);
}

async createModelReference(uri: URI): Promise<MonacoEditorModel | undefined> {
return this.monacoModelService.createModelReference(uri).then(ref => ref.object);
}
}
27 changes: 12 additions & 15 deletions packages/plugin-ext/src/plugin/documents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,11 @@ export class DocumentsExtImpl implements DocumentsExt {
protected async fireTextDocumentWillSaveEvent({
document, reason, fireEvent, accept
}: {
document: theia.TextDocument,
reason: theia.TextDocumentSaveReason,
fireEvent: (e: theia.TextDocumentWillSaveEvent) => any,
accept: (operation: SingleEditOperation) => void
}): Promise<void> {
document: theia.TextDocument,
reason: theia.TextDocumentSaveReason,
fireEvent: (e: theia.TextDocumentWillSaveEvent) => any,
accept: (operation: SingleEditOperation) => void
}): Promise<void> {

const promises: PromiseLike<TextEdit[] | any>[] = [];
fireEvent(Object.freeze({
Expand Down Expand Up @@ -203,7 +203,7 @@ export class DocumentsExtImpl implements DocumentsExt {
* @param uri path to the resource
* @param options if options exists, resource will be opened in editor, otherwise only document object is returned
*/
async openDocument(uri: URI, options?: theia.TextDocumentShowOptions): Promise<DocumentDataExt | undefined> {
async showDocument(uri: URI, options?: theia.TextDocumentShowOptions): Promise<DocumentDataExt | undefined> {
const cached = this.editorsAndDocuments.getDocument(uri.toString());
if (cached) {
return cached;
Expand Down Expand Up @@ -233,6 +233,11 @@ export class DocumentsExtImpl implements DocumentsExt {
}
}

async openDocument(uri: URI): Promise<DocumentDataExt | undefined> {
await this.proxy.$tryOpenDocument(uri);
return this.editorsAndDocuments.getDocument(uri.toString());
}

private async loadDocument(uri: URI, options?: theia.TextDocumentShowOptions): Promise<DocumentDataExt | undefined> {
let documentOptions: TextDocumentShowOptions | undefined;
if (options) {
Expand All @@ -253,15 +258,7 @@ export class DocumentsExtImpl implements DocumentsExt {
viewColumn: options.viewColumn
};
}
await this.proxy.$tryOpenDocument(uri, documentOptions);

// below block of code needs to be removed after fix https://github.com/theia-ide/theia/issues/5079
if (!options) {
const document = this.editorsAndDocuments.getDocument(uri.toString());
await this.proxy.$tryCloseDocument(uri);
return document;
}

await this.proxy.$tryShowDocument(uri, documentOptions);
return this.editorsAndDocuments.getDocument(uri.toString());
}

Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-ext/src/plugin/plugin-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ export function createAPIFactory(
documentOptions = { preserveFocus };
}
}
await documents.openDocument(uri, documentOptions);
await documents.showDocument(uri, documentOptions);
const textEditor = editors.getVisibleTextEditors().find(editor => editor.document.uri.toString() === uri.toString());
if (textEditor) {
return Promise.resolve(textEditor);
Expand Down

0 comments on commit 7dbd18d

Please sign in to comment.