diff --git a/spec/workspace-spec.coffee b/spec/workspace-spec.coffee index 937069482..4ca654d18 100644 --- a/spec/workspace-spec.coffee +++ b/spec/workspace-spec.coffee @@ -7,49 +7,119 @@ describe "Workspace", -> atom.project.setPath(atom.project.resolve('dir')) workspace = new Workspace - describe "::open(uri)", -> + describe "::open(uri, options)", -> beforeEach -> - spyOn(workspace.activePane, 'activate') - - describe "when called without a uri", -> - it "adds and activates an empty editor on the active pane", -> - editor = null - waitsForPromise -> - workspace.open().then (o) -> editor = o - - runs -> - expect(editor.getPath()).toBeUndefined() - expect(workspace.activePane.items).toEqual [editor] - expect(workspace.activePaneItem).toBe editor - expect(workspace.activePane.activate).toHaveBeenCalled() - - describe "when called with a uri", -> - describe "when the active pane already has an editor for the given uri", -> - it "activates the existing editor on the active pane", -> - editor1 = workspace.openSync('a') - editor2 = workspace.openSync('b') + spyOn(workspace.activePane, 'activate').andCallThrough() + describe "when the 'searchAllPanes' option is false (default)", -> + describe "when called without a uri", -> + it "adds and activates an empty editor on the active pane", -> editor = null waitsForPromise -> - workspace.open('a').then (o) -> editor = o + workspace.open().then (o) -> editor = o runs -> - expect(editor).toBe editor1 - expect(workspace.activePaneItem).toBe editor - expect(workspace.activePane.activate).toHaveBeenCalled() - - describe "when the active pane does not have an editor for the given uri", -> - it "adds and activates a new editor for the given path on the active pane", -> - editor = null - waitsForPromise -> - workspace.open('a').then (o) -> editor = o - - runs -> - expect(editor.getUri()).toBe 'a' - expect(workspace.activePaneItem).toBe editor + expect(editor.getPath()).toBeUndefined() expect(workspace.activePane.items).toEqual [editor] + expect(workspace.activePaneItem).toBe editor expect(workspace.activePane.activate).toHaveBeenCalled() + describe "when called with a uri", -> + describe "when the active pane already has an editor for the given uri", -> + it "activates the existing editor on the active pane", -> + editor1 = workspace.openSync('a') + editor2 = workspace.openSync('b') + + editor = null + waitsForPromise -> + workspace.open('a').then (o) -> editor = o + + runs -> + expect(editor).toBe editor1 + expect(workspace.activePaneItem).toBe editor + expect(workspace.activePane.activate).toHaveBeenCalled() + + describe "when the active pane does not have an editor for the given uri", -> + it "adds and activates a new editor for the given path on the active pane", -> + editor = null + waitsForPromise -> + workspace.open('a').then (o) -> editor = o + + runs -> + expect(editor.getUri()).toBe 'a' + expect(workspace.activePaneItem).toBe editor + expect(workspace.activePane.items).toEqual [editor] + expect(workspace.activePane.activate).toHaveBeenCalled() + + describe "when the 'searchAllPanes' option is true", -> + describe "when an editor for the given uri is already open on an inactive pane", -> + it "activates the existing editor on the inactive pane, then activates that pane", -> + editor1 = workspace.openSync('a') + pane1 = workspace.activePane + pane2 = workspace.activePane.splitRight() + editor2 = workspace.openSync('b') + expect(workspace.activePaneItem).toBe editor2 + + waitsForPromise -> + workspace.open('a', searchAllPanes: true) + + runs -> + expect(workspace.activePane).toBe pane1 + expect(workspace.activePaneItem).toBe editor1 + + describe "when no editor for the given uri is open in any pane", -> + it "opens an editor for the given uri in the active pane", -> + editor = null + waitsForPromise -> + workspace.open('a', searchAllPanes: true).then (o) -> editor = o + + runs -> + expect(workspace.activePaneItem).toBe editor + + describe "when the 'split' option is set", -> + describe "when the 'split' option is 'left'", -> + it "opens the editor in the leftmost pane of the current pane axis", -> + pane1 = workspace.activePane + pane2 = pane1.splitRight() + expect(workspace.activePane).toBe pane2 + + editor = null + waitsForPromise -> + workspace.open('a', split: 'left').then (o) -> editor = o + + runs -> + expect(workspace.activePane).toBe pane1 + expect(pane1.items).toEqual [editor] + expect(pane2.items).toEqual [] + + describe "when the 'split' option is 'right'", -> + it "activates the editor on the existing right pane", -> + pane1 = workspace.activePane + pane2 = pane1.splitRight() + pane1.activate() + + editor = null + waitsForPromise -> + workspace.open('a', split: 'right').then (o) -> editor = o + + runs -> + expect(workspace.activePane).toBe pane2 + expect(pane2.items).toEqual [editor] + expect(pane1.items).toEqual [] + + it "splits the current pane if the right pane doesn't exist", -> + pane1 = workspace.activePane + + editor = null + waitsForPromise -> + workspace.open('a', split: 'right').then (o) -> editor = o + + runs -> + pane2 = workspace.activePane + expect(workspace.paneContainer.root.children).toEqual [pane1, pane2] + expect(pane2.items).toEqual [editor] + expect(pane1.items).toEqual [] + describe "::openSync(uri, options)", -> [activePane, initialItemCount] = [] @@ -92,61 +162,6 @@ describe "Workspace", -> workspace.openSync('b', activatePane: false) expect(activePane.activate).not.toHaveBeenCalled() - describe "::openSingletonSync(uri, options)", -> - describe "when an editor for the given uri is already open on the active pane", -> - it "activates the existing editor", -> - editor1 = workspace.openSync('a') - editor2 = workspace.openSync('b') - expect(workspace.activePaneItem).toBe editor2 - workspace.openSingletonSync('a') - expect(workspace.activePaneItem).toBe editor1 - - describe "when an editor for the given uri is already open on an inactive pane", -> - it "activates the existing editor on the inactive pane, then activates that pane", -> - editor1 = workspace.openSync('a') - pane1 = workspace.activePane - pane2 = workspace.activePane.splitRight() - editor2 = workspace.openSync('b') - expect(workspace.activePaneItem).toBe editor2 - workspace.openSingletonSync('a') - expect(workspace.activePane).toBe pane1 - expect(workspace.activePaneItem).toBe editor1 - - describe "when no editor for the given uri is open in any pane", -> - it "opens an editor for the given uri in the active pane", -> - editor1 = workspace.openSingletonSync('a') - expect(workspace.activePaneItem).toBe editor1 - - describe "when the 'split' option is 'left'", -> - it "opens the editor in the leftmost pane of the current pane axis", -> - pane1 = workspace.activePane - pane2 = pane1.splitRight() - expect(workspace.activePane).toBe pane2 - editor1 = workspace.openSingletonSync('a', split: 'left') - expect(workspace.activePane).toBe pane1 - expect(pane1.items).toEqual [editor1] - expect(pane2.items).toEqual [] - - describe "when the 'split' option is 'right'", -> - describe "when the active pane is in a horizontal pane axis", -> - it "activates the editor on the rightmost pane of the current pane axis", -> - pane1 = workspace.activePane - pane2 = pane1.splitRight() - pane1.activate() - editor1 = workspace.openSingletonSync('a', split: 'right') - expect(workspace.activePane).toBe pane2 - expect(pane2.items).toEqual [editor1] - expect(pane1.items).toEqual [] - - describe "when the active pane is not in a horizontal pane axis", -> - it "splits the current pane to the right, then activates the editor on the right pane", -> - pane1 = workspace.activePane - editor1 = workspace.openSingletonSync('a', split: 'right') - pane2 = workspace.activePane - expect(workspace.paneContainer.root.children).toEqual [pane1, pane2] - expect(pane2.items).toEqual [editor1] - expect(pane1.items).toEqual [] - describe "::reopenItemSync()", -> it "opens the uri associated with the last closed pane that isn't currently open", -> pane = workspace.activePane diff --git a/src/workspace.coffee b/src/workspace.coffee index 21663bb59..c3d9cb48d 100644 --- a/src/workspace.coffee +++ b/src/workspace.coffee @@ -57,20 +57,30 @@ class Workspace extends Model changeFocus = options.changeFocus ? true filePath = atom.project.resolve(filePath) initialLine = options.initialLine - activePane = @activePane + searchAllPanes = options.searchAllPanes + split = options.split + uri = atom.project.relativize(filePath) - editor = activePane.itemForUri(atom.project.relativize(filePath)) if activePane and filePath - promise = atom.project.open(filePath, {initialLine}) if not editor + pane = switch split + when 'left' + @activePane.findLeftmostSibling() + when 'right' + @activePane.findOrCreateRightmostSibling() + else + if searchAllPanes + @paneContainer.paneForUri(uri) ? @activePane + else + @activePane - Q(editor ? promise) + Q(pane.itemForUri(uri) ? atom.project.open(filePath, options)) .then (editor) => - if not activePane - activePane = new Pane(items: [editor]) - @paneContainer.root = activePane + if not pane + pane = new Pane(items: [editor]) + @paneContainer.root = pane @itemOpened(editor) - activePane.activateItem(editor) - activePane.activate() if changeFocus + pane.activateItem(editor) + pane.activate() if changeFocus @emit "uri-opened" editor .catch (error) ->