Project folder path of opened file


#1

Hi,

We can add as many project folders. I am trying to find the project folder root path of the opened file.

Eg :

Hello

  • Nested
  • onefile.js
    World
  • anotherfile.js

Assume that Hello and World are added as two project folders via right click + Add Project Folder . And the opened file is onefile.js .

We can get the directories eg : Hello , World via

atom.project.getDirectories();

I am looking for how we can get the path of the project folder. ie Hello in this case.
I am not actually looking something as

path.resolve(__dirname)

or

path.resolve(__filename)

or

textEditor.getPath()

Is it possible ?

Thank you


#2

Once you have a Directory use Directory::getPath().


#3

Have you tried atom.project.getPaths()?


#4

How do you find which path matches which project?


#5

There’s only one project per window. If you mean which path matches which project root, I believe they’re returned in the same order as displayed in.


#6

Now I’m confused. You can have multiple projects in the tree all in one window, right?


#7

I’m using the term “project” to refer to the object referred to be the atom.project global. There is always one and only one Project. It may have multiple “project roots” (which are the directories that are shown in the tree view) that are directories on disk that each may contain a software development project.

Yes, it does get very confusing.


#8

I thought atom.project was nothing other than an atom software class/object used to provide methods to manage projects. And a real project is a disk directory, usually with a git sub-dir.

There is nothing that ties different root dirs together, is there? Other IDEs often allow a project to include multiple directories but Atom doesn’t. Atom just allows you to open multiple projects in one window.


#9

The Project instance at atom.project does. It keeps track of all the different “root dirs” which we call “project roots”.


#10

That is just on object instance in memory, right? There is no atom command to open up a master project that contains multiple root directories. A window setup is persisted but only temporarily until some other setup is saved.


#11

Atom persists the Project state on exit whether that is single-root or multi-root. It is true that Atom doesn’t have a very obvious way of launching that same state but it does exist. I haven’t verified it lately, but if you launch Atom with atom dir1 dir2 dir3 do some stuff and close it … then launch later with the same command, it should open that same saved state.

See this Issue for a summary on Atom launch modes:


#12

So using the same command line will restore the entire window state with the same open-tabs, etc? So the actual name of the “project” (as you think of it), is the list of dirs, like dir1 dir2 dir3. This is extremely non-intuitive. It should not be documented until a reasonable scheme is implemented. It would be embarrassing (grin).


#13

Hi all,

Thank you for all the comments. Let me clarify the actual problem, so you guys know what I am trying to do.

I was trying to build a hyperclick provider for php . In that I need to pass the root folder so that it can detect some other files from the root.

The code that does is https://github.com/harikt/php-hyperclick/blob/master/lib/main.js#L17-L25 . So via atom api you can get the directory, and iterate through all the directories and path. As @leedohm mentioned you can use atom.project.getPaths() , thank you I wasn’t aware of it.

ref = atom.project.getDirectories();
currentFile = path.dirname(textEditor.getPath());
projectPath = '';
for (i = 0, len = ref.length; i < len; i++) {
  directory = ref[i];
  if (currentFile.indexOf(directory.path) > -1) {
      projectPath = directory.path;
  }
}

This was what I was doing, but the projectPath will be different when we have same directory for project paths.

Lets see the projects added and file opened :

Projects :

/var/www/projects/project1
/var/www/projects/project2
/var/www/projects/project3
/var/www/projects/project4

and the opened file is /var/www/projects/project2/src/Model/Table/ArticleTable.php , the code if (currentFile.indexOf(directory.path) > -1) { some how gave false positive. I may want to look more why so then.

Thank you .


#14

To get the current applicable project root for a given file, I did this in Coffeescript:

# If there is no file, we can't find a project root from it.
if not @buffer?.file?.path
    return

# Get the project root and the relative path to the project.
[projectPath, relativePath] = atom.project.relativizePath(@buffer.file.path)

From what I’m understanding, this may be what you are looking for. The key function is relativizePath which takes a full path and finds the correct root.


#15

Perfect.

This was what I was trying to get.

Thank you.


#16

A Directory object also has a contain method you could use.

filePath = atom.workspace.getActivePaneItem().buffer.file.path;
let projectPath = "";
atom.project.getDirectories().forEach(function(dir){
	if (dir.contains(filePath)) {
		projectPath = dir.path;
	}
});
console.log(projectPath);

#17

New method:

atom.packages.getActivePackage('tree-view').mainModule.provideTreeView().selectedPaths()[0];

Return selected project folder.


#18

This is great, however the getActivePackage method is not available at startup. How does one wait for the tree-view package to load before this request?