How do I get current Project instance having VirtualFile instance only?

I'm trying to detect FileType for my VirtualFile using FileTypeIdentifiableByVirtualFile#isMyFileType method in my FileType subclass. But this knowledge is hidden in one of my project's components, thus I need Project instance. So, how do I get Project instance when I have nothing but VirtualFile argument?

9 comments
Comment actions Permalink

There is no way since same VirtualFile file can be opened in several
projects

Max Ishchenko wrote:

I'm trying to detect FileType for my VirtualFile using FileTypeIdentifiableByVirtualFile#isMyFileType method in my FileType subclass. But this knowledge is hidden in one of my project's components, thus I need Project instance. So, how do I get Project instance when I have nothing but VirtualFile ar
gument?

---
Original message URL: http://www.jetbrains.net/devnet/message/5242236#5242236



--
Best regards,
Maxim Mossienko
IntelliJ Labs / JetBrains Inc.
http://www.intellij.com
"Develop with pleasure!"

0
Comment actions Permalink

Ok, VirtualFile is no help for me. Can I get information about the project inside FileTypeIdentifiableByVirtualFile#isMyFileType call the other way? Per project FileType instantiation looks like a bad idea.

0
Comment actions Permalink

Hello Max,

Ok, VirtualFile is no help for me. Can I get information about the
project inside FileTypeIdentifiableByVirtualFile#isMyFileType call the
other way? Per project FileType instantiation looks like a bad idea.


File types are an application-level concept; you can't have a file which
has different file types in different projects. So no, you cannot and must
not use any project-specific information in that method.

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


0
Comment actions Permalink

Ok, now I get that standard FileType approach won't work for me.
My problem is that my language files have *.conf extension but I don't want all *.conf files to be treated as written in my language. The set of files to be treated as written in my language is chosen in project settings. What kind of approach would work for me here?

0
Comment actions Permalink

Hello Max,

One option is to store a persistent attribute on the VirtualFiles selected
in the project settings, and to use this attribute when determining file
type by virtual file.

Ok, now I get that standard FileType approach won't work for me.

My problem is that my language files have *.conf extension but I don't
want all *.conf files to be treated as written in my language. The set
of files to be treated as written in my language is chosen in project
settings. What kind of approach would work for me here?


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


0
Comment actions Permalink

What do you mean by storing a persistent attribute on a VirtualFile?
I could use putUserData/getUserData to mark my VirtualFiles, but the problem is that VirtualFile instance is shared between opened projects. And if I have two different projects opened, I will effectively get files marked in project A as marked for project B and vice versa. This effect is not a showstopper, but not very clean approach, indeed. It can be notable if marked files are located outside project roots.
Or maybe you meant something different by storing a persistent attribute?

0
Comment actions Permalink

Hello Max,

No, I don't mean getUserData/putUserData; I mean using the FileAttribute
class to add attributes which will be stored in the VFS on disk. Please see
PerforceCachingContentRevision class in the Perforce plugin sources for a
usage example.

Files will be marked for all projects, but as I've said there's no way
around it; the file type can't be different in different projects.

What do you mean by storing a persistent attribute on a VirtualFile?

I could use putUserData/getUserData to mark my VirtualFiles, but the
problem is that VirtualFile instance is shared between opened
projects. And if I have two different projects opened, I will
effectively get files marked in project A as marked for project B and
vice versa. This effect is not a show

stopper, but not very clean approach, indeed. It can be notable if
marked files are located outside project roots.

Or maybe you meant something different by storing a persistent
attribute?


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


1
Comment actions Permalink

Well, I successfully implemented VirtualFile.getUserData/putUserData approach. Can't  it break under certain conditions? Can't I suddenly get a different VirtualFile instance for the same file on disk?
I didn't use FileAttribute approach because I need no attributes persistence. I mark files as belonging to my type on application startup.

0
Comment actions Permalink

Hello Max,

Well, I successfully implemented VirtualFile.getUserData/putUserData
approach. Can't it break under certain conditions? Can't I suddenly
get a different VirtualFile instance for the same file on disk?


You could get a different instance if a file was deleted and recreated. Otherwise
the same instance will be used.

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


0

Please sign in to leave a comment.