Merge pull request #51 from github/status-bar-buffer-modified-indicator

Add buffer modified indicator
This commit is contained in:
Will Farrington 2012-10-09 09:36:46 -07:00
commit ec937d7ea8
4 changed files with 66 additions and 7 deletions

View File

@ -3,7 +3,7 @@ RootView = require 'root-view'
StatusBar = require 'status-bar'
describe "StatusBar", ->
[rootView, editor, statusBar] = []
[rootView, editor, statusBar, buffer] = []
beforeEach ->
rootView = new RootView(require.resolve('fixtures/sample.js'))
@ -11,6 +11,7 @@ describe "StatusBar", ->
StatusBar.activate(rootView)
editor = rootView.getActiveEditor()
statusBar = rootView.find('.status-bar').view()
buffer = editor.getBuffer()
afterEach ->
rootView.remove()
@ -24,8 +25,9 @@ describe "StatusBar", ->
expect(rootView.panes.find('.pane > .status-bar').length).toBe 2
describe ".initialize(editor)", ->
it "displays the editor's buffer path and cursor buffer position", ->
it "displays the editor's buffer path, cursor buffer position, and buffer modified indicator", ->
expect(statusBar.currentPath.text()).toBe 'sample.js'
expect(statusBar.bufferModified.text()).toBe ''
expect(statusBar.cursorPosition.text()).toBe '1,1'
describe "when associated with an unsaved buffer", ->
@ -44,6 +46,27 @@ describe "StatusBar", ->
rootView.open(require.resolve 'fixtures/sample.txt')
expect(statusBar.currentPath.text()).toBe 'sample.txt'
describe "when the associated editor's buffer's content changes", ->
it "enables the buffer modified indicator", ->
expect(statusBar.bufferModified.text()).toBe ''
editor.insertText("\n")
expect(statusBar.bufferModified.text()).toBe '*'
editor.backspace()
describe "when the buffer content has changed from the content on disk", ->
it "disables the buffer modified indicator on save", ->
editor.insertText("\n")
editor.save()
expect(statusBar.bufferModified.text()).toBe ''
editor.backspace()
editor.save()
it "disables the buffer modified indicator if the content matches again", ->
editor.insertText("\n")
expect(statusBar.bufferModified.text()).toBe '*'
editor.backspace()
expect(statusBar.bufferModified.text()).toBe ''
describe "when the associated editor's cursor position changes", ->
it "updates the cursor position in the status bar", ->
editor.setCursorScreenPosition([1, 2])

View File

@ -14,6 +14,7 @@ class Buffer
@idCounter = 1
undoManager: null
modified: null
contentOnDisk: null
modifiedOnDisk: null
lines: null
file: null
@ -30,7 +31,8 @@ class Buffer
if path
throw "Path '#{path}' does not exist" unless fs.exists(path)
@setPath(path)
@setText(fs.read(@getPath()))
@contentOnDisk = fs.read(@getPath())
@setText(@contentOnDisk)
else
@setText('')
@ -68,7 +70,8 @@ class Buffer
@trigger "path-change", this
reload: ->
@setText(fs.read(@file.getPath()))
@contentOnDisk = fs.read(@getPath())
@setText(@contentOnDisk)
@modified = false
@modifiedOnDisk = false
@ -188,7 +191,9 @@ class Buffer
change: (oldRange, newText) ->
oldRange = Range.fromObject(oldRange)
operation = new BufferChangeOperation({buffer: this, oldRange, newText})
@pushOperation(operation)
range = @pushOperation(operation)
@trigger 'buffer-change'
range
clipPosition: (position) ->
{ row, column } = Point.fromObject(position)
@ -234,7 +239,9 @@ class Buffer
@modified = false
@modifiedOnDisk = false
@setPath(path)
@contentOnDisk = @getText()
@trigger 'after-save'
@trigger 'buffer-change'
isInConflict: ->
@isModified() and @isModifiedOnDisk()
@ -245,6 +252,9 @@ class Buffer
isModified: ->
@modified
contentDifferentOnDisk: ->
@contentOnDisk != @getText()
getAnchors: -> new Array(@anchors...)
addAnchor: (options) ->

View File

@ -17,7 +17,9 @@ class StatusBar extends View
@content: ->
@div class: 'status-bar', =>
@div class: 'current-path', outlet: 'currentPath'
@div class: 'file-info', =>
@div class: 'current-path', outlet: 'currentPath'
@div class: 'buffer-modified', outlet: 'bufferModified'
@div class: 'cursor-position', outlet: 'cursorPosition'
initialize: (@rootView, @editor) ->
@ -27,6 +29,16 @@ class StatusBar extends View
@updateCursorPositionText()
@editor.on 'cursor-move', => @updateCursorPositionText()
@updateBufferModifiedText()
@editor.getBuffer().on 'buffer-change', => @updateBufferModifiedText()
updateBufferModifiedText: ->
buffer = @editor.getBuffer()
if buffer.isModified() and buffer.contentDifferentOnDisk()
@bufferModified.text('*')
else
@bufferModified.text('')
updatePathText: ->
path = @editor.getPath()
if path
@ -37,4 +49,3 @@ class StatusBar extends View
updateCursorPositionText: ->
{ row, column } = @editor.getCursorBufferPosition()
@cursorPosition.text("#{row + 1},#{column + 1}")

View File

@ -5,6 +5,21 @@
position: relative;
}
.status-bar .file-info {
float: left;
display: inline-block;
}
.status-bar .file-info .current-path {
float: left;
display: inline-block;
}
.status-bar .file-info .buffer-modified {
float: right;
display: inline-block;
}
.status-bar .cursor-position {
position: absolute;
right: 5px;