Complete hashes in named arguments and lists, and the variants of cva()

Open
#133 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
55/100
Issue type
Feature
Clarity
Clearly specified
Activity status
Active
Tech stack
php, symfony

Research direction

Start by reading the implementation and tests for issue #108, which handles completion from a Twig function parameter's array shape. Trace how PHP parameter types and @psalm-type or @phpstan-type aliases are resolved, then follow the completion path for hash and sequence literals. Add coverage for named arguments, collection value types, and cva() variant suggestions; done means the listed completion cases work for general functions and the cva() variants.

Written by the indexing model from the issue text.

Description

Hi 👋🏻

This follows #108, which completes the hash of a Twig function argument from the array shape of its PHP parameter. symfony/ux-css now has a cva() Twig function (symfony/ux#4243) that needs a bit more: like html_cva() of twig/html-extra, it takes four named arguments.

Here is its PHP method:

/**
 * @param CssStyles                                  $base
 * @param array<string, array<array-key, CssStyles>> $variants
 * @param list<array{css?: CssStyles}>               $compoundVariants
 * @param array<string, string|int|float|bool>       $defaultVariants
 */
public function cva(array $base = [], array $variants = [], array $compoundVariants = [], array $defaultVariants = []): Cva

And here is how it is used in Twig:

{% set button = cva(
    base: { display: 'inline-flex', rounded: 'md' },
    variants: {
        variant: {
            primary: { bg: 'blue.600', color: 'white' },
            ghost: { bg: 'transparent', _hover: { bg: 'gray.100' } },
        },
        size: {
            sm: { h: '8', px: '3' },
            lg: { h: '12', px: '6' },
        },
    },
    compoundVariants: [
        { variant: 'ghost', size: 'sm', css: { px: '2' } },
    ],
    defaultVariants: { variant: 'primary', size: 'sm' },
) %}

These are the completions I would expect (▌ is the cursor):

Cursor Expected items
base: { ▌ } CSS properties: display, alignItems...
base: { color: '▌' } color tokens: blue.600, gray.700...
variants: { variant: { primary: { ▌ } } } CSS properties
compoundVariants: [{ ▌ }] variant, size, css
compoundVariants: [{ variant: '▌' }] primary, ghost
compoundVariants: [{ variant: ['▌'] }] primary, ghost
compoundVariants: [{ css: { ▌ } }] CSS properties
defaultVariants: { ▌ } variant, size
defaultVariants: { size: '▌' } sm, lg

This needs four rules:

  1. A hash passed as a named argument, fn(name: { ... }), completes from the type of the PHP parameter of that name.
  2. Collection types array<K, V>, list<V> and V[] are followed to their value type, also through @psalm-type and @phpstan-type aliases.
  3. In a sequence literal [{ ... }], each item completes from the value type of the list.
  4. When the PHP callable has variants, compoundVariants and defaultVariants parameters (or defaultVariant, for html_cva()), the default variants and each compound variant also complete the variant names and the values declared in the variants argument of the same call.

Rules 1 to 3 apply to every Twig function, only rule 4 is specific to cva().

The PhpStorm plugin does the same in Haehnchen/idea-php-symfony2-plugin#2910.

Thanks!

Dominant language
PHP
Stars
171
Forks
5
PR merge metrics
No merged PRs in 30d

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from symfony/language-tools

All issues in symfony/language-tools

Similar issues

More PHP issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.