Project.open returns an editSession instead of a buffer.

First step in removing the coupling of Editor and Buffer. Editor should get all information about the active buffer from the activeEditSession.
This commit is contained in:
Corey Johnson & Nathan Sobo
2012-06-18 15:56:55 -07:00
parent 4bd5a017cf
commit 4659fd7dc3
10 changed files with 78 additions and 93 deletions

View File

@@ -39,19 +39,6 @@ describe 'Buffer', ->
expect(eventHandler).toHaveBeenCalledWith(buffer)
describe ".isModified()", ->
describe "when deserialized", ->
it "returns false", ->
buffer = Buffer.deserialize(buffer.serialize(), new Project)
expect(buffer.isModified()).toBe false
buffer = Buffer.deserialize((new Buffer).serialize(), new Project)
expect(buffer.isModified()).toBe false
it "returns is true if buffer no path and had changes", ->
buffer = new Buffer
buffer.insert([0,0], "oh hi")
expect(buffer.isModified()).toBe true
it "returns true when user changes buffer", ->
expect(buffer.isModified()).toBeFalsy()
buffer.insert([0,0], "hi")
@@ -68,25 +55,6 @@ describe 'Buffer', ->
buffer.save()
expect(buffer.isModified()).toBe false
describe '.deserialize(state, project)', ->
project = null
beforeEach ->
project = new Project(fs.directory(filePath))
describe 'when the state has a path', ->
it 'use the project to open the path', ->
savedBuffer = project.open(filePath)
buffer = Buffer.deserialize(savedBuffer.serialize(), project)
expect(buffer).toBe savedBuffer
describe 'when the state has text (and no path)', ->
it 'creates a new empty buffer (does not serialze unsaved text)', ->
unsavedBuffer = project.open()
unsavedBuffer.setText("OMGWTFBBQ")
buffer = Buffer.deserialize(unsavedBuffer.serialize(), project)
expect(buffer.getText()).toBe ""
describe ".getLines()", ->
it "returns an array of lines in the text contents", ->
expect(buffer.getLines().length).toBe fileContents.split("\n").length

View File

@@ -66,6 +66,7 @@ describe "Editor", ->
expect(editor.serialize).toHaveBeenCalled()
expect(Editor.deserialize).toHaveBeenCalled()
expect(newEditor.buffer).toBe editor.buffer
expect(newEditor.getCursorScreenPosition()).toEqual editor.getCursorScreenPosition()
expect(newEditor.editSessions[0]).toEqual(editor.editSessions[0])

View File

@@ -14,30 +14,30 @@ describe "Project", ->
project.on 'new-buffer', newBufferHandler
describe "when given an absolute path that hasn't been opened previously", ->
it "returns a new buffer for the given path and emits a 'new-buffer' event", ->
buffer = project.open(absolutePath)
expect(buffer.path).toBe absolutePath
expect(newBufferHandler).toHaveBeenCalledWith buffer
it "returns a new edit session for the given path and emits a 'new-buffer' event", ->
editSession = project.open(absolutePath)
expect(editSession.buffer.path).toBe absolutePath
expect(newBufferHandler).toHaveBeenCalledWith editSession.buffer
describe "when given a relative path that hasn't been opened previously", ->
it "returns a buffer for the given path (relative to the project root) and emits a 'new-buffer' event", ->
buffer = project.open('a')
expect(buffer.path).toBe absolutePath
expect(newBufferHandler).toHaveBeenCalledWith buffer
it "returns a new edit session for the given path (relative to the project root) and emits a 'new-buffer' event", ->
editSession = project.open('a')
expect(editSession.buffer.path).toBe absolutePath
expect(newBufferHandler).toHaveBeenCalledWith editSession.buffer
describe "when passed the path to a buffer that has already been opened", ->
it "returns the previously opened buffer", ->
buffer = project.open(absolutePath)
it "returns a new edit session containing previously opened buffer", ->
editSession = project.open(absolutePath)
newBufferHandler.reset()
expect(project.open(absolutePath)).toBe buffer
expect(project.open('a')).toBe buffer
expect(project.open(absolutePath).buffer).toBe editSession.buffer
expect(project.open('a').buffer).toBe editSession.buffer
expect(newBufferHandler).not.toHaveBeenCalled()
describe "when not passed a path", ->
it "returns a new buffer and emits a new-buffer event", ->
buffer = project.open()
expect(buffer.path).toBeUndefined()
expect(newBufferHandler).toHaveBeenCalledWith(buffer)
it "returns a new edit session and emits a new-buffer event", ->
editSession = project.open()
expect(editSession.buffer.getPath()).toBeUndefined()
expect(newBufferHandler).toHaveBeenCalledWith(editSession.buffer)
describe ".resolve(path)", ->
it "returns an absolute path based on the project's root", ->

View File

@@ -1,5 +1,6 @@
CommandInterpreter = require 'command-interpreter'
Buffer = require 'buffer'
EditSession = require 'edit-session'
Editor = require 'editor'
describe "CommandInterpreter", ->
@@ -7,7 +8,8 @@ describe "CommandInterpreter", ->
beforeEach ->
buffer = new Buffer(require.resolve 'fixtures/sample.js')
editor = new Editor({buffer})
editSession = new EditSession({buffer})
editor = new Editor({editSessions:[editSession]})
interpreter = new CommandInterpreter()
describe "addresses", ->

View File

@@ -12,9 +12,6 @@ class Buffer
lines: null
path: null
@deserialize: (state, project) ->
project.open(state.path)
constructor: (path) ->
@id = @constructor.idCounter++
@setPath(path)
@@ -26,9 +23,6 @@ class Buffer
@undoManager = new UndoManager(this)
@modified = false
serialize: ->
path: @getPath()
getPath: ->
@path

View File

@@ -11,13 +11,7 @@ class EditSession
@idCounter: 1
@deserialize: (state, editor, rootView) ->
buffer = Buffer.deserialize(state.buffer, rootView.project)
session = new EditSession(
buffer: buffer
tabText: editor.tabText
autoIndent: editor.autoIndent
softTabs: editor.softTabs
)
session = rootView.project.open(state.buffer)
session.setScrollTop(state.scrollTop)
session.setScrollLeft(state.scrollLeft)
session.setCursorScreenPosition(state.cursorScreenPosition)
@@ -54,7 +48,7 @@ class EditSession
@displayBuffer.destroy()
serialize: ->
buffer: @buffer.serialize()
buffer: @buffer.getPath()
scrollTop: @getScrollTop()
scrollLeft: @getScrollLeft()
cursorScreenPosition: @getCursorScreenPosition().serialize()

View File

@@ -50,13 +50,13 @@ class Editor extends View
lineOverdraw: 100
@deserialize: (state, rootView) ->
editor = new Editor(suppressBufferCreation: true, mini: state.mini)
editor.editSessions = state.editSessions.map (state) -> EditSession.deserialize(state, editor, rootView)
editor.setActiveEditSessionIndex(state.activeEditSessionIndex)
editSessions = state.editSessions.map (state) -> EditSession.deserialize(state, editor, rootView)
editor = new Editor(activeEditSessionIndex: state.activeEditSessionIndex, editSessions: editSessions, suppressBufferCreation: true, mini: state.mini)
editor.isFocused = state.isFocused
editor
initialize: ({buffer, suppressBufferCreation, @mini} = {}) ->
initialize: ({activeEditSessionIndex, editSessions, suppressBufferCreation, @mini} = {}) ->
requireStylesheet 'editor.css'
requireStylesheet 'theme/twilight.css'
@@ -66,12 +66,21 @@ class Editor extends View
@handleEvents()
@cursorViews = []
@selectionViews = []
@editSessions = []
@editSessions = editSessions ? []
if buffer?
@setBuffer(buffer)
if activeEditSessionIndex?
@setActiveEditSessionIndex(activeEditSessionIndex)
else if @editSessions.length > 0
@setActiveEditSessionIndex(0)
else if !suppressBufferCreation
@setBuffer(new Buffer)
editSession = new EditSession
softWrapColumn: @calcSoftWrapColumn()
buffer: new Buffer()
tabText: @tabText
autoIndent: @autoIndent
softTabs: @softTabs
@setActiveEditSession(editSession)
serialize: ->
@saveActiveEditSession()
@@ -336,6 +345,15 @@ class Editor extends View
setBuffer: (buffer) ->
@activateEditSessionForBuffer(buffer)
setActiveEditSession: (editSession) ->
index = @editSessionIndexForBuffer(editSession.buffer)
unless index?
index = @editSessions.length
@editSessions.push(editSession)
@setActiveEditSessionIndex(index)
activateEditSessionForBuffer: (buffer) ->
index = @editSessionIndexForBuffer(buffer)
unless index?

View File

@@ -3,17 +3,18 @@ _ = require 'underscore'
$ = require 'jquery'
Buffer = require 'buffer'
EditSession = require 'edit-session'
EventEmitter = require 'event-emitter'
Directory = require 'directory'
module.exports =
class Project
rootDirectory: null
buffers: null
editSessions: null
constructor: (path) ->
@setPath(path)
@buffers = []
@editSessions = []
getPath: ->
@rootDirectory?.path
@@ -48,19 +49,6 @@ class Project
ignorePath: (path) ->
fs.base(path).match(/\.DS_Store/) or path.match(/(^|\/)\.git(\/|$)/)
open: (filePath) ->
if filePath?
filePath = @resolve(filePath)
@bufferWithPath(filePath) ? @buildBuffer(filePath)
else
@buildBuffer()
buildBuffer: (filePath) ->
buffer = new Buffer(filePath)
@buffers.push(buffer)
@trigger 'new-buffer', buffer
buffer
resolve: (filePath) ->
filePath = fs.join(@getPath(), filePath) unless filePath[0] == '/'
fs.absolute filePath
@@ -68,10 +56,30 @@ class Project
relativize: (fullPath) ->
fullPath.replace(@getPath(), "").replace(/^\//, '')
bufferWithId: (id) ->
return buffer for buffer in @buffers when buffer.id == id
open: (filePath) ->
if filePath?
filePath = @resolve(filePath)
buffer = @bufferWithPath(filePath) ? @buildBuffer(filePath)
else
buffer = @buildBuffer()
editSession = new EditSession({buffer, tabText: " ", autoIndent: true, softTabs: true, softWrapColumn: null})
@editSessions.push editSession
editSession
buildBuffer: (filePath) ->
buffer = new Buffer(filePath)
@trigger 'new-buffer', buffer
buffer
getBuffers: ->
buffers = []
for editSession in @editSessions when not _.include(buffers, editSession.buffer)
buffers.push editSession.buffer
buffers
bufferWithPath: (path) ->
return buffer for buffer in @buffers when buffer.path == path
return editSession.buffer for editSession in @editSessions when editSession.buffer.getPath() == path
_.extend Project.prototype, EventEmitter

View File

@@ -91,12 +91,12 @@ class RootView extends View
@remove()
open: (path, changeFocus=true) ->
buffer = @project.open(path)
editSession = @project.open(path)
if @activeEditor()
@activeEditor().setBuffer(buffer)
@activeEditor().setActiveEditSession(editSession)
else
editor = new Editor({ buffer })
editor = new Editor(editSessions: [editSession])
pane = new Pane(editor)
@panes.append(pane)
if changeFocus

View File

@@ -1,6 +1,6 @@
module.exports =
activate: (rootView) ->
for buffer in rootView.project.buffers
for buffer in rootView.project.getBuffers()
@stripTrailingWhitespaceBeforeSave(buffer)
rootView.project.on 'new-buffer', (buffer) =>