Refactor open; separate creation and placement of items

This is a refactoring that sets up the work for #13878 (docks).

Because items will be able to dictate their preferred locations, we
need to detangle the item creation from their placement.
This commit is contained in:
Matthew Dapena-Tretter
2017-03-09 14:25:16 -08:00
parent 4b3e2ccca4
commit e85ed1dc7f

View File

@@ -511,9 +511,8 @@ module.exports = class Workspace extends Model {
//
// Returns a {Promise} that resolves to the {TextEditor} for the file URI.
open (uri_, options = {}) {
const { searchAllPanes } = options
const { split } = options
const uri = this.project.resolvePath(uri_)
const {searchAllPanes, split} = options
if (!atom.config.get('core.allowPendingPaneItems')) {
options.pending = false
@@ -526,7 +525,7 @@ module.exports = class Workspace extends Model {
}
let pane
if (searchAllPanes) { pane = this.paneContainer.paneForURI(uri) }
if (searchAllPanes) { pane = this.paneForURI(uri) }
if (pane == null) {
switch (split) {
case 'left':
@@ -547,7 +546,16 @@ module.exports = class Workspace extends Model {
}
}
return this.openURIInPane(uri, pane, options)
let item
if (uri != null) {
item = pane.itemForURI(uri)
}
if (item == null) {
item = this.createItemForURI(uri, options)
}
return Promise.resolve(item)
.then(item => this.openItem(item, Object.assign({pane, uri}, options)))
}
// Open Atom's license in the active pane.
@@ -597,26 +605,28 @@ module.exports = class Workspace extends Model {
}
openURIInPane (uri, pane, options = {}) {
const activatePane = options.activatePane != null ? options.activatePane : true
const activateItem = options.activateItem != null ? options.activateItem : true
let item
if (uri != null) {
item = pane.itemForURI(uri)
if (item == null) {
for (let opener of this.getOpeners()) {
item = opener(uri, options)
if (item != null) break
}
} else if (!options.pending && (pane.getPendingItem() === item)) {
pane.clearPendingItem()
}
if (item == null) {
item = this.createItemForURI(uri, options)
}
return Promise.resolve(item)
.then(item => this.openItem(item, Object.assign({pane, uri}, options)))
}
// Returns a {Promise} that resolves to the {TextEditor} (or other item) for the given URI.
createItemForURI (uri, options) {
if (uri != null) {
for (let opener of this.getOpeners()) {
const item = opener(uri, options)
if (item != null) return Promise.resolve(item)
}
}
try {
if (item == null) {
item = this.openTextFile(uri, options)
}
return this.openTextFile(uri, options)
} catch (error) {
switch (error.code) {
case 'CANCELLED':
@@ -644,40 +654,46 @@ module.exports = class Workspace extends Model {
throw error
}
}
}
return Promise.resolve(item)
.then(item => {
let initialColumn
if (pane.isDestroyed()) {
return item
}
openItem (item, options = {}) {
const {pane} = options
this.itemOpened(item)
if (activateItem) {
pane.activateItem(item, {pending: options.pending})
}
if (activatePane) {
pane.activate()
}
if (item == null) return undefined
if (pane.isDestroyed()) return item
let initialLine = initialColumn = 0
if (!Number.isNaN(options.initialLine)) {
initialLine = options.initialLine
}
if (!Number.isNaN(options.initialColumn)) {
initialColumn = options.initialColumn
}
if ((initialLine >= 0) || (initialColumn >= 0)) {
if (typeof item.setCursorBufferPosition === 'function') {
item.setCursorBufferPosition([initialLine, initialColumn])
}
}
if (!options.pending && (pane.getPendingItem() === item)) {
pane.clearPendingItem()
}
const index = pane.getActiveItemIndex()
this.emitter.emit('did-open', {uri, pane, item, index})
return item
const activatePane = options.activatePane != null ? options.activatePane : true
const activateItem = options.activateItem != null ? options.activateItem : true
this.itemOpened(item)
if (activateItem) {
pane.activateItem(item, {pending: options.pending})
}
if (activatePane) {
pane.activate()
}
let initialColumn = 0
let initialLine = 0
if (!Number.isNaN(options.initialLine)) {
initialLine = options.initialLine
}
if (!Number.isNaN(options.initialColumn)) {
initialColumn = options.initialColumn
}
if ((initialLine >= 0) || (initialColumn >= 0)) {
if (typeof item.setCursorBufferPosition === 'function') {
item.setCursorBufferPosition([initialLine, initialColumn])
}
)
}
const index = pane.getActiveItemIndex()
const uri = options.uri == null && typeof item.getURI === 'function' ? item.getURI() : options.uri
this.emitter.emit('did-open', {uri, pane, item, index})
return item
}
openTextFile (uri, options) {