Set modified flag to true when there is a buffer change.

This commit is contained in:
Corey Johnson
2012-06-11 17:34:53 -07:00
parent 869df0bbc6
commit 9339791260
2 changed files with 39 additions and 12 deletions

View File

@@ -31,6 +31,32 @@ describe 'Buffer', ->
buffer = new Buffer
expect(buffer.getText()).toBe ""
describe "path-change event", ->
it "emits path-change event when path is changed", ->
eventHandler = jasmine.createSpy('eventHandler')
buffer.on 'path-change', eventHandler
buffer.setPath("moo.text")
expect(eventHandler).toHaveBeenCalledWith(buffer)
describe ".isModified()", ->
describe "when deserialized", ->
it "returns false", ->
buffer = Buffer.deserialize(buffer.serialize(), new Project)
expect(buffer.isModified()).toBe false
buffer = Buffer.deserialize((new Buffer).serialize(), new Project)
expect(buffer.isModified()).toBe false
it "returns is true if buffer no path and had changes", ->
buffer = new Buffer
buffer.insert([0,0], "oh hi")
expect(buffer.isModified()).toBe true
it "returns true when user changes buffer", ->
expect(buffer.isModified()).toBeFalsy()
buffer.insert([0,0], "hi")
expect(buffer.isModified()).toBe true
describe '.deserialize(state, project)', ->
project = null
@@ -365,7 +391,7 @@ describe 'Buffer', ->
expect(ranges.length).toBe 2
describe "backwardsScanInRange(range, regex, fn)", ->
describe ".backwardsScanInRange(range, regex, fn)", ->
describe "when given a regex with no global flag", ->
it "calls the iterator with the last match for the given regex in the given range", ->
matches = []
@@ -446,11 +472,3 @@ 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-change event", ->
it "emits path-change event when path is changed", ->
eventHandler = jasmine.createSpy('eventHandler')
buffer.on 'path-change', eventHandler
buffer.setPath("moo.text")
expect(eventHandler).toHaveBeenCalledWith(buffer)

View File

@@ -8,16 +8,19 @@ UndoManager = require 'undo-manager'
module.exports =
class Buffer
@idCounter = 1
modified: null
lines: null
path: null
@deserialize: (state, project) ->
if state.path
project.open(state.path)
buffer = project.open(state.path)
else
buffer = project.bufferWithId(state.id) ? project.open()
buffer.setText(state.text)
buffer
buffer.modified = state.modified
buffer
constructor: (path) ->
@id = @constructor.idCounter++
@@ -28,12 +31,13 @@ class Buffer
else
@setText('')
@undoManager = new UndoManager(this)
@modified = false
serialize: ->
if @getPath()
{ path: @path }
else
{ text: @getText(), id: @id }
{ text: @getText(), id: @id , modified: @modified}
getPath: ->
@path
@@ -139,6 +143,8 @@ class Buffer
newTextLines[lastLineIndex] += suffix
@lines[oldRange.start.row..oldRange.end.row] = newTextLines
@modified = true
@trigger 'change', { oldRange, newRange, oldText, newText }
newRange
@@ -164,6 +170,9 @@ class Buffer
@setPath(path)
@save()
isModified: ->
@modified
getMode: ->
return @mode if @mode
extension = if @getPath() then @getPath().split('/').pop().split('.').pop() else null