From f6414c543caef8a4f9925d9af8343a073288f366 Mon Sep 17 00:00:00 2001 From: Corey Johnson Date: Wed, 7 Mar 2012 12:43:29 -0800 Subject: [PATCH] Add save command and keyboard shortcuts to editor. --- spec/atom/editor-spec.coffee | 49 +++++++++++++++++++++++++++++++++--- src/atom/editor.coffee | 11 +++++++- 2 files changed, 56 insertions(+), 4 deletions(-) diff --git a/spec/atom/editor-spec.coffee b/spec/atom/editor-spec.coffee index cfb441360..a7e962042 100644 --- a/spec/atom/editor-spec.coffee +++ b/spec/atom/editor-spec.coffee @@ -163,9 +163,6 @@ describe "Editor", -> expect(editor.gutter.find('.line-number:eq(3)').text()).toBe '4' expect(editor.gutter.find('.line-number:eq(4)').text()).toBe '7' - - - describe "cursor movement", -> describe ".setCursorScreenPosition({row, column})", -> beforeEach -> @@ -906,3 +903,49 @@ describe "Editor", -> expect(editor.lines.find('.line:eq(5)').text()).toBe ' current = items.shift();' expect(editor.getCursorBufferPosition()).toEqual [4, 29] + + describe ".save()", -> + describe "when the current buffer has a path", -> + tempFilePath = null + + beforeEach -> + tempFilePath = '/tmp/temp.txt' + expect(fs.remove(tempFilePath)) + editor.setBuffer new Buffer(tempFilePath) + expect(editor.buffer.path).toBe tempFilePath + + it "saves the current buffer to disk", -> + editor.buffer.setText 'Edited!' + expect(fs.exists(tempFilePath)).toBeFalsy() + + editor.save() + + expect(fs.exists(tempFilePath)).toBeTruthy() + expect(fs.read(tempFilePath)).toBe 'Edited!' + + describe "when the current buffer has no path", -> + selectedFilePath = null + beforeEach -> + editor.setBuffer new Buffer() + expect(editor.buffer.path).toBeUndefined() + editor.buffer.setText 'Save me to a new path' + spyOn($native, 'saveDialog').andCallFake -> selectedFilePath + + it "presents a 'save as' dialog", -> + editor.save() + expect($native.saveDialog).toHaveBeenCalled() + + describe "when a path is chosen", -> + it "saves the buffer to the chosen path", -> + selectedFilePath = '/tmp/temp.txt' + + editor.save() + + expect(fs.exists(selectedFilePath)).toBeTruthy() + expect(fs.read(selectedFilePath)).toBe 'Save me to a new path' + + describe "when dialog is cancelled", -> + it "does not save the buffer", -> + selectedFilePath = null + editor.save() + expect(fs.exists(selectedFilePath)).toBeFalsy() diff --git a/src/atom/editor.coffee b/src/atom/editor.coffee index 362f23e61..e882377a4 100644 --- a/src/atom/editor.coffee +++ b/src/atom/editor.coffee @@ -46,6 +46,7 @@ class Editor extends View bindKeys: -> window.keymap.bindKeys '*:not(.editor *)', + 'meta-s': 'save' right: 'move-right' left: 'move-left' down: 'move-down' @@ -56,7 +57,7 @@ class Editor extends View 'shift-down': 'select-down' enter: 'newline' backspace: 'backspace' - delete: 'delete' + 'delete': 'delete' 'meta-x': 'cut' 'meta-c': 'copy' 'meta-v': 'paste' @@ -65,6 +66,7 @@ class Editor extends View 'alt-meta-w': 'toggle-soft-wrap' 'alt-meta-f': 'fold-selection' + @on 'save', => @save() @on 'move-right', => @moveCursorRight() @on 'move-left', => @moveCursorLeft() @on 'move-down', => @moveCursorDown() @@ -237,6 +239,13 @@ class Editor extends View @removeClass 'soft-wrap' $(window).off 'resize', @_setMaxLineLength + save: -> + if not @buffer.path + path = $native.saveDialog() + return if not path + @buffer.path = path + + @buffer.save() clipScreenPosition: (screenPosition, options={}) -> @renderer.clipScreenPosition(screenPosition, options)