RootView.open takes an 'allowActiveEditorChange' option

When the 'allowActiveEditorChange' option is true, RootView will try to activate an existing edit session for the given path on *any* editor, and switch focus there. This will be used by the fuzzy-finder for the meta-b option, which should open the chosen buffer on the editor that contains it, even if it isn't currently active.
This commit is contained in:
Nathan Sobo
2012-06-29 09:48:16 -06:00
parent db212fc077
commit 22515e7ba0
4 changed files with 128 additions and 12 deletions

View File

@@ -498,9 +498,101 @@ describe "RootView", ->
rootView.trigger 'decrease-font-size'
expect(rootView.getFontSize()).toBe fontSizeBefore
it "does not allow the font size to be less than 1", ->
rootView.setFontSize(1)
expect(rootView.getFontSize()).toBe 1
it "does not allow the font size to be less than 1", ->
rootView.setFontSize(1)
expect(rootView.getFontSize()).toBe 1
rootView.setFontSize(0)
expect(rootView.getFontSize()).toBe 1
describe ".open(path, options)", ->
describe "when there is no active editor", ->
beforeEach ->
rootView.activeEditor().removeActiveEditSession()
expect(rootView.activeEditor()).toBeUndefined()
describe "when called with no path", ->
it "opens an empty buffer in a new editor", ->
rootView.open()
expect(rootView.activeEditor()).toBeDefined()
expect(rootView.activeEditor().buffer.path).toBeUndefined()
describe "when called with a path", ->
it "opens a buffer with the given path in a new editor", ->
rootView.open('b')
expect(rootView.activeEditor()).toBeDefined()
expect(rootView.activeEditor().buffer.path).toBe require.resolve('fixtures/dir/b')
describe "when there is an active editor", ->
beforeEach ->
expect(rootView.activeEditor()).toBeDefined()
describe "when called with no path", ->
it "opens an empty buffer in the active editor", ->
rootView.open()
expect(rootView.activeEditor().buffer.path).toBeUndefined()
describe "when called with a path", ->
[editor1, editor2] = []
beforeEach ->
rootView.attachToDom()
editor1 = rootView.activeEditor()
editor2 = editor1.splitRight()
rootView.open('b')
editor2.loadPreviousEditSession()
editor1.focus()
describe "when allowActiveEditorChange is false (the default)", ->
activeEditor = null
beforeEach ->
activeEditor = rootView.activeEditor()
describe "when the active editor has an edit session for the given path", ->
it "re-activates the existing edit session", ->
expect(activeEditor.buffer.path).toBe require.resolve('fixtures/dir/a')
previousEditSession = activeEditor.activeEditSession
rootView.open('b')
expect(activeEditor.activeEditSession).not.toBe previousEditSession
rootView.open('a')
expect(activeEditor.activeEditSession).toBe previousEditSession
describe "when the active editor does not have an edit session for the given path", ->
it "creates a new edit session for the given path in the active editor", ->
rootView.open('b')
expect(activeEditor.editSessions.length).toBe 2
describe "when the 'allowActiveEditorChange' option is true", ->
describe "when the active editor has an edit session for the given path", ->
it "re-activates the existing edit session regardless of whether any other editor also has an edit session for the path", ->
activeEditor = rootView.activeEditor()
expect(activeEditor.buffer.path).toBe require.resolve('fixtures/dir/a')
previousEditSession = activeEditor.activeEditSession
rootView.open('b')
expect(activeEditor.activeEditSession).not.toBe previousEditSession
rootView.open('a', allowActiveEditorChange: true)
expect(activeEditor.activeEditSession).toBe previousEditSession
describe "when the active editor does *not* have an edit session for the given path", ->
describe "when another editor has an edit session for the path", ->
it "focuses the other editor and activates its edit session for the path", ->
expect(rootView.activeEditor()).toBe editor1
rootView.open('b', allowActiveEditorChange: true)
expect(rootView.activeEditor()).toBe editor2
expect(editor2.buffer.path).toBe require.resolve('fixtures/dir/b')
describe "when no other editor has an edit session for the path either", ->
it "creates a new edit session for the path on the current active editor", ->
path = require.resolve('fixtures/sample.js')
rootView.open(path, allowActiveEditorChange: true)
expect(rootView.activeEditor()).toBe editor1
expect(editor1.buffer.path).toBe path
rootView.setFontSize(0)
expect(rootView.getFontSize()).toBe 1

View File

@@ -389,6 +389,13 @@ class Editor extends View
@renderWhenAttached()
activateEditSessionForPath: (path) ->
for editSession, index in @editSessions
if editSession.buffer.getPath() == path
@setActiveEditSessionIndex(index)
return true
false
getOpenBufferPaths: ->
editSession.buffer.path for editSession in @editSessions when editSession.buffer.path?

View File

@@ -91,13 +91,12 @@ class RootView extends View
extension.deactivate?() for name, extension of @extensions
@remove()
open: (path, changeFocus=true) ->
editSession = @project.open(path)
open: (path, options = {}) ->
changeFocus = options.changeFocus ? true
allowActiveEditorChange = options.allowActiveEditorChange ? false
if @activeEditor()
@activeEditor().edit(editSession)
else
editor = new Editor(editSession: editSession)
unless @openInExistingEditor(path, allowActiveEditorChange)
editor = new Editor(editSession: @project.open(path))
pane = new Pane(editor)
@panes.append(pane)
if changeFocus
@@ -105,6 +104,24 @@ class RootView extends View
else
@makeEditorActive(editor)
openInExistingEditor: (path, allowActiveEditorChange) ->
if activeEditor = @activeEditor()
path = @project.resolve(path) if path
if activeEditor.activateEditSessionForPath(path)
return true
if allowActiveEditorChange
for editor in @editors()
if editor.activateEditSessionForPath(path)
editor.focus()
return true
activeEditor.edit(@project.open(path))
true
else
false
editorFocused: (editor) ->
@makeEditorActive(editor) if @panes.containsElement(editor)

View File

@@ -156,7 +156,7 @@ class TreeView extends View
if (selectedEntry instanceof DirectoryView)
selectedEntry.view().toggleExpansion()
else if (selectedEntry instanceof FileView)
@rootView.open(selectedEntry.getPath(), false)
@rootView.open(selectedEntry.getPath(), changeFocus: false)
moveSelectedEntry: ->
entry = @selectedEntry()