Add some code to a file
Answered
Hi,
I got a VirtualFile from `VirtualFileManager.getInstance().findFileByUrl` so I can have a PsiFile like this:
val psi: PsiFile? = PsiManager.getInstance(event.project!!).findFile(file!!)
The file looks like this:
<?php
/**
* @package Zend Framework
* @author Rtransat
*/
namespace Rtransat;
return [
'factories' => [
Model\Infrastructure\Api\Adapter::class => Model\Infrastructure\Api\AdapterFactory::class,
]
];
I would like to add a line in the php array in the factories index, but I really don't know how to parse the PsiElement (even with the PsiViewer plugin) to add the line.
Regards.
Post is closed for comments.
Could you please show what you tried or what specific problems you encountered? Maybe http://www.jetbrains.org/intellij/sdk/docs/basics/architectural_overview/modifying_psi.html helps?
The thing is I don't know how to manipulate an entire PsiFile from this full content (the entire file). And the docs don't help me because I don't understand how to get some PsiElement, edit them or add other PsiElement to another.
In this forum or youtube, stackoverflow, blog post I see the psi manipulation but from the carret in the editor not from the file it self.
I've tried to follow this blog post (see findInjectParameterList function) : https://medium.com/@andresdom/developing-an-intellij-webstorm-javascript-plugin-65416f9afea3
I've tried to get the elements like this :
In my case I think I don't need to each every ArrayCreationExpression. I just want to get the factories array (if exists) and add a key => value at the end.
The documentation about PsiFile is too small for me. I think the PsiFile/PsiElement are difficult at first and when we understand how it's works it's more simple but in the meantime I'm stuck.
Could you provide your help Yann? 😁
Please check com.jetbrains.php.lang.psi.PhpPsiElementFactory to create new PHP PSI elements. Then use standward ways of manipulating existing PSI via PsiElement#add/replace to manipulate tree. See also PsiTreeUtil for useful methods to navigate within tree.
Thank you. Do you have just a simple example to show me?
PSI manipulation is always very specific.
See http://www.jetbrains.org/intellij/sdk/docs/products/phpstorm/existing_plugins.html, or check out plugins for PHP with source code from this list: https://plugins.jetbrains.com/search?correctionAllowed=true&orderBy=&pr=&search=php&should_have_source=true
The issue is, I don't know how to get the PsiElement which contains the factories array to use PsiElement.add(newPsiElementKeyValue).
How to get the PHP array?
I found this https://github.com/Haehnchen/idea-php-symfony2-plugin/blob/master/src/main/java/fr/adrienbrault/idea/symfony2plugin/util/PhpElementsUtil.java and https://github.com/Haehnchen/idea-php-oxid-plugin/blob/edb86e6b41e422957cc86ea78293f7427ff1e764/src/de/espend/idea/oxid/utils/MetadataUtil.java but I can not get ArrayCreationExpression element.
I have try something. Here my code:
But I'm stuck with this. I don't know if it's works (in this example I try to add a function just for the test) and even if it's work I don't know how to get the new PsiFile and save the file.
Can you help me please?