Skip to content

Commit

Permalink
Merge pull request atom#10178 from atom/ku-pending-editor
Browse files Browse the repository at this point in the history
Open file in pending state on single click
  • Loading branch information
Nathan Sobo committed Jan 8, 2016
2 parents dd1eb75 + 5738b14 commit db74f4a
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 4 deletions.
22 changes: 22 additions & 0 deletions spec/pane-spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ describe "Pane", ->
onDidDestroy: (fn) -> @emitter.on('did-destroy', fn)
destroy: -> @destroyed = true; @emitter.emit('did-destroy')
isDestroyed: -> @destroyed
isPending: -> @pending
pending: false

beforeEach ->
confirm = spyOn(atom.applicationDelegate, 'confirm')
Expand Down Expand Up @@ -153,6 +155,26 @@ describe "Pane", ->
pane.activateItem(pane.itemAtIndex(1))
expect(observed).toEqual [pane.itemAtIndex(1)]

it "replaces pending items", ->
itemC = new Item("C")
itemD = new Item("D")
itemC.pending = true
itemD.pending = true

expect(itemC.isPending()).toBe true
pane.activateItem(itemC)
expect(pane.getItems().length).toBe 3
expect(pane.getActiveItem()).toBe pane.itemAtIndex(1)

expect(itemD.isPending()).toBe true
pane.activateItem(itemD)
expect(pane.getItems().length).toBe 3
expect(pane.getActiveItem()).toBe pane.itemAtIndex(1)

pane.activateItem(pane.itemAtIndex(2))
expect(pane.getItems().length).toBe 2
expect(pane.getActiveItem()).toBe pane.itemAtIndex(1)

describe "::activateNextItem() and ::activatePreviousItem()", ->
it "sets the active item to the next/previous item, looping around at either end", ->
pane = new Pane(paneParams(items: [new Item("A"), new Item("B"), new Item("C")]))
Expand Down
26 changes: 26 additions & 0 deletions spec/text-editor-spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -5804,3 +5804,29 @@ describe "TextEditor", ->
screenRange: marker1.getRange(),
rangeIsReversed: false
}

describe "pending state", ->
editor1 = null
beforeEach ->
waitsForPromise ->
atom.workspace.open('sample.txt', pending: true).then (o) -> editor1 = o

it "should open file in pending state if 'pending' option is true", ->
expect(editor1.isPending()).toBe true
expect(editor.isPending()).toBe false # By default pending status is false

it "invokes ::onDidTerminatePendingState observers if pending status is terminated", ->
events = []
editor1.onDidTerminatePendingState (event) -> events.push(event)
editor1.terminatePendingState()
expect(editor1.isPending()).toBe false
expect(events).toEqual [editor1]

it "should terminate pending state when buffer is changed", ->
events = []
editor1.onDidTerminatePendingState (event) -> events.push(event)
expect(editor1.isPending()).toBe true
editor1.insertText('I\'ll be back!')
advanceClock(500)
expect(editor1.isPending()).toBe false
expect(events).toEqual [editor1]
11 changes: 8 additions & 3 deletions src/pane.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -337,13 +337,19 @@ class Pane extends Model
#
# * `index` {Number}
activateItemAtIndex: (index) ->
@activateItem(@itemAtIndex(index))
item = @itemAtIndex(index) or @getActiveItem()
@setActiveItem(item)

# Public: Make the given item *active*, causing it to be displayed by
# the pane's view.
activateItem: (item) ->
if item?
@addItem(item, @getActiveItemIndex() + 1, false)
if @activeItem?.isPending?()
index = @getActiveItemIndex()
@destroyActiveItem() unless item is @activeItem
else
index = @getActiveItemIndex() + 1
@addItem(item, index, false)
@setActiveItem(item)

# Public: Add the given item to the pane.
Expand Down Expand Up @@ -574,7 +580,6 @@ class Pane extends Model
# Public: Makes this pane the *active* pane, causing it to gain focus.
activate: ->
throw new Error("Pane has been destroyed") if @isDestroyed()

@container?.setActivePane(this)
@emitter.emit 'did-activate'

Expand Down
15 changes: 14 additions & 1 deletion src/text-editor.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class TextEditor extends Model
softWrapped, @displayBuffer, @selectionsMarkerLayer, buffer, suppressCursorCreation,
@mini, @placeholderText, lineNumberGutterVisible, largeFileMode, @config,
@notificationManager, @packageManager, @clipboard, @viewRegistry, @grammarRegistry,
@project, @assert, @applicationDelegate
@project, @assert, @applicationDelegate, @pending
} = params

throw new Error("Must pass a config parameter when constructing TextEditors") unless @config?
Expand Down Expand Up @@ -161,6 +161,9 @@ class TextEditor extends Model
@disposables.add @buffer.onDidChangeEncoding =>
@emitter.emit 'did-change-encoding', @getEncoding()
@disposables.add @buffer.onDidDestroy => @destroy()
if @pending
@disposables.add @buffer.onDidChangeModified =>
@terminatePendingState() if @buffer.isModified()

@preserveCursorPositionOnBufferReload()

Expand Down Expand Up @@ -569,6 +572,13 @@ class TextEditor extends Model
getEditorWidthInChars: ->
@displayBuffer.getEditorWidthInChars()

onDidTerminatePendingState: (callback) ->
@emitter.on 'did-terminate-pending-state', callback

terminatePendingState: ->
@pending = false
@emitter.emit 'did-terminate-pending-state', this

###
Section: File Details
###
Expand Down Expand Up @@ -652,6 +662,9 @@ class TextEditor extends Model
# Essential: Returns {Boolean} `true` if this editor has no content.
isEmpty: -> @buffer.isEmpty()

# Returns {Boolean} `true` if this editor is pending and `false` if it is permanent.
isPending: -> Boolean(@pending)

# Copies the current file path to the native clipboard.
copyPathToClipboard: ->
if filePath = @getPath()
Expand Down

0 comments on commit db74f4a

Please sign in to comment.