diff --git a/spec/app/project-spec.coffee b/spec/app/project-spec.coffee index 925e6a1ab..a5eababb1 100644 --- a/spec/app/project-spec.coffee +++ b/spec/app/project-spec.coffee @@ -6,6 +6,39 @@ describe "Project", -> beforeEach -> project = new Project(require.resolve('fixtures/dir')) + describe ".open(path)", -> + [absolutePath, newBufferHandler] = [] + beforeEach -> + absolutePath = require.resolve('fixtures/dir/a') + newBufferHandler = jasmine.createSpy('newBufferHandler') + 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 + + 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 + + describe "when passed the path to a buffer that has already been opened", -> + it "returns the previously opened buffer", -> + buffer = project.open(absolutePath) + newBufferHandler.reset() + expect(project.open(absolutePath)).toBe buffer + expect(project.open('a')).toBe 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) + describe ".getFilePaths()", -> it "returns a promise which resolves to a list of all file paths in the project, recursively", -> expectedPaths = (path.replace(project.path, '') for path in fs.listTree(project.path) when fs.isFile path) @@ -14,28 +47,9 @@ describe "Project", -> project.getFilePaths().done (result) -> expect(result).toEqual(expectedPaths) - describe ".open(path)", -> - absolutePath = null - 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).path).toBe absolutePath - - describe "when given a relative path", -> - it "returns a buffer for the given path (relative to the project root)", -> - expect(project.open('a').path).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/app/root-view-spec.coffee b/spec/app/root-view-spec.coffee index d67be0d70..2912b5a8d 100644 --- a/spec/app/root-view-spec.coffee +++ b/spec/app/root-view-spec.coffee @@ -396,7 +396,7 @@ describe "RootView", -> it "creates a project if there isn't one yet and the buffer was previously unsaved", -> rootView = new RootView - expect(rootView.project).toBeUndefined() + expect(rootView.project.path).toBeUndefined() rootView.activeEditor().buffer.saveAs('/tmp/ignore-me') expect(rootView.project.path).toBe '/tmp/' diff --git a/src/app/project.coffee b/src/app/project.coffee index dc07f1bae..b6791a0f1 100644 --- a/src/app/project.coffee +++ b/src/app/project.coffee @@ -1,8 +1,9 @@ fs = require 'fs' Buffer = require 'buffer' +_ = require 'underscore' +EventEmitter = require 'event-emitter' module.exports = - class Project buffers: null @@ -15,10 +16,19 @@ class Project path.replace(projectPath, "") for path in paths when fs.isFile(path) open: (filePath) -> - filePath = @resolve filePath - @buffers[filePath] ?= new Buffer(filePath) + if filePath? + filePath = @resolve(filePath) + buffer = @buffers[filePath] + unless buffer + @buffers[filePath] = buffer = new Buffer(filePath) + @trigger 'new-buffer', buffer + else + buffer = new Buffer + @trigger 'new-buffer', buffer + buffer resolve: (filePath) -> filePath = fs.join(@path, filePath) unless filePath[0] == '/' fs.absolute filePath +_.extend Project.prototype, EventEmitter diff --git a/src/app/root-view.coffee b/src/app/root-view.coffee index cbfae719c..849e5cb29 100644 --- a/src/app/root-view.coffee +++ b/src/app/root-view.coffee @@ -42,6 +42,7 @@ class RootView extends View @project = new Project(fs.directory(pathToOpen)) @open(pathToOpen) if fs.isFile(pathToOpen) else if not panesViewState? + @project = new Project @open() @deserializePanes(panesViewState) if panesViewState @@ -65,7 +66,7 @@ class RootView extends View when 'Editor' then Editor.deserialize(viewState) open: (path) -> - buffer = if path then @project.open(path) else new Buffer + buffer = @project.open(path) if @activeEditor() @activeEditor().setBuffer(buffer) @@ -86,7 +87,7 @@ class RootView extends View .on 'buffer-path-change.root-view', => path = editor.buffer.path @setTitle(path) - @project ?= new Project(fs.directory(path)) if path + @project.path ?= fs.directory(path) if path @setTitle(editor.buffer.path) @@ -108,7 +109,7 @@ class RootView extends View rootPane?.adjustDimensions() toggleFileFinder: -> - return unless @project + return unless @project.path? if @fileFinder and @fileFinder.parent()[0] @fileFinder.remove()