Merge pull request #5641 from atom/mb-project-remove-path

Add Project::removePath
This commit is contained in:
Max Brunsfeld
2015-02-19 10:43:39 -08:00
2 changed files with 51 additions and 0 deletions

View File

@@ -330,6 +330,34 @@ describe "Project", ->
expect(atom.project.getPaths()).toEqual([oldPath])
expect(onDidChangePathsSpy).not.toHaveBeenCalled()
describe ".removePath(path)", ->
onDidChangePathsSpy = null
beforeEach ->
onDidChangePathsSpy = jasmine.createSpy('onDidChangePaths listener')
atom.project.onDidChangePaths(onDidChangePathsSpy)
it "removes the directory and repository for the path", ->
result = atom.project.removePath(atom.project.getPaths()[0])
expect(atom.project.getDirectories()).toEqual([])
expect(atom.project.getRepositories()).toEqual([])
expect(atom.project.getPaths()).toEqual([])
expect(result).toBe true
expect(onDidChangePathsSpy).toHaveBeenCalled()
it "does nothing if the path is not one of the project's root paths", ->
originalPaths = atom.project.getPaths()
result = atom.project.removePath(originalPaths[0] + "xyz")
expect(result).toBe false
expect(atom.project.getPaths()).toEqual(originalPaths)
expect(onDidChangePathsSpy).not.toHaveBeenCalled()
it "doesn't destroy the repository if it is shared by another root directory", ->
atom.project.setPaths([__dirname, path.join(__dirname, "..", "src")])
atom.project.removePath(__dirname)
expect(atom.project.getPaths()).toEqual([path.join(__dirname, "..", "src")])
expect(atom.project.getRepositories()[0].isSubmodule("src")).toBe false
describe ".relativize(path)", ->
it "returns the path, relative to whichever root directory it is inside of", ->
atom.project.addPath(temp.mkdirSync("another-path"))

View File

@@ -209,6 +209,29 @@ class Project extends Model
@emit "path-changed"
@emitter.emit 'did-change-paths', @getPaths()
# Public: remove a path from the project's list of root paths.
#
# * `projectPath` {String} The path to remove.
removePath: (projectPath) ->
projectPath = path.normalize(projectPath)
indexToRemove = null
for directory, i in @rootDirectories
if directory.getPath() is projectPath
indexToRemove = i
break
if indexToRemove?
[removedDirectory] = @rootDirectories.splice(indexToRemove, 1)
[removedRepository] = @repositories.splice(indexToRemove, 1)
removedDirectory.off()
removedRepository?.destroy() unless removedRepository in @repositories
@emit "path-changed"
@emitter.emit "did-change-paths", @getPaths()
true
else
false
# Public: Get an {Array} of {Directory}s associated with this project.
getDirectories: ->
@rootDirectories