Eliminate Editor.open

This commit is contained in:
Corey Johnson & Nathan Sobo 2012-01-05 11:26:14 -08:00
parent b5b1ac67fa
commit 1c24ad0fdd
3 changed files with 12 additions and 35 deletions

View File

@ -13,7 +13,6 @@ describe "Editor", ->
beforeEach ->
filePath = require.resolve 'fixtures/sample.txt'
tempFilePath = '/tmp/temp.txt'
spyOn(Editor.prototype.viewProperties, 'open').andCallThrough()
editor = Editor.build()
afterEach ->
@ -44,41 +43,19 @@ describe "Editor", ->
expect(editor.getAceSession().getMode().name).toBe 'javascript'
describe "open(url)", ->
describe "when called with a url", ->
it "loads a buffer for the given url into the editor", ->
editor.open(filePath)
fileContents = fs.read(filePath)
expect(editor.getAceSession().getValue()).toBe fileContents
expect(editor.buffer.url).toBe(filePath)
expect(editor.buffer.getText()).toEqual fileContents
it "sets the mode on the session based on the file extension", ->
editor.open('something.js')
expect(editor.getAceSession().getMode().name).toBe 'javascript'
editor.open('something.text')
expect(editor.getAceSession().getMode().name).toBe 'text'
describe "when called with null", ->
it "loads an empty buffer with no url", ->
editor.open()
expect(editor.getAceSession().getValue()).toBe ""
expect(editor.buffer.url).toBeUndefined()
expect(editor.buffer.getText()).toEqual ""
describe "when the text is changed via the ace editor", ->
it "updates the buffer text", ->
editor.open(filePath)
expect(editor.buffer.getText()).not.toMatch /^.ooo/
buffer = new Buffer(filePath)
editor.setBuffer(buffer)
expect(buffer.getText()).not.toMatch /^.ooo/
editor.getAceSession().insert {row: 0, column: 1}, 'ooo'
expect(editor.buffer.getText()).toMatch /^.ooo/
expect(buffer.getText()).toMatch /^.ooo/
describe "save", ->
describe "when the current buffer has a url", ->
beforeEach ->
editor.open tempFilePath
expect(editor.buffer.url).toBe tempFilePath
buffer = new Buffer(tempFilePath)
editor.setBuffer(buffer)
it "saves the current buffer to disk", ->
editor.buffer.setText 'Edited buffer!'

View File

@ -15,7 +15,7 @@ class Editor extends Template
initialize: () ->
@buildAceEditor()
@open()
@setBuffer(new Buffer)
shutdown: ->
@destroy()
@ -27,9 +27,6 @@ class Editor extends Template
session = new EditSession(@buffer.aceDocument, @buffer.getMode())
@aceEditor.setSession(session)
open: (url) ->
@setBuffer(new Buffer(url))
buildAceEditor: ->
@aceEditor = ace.edit this[0]
@aceEditor.setTheme(require "ace/theme/twilight")

View File

@ -2,6 +2,7 @@ $ = require 'jquery'
fs = require 'fs'
Template = require 'template'
Buffer = require 'buffer'
Editor = require 'editor'
FileFinder = require 'file-finder'
Project = require 'project'
@ -23,7 +24,7 @@ class RootView extends Template
if url
@project = new Project(fs.directory(url))
@editor.open(url) if fs.isFile(url)
@editor.setBuffer(new Buffer(url)) if fs.isFile(url)
addPane: (view) ->
pane = $('<div class="pane">')
@ -41,6 +42,8 @@ class RootView extends Template
relativePaths = (path.replace(@project.url, "") for path in paths)
@fileFinder = FileFinder.build
urls: relativePaths
selected: (relativePath) => @editor.open(@project.url + relativePath)
selected: (relativePath) =>
buffer = new Buffer(@project.url + relativePath)
@editor.setBuffer buffer
@addPane(@fileFinder)
@fileFinder.input.focus()