skip to Main Content

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:
Screenshot from php storm auto complete for asset helper function

Can we achieve the same for custom functions?

2

Answers


  1. 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.

    Login or Signup to reply.
  2. 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:

    • The function name better be unique
    • the path will be relative to the folders marked as "Resource Root" (or the current folder / project root if none like that found).

    Worth checking it out anyway.


    Here is what can be achieved right now:

    1. Settings/Preferences | Editor | Language Injections
    2. Make a new rule of Generic Js kind
    3. Inject HTML language with an additional Prefix and Suffix, 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> and href).
    4. The pattern to use: + jsArgument("assetMe", 0):
      • assetMe is the function name where to inject it
      • 0 in the parameter number (first one in this case)

    This how it looks and works (tested in a plain JavaScript file):

    enter image description here

    Sample test code:

    function assetMe(path: string) {
        return `/public/static/${path}`;
    }
    
    assetMe('');
    

    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):

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search