From 8f81831ad4f8f5ef4aa5e8f79aa51eb67d41f56d Mon Sep 17 00:00:00 2001 From: Wliu <50Wliu@users.noreply.github.com> Date: Sat, 18 Nov 2017 00:56:22 +0100 Subject: [PATCH] Convert large file warning --- src/workspace.js | 47 +++++++++++++++++++++++++++++++---------------- 1 file changed, 31 insertions(+), 16 deletions(-) diff --git a/src/workspace.js b/src/workspace.js index 8de7adc65..8e9d3b2df 100644 --- a/src/workspace.js +++ b/src/workspace.js @@ -1158,16 +1158,17 @@ module.exports = class Workspace extends Model { // * `uri` A {String} containing a URI. // // Returns a {Promise} that resolves to the {TextEditor} (or other item) for the given URI. - createItemForURI (uri, options) { + async createItemForURI (uri, options) { if (uri != null) { - for (let opener of this.getOpeners()) { + for (const opener of this.getOpeners()) { const item = opener(uri, options) - if (item != null) return Promise.resolve(item) + if (item != null) return item } } try { - return this.openTextFile(uri, options) + const item = await this.openTextFile(uri, options) + return item } catch (error) { switch (error.code) { case 'CANCELLED': @@ -1197,7 +1198,7 @@ module.exports = class Workspace extends Model { } } - openTextFile (uri, options) { + async openTextFile (uri, options) { const filePath = this.project.resolvePath(uri) if (filePath != null) { @@ -1214,23 +1215,37 @@ module.exports = class Workspace extends Model { const fileSize = fs.getSizeSync(filePath) const largeFileMode = fileSize >= (2 * 1048576) // 2MB - if (fileSize >= (this.config.get('core.warnOnLargeFileLimit') * 1048576)) { // 20MB by default - const choice = this.applicationDelegate.confirm({ + + let resolveConfirmFileOpenPromise, rejectConfirmFileOpenPromise = [] + const confirmFileOpenPromise = new Promise((resolve, reject) => { + resolveConfirmFileOpenPromise = resolve + rejectConfirmFileOpenPromise = reject + }) + if (fileSize >= (this.config.get('core.warnOnLargeFileLimit') * 1048576)) { // 40MB by default + this.applicationDelegate.confirm({ message: 'Atom will be unresponsive during the loading of very large files.', detailedMessage: 'Do you still want to load this file?', buttons: ['Proceed', 'Cancel'] + }, response => { + if (response === 1) { + rejectConfirmFileOpenPromise() + } else { + resolveConfirmFileOpenPromise() + } }) - if (choice === 1) { - const error = new Error() - error.code = 'CANCELLED' - throw error - } + } else { + resolveConfirmFileOpenPromise() } - return this.project.bufferForPath(filePath, options) - .then(buffer => { - return this.textEditorRegistry.build(Object.assign({buffer, largeFileMode, autoHeight: false}, options)) - }) + try { + await confirmFileOpenPromise + const buffer = await this.project.bufferForPath(filePath, options) + return this.textEditorRegistry.build(Object.assign({buffer, largeFileMode, autoHeight: false}, options)) + } catch (e) { + const error = new Error() + error.code = 'CANCELLED' + throw error + } } handleGrammarUsed (grammar) {