From 4e78b29e81d7016df08edef86cf6b0d5d17390ee Mon Sep 17 00:00:00 2001 From: Corey Johnson & Nathan Sobo Date: Tue, 19 Jun 2012 17:03:45 -0700 Subject: [PATCH] Replace Editor.setBuffer with Editor.edit --- spec/app/editor-spec.coffee | 105 ++++++++++++----------- spec/app/root-view-spec.coffee | 20 +++-- spec/extensions/autocomplete-spec.coffee | 6 +- src/app/editor.coffee | 35 ++------ src/app/root-view.coffee | 2 +- 5 files changed, 79 insertions(+), 89 deletions(-) diff --git a/spec/app/editor-spec.coffee b/spec/app/editor-spec.coffee index e1645d90a..b57b5d820 100644 --- a/spec/app/editor-spec.coffee +++ b/spec/app/editor-spec.coffee @@ -1,4 +1,5 @@ RootView = require 'root-view' +EditSession = require 'edit-session' Buffer = require 'buffer' Editor = require 'editor' Range = require 'range' @@ -92,16 +93,15 @@ describe "Editor", -> expect(editor).toMatchSelector ":has(:focus)" it "unsubscribes from the buffer when it is removed from the dom", -> - buffer = new Buffer - previousSubscriptionCount = buffer.subscriptionCount() - + editSession = project.open('sample.txt') + previousSubscriptionCount = editSession.buffer.subscriptionCount() editor.attachToDom() - editor.setBuffer(buffer) + editor.edit(editSession) - expect(buffer.subscriptionCount()).toBeGreaterThan previousSubscriptionCount + expect(editSession.buffer.subscriptionCount()).toBeGreaterThan previousSubscriptionCount expect($('.editor')).toExist() editor.remove() - expect(buffer.subscriptionCount()).toBe previousSubscriptionCount + expect(editSession.buffer.subscriptionCount()).toBeLessThan previousSubscriptionCount expect($('.editor')).not.toExist() describe "when the editor recieves focus", -> @@ -125,19 +125,20 @@ describe "Editor", -> describe ".remove()", -> it "removes subscriptions from all edit session buffers", -> - otherBuffer = new Buffer(require.resolve('fixtures/sample.txt')) - expect(buffer.subscriptionCount()).toBeGreaterThan 1 + previousEditSession = editor.activeEditSession + otherEditSession = project.open('sample.txt') + expect(previousEditSession.buffer.subscriptionCount()).toBeGreaterThan 1 - editor.setBuffer(otherBuffer) - expect(otherBuffer.subscriptionCount()).toBeGreaterThan 1 + editor.edit(otherEditSession) + expect(otherEditSession.buffer.subscriptionCount()).toBeGreaterThan 1 editor.remove() - expect(buffer.subscriptionCount()).toBe 1 - expect(otherBuffer.subscriptionCount()).toBe 1 + expect(previousEditSession.buffer.subscriptionCount()).toBe 1 + expect(otherEditSession.buffer.subscriptionCount()).toBe 1 describe "when 'close' is triggered", -> it "closes active edit session and loads next edit session", -> - editor.setBuffer(new Buffer()) + editor.edit(project.open()) spyOn(editor, "remove") editor.trigger "close" expect(editor.remove).not.toHaveBeenCalled() @@ -166,62 +167,62 @@ describe "Editor", -> expect(editor.remove).not.toHaveBeenCalled() expect($native.alert).toHaveBeenCalled() - describe ".setBuffer(buffer)", -> - otherBuffer = null + describe ".edit(editSession)", -> + otherEditSession = null beforeEach -> - otherBuffer = new Buffer + otherEditSession = project.open() - describe "when the buffer wasn't previously assigned to this editor", -> - it "creates a new EditSession for it", -> - editor.setBuffer(otherBuffer) - expect(editor.activeEditSession.buffer).toBe otherBuffer + describe "when the edit session wasn't previously assigned to this editor", -> + it "adds edit session to editor", -> + originalEditSessionCount = editor.editSessions.length + editor.edit(otherEditSession) + expect(editor.activeEditSession).toBe otherEditSession + expect(editor.editSessions.length).toBe originalEditSessionCount + 1 - describe "when the buffer was previously assigned to this editor", -> - it "restores the previous edit session associated with the buffer", -> + describe "when the edit session was previously assigned to this editor", -> + it "restores the previous edit session associated with the editor", -> previousEditSession = editor.activeEditSession - editor.setBuffer(otherBuffer) + editor.edit(otherEditSession) expect(editor.activeEditSession).not.toBe previousEditSession - editor.setBuffer(buffer) + editor.edit(previousEditSession) expect(editor.activeEditSession).toBe previousEditSession it "unsubscribes from the previously assigned buffer", -> - editor.setBuffer(otherBuffer) + previousEditSession = editor.activeEditSession + previousSubscriptionCount = previousEditSession.buffer.subscriptionCount() + editor.edit(otherEditSession) + expect(previousEditSession.buffer.subscriptionCount()).toBe previousSubscriptionCount - 1 - previousSubscriptionCount = buffer.subscriptionCount() + editor.edit(previousEditSession) + expect(previousEditSession.buffer.subscriptionCount()).toBe previousSubscriptionCount - editor.setBuffer(buffer) - editor.setBuffer(otherBuffer) + editor.edit(otherEditSession) + expect(previousEditSession.buffer.subscriptionCount()).toBe previousSubscriptionCount - 1 - expect(buffer.subscriptionCount()).toBe previousSubscriptionCount - - it "handles buffer manipulation correctly after switching to a new buffer", -> + it "handles buffer manipulation correctly after switching to a new edit session", -> editor.attachToDom() editor.insertText("abc\n") expect(editor.lineElementForScreenRow(0).text()).toBe 'abc' - editor.setBuffer(otherBuffer) + editor.edit(otherEditSession) expect(editor.lineElementForScreenRow(0).html()).toBe ' ' editor.insertText("def\n") expect(editor.lineElementForScreenRow(0).text()).toBe 'def' describe "switching edit sessions", -> - [buffer0, buffer1, buffer2] = [] [session0, session1, session2] = [] beforeEach -> - buffer0 = buffer session0 = editor.activeEditSession - buffer1 = new Buffer(require.resolve('fixtures/sample.txt')) - editor.setBuffer(buffer1) + editor.edit(project.open('sample.txt')) session1 = editor.activeEditSession - buffer2 = new Buffer(require.resolve('fixtures/two-hundred.txt')) - editor.setBuffer(buffer2) + editor.edit(project.open('two-hundred.txt')) session2 = editor.activeEditSession describe ".setActiveEditSessionIndex(index)", -> @@ -234,10 +235,10 @@ describe "Editor", -> expect(editor.scrollTop()).toBe 750 editor.setActiveEditSessionIndex(0) - expect(editor.buffer).toBe buffer0 + expect(editor.buffer).toBe session0.buffer editor.setActiveEditSessionIndex(2) - expect(editor.buffer).toBe buffer2 + expect(editor.buffer).toBe session2.buffer expect(editor.getCursorScreenPosition()).toEqual [43, 1] expect(editor.verticalScrollbar.prop('scrollHeight')).toBe previousScrollHeight expect(editor.scrollTop()).toBe 750 @@ -274,7 +275,10 @@ describe "Editor", -> beforeEach -> tempFilePath = '/tmp/atom-temp.txt' - editor.setBuffer new Buffer(tempFilePath) + rootView = new RootView(tempFilePath) + project = rootView.project + + editor.edit(project.open(tempFilePath)) expect(editor.buffer.getPath()).toBe tempFilePath afterEach -> @@ -292,7 +296,8 @@ describe "Editor", -> describe "when the current buffer has no path", -> selectedFilePath = null beforeEach -> - editor.setBuffer new Buffer() + editor.edit(project.open()) + expect(editor.buffer.getPath()).toBeUndefined() editor.buffer.setText 'Save me to a new path' spyOn($native, 'saveDialog').andCallFake -> selectedFilePath @@ -408,7 +413,7 @@ describe "Editor", -> it "emits event when editor receives a new buffer", -> eventHandler = jasmine.createSpy('eventHandler') editor.on 'editor-path-change', eventHandler - editor.setBuffer(new Buffer("something.txt")) + editor.edit(project.open("something.txt")) expect(eventHandler).toHaveBeenCalled() it "stops listening to events on previously set buffers", -> @@ -416,7 +421,7 @@ describe "Editor", -> oldBuffer = editor.buffer editor.on 'editor-path-change', eventHandler - editor.setBuffer(new Buffer("something.txt")) + editor.edit(project.open("something.txt")) expect(eventHandler).toHaveBeenCalled() eventHandler.reset() @@ -1020,9 +1025,9 @@ describe "Editor", -> expect(editor.bufferPositionForScreenPosition(editor.getCursorScreenPosition())).toEqual [3, 60] it "wraps the lines of any newly assigned buffers", -> - otherBuffer = new Buffer - otherBuffer.setText([1..100].join('')) - editor.setBuffer(otherBuffer) + otherEditSession = project.open() + otherEditSession.buffer.setText([1..100].join('')) + editor.edit(otherEditSession) expect(editor.renderedLines.find('.line').length).toBeGreaterThan(1) it "unwraps lines and cancels window resize listener when softwrap is disabled", -> @@ -1316,7 +1321,7 @@ describe "Editor", -> describe "when autoscrolling at the end of the document", -> it "renders lines properly", -> - editor.setBuffer(new Buffer(require.resolve 'fixtures/two-hundred.txt')) + editor.edit(project.open('fixtures/two-hundred.txt')) editor.attachToDom(heightInLines: 5.5) expect(editor.renderedLines.find('.line').length).toBe 8 @@ -1481,7 +1486,9 @@ describe "Editor", -> describe "folding", -> beforeEach -> - editor.setBuffer(new Buffer(require.resolve('fixtures/two-hundred.txt'))) + editSession = project.open('two-hundred.txt') + buffer = editSession.buffer + editor.edit(editSession) editor.attachToDom() describe "when a fold-selection event is triggered", -> diff --git a/spec/app/root-view-spec.coffee b/spec/app/root-view-spec.coffee index 1503e0879..eef34f027 100644 --- a/spec/app/root-view-spec.coffee +++ b/spec/app/root-view-spec.coffee @@ -7,7 +7,6 @@ Editor = require 'editor' describe "RootView", -> rootView = null - project = null path = null beforeEach -> @@ -15,7 +14,6 @@ describe "RootView", -> rootView = new RootView(path) rootView.enableKeymap() rootView.focus() - project = rootView.project describe "initialize(pathToOpen)", -> describe "when called with a pathToOpen", -> @@ -61,14 +59,18 @@ describe "RootView", -> describe "when the serialized RootView has a project", -> beforeEach -> + path = require.resolve 'fixtures' + rootView = new RootView(path) + rootView.open('dir/a') + editor1 = rootView.activeEditor() editor2 = editor1.splitRight() editor3 = editor2.splitRight() editor4 = editor2.splitDown() - editor2.setBuffer(new Buffer(require.resolve 'fixtures/dir/b')) - editor3.setBuffer(new Buffer(require.resolve 'fixtures/sample.js')) + editor2.edit(rootView.project.open('dir/b')) + editor3.edit(rootView.project.open('sample.js')) editor3.setCursorScreenPosition([2, 3]) - editor4.setBuffer(new Buffer(require.resolve 'fixtures/sample.txt')) + editor4.edit(rootView.project.open('sample.txt')) editor4.setCursorScreenPosition([0, 2]) rootView.attachToDom() editor2.focus() @@ -440,14 +442,14 @@ describe "RootView", -> expect(document.title).toBe path editor2 = rootView.activeEditor().splitLeft() - editor2.setBuffer(new Buffer("second.txt")) + editor2.edit(rootView.project.open("second.txt")) expect(pathChangeHandler).toHaveBeenCalled() - expect(document.title).toBe "second.txt" + expect(document.title).toBe rootView.project.resolve("second.txt") pathChangeHandler.reset() editor1.buffer.setPath("should-not-be-title.txt") expect(pathChangeHandler).not.toHaveBeenCalled() - expect(document.title).toBe "second.txt" + expect(document.title).toBe rootView.project.resolve("second.txt") it "creates a project if there isn't one yet and the buffer was previously unsaved", -> rootView = new RootView @@ -475,7 +477,7 @@ describe "RootView", -> rootView.focus() expect(pathChangeHandler).not.toHaveBeenCalled() - editor2.setBuffer editor1.buffer + editor2.edit(editor1.activeEditSession) editor2.focus() expect(pathChangeHandler).not.toHaveBeenCalled() diff --git a/spec/extensions/autocomplete-spec.coffee b/spec/extensions/autocomplete-spec.coffee index 9b77e823f..6b70d24a9 100644 --- a/spec/extensions/autocomplete-spec.coffee +++ b/spec/extensions/autocomplete-spec.coffee @@ -345,13 +345,13 @@ describe "Autocomplete", -> autocomplete.attach() expect(autocomplete.buildWordList).not.toHaveBeenCalled() - describe "when a new buffer is assigned on editor", -> + describe "when a new edit session is assigned on editor", -> it 'creates and uses a new word list based on new buffer', -> wordList = autocomplete.wordList expect(wordList).toContain "quicksort" expect(wordList).not.toContain "Some" - editor.setBuffer new Buffer(require.resolve('fixtures/sample.txt')) + editor.edit(fixturesProject.open('sample.txt')) wordList = autocomplete.wordList expect(wordList).not.toContain "quicksort" @@ -359,7 +359,7 @@ describe "Autocomplete", -> it 'stops listening to previous buffers change events', -> previousBuffer = editor.buffer - editor.setBuffer new Buffer(require.resolve('fixtures/sample.txt')) + editor.edit(fixturesProject.open('sample.txt')) spyOn(autocomplete, "buildWordList") previousBuffer.change([[0,0],[0,1]], "sauron") diff --git a/src/app/editor.coffee b/src/app/editor.coffee index 740e70965..ca6599699 100644 --- a/src/app/editor.coffee +++ b/src/app/editor.coffee @@ -72,7 +72,8 @@ class Editor extends View @editSessions = [] if editSession? - @setActiveEditSession(editSession) + @editSessions.push editSession + @setActiveEditSessionIndex(0) else if @mini editSession = new EditSession softWrapColumn: @calcSoftWrapColumn() @@ -81,7 +82,8 @@ class Editor extends View autoIndent: @autoIndent softTabs: @softTabs - @setActiveEditSession(editSession) + @editSessions.push editSession + @setActiveEditSessionIndex(0) else throw new Error("Editor initialization requires an editSession") @@ -348,37 +350,15 @@ class Editor extends View @trigger 'editor-open', [this] - setBuffer: (buffer) -> - @activateEditSessionForBuffer(buffer) + edit: (editSession) -> + index = @editSessions.indexOf(editSession) - setActiveEditSession: (editSession) -> - index = @editSessionIndexForBuffer(editSession.buffer) - - unless index? + if index == -1 index = @editSessions.length @editSessions.push(editSession) @setActiveEditSessionIndex(index) - activateEditSessionForBuffer: (buffer) -> - index = @editSessionIndexForBuffer(buffer) - unless index? - index = @editSessions.length - @editSessions.push(new EditSession( - softWrapColumn: @calcSoftWrapColumn() - buffer: buffer - tabText: @tabText - autoIndent: @autoIndent - softTabs: @softTabs - )) - - @setActiveEditSessionIndex(index) - - editSessionIndexForBuffer: (buffer) -> - for editSession, index in @editSessions - return index if editSession.buffer == buffer - null - removeActiveEditSession: -> if @editSessions.length == 1 @remove() @@ -411,6 +391,7 @@ class Editor extends View @unsubscribeFromBuffer() if @buffer @buffer = @activeEditSession.buffer @buffer.on "path-change.editor#{@id}", => @trigger 'editor-path-change' + @trigger 'editor-path-change' @renderWhenAttached() diff --git a/src/app/root-view.coffee b/src/app/root-view.coffee index 89e764ae4..e9269c1da 100644 --- a/src/app/root-view.coffee +++ b/src/app/root-view.coffee @@ -94,7 +94,7 @@ class RootView extends View editSession = @project.open(path) if @activeEditor() - @activeEditor().setActiveEditSession(editSession) + @activeEditor().edit(editSession) else editor = new Editor(editSession: editSession) pane = new Pane(editor)