From 27dd97069a6de2066e1f2d71eb3727bbd167f9c5 Mon Sep 17 00:00:00 2001 From: Corey Johnson & Nathan Sobo Date: Thu, 15 Dec 2011 14:13:34 -0800 Subject: [PATCH] Attach and populate the ace editor with buffer text when an editor is created. --- spec/atom/app-spec.coffee | 4 +++- spec/atom/buffer-spec.coffee | 9 +++++++++ spec/atom/editor-spec.coffee | 34 ++++++++++++++++++++++++++++++++++ src/atom/buffer.coffee | 5 ++++- src/atom/editor.coffee | 15 +++++++++++++++ 5 files changed, 65 insertions(+), 2 deletions(-) create mode 100644 spec/atom/buffer-spec.coffee create mode 100644 spec/atom/editor-spec.coffee diff --git a/spec/atom/app-spec.coffee b/spec/atom/app-spec.coffee index 59a6adad2..246fd05b4 100644 --- a/spec/atom/app-spec.coffee +++ b/spec/atom/app-spec.coffee @@ -1,4 +1,5 @@ App = require 'app' +fs = require 'fs' describe "App", -> app = null @@ -18,7 +19,8 @@ describe "App", -> expect(app.windows().length).toBe 1 newWindow = app.windows()[0] - + expect(newWindow.editor).toBeDefined() expect(newWindow.editor.buffer).toBeDefined() expect(newWindow.editor.buffer.url).toEqual filePath + expect(newWindow.editor.buffer.text).toEqual fs.read(filePath) diff --git a/spec/atom/buffer-spec.coffee b/spec/atom/buffer-spec.coffee new file mode 100644 index 000000000..e182674fe --- /dev/null +++ b/spec/atom/buffer-spec.coffee @@ -0,0 +1,9 @@ +Buffer = require 'buffer' +fs = require 'fs' + +describe 'Buffer', -> + describe 'constructor', -> + it "loads the contents of the given url", -> + filePath = require.resolve 'fixtures/sample.txt' + buffer = new Buffer filePath + expect(buffer.text).toBe fs.read(filePath) diff --git a/spec/atom/editor-spec.coffee b/spec/atom/editor-spec.coffee new file mode 100644 index 000000000..fd8b9ae24 --- /dev/null +++ b/spec/atom/editor-spec.coffee @@ -0,0 +1,34 @@ +Editor = require 'editor' +$ = require 'jquery' +ck = require 'coffeekup' + +describe "Editor", -> + mainDiv = null; editor = null; filePath = null + + beforeEach -> + filePath = require.resolve 'fixtures/sample.txt' + mainDiv = $("
") + $("#jasmine-content").append(mainDiv) + editor = new Editor filePath + + afterEach -> + editor.destroy() + + describe "constructor", -> + it "attaches itself to the #main element and opens a buffer with the given url", -> + expect(editor.buffer.url).toEqual filePath + expect(mainDiv.children('#editor').html()).not.toBe '' + + it "populates the editor with the contents of the buffer", -> + expect(editor.aceEditor.getSession().getValue()).toBe editor.buffer.text + + describe 'destroy', -> + it 'destroys the ace editor and removes #editor from the dom.', -> + spyOn editor.aceEditor, 'destroy' + + editor.destroy() + expect(editor.aceEditor.destroy).toHaveBeenCalled() + expect(mainDiv.children('#editor').length).toBe 0 + + describe "when the text is changed via the ace editor", -> + it "updates the buffer text", -> diff --git a/src/atom/buffer.coffee b/src/atom/buffer.coffee index f6095cfc3..b69e3cb3f 100644 --- a/src/atom/buffer.coffee +++ b/src/atom/buffer.coffee @@ -1,6 +1,9 @@ +fs = require 'fs' + module.exports = class Buffer + text: null url: null constructor: (@url) -> - + @text = fs.read @url diff --git a/src/atom/editor.coffee b/src/atom/editor.coffee index 824acf20c..0285e5e81 100644 --- a/src/atom/editor.coffee +++ b/src/atom/editor.coffee @@ -1,9 +1,24 @@ Buffer = require 'buffer' +ace = require 'ace/ace' +$ = require 'jquery' module.exports = class Editor + aceEditor: null buffer: null constructor: (url) -> @buffer = new Buffer(url) + @buildAceEditor() + @setText @buffer.text + + setText: (text) -> + @aceEditor.getSession().setValue @buffer.text + + buildAceEditor: -> + $('#main').append("
") + @aceEditor = ace.edit 'editor' + + destroy: -> + @aceEditor.destroy()