From 28f39cbdaba3af05388664565878ad09096309c7 Mon Sep 17 00:00:00 2001 From: Corey Johnson Date: Tue, 1 May 2012 09:18:12 -0700 Subject: [PATCH] When project's constructor is given a file as its path, it sets its path to the parent directory of the given file --- spec/app/project-spec.coffee | 15 ++++++++++++++- src/app/directory.coffee | 2 ++ src/app/project.coffee | 12 +++++++++++- src/app/root-view.coffee | 5 ++--- 4 files changed, 29 insertions(+), 5 deletions(-) diff --git a/spec/app/project-spec.coffee b/spec/app/project-spec.coffee index 7088bf070..c53b33c70 100644 --- a/spec/app/project-spec.coffee +++ b/spec/app/project-spec.coffee @@ -18,7 +18,7 @@ describe "Project", -> 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') @@ -60,3 +60,16 @@ describe "Project", -> expect(project.relativize(fs.join(absolutePath, "b"))).toBe "b" expect(project.relativize(fs.join(absolutePath, "b/file.coffee"))).toBe "b/file.coffee" expect(project.relativize(fs.join(absolutePath, "file.coffee"))).toBe "file.coffee" + + describe ".setPath(path)", -> + describe "when path is a file", -> + it "sets its path to the files parent directory and updates the root directory", -> + project.setPath(require.resolve('fixtures/dir/a')) + expect(project.getPath()).toEqual require.resolve('fixtures/dir') + expect(project.getRootDirectory().path).toEqual require.resolve('fixtures/dir') + + describe "when path is a directory", -> + it "sets its path to the directory and updates the root directory", -> + project.setPath(require.resolve('fixtures/dir/a-dir')) + expect(project.getPath()).toEqual require.resolve('fixtures/dir/a-dir') + expect(project.getRootDirectory().path).toEqual require.resolve('fixtures/dir/a-dir') diff --git a/src/app/directory.coffee b/src/app/directory.coffee index 15175d934..371337bf3 100644 --- a/src/app/directory.coffee +++ b/src/app/directory.coffee @@ -7,6 +7,8 @@ module.exports = class Directory @idCounter = 0 + path: null + constructor: (@path) -> @id = ++Directory.idCounter diff --git a/src/app/project.coffee b/src/app/project.coffee index 9b2a42318..e54db5516 100644 --- a/src/app/project.coffee +++ b/src/app/project.coffee @@ -6,10 +6,20 @@ Directory = require 'directory' module.exports = class Project + path: null + rootDirectory: null buffers: null - constructor: (@path) -> + constructor: (path) -> + @setPath(path) @buffers = [] + + getPath: -> + @path + + setPath: (path) -> + @path = if fs.isDirectory(path) then path else fs.directory(path) + @rootDirectory.off() if @rootDirectory @rootDirectory = new Directory(@path) getRootDirectory: -> diff --git a/src/app/root-view.coffee b/src/app/root-view.coffee index d3cac10f7..652bba5ff 100644 --- a/src/app/root-view.coffee +++ b/src/app/root-view.coffee @@ -35,14 +35,13 @@ class RootView extends View @setTitle(@project?.path) @on 'active-editor-path-change', (e, path) => - @project.path ?= fs.directory(path) if path + @project.setPath(path) unless @project.getPath() @setTitle(path) - @commandPanel = new CommandPanel({rootView: this}) if pathToOpen? - @project = new Project(fs.directory(pathToOpen)) + @project = new Project(pathToOpen) @open(pathToOpen) if fs.isFile(pathToOpen) else @project = new Project(projectPath)