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

@@ -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) =>