mirror of
https://github.com/atom/atom.git
synced 2026-01-24 06:18:03 -05:00
Buffer emits a 'path-changed' event when its path changes
This commit is contained in:
@@ -179,6 +179,27 @@ describe 'Buffer', ->
|
||||
buffer = new Buffer
|
||||
expect(-> buffer.save()).toThrow()
|
||||
|
||||
describe ".saveAs(path)", ->
|
||||
filePath = null
|
||||
|
||||
beforeEach ->
|
||||
filePath = require.resolve('fixtures') + '/temp.txt'
|
||||
expect(fs.exists(filePath)).toBeFalsy()
|
||||
|
||||
afterEach ->
|
||||
fs.remove filePath
|
||||
|
||||
it "saves the contents of the buffer to the path", ->
|
||||
buffer = new Buffer()
|
||||
eventHandler = jasmine.createSpy('eventHandler')
|
||||
buffer.on 'path-changed', eventHandler
|
||||
|
||||
buffer.setText 'Buffer contents!'
|
||||
buffer.saveAs(filePath)
|
||||
expect(fs.read(filePath)).toEqual 'Buffer contents!'
|
||||
|
||||
expect(eventHandler).toHaveBeenCalledWith(buffer)
|
||||
|
||||
describe ".getTextInRange(range)", ->
|
||||
describe "when range is empty", ->
|
||||
it "returns an empty string", ->
|
||||
@@ -382,3 +403,11 @@ describe 'Buffer', ->
|
||||
expect(buffer.positionForCharacterIndex(30)).toEqual [1, 0]
|
||||
expect(buffer.positionForCharacterIndex(61)).toEqual [2, 0]
|
||||
expect(buffer.positionForCharacterIndex(408)).toEqual [12, 2]
|
||||
|
||||
|
||||
describe "path-changed event", ->
|
||||
it "emits path-changed event when path is changed", ->
|
||||
eventHandler = jasmine.createSpy('eventHandler')
|
||||
buffer.on 'path-changed', eventHandler
|
||||
buffer.setPath("moo.text")
|
||||
expect(eventHandler).toHaveBeenCalledWith(buffer)
|
||||
|
||||
@@ -9,17 +9,26 @@ module.exports =
|
||||
class Buffer
|
||||
@idCounter = 1
|
||||
lines: null
|
||||
path: null
|
||||
|
||||
constructor: (@path) ->
|
||||
constructor: (path) ->
|
||||
@id = @constructor.idCounter++
|
||||
@url = @path # we want this to be path on master, but let's not break it on a branch
|
||||
@setPath(path)
|
||||
@lines = ['']
|
||||
if @path and fs.exists(@path)
|
||||
@setText(fs.read(@path))
|
||||
if @getPath() and fs.exists(@getPath())
|
||||
@setText(fs.read(@getPath()))
|
||||
else
|
||||
@setText('')
|
||||
@undoManager = new UndoManager(this)
|
||||
|
||||
getPath: ->
|
||||
@path
|
||||
|
||||
setPath: (path) ->
|
||||
@url = path # we want this to be path on master, but let's not break it on a branch
|
||||
@path = path
|
||||
@trigger "path-changed", this
|
||||
|
||||
getText: ->
|
||||
@lines.join('\n')
|
||||
|
||||
@@ -126,12 +135,16 @@ class Buffer
|
||||
@undoManager.redo()
|
||||
|
||||
save: ->
|
||||
if not @path then throw new Error("Tried to save buffer with no url")
|
||||
fs.write @path, @getText()
|
||||
if not @getPath() then throw new Error("Tried to save buffer with no url")
|
||||
fs.write @getPath(), @getText()
|
||||
|
||||
saveAs: (path) ->
|
||||
@setPath(path)
|
||||
@save()
|
||||
|
||||
getMode: ->
|
||||
return @mode if @mode
|
||||
extension = if @path then @path.split('/').pop().split('.').pop() else null
|
||||
extension = if @getPath() then @getPath().split('/').pop().split('.').pop() else null
|
||||
modeName = switch extension
|
||||
when 'js' then 'javascript'
|
||||
when 'coffee' then 'coffee'
|
||||
|
||||
Reference in New Issue
Block a user