ide.json: Completion


Overview

Laravel Idea allows to add its string completions to other method parameters.

Example ide.json:

{
    "$schema": "https://laravel-ide.com/schema/laravel-ide-v2.json",
    "completions": [
        {
            "complete": "gate",
            "condition": [
                {
                    "methodNames": ["allows", "denies"],
                    "classFqn": ["\\YourPackage\\SomeGateClass"],
                    "parameters": [1]
                },
                {
                    "functionNames": ["gateAllows"]
                },
                {
                    "newClassFqn": ["\\YourPackage\\GateRule"],
                    "parameters": [2]
                }
            ]
        }
    ]
}

If Laravel Idea meets this ide.json file in the project it will complete gates and policies for:

  • the first parameter of allows and denies method of \YourPackage\SomeGateClass class. Both static and dynamic calls.
  • all parameters of gateAllows function
  • the second parameter of new \YourPackage\GateRule() calls

Available completions

"complete" value Completion
routeName Route name.
routeAction Route action. As for action() function.
routeMiddleware Route middleware.
configKey Config key. Might be filtered by prefix. Details
viewName View name.
translationKey Localization key.
validationRule One validation rule in the string.
validationRules One or more validation rules.
staticStrings Prepared set of completions. Details
gate Gate or policy. Aliases: "policy", "authRule"
assetPath Asset file path. As for asset() function.
moduleName Module name. If application has any.
directoryFiles File or directory in some directory. As for public_path() function. Details
artisanCommandName Artisan command name. "route:list" and all others.
artisanCommandParameter Parameters for artisan command name given in the previous parameter.
inertiaPage Inertia page.
livewireComponent Livewire component name.
classFields Fields of callee class. Details
cssClassName One CSS class.
cssClassNames Several CSS classes delimited by space.
environmentVariable Environment variable. As for 'env()' function.

configKey completion

By default, it completes all config keys, but you can filter them by prefix.

{
    "$schema": "https://laravel-ide.com/schema/laravel-ide-v2.json",
    "completions": [
        {
            "complete": "configKey",
            "options": {
                "prefix": "database.connections"
            },
            "condition": [
                {
                    "functionNames": ["connection"],
                    "parameters": [1]
                }
            ]
        }
    ]
}

The first parameter of connection function calls will be completed by "connections" array keys: "mysql", "sqlite", etc.

staticStrings completion

This completion just completes a specified set of strings.

{
    "$schema": "https://laravel-ide.com/schema/laravel-ide-v2.json",
    "completions": [
        {
            "complete": "staticStrings",
            "options": {
                "strings": ["longitude", "latitude"]
            },
            "condition": [
                {
                    "newClassNames": ["Axis"],
                    "parameters": [1]
                }
            ]
        }
    ]
}

"longitude", "latitude" will be completed for new \SomeNamespace\Axis('here') call.

directoryFiles completion

Laravel Idea can complete directories and files from some directory. Let's say user has resources/docs directory in the root project folder with these files and directories:

  • index.md
  • img.png
  • overview.md
  • ide_json
  • ide_json/overview.md
  • ide_json/completion.md
{
    "$schema": "https://laravel-ide.com/schema/laravel-ide-v2.json",
    "completions": [
        {
            "complete": "directoryFiles",
            "options": {
                "directory": "/resources/docs"
            },
            "condition": [
                {
                    "functionNames": ["allFiles"],
                    "parameters": [1]
                }
            ]
        },
        {
            "complete": "directoryFiles",
            "options": {
                "directory": "/resources/docs",
                "suffixToClear": ".md"
            },
            "condition": [
                {
                    "functionNames": ["mdFiles"],
                    "parameters": [1]
                }
            ]
        }
    ]
}

Completions for allFiles function first parameter:

  • index.md
  • img.png
  • overview.md
  • ide_json
  • ide_json/overview.md
  • ide_json/completion.md

Completions for mdFiles function first parameter:

  • index
  • overview
  • ide_json/overview
  • ide_json/completion

"directory" parameter is required. Path is calculated as relative to the ide.json file location, but also can be absolute from project root, if started by "/". Examples:

  • "/resources/views" - "resources/views" folder in the Laravel project root(usually PhpStorm project root).
  • "resources/views" - "resources/views" folder in the folder containing ide.json file.
  • "../../resources/views" - "resources/views" folder in the folder 2 level up from the folder containing ide.json file.

"suffixToClear" parameter is optional. If exists Laravel Idea will filter files with this suffix and clear it for completion

If you have multiple directories to complete, "directories" option can be used instead of "directory":

{
    "$schema": "https://laravel-ide.com/schema/laravel-ide-v2.json",
    "completions": [
        {
            "complete": "directoryFiles",
            "options": {
                "directories": ["/resources/docs", "/resources/docs2"]
            },
            "condition": [
                {
                    "functionNames": ["multipleFolders"],
                    "parameters": [1]
                }
            ]
        }
    ]
}

Laravel Idea scans folders recursively by default. It can be changed by "recursive" option.

{
    "$schema": "https://laravel-ide.com/schema/laravel-ide-v2.json",
    "completions": [
        {
            "complete": "directoryFiles",
            "options": {
                "directory": "/resources/docs",
                "recursive": false
            },
            "condition": [
                {
                    "functionNames": ["nonRecursiveFolders"],
                    "parameters": [1]
                }
            ]
        }
    ]
}

classFields completion

Laravel Idea can complete fields from the class. Example PR - https://github.com/spatie/laravel-data/pull/182

Conditions

Conditions help Laravel Idea to understand where it should complete. Example:

{
    "$schema": "https://laravel-ide.com/schema/laravel-ide-v2.json",
    "completions": [
        {
            "complete": "routeName",
            "condition": [
                {
                    "functionNames": ["allFiles"],
                    "parameters": [1]
                },
                {
                    "place": "arrayKey",
                    "methodNames": ["method"],
                    "classFqn": ["\\Namespace\\Class"]
                }
            ]
        }
    ]
}

Route names will be completed for the first parameter of allFiles function and for the array keys of all parameters \\Namespace\\Class::method calls for both static and dynamic variants.

allFiles('<route names completion>');
allFiles('', '<no completion>');
allFiles(['<no completion>']);

\\Namespace\\Class::method('<no completion>');
\\Namespace\\Class::method(['<route names completion>']);
\\Namespace\\Class::method(['<route names completion>' => 1]);
$object = new \\Namespace\\Class(); 
$object->method(1, 2, 3, ['<route names completion>']);

Conditions: place

place parameter describes the place where strings should be placed to be completed. Default value: "parameter".

parameter place

Completion for strings right in the parameters.

{
    "$schema": "https://laravel-ide.com/schema/laravel-ide-v2.json",
    "completions": [
        {
            "complete": "routeName",
            "condition": [
                {
                    "place": "parameter",
                    "functionNames": ["test"]
                }
            ]
        }
    ]
}
test('<route names completion>');
test(['<no completion>']);

arrayKey place

Completion for string keys in the parameter arrays.

{
    "$schema": "https://laravel-ide.com/schema/laravel-ide-v2.json",
    "completions": [
        {
            "complete": "routeName",
            "condition": [
                {
                    "place": "arrayKey",
                    "functionNames": ["test"]
                }
            ]
        }
    ]
}
test('<no completion>');
test(['<route names completion and converts it to key>']);
test(['<route names completion>' => '']);

arrayOnlyKey place

The same as arrayKey but completes only key, not value.

{
    "$schema": "https://laravel-ide.com/schema/laravel-ide-v2.json",
    "completions": [
        {
            "complete": "routeName",
            "condition": [
                {
                    "place": "arrayKey",
                    "functionNames": ["test"]
                }
            ]
        }
    ]
}
test('<no completion>');
test(['<no completion>']);
test(['<route names completion>' => '']);

arrayValue place

Completion for string values in the parameter arrays.

{
    "$schema": "https://laravel-ide.com/schema/laravel-ide-v2.json",
    "completions": [
        {
            "complete": "routeName",
            "condition": [
                {
                    "place": "arrayValue",
                    "functionNames": ["test"]
                }
            ]
        }
    ]
}
test('<no completion>');
test(['<route names completion>']);
test(['<no completion>' => '']);

arrayValueWithKey place

Completion for string values for some (or all) keys. "keys" parameter wll be used to filter needed keys. Completion will be for all keys if "keys" is omitted.

{
    "$schema": "https://laravel-ide.com/schema/laravel-ide-v2.json",
    "completions": [
        {
            "complete": "routeName",
            "condition": [
                {
                    "place": "arrayValueWithKey",
                    "keys": ["route", "routeName"],
                    "functionNames": ["test"]
                }
            ]
        }
    ]
}
test('<no completion>');
test(['route' => '<route names completion>']);
test(['routeName' => '<route names completion>']);
test(['anotherKey' => '<no completion>']);

arrayValueWithKeyArray place

The same as arrayValueWithKey but for arrays.

{
    "$schema": "https://laravel-ide.com/schema/laravel-ide-v2.json",
    "completions": [
        {
            "complete": "routeName",
            "condition": [
                {
                    "place": "arrayValueWithKeyArray",
                    "keys": ["route", "routeName"],
                    "functionNames": ["test"]
                }
            ]
        }
    ]
}
test('<no completion>');
test(['route' => '<no completion>']);
test(['route' => ['<route names completion>']]);
test(['routeName' => [1, 2, '<route names completion>']]);
test(['anotherKey' => ['<no completion>']]);

arrayInArrayKey place

The same as arrayKey but for second level arrays.

{
    "$schema": "https://laravel-ide.com/schema/laravel-ide-v2.json",
    "completions": [
        {
            "complete": "routeName",
            "condition": [
                {
                    "place": "arrayInArrayKey",
                    "functionNames": ["test"]
                }
            ]
        }
    ]
}
test('<no completion>');
test(['<no completion>']);
test([['key' => 'value'], ['<route names completion and converts it to key>']]);
test([['<route names completion>' => '']]);

arrayInArrayOnlyKey place

The same as arrayOnlyKey but for second level arrays.

{
    "$schema": "https://laravel-ide.com/schema/laravel-ide-v2.json",
    "completions": [
        {
            "complete": "routeName",
            "condition": [
                {
                    "place": "arrayInArrayOnlyKey",
                    "functionNames": ["test"]
                }
            ]
        }
    ]
}
test('<no completion>');
test(['<no completion>']);
test([['<no completion>']]);
test([['key' => 'value'], ['<route names completion>' => '']]);

arrayInArrayValue place

The same as arrayValue but for second level arrays.

{
    "$schema": "https://laravel-ide.com/schema/laravel-ide-v2.json",
    "completions": [
        {
            "complete": "routeName",
            "condition": [
                {
                    "place": "arrayInArrayValue",
                    "functionNames": ["test"]
                }
            ]
        }
    ]
}
test('<no completion>');
test(['<no completion>']);
test([['<route names completion>']]);
test([['<no completion>' => '']]);

Conditions: functions and methods

Completions should be declared for function or method calls. It's mandatory to declare at least one filter: for functions, for methods or for new Class() calls.

Functions

Completions for functions is the simplest one.

{
    "$schema": "https://laravel-ide.com/schema/laravel-ide-v2.json",
    "completions": [
        {
            "complete": "validationRule",
            "condition": [
                {
                    "functionNames": ["applyRules"]
                },
                {
                    "place": "arrayValue",
                    "functionNames": ["applyRules"],
                    "parameters": [1]
                }
            ]
        }
    ]
}

Completion conditions can be combined as here. So, validation rules will be completed for all string parameters of applyRules function and for array values of the first parameter.

"parameters" value declares for which parameters completion will work. The first parameter's index is 1 (not 0). If omitted, completion will work for all parameters.

anotherFunction('<no completion>');
applyRules('<validation rules completion>');
applyRules('required', '<validation rules completion>');
applyRules(['<validation rules completion>']);
applyRules(['required', '<validation rules completion>']);
applyRules([], ['<no completion>']);

Methods

If you want to complete something for method parameters you should fill methodNames array and at least 1 of these parameters: classFqn, classNames, classParentFqn.

methodNames array contains all method names which should be completed.

  • classFqn array contains Full qualified names(FQN) for classes, which methods should be completed.
  • classNames array can be used if you for some reason decided to use only name of classes instead of FQN. It's not recommended.
  • classParentFqn array can be used if you can't provide exact class names for methods, but all these classes have common parent class or implement common interface. It takes more resources to calculate, so better to use classFqn if possible.

Completion will work for both static and dynamic calls.

{
    "$schema": "https://laravel-ide.com/schema/laravel-ide-v2.json",
    "completions": [
        {
            "complete": "validationRule",
            "condition": [
                {
                    "methodNames": ["rule"],
                    "parameters": [1],
                    "classFqn": ["\\PackageNamespace\\Validation"]
                },
                {
                    "methodNames": ["rules"],
                    "classParentFqn": ["\\PackageNamespace\\BaseValidation"]
                }
            ]
        }
    ]
}
$validation = \\PackageNamespace\\Validation();
$validation->rule('<validation rules completion>');
\\PackageNamespace\\Validation::rule('<validation rules completion>');

SomeClassInheritedFromBaseValidation::rules('required', '<validation rules completion>');

"new Class()" calls

A bit different configuration is needed to complete constructor parameters. newClassFqn, newClassNames and newClassParentFqn parameters have the same difference as "classFqn" and others from previous part. "parameters" parameter logic is also the same.

{
    "$schema": "https://laravel-ide.com/schema/laravel-ide-v2.json",
    "completions": [
        {
            "complete": "validationRule",
            "condition": [
                {
                    "parameters": [1],
                    "newClassFqn": ["\\PackageNamespace\\Validation"]
                },
                {
                    "newClassParentFqn": ["\\PackageNamespace\\BaseValidation"]
                }
            ]
        }
    ]
}
new \\PackageNamespace\\Validation('<validation rules completion>');

new SomeClassInheritedFromBaseValidation('required', '<validation rules completion>');