diff --git a/spec/workspace-spec.coffee b/spec/workspace-spec.coffee index 636755346..9a99d3ed2 100644 --- a/spec/workspace-spec.coffee +++ b/spec/workspace-spec.coffee @@ -224,7 +224,7 @@ describe "Workspace", -> expect(workspace.paneContainer.root.children[0]).toBe pane1 expect(workspace.paneContainer.root.children[1]).toBe pane4 - describe "when the file is large (over 2mb)", -> + describe "when the file is over 2MB", -> it "opens the editor with largeFileMode: true", -> spyOn(fs, 'getSizeSync').andReturn 2 * 1048577 # 2MB @@ -235,6 +235,30 @@ describe "Workspace", -> runs -> expect(editor.displayBuffer.largeFileMode).toBe true + describe "when the file is over 20MB", -> + it "prompts the user to make sure they want to open a file this big", -> + spyOn(fs, 'getSizeSync').andReturn 20 * 1048577 # 20MB + spyOn(atom, 'confirm').andCallFake -> selectedButtonIndex + selectedButtonIndex = 1 # cancel + + editor = null + waitsForPromise -> + workspace.open('sample.js').then (e) -> editor = e + + runs -> + expect(editor).toBeUndefined() + expect(atom.confirm).toHaveBeenCalled() + + atom.confirm.reset() + selectedButtonIndex = 0 # open the file + + waitsForPromise -> + workspace.open('sample.js').then (e) -> editor = e + + runs -> + expect(atom.confirm).toHaveBeenCalled() + expect(editor.displayBuffer.largeFileMode).toBe true + describe "when passed a path that matches a custom opener", -> it "returns the resource returned by the custom opener", -> fooOpener = (pathToOpen, options) -> {foo: pathToOpen, options} if pathToOpen?.match(/\.foo/) diff --git a/src/project.coffee b/src/project.coffee index c10ce4a3b..b21e74076 100644 --- a/src/project.coffee +++ b/src/project.coffee @@ -323,8 +323,22 @@ class Project extends Model # allow ENOENT errors to create an editor for paths that dont exist throw error unless error.code is 'ENOENT' - @bufferForPath(filePath).then (buffer) => - @buildEditorForBuffer(buffer, options) + absoluteFilePath = @resolvePath(filePath) + + fileSize = fs.getSizeSync(absoluteFilePath) + + if fileSize >= 20 * 1048576 # 20MB + choice = atom.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"] + if choice is 1 + error = new Error + error.code = 'CANCELLED' + throw error + + @bufferForPath(absoluteFilePath).then (buffer) => + @buildEditorForBuffer(buffer, _.extend({fileSize}, options)) # Retrieves all the {TextBuffer}s in the project; that is, the # buffers for all open files. @@ -354,8 +368,7 @@ class Project extends Model # * `filePath` A {String} representing a path. If `null`, an "Untitled" buffer is created. # # Returns a promise that resolves to the {TextBuffer}. - bufferForPath: (filePath) -> - absoluteFilePath = @resolvePath(filePath) + bufferForPath: (absoluteFilePath) -> existingBuffer = @findBufferForPath(absoluteFilePath) if absoluteFilePath Q(existingBuffer ? @buildBuffer(absoluteFilePath)) @@ -405,7 +418,7 @@ class Project extends Model buffer?.destroy() buildEditorForBuffer: (buffer, editorOptions) -> - largeFileMode = fs.getSizeSync(buffer.getPath()) >= 2 * 1048576 # 2MB + largeFileMode = editorOptions.fileSize >= 2 * 1048576 # 2MB editor = new TextEditor(_.extend({buffer, largeFileMode, registerEditor: true}, editorOptions)) editor diff --git a/src/workspace.coffee b/src/workspace.coffee index afc197792..564152df6 100644 --- a/src/workspace.coffee +++ b/src/workspace.coffee @@ -437,15 +437,16 @@ class Workspace extends Model item ?= atom.project.open(uri, options) catch error switch error.code - when 'EFILETOOLARGE' - atom.notifications.addWarning("#{error.message} Large file support is being tracked at [atom/atom#307](https://github.com/atom/atom/issues/307).") + when 'CANCELLED' + return Q() when 'EACCES' atom.notifications.addWarning("Permission denied '#{error.path}'") + return Q() when 'EPERM', 'EBUSY' atom.notifications.addWarning("Unable to open '#{error.path}'", detail: error.message) + return Q() else throw error - return Q() Q(item) .then (item) =>