Get file from path to have a virtual file

Answered

Hi

Hi would like to have a virtual file from a custom file in my plugin project:

File file = new File("/templates/query.php");
MyPluginProjectComponent.getLogger().info(file.exists() ? "true" : "false");
VirtualFile virtualFile = LocalFileSystem.getInstance().findFileByIoFile(file);

The file is located in resources folder. How to load a file from a path in the plugin?

The objective here, is to have a virtual file or a psi file to write on the disk the query.php file from my action in "NewGroup" group-id

 

Or may be it's better to create a file on disk programmatically with PSI? But I don't find any plugin or simple example to do that.

0
5 comments

See com.intellij.util.ResourceUtil util class

0

Thank you for the reply.

 

I think it will be better if I create a PHP file with PSI.

It's possible to create a virtual file from a PSI file?

For example I would like to create a php file with PSI:

- add an opening php tag

- doc block

- class declaration

- constructor

- ...

 

I can't find any resources for this :/

I check into the PHP plugin, but the plugin create file from a code template and I want to create a file with PSI.

An API exists to create file from PSI?

For example, a PSI to create a constructor, add a whitespace, add a method, properties, ...?

 

 

0

The current code works but in the log file the text is false :/

URL url = ResourceUtil.getResource(getClass().getClassLoader(), "/templates/", "query.php");
System.out.println(url.getFile());
File file = new File(url.getFile());
ZendGeneratorProjectComponent.getLogger().info(file.exists() ? "true" : "false");

file:/home/florent/Code/Java/zend-generator/build/idea-sandbox/plugins/zend-generator/lib/zend-generator-1.0-SNAPSHOT.jar!/templates/query.php

Why it can be loaded? (The path and the file are correct in the jar file)

0

See com.intellij.openapi.vfs.VfsUtil#findFileByURL

1

Yes, it's working. Thank you ;)

 

URL url = ResourceUtil.getResource(getClass(), "templates", "query.php");
VirtualFile virtualFile = VfsUtil.findFileByURL(url);
PsiFile file = PsiManager.getInstance(e.getProject()).findFile(virtualFile);
1

Please sign in to leave a comment.