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

Add ability to close editor and retrieve document without opening the last one #5080

Merged
merged 1 commit into from
May 7, 2019
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
1 change: 1 addition & 0 deletions packages/plugin-ext/src/api/plugin-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -732,6 +732,7 @@ export interface DocumentsMain {
$tryCreateDocument(options?: { language?: string; content?: string; }): Promise<UriComponents>;
$tryOpenDocument(uri: UriComponents, options?: TextDocumentShowOptions): Promise<void>;
$trySaveDocument(uri: UriComponents): Promise<boolean>;
$tryCloseDocument(uri: UriComponents): Promise<boolean>;
}

export interface EnvMain {
Expand Down
11 changes: 11 additions & 0 deletions packages/plugin-ext/src/main/browser/documents-main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,4 +188,15 @@ export class DocumentsMainImpl implements DocumentsMain {
return false;
}

async $tryCloseDocument(uri: UriComponents): Promise<boolean> {
const widget = await this.editorManger.getByUri(new URI(CodeURI.revive(uri)));
if (widget) {
await Saveable.save(widget);
widget.close();
return true;
}

return false;
}

}
14 changes: 14 additions & 0 deletions packages/plugin-ext/src/plugin/documents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,12 @@ export class DocumentsExtImpl implements DocumentsExt {
return undefined;
}

/**
* Retrieve document and open it in the editor if need.
*
* @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> {
const cached = this.editorsAndDocuments.getDocument(uri.toString());
if (cached) {
Expand Down Expand Up @@ -248,6 +254,14 @@ export class DocumentsExtImpl implements DocumentsExt {
};
}
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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it worth mentioning in the method's doc that omitting the options parameter actually means opening a document without showing it on UI? To make it a bit clearer...
And adding the link here to the related issue might help someone quicker understand why it has been implemented in such way and this is actually a temporary fix.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added comment with dependent issue and jsdoc

const document = this.editorsAndDocuments.getDocument(uri.toString());
await this.proxy.$tryCloseDocument(uri);
return document;
}

return this.editorsAndDocuments.getDocument(uri.toString());
}

Expand Down