From 4659fd7dc329d2ff1a386c15f0e2e2d375cc2f6d Mon Sep 17 00:00:00 2001 From: Corey Johnson & Nathan Sobo Date: Mon, 18 Jun 2012 15:56:55 -0700 Subject: [PATCH] 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. --- spec/app/buffer-spec.coffee | 32 -------------- spec/app/editor-spec.coffee | 1 + spec/app/project-spec.coffee | 32 +++++++------- .../command-interpreter-spec.coffee | 4 +- src/app/buffer.coffee | 6 --- src/app/edit-session.coffee | 10 +---- src/app/editor.coffee | 34 ++++++++++---- src/app/project.coffee | 44 +++++++++++-------- src/app/root-view.coffee | 6 +-- .../strip-trailing-whitespace.coffee | 2 +- 10 files changed, 78 insertions(+), 93 deletions(-) diff --git a/spec/app/buffer-spec.coffee b/spec/app/buffer-spec.coffee index 676c90218..78b15ce55 100644 --- a/spec/app/buffer-spec.coffee +++ b/spec/app/buffer-spec.coffee @@ -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 diff --git a/spec/app/editor-spec.coffee b/spec/app/editor-spec.coffee index c629626c7..6015da035 100644 --- a/spec/app/editor-spec.coffee +++ b/spec/app/editor-spec.coffee @@ -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]) diff --git a/spec/app/project-spec.coffee b/spec/app/project-spec.coffee index 131f814f3..2cab3e5a1 100644 --- a/spec/app/project-spec.coffee +++ b/spec/app/project-spec.coffee @@ -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", -> diff --git a/spec/extensions/command-interpreter-spec.coffee b/spec/extensions/command-interpreter-spec.coffee index e68795ebe..feed27db9 100644 --- a/spec/extensions/command-interpreter-spec.coffee +++ b/spec/extensions/command-interpreter-spec.coffee @@ -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", -> diff --git a/src/app/buffer.coffee b/src/app/buffer.coffee index 7fbf8792f..d35168063 100644 --- a/src/app/buffer.coffee +++ b/src/app/buffer.coffee @@ -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 diff --git a/src/app/edit-session.coffee b/src/app/edit-session.coffee index e5f82eb1a..242c70251 100644 --- a/src/app/edit-session.coffee +++ b/src/app/edit-session.coffee @@ -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() diff --git a/src/app/editor.coffee b/src/app/editor.coffee index ff6cf7abf..e9ec592ec 100644 --- a/src/app/editor.coffee +++ b/src/app/editor.coffee @@ -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? diff --git a/src/app/project.coffee b/src/app/project.coffee index 673e3b704..7fd7eab96 100644 --- a/src/app/project.coffee +++ b/src/app/project.coffee @@ -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 diff --git a/src/app/root-view.coffee b/src/app/root-view.coffee index b04ae057d..dc7fff0f6 100644 --- a/src/app/root-view.coffee +++ b/src/app/root-view.coffee @@ -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 diff --git a/src/extensions/strip-trailing-whitespace.coffee b/src/extensions/strip-trailing-whitespace.coffee index 86db0c10b..ebf482741 100644 --- a/src/extensions/strip-trailing-whitespace.coffee +++ b/src/extensions/strip-trailing-whitespace.coffee @@ -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) =>