How can I enable auto-complete in PhpStorm for a function that takes a string input, where the string represents a file path?
For example, with JavaScript, I define a function like this:
function asset(path: string) {
return `/public/static/${path}`;
};
The goal is to have the path values auto-completed as I type. How can this be achieved?
For instance, in Laravel, we have auto-completion like this:
Can we achieve the same for custom functions?
2
Answers
Refer to this thread: Force autocomplete suggestions for file paths
It seems that what you are asking for is not a standard feature of PHPStorm. The answer of the given thread also discusses a manual solution, but that only lasts until the file or project is closed.
You can make such an injection by utilizing some intermediate language (where the file reference works)… but it will have some limitations and may not work exactly as you want:
Worth checking it out anyway.
Here is what can be achieved right now:
Settings/Preferences | Editor | Language Injections
Generic Js
kindHTML
language with an additionalPrefix
andSuffix
, This way the IDE would think that we are in that HTML attribute where injection already works (you can try another TAG/attribute that may work better, here I have used<a>
andhref
).+ jsArgument("assetMe", 0)
:assetMe
is the function name where to inject it0
in the parameter number (first one in this case)This how it looks and works (tested in a plain JavaScript file):
Sample test code:
This is how it works here in my simple test (lists files from the same folder as I do not have any folders marked as Resource Root):