Reputation: 321
I'm trying to add a static method to my TestHelper class (see GIF). I want vscode to give me an option "create static method test in TestHelper". I used IntelliJ before and it was working there.
Does VSCode really don't have this feature? The only plugins I have installed is "PHP Intelliphense" and "Settings Sync"
Thanks for your help.
Upvotes: 0
Views: 1178
Reputation: 28753
With the extension My Code Actions v0.5.0 you can construct actions to add this method to the class if the file name equals the class name.
You can modify any property based on the captured fields, and add static text.
I have added a version that does not need a diagnostic (I don't have a PHP setup) but only uses the content of the editor at the cursor.
If you also need to create the file and the class have a look at the extension page for an example (that is for Angular).
Add this to your settings.json
"my-code-actions.actions": {
"[php]": {
"Add method {{diag:$1}} to {{atCursor:$1}}": {
"diagnostics": ["Undefined method '(.*?)'."],
"atCursor": "([_a-zA-Z0-9]+)::{{diag:$1}}",
"text": "public function {{diag:$1}}() { }\n",
"file": "{{atCursor:$1}}.php",
"where": "afterLast",
"insertFind": "class {{atCursor:$1}} {"
},
"No diag: Add method {{atCursor:$2}} to {{atCursor:$1}}": {
"atCursor": "([_a-zA-Z0-9]+)::([_a-zA-Z0-9]+)",
"text": "public function {{atCursor:$2}}() { }\n",
"file": "{{atCursor:$1}}.php",
"where": "afterLast",
"insertFind": "class {{atCursor:$1}} {"
}
}
}
Upvotes: 1