From fb7f5deb947f7bfa9dc557bb1fe2b2f7cd10b7fe Mon Sep 17 00:00:00 2001 From: Will Farrington Date: Sat, 29 Sep 2012 19:18:18 -0700 Subject: [PATCH 1/8] first spike of buffer modified indicator --- src/app/buffer.coffee | 2 ++ src/app/status-bar.coffee | 14 ++++++++++++-- static/status-bar.css | 8 ++++++++ 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/app/buffer.coffee b/src/app/buffer.coffee index 52eab6db8..f388c88d3 100644 --- a/src/app/buffer.coffee +++ b/src/app/buffer.coffee @@ -186,6 +186,7 @@ class Buffer @change(range, '') change: (oldRange, newText) -> + @trigger 'buffer-change' oldRange = Range.fromObject(oldRange) operation = new BufferChangeOperation({buffer: this, oldRange, newText}) @pushOperation(operation) @@ -223,6 +224,7 @@ class Buffer @undoManager.redo(editSession) save: -> + @trigger 'buffer-change' @saveAs(@getPath()) saveAs: (path) -> diff --git a/src/app/status-bar.coffee b/src/app/status-bar.coffee index 0894851b5..9fe3de909 100644 --- a/src/app/status-bar.coffee +++ b/src/app/status-bar.coffee @@ -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,15 @@ class StatusBar extends View @updateCursorPositionText() @editor.on 'cursor-move', => @updateCursorPositionText() + @updateBufferModifiedText() + @editor.getBuffer().on 'buffer-change', => @updateBufferModifiedText() + + updateBufferModifiedText: -> + if @editor.getBuffer().isModified() + @bufferModified.text('*') + else + @bufferModified.text('') + updatePathText: -> path = @editor.getPath() if path @@ -37,4 +48,3 @@ class StatusBar extends View updateCursorPositionText: -> { row, column } = @editor.getCursorBufferPosition() @cursorPosition.text("#{row + 1},#{column + 1}") - diff --git a/static/status-bar.css b/static/status-bar.css index cbe86bb1f..5f57cac90 100644 --- a/static/status-bar.css +++ b/static/status-bar.css @@ -5,6 +5,14 @@ position: relative; } +.status-bar .file-info { + float: left; +} + +.status-bar .buffer-modified { + float: right; +} + .status-bar .cursor-position { position: absolute; right: 5px; From da62c5c202bb036449274c3fe241edc99c07b625 Mon Sep 17 00:00:00 2001 From: Will Farrington Date: Sat, 29 Sep 2012 19:24:56 -0700 Subject: [PATCH 2/8] fix display of modified identifier to remain inline --- static/status-bar.css | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/static/status-bar.css b/static/status-bar.css index 5f57cac90..bdc20d297 100644 --- a/static/status-bar.css +++ b/static/status-bar.css @@ -7,10 +7,17 @@ .status-bar .file-info { float: left; + display: inline-block; +} + +.status-bar .file-info .current-path { + float: left; + display: inline-block; } .status-bar .buffer-modified { float: right; + display: inline-block; } .status-bar .cursor-position { From ef8604325281b96680df4a561b5af4da7a7d0a62 Mon Sep 17 00:00:00 2001 From: Will Farrington Date: Sat, 29 Sep 2012 19:37:39 -0700 Subject: [PATCH 3/8] css style consistency --- static/status-bar.css | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/static/status-bar.css b/static/status-bar.css index bdc20d297..0c20bee03 100644 --- a/static/status-bar.css +++ b/static/status-bar.css @@ -15,7 +15,7 @@ display: inline-block; } -.status-bar .buffer-modified { +.status-bar .file-info .buffer-modified { float: right; display: inline-block; } From 12f145f95990a45a71901991e252a0aa555b1bff Mon Sep 17 00:00:00 2001 From: Will Farrington Date: Sat, 29 Sep 2012 23:21:45 -0700 Subject: [PATCH 4/8] Make Buffer#saveAs trigger the buffer-change instead --- src/app/buffer.coffee | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/app/buffer.coffee b/src/app/buffer.coffee index f388c88d3..664c1e198 100644 --- a/src/app/buffer.coffee +++ b/src/app/buffer.coffee @@ -224,7 +224,6 @@ class Buffer @undoManager.redo(editSession) save: -> - @trigger 'buffer-change' @saveAs(@getPath()) saveAs: (path) -> @@ -237,6 +236,7 @@ class Buffer @modifiedOnDisk = false @setPath(path) @trigger 'after-save' + @trigger 'buffer-change' isInConflict: -> @isModified() and @isModifiedOnDisk() From 214735f1d7aecc7e45ba75d12b0d9c9bdb0cff59 Mon Sep 17 00:00:00 2001 From: Will Farrington Date: Sun, 7 Oct 2012 09:48:46 -1000 Subject: [PATCH 5/8] add some specs for buffer modified indicator --- spec/app/status-bar-spec.coffee | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/spec/app/status-bar-spec.coffee b/spec/app/status-bar-spec.coffee index 9beefc5cf..5890377e3 100644 --- a/spec/app/status-bar-spec.coffee +++ b/spec/app/status-bar-spec.coffee @@ -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,16 @@ 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 "updates the buffer modified indicator", -> + expect(statusBar.bufferModified.text()).toBe '' + oldText = buffer.getText() + editor.insertText("/") + expect(statusBar.bufferModified.text()).toBe '*' + editor.backspace() + buffer.save() + 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]) From 602f4ee233fc05483d016649186f0add251558cb Mon Sep 17 00:00:00 2001 From: Chris Wanstrath Date: Mon, 8 Oct 2012 20:28:18 -0700 Subject: [PATCH 6/8] trigger buffer-change after the change, not before --- src/app/buffer.coffee | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/app/buffer.coffee b/src/app/buffer.coffee index 664c1e198..894961219 100644 --- a/src/app/buffer.coffee +++ b/src/app/buffer.coffee @@ -186,10 +186,11 @@ class Buffer @change(range, '') change: (oldRange, newText) -> - @trigger 'buffer-change' 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) From 4b3505d9b5e3b8fc6040fcbc4a7977039cfa7904 Mon Sep 17 00:00:00 2001 From: Will Farrington Date: Mon, 8 Oct 2012 17:55:35 -1000 Subject: [PATCH 7/8] Disable buffer modified indicator if buffer content matches content on disk --- spec/app/status-bar-spec.coffee | 19 +++++++++++++++---- src/app/buffer.coffee | 3 +++ src/app/status-bar.coffee | 3 ++- 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/spec/app/status-bar-spec.coffee b/spec/app/status-bar-spec.coffee index 5890377e3..a433681b2 100644 --- a/spec/app/status-bar-spec.coffee +++ b/spec/app/status-bar-spec.coffee @@ -47,13 +47,24 @@ describe "StatusBar", -> expect(statusBar.currentPath.text()).toBe 'sample.txt' describe "when the associated editor's buffer's content changes", -> - it "updates the buffer modified indicator", -> + it "enables the buffer modified indicator", -> expect(statusBar.bufferModified.text()).toBe '' - oldText = buffer.getText() - editor.insertText("/") + 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() - buffer.save() expect(statusBar.bufferModified.text()).toBe '' describe "when the associated editor's cursor position changes", -> diff --git a/src/app/buffer.coffee b/src/app/buffer.coffee index 894961219..ccf5f255b 100644 --- a/src/app/buffer.coffee +++ b/src/app/buffer.coffee @@ -248,6 +248,9 @@ class Buffer isModified: -> @modified + contentDifferentOnDisk: -> + fs.read(@file.getPath()) != @getText() + getAnchors: -> new Array(@anchors...) addAnchor: (options) -> diff --git a/src/app/status-bar.coffee b/src/app/status-bar.coffee index 9fe3de909..3b755fb86 100644 --- a/src/app/status-bar.coffee +++ b/src/app/status-bar.coffee @@ -33,7 +33,8 @@ class StatusBar extends View @editor.getBuffer().on 'buffer-change', => @updateBufferModifiedText() updateBufferModifiedText: -> - if @editor.getBuffer().isModified() + buffer = @editor.getBuffer() + if buffer.isModified() and buffer.contentDifferentOnDisk() @bufferModified.text('*') else @bufferModified.text('') From d5b0e7acd2f542f677ec6f1a40d4d4e675d42121 Mon Sep 17 00:00:00 2001 From: Will Farrington Date: Mon, 8 Oct 2012 18:17:15 -1000 Subject: [PATCH 8/8] memoize Buffer @contentOnDisk so we don't re-read the whole file on every buffer-change event --- src/app/buffer.coffee | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/app/buffer.coffee b/src/app/buffer.coffee index ccf5f255b..ef4468efd 100644 --- a/src/app/buffer.coffee +++ b/src/app/buffer.coffee @@ -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 @@ -236,6 +239,7 @@ class Buffer @modified = false @modifiedOnDisk = false @setPath(path) + @contentOnDisk = @getText() @trigger 'after-save' @trigger 'buffer-change' @@ -249,7 +253,7 @@ class Buffer @modified contentDifferentOnDisk: -> - fs.read(@file.getPath()) != @getText() + @contentOnDisk != @getText() getAnchors: -> new Array(@anchors...)