Identifying Current Content Root in Module

Hi,

Having figured out how to obtain references to the current project, module, selected file(s), etc, I'm mystified by something that is probably terribly simple. I need to determine in which content entry the user is working. The goal is that if a user selects any number of files or directories in the project pane, I need to determine in which content root each of those selections belong. I imagine I could get the parent of every selection, iterating until I hit a matching content entry, but that seems awfully clunky.

Thanks a lot.

Sebastien

2 comments

Hello Sebastien,

Having figured out how to obtain references to the current project,
module, selected file(s), etc, I'm mystified by something that is
probably terribly simple. I need to determine in which content entry
the user is working. The goal is that if a user selects any number of
files or directories in the project pane, I need to determine in which
content root each of those selections belong. I imagine I could get
the parent of every selection, iterating until I hit a matching
content entry, but that seems awfully clunky.


ProjectRootManager.getInstance(project).getFileIndex().getContentRootForFile(VirtualFile
f)

--
Dmitry Jemerov
Development Lead
JetBrains, Inc.
http://www.jetbrains.com/
"Develop with Pleasure!"


0

Hi Dmitry,

Thanks for the speedy response. I wasn't very accurate in my description because I mixed two things. I actually need to get a reference to the content root's ContentEntry, not necessarily the content root's VirtualFile. So while your suggestion allows me to obtain the right content root for a selected file, I can't find a simpler way to get the content entry other than iterating through the collection that the ModuleRootManager can provide via getContentEntries().

Perhaps I'm missing something. My understanding is that the only way to programmatically modify the source folders, test source folders and exclude folders is to have a reference to the proper ContentEntry. BTW, is there always a one-to-one relationship between the content roots in a module and the content entries?

Thanks,

Sebastien

0

Please sign in to leave a comment.