mirror of
https://github.com/atom/atom.git
synced 2026-04-06 03:02:13 -04:00
Merge pull request #5753 from atom/mb-project-split-path
Add API for getting paths relative to project dirs
This commit is contained in:
@@ -438,6 +438,23 @@ describe "Project", ->
|
||||
randomPath = path.join("some", "random", "path")
|
||||
expect(atom.project.relativize(randomPath)).toBe randomPath
|
||||
|
||||
describe ".relativizePath(path)", ->
|
||||
it "returns the root path that contains the given path, and the path relativized to that root path", ->
|
||||
atom.project.addPath(temp.mkdirSync("another-path"))
|
||||
|
||||
rootPath = atom.project.getPaths()[0]
|
||||
childPath = path.join(rootPath, "some", "child", "directory")
|
||||
expect(atom.project.relativizePath(childPath)).toEqual [rootPath, path.join("some", "child", "directory")]
|
||||
|
||||
rootPath = atom.project.getPaths()[1]
|
||||
childPath = path.join(rootPath, "some", "child", "directory")
|
||||
expect(atom.project.relativizePath(childPath)).toEqual [rootPath, path.join("some", "child", "directory")]
|
||||
|
||||
describe "when the given path isn't inside of any of the project's path", ->
|
||||
it "returns null for the root path, and the given path unchanged", ->
|
||||
randomPath = path.join("some", "random", "path")
|
||||
expect(atom.project.relativizePath(randomPath)).toEqual [null, randomPath]
|
||||
|
||||
describe ".contains(path)", ->
|
||||
it "returns whether or not the given path is in one of the root directories", ->
|
||||
rootPath = atom.project.getPaths()[0]
|
||||
|
||||
@@ -266,15 +266,25 @@ class Project extends Model
|
||||
else
|
||||
undefined
|
||||
|
||||
# Public: Make the given path relative to the project directory.
|
||||
#
|
||||
# * `fullPath` {String} full path
|
||||
relativize: (fullPath) ->
|
||||
@relativizePath(fullPath)[1]
|
||||
|
||||
# Public: Get the path to the project directory that contains the given path,
|
||||
# and the relative path from that project directory to the given path.
|
||||
#
|
||||
# * `fullPath` {String} An absolute path.
|
||||
#
|
||||
# Returns an {Array} with two elements:
|
||||
# * `projectPath` The {String} path to the project directory that contains the
|
||||
# given path, or `null` if none is found.
|
||||
# * `relativePath` {String} The relative path from the project directory to
|
||||
# the given path.
|
||||
relativizePath: (fullPath) ->
|
||||
return fullPath if fullPath?.match(/[A-Za-z0-9+-.]+:\/\//) # leave path alone if it has a scheme
|
||||
for rootDirectory in @rootDirectories
|
||||
relativePath = rootDirectory.relativize(fullPath)
|
||||
return relativePath if relativePath isnt fullPath
|
||||
fullPath
|
||||
return [rootDirectory.getPath(), relativePath] unless relativePath is fullPath
|
||||
[null, fullPath]
|
||||
|
||||
# Public: Determines whether the given path (real or symbolic) is inside the
|
||||
# project's directory.
|
||||
|
||||
Reference in New Issue
Block a user