From 18b9782b166e186557d760274c6fea8139a8b9ed Mon Sep 17 00:00:00 2001 From: Corey Johnson & Nathan Sobo Date: Thu, 5 Jan 2012 16:33:53 -0800 Subject: [PATCH] Opening a previously opened url restores the same buffer and session. --- spec/atom/editor-spec.coffee | 12 ++++++++++++ spec/atom/project-spec.coffee | 12 ++++++++++++ spec/stdlib/fs-spec.coffee | 6 ++---- src/atom/editor.coffee | 7 +++++-- src/atom/project.coffee | 12 +++++++----- 5 files changed, 38 insertions(+), 11 deletions(-) diff --git a/spec/atom/editor-spec.coffee b/spec/atom/editor-spec.coffee index f47649201..9f4f80611 100644 --- a/spec/atom/editor-spec.coffee +++ b/spec/atom/editor-spec.coffee @@ -37,6 +37,18 @@ describe "Editor", -> fileContents = fs.read(filePath) expect(editor.getAceSession().getValue()).toBe fileContents + it "restores the ace edit session for a previously assigned buffer", -> + buffer = new Buffer filePath + editor.setBuffer buffer + + aceSession = editor.getAceSession() + + editor.setBuffer new Buffer(tempFilePath) + expect(editor.getAceSession()).not.toBe(aceSession) + + editor.setBuffer(buffer) + expect(editor.getAceSession()).toBe aceSession + it "sets the language mode based on the file extension", -> buffer = new Buffer "something.js" editor.setBuffer buffer diff --git a/spec/atom/project-spec.coffee b/spec/atom/project-spec.coffee index 72032caa0..9d50de015 100644 --- a/spec/atom/project-spec.coffee +++ b/spec/atom/project-spec.coffee @@ -19,6 +19,11 @@ describe "Project", -> beforeEach -> absolutePath = require.resolve('fixtures/dir/a') + it "always returns the same buffer for the same canonical path", -> + buffer = project.open(absolutePath) + expect(project.open(absolutePath)).toBe buffer + expect(project.open('a')).toBe buffer + describe "when given an absolute path", -> it "returns a buffer for the given path", -> expect(project.open(absolutePath).url).toBe absolutePath @@ -27,3 +32,10 @@ describe "Project", -> it "returns a buffer for the given path (relative to the project root)", -> expect(project.open('a').url).toBe absolutePath + describe ".resolve(path)", -> + it "returns an absolute path based on the project's root", -> + absolutePath = require.resolve('fixtures/dir/a') + expect(project.resolve('a')).toBe absolutePath + expect(project.resolve(absolutePath + '/../a')).toBe absolutePath + expect(project.resolve('a/../a')).toBe absolutePath + diff --git a/spec/stdlib/fs-spec.coffee b/spec/stdlib/fs-spec.coffee index 7c8875b30..1e43de342 100644 --- a/spec/stdlib/fs-spec.coffee +++ b/spec/stdlib/fs-spec.coffee @@ -25,15 +25,13 @@ describe "fs", -> describe "when recursive is true", -> it "returns a promise that resolves to the recursive contents of that directory that are files", -> - waitsFor (complete) -> + waitsForPromise -> fs.async.listFiles(directoryPath, true).done (result) -> expect(result).toEqual (path for path in fs.list(directoryPath, true) when fs.isFile(path)) - complete() describe "when recursive is false", -> it "returns a promise that resolves to the contents of that directory that are files", -> - waitsFor (complete) -> + waitsForPromise -> fs.async.listFiles(directoryPath).done (result) -> expect(result).toEqual (path for path in fs.list(directoryPath) when fs.isFile(path)) - complete() diff --git a/src/atom/editor.coffee b/src/atom/editor.coffee index 13e387560..aafd81c5a 100644 --- a/src/atom/editor.coffee +++ b/src/atom/editor.coffee @@ -14,6 +14,7 @@ class Editor extends Template buffer: null initialize: () -> + @aceSessions = {} @buildAceEditor() @setBuffer(new Buffer) @@ -24,8 +25,10 @@ class Editor extends Template @aceEditor.destroy() setBuffer: (@buffer) -> - session = new EditSession(@buffer.aceDocument, @buffer.getMode()) - @aceEditor.setSession(session) + @aceEditor.setSession @getAceSessionForBuffer(buffer) + + getAceSessionForBuffer: (buffer) -> + @aceSessions[@buffer.url] ?= new EditSession(@buffer.aceDocument, @buffer.getMode()) buildAceEditor: -> @aceEditor = ace.edit this[0] diff --git a/src/atom/project.coffee b/src/atom/project.coffee index ca0369f3d..8e0e134cf 100644 --- a/src/atom/project.coffee +++ b/src/atom/project.coffee @@ -4,17 +4,19 @@ Buffer = require 'buffer' module.exports = class Project + buffers: null + constructor: (@url) -> + @buffers = {} getFilePaths: -> fs.async.listFiles(@url, true) open: (filePath) -> - new Buffer(@resolve(filePath)) + filePath = @resolve filePath + @buffers[filePath] ?= new Buffer(filePath) resolve: (filePath) -> - if filePath[0] == '/' - filePath - else - fs.join(@url, filePath) + filePath = fs.join(@url, filePath) unless filePath[0] == '/' + fs.absolute filePath