Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Indentation in hash parameters #243

Closed
lukepass opened this issue Jul 2, 2024 · 5 comments
Closed

Indentation in hash parameters #243

lukepass opened this issue Jul 2, 2024 · 5 comments
Labels

Comments

@lukepass
Copy link

lukepass commented Jul 2, 2024

Expected behavior

I wish that hash arguments were indented when on multiple lines. For example:

From:

{{ form_start(form, {
                    attr: {
                    class: 'quiz',
                    'data-controller': 'ajax-form quiz',
                    'data-ajax-form-targets-value': ['#ajax-content-1', '#ajax-content-2', '#lesson-footer']|serialize('json') ,
    'data-ajax-form-spinner-html-value': include('shared/_spinner.html.twig'),
    'data-ajax-form-scroll-to-value': '#ajax-content-1',
}
}) }}

To:

{{ form_start(form, {
    attr: {
        class: 'quiz',
        'data-controller': 'ajax-form quiz',
        'data-ajax-form-targets-value': ['#ajax-content-1', '#ajax-content-2', '#lesson-footer']|serialize('json') ,
        'data-ajax-form-spinner-html-value': include('shared/_spinner.html.twig'),
        'data-ajax-form-scroll-to-value': '#ajax-content-1',
    }
}) }}

Actual behavior

Hash indentation is not changed.

@VincentLanglet
Copy link
Owner

Hi, do you want to try working on a rule ?

@lukepass
Copy link
Author

lukepass commented Jul 2, 2024

Yes, of course! I would like to! How can I do that?

@VincentLanglet
Copy link
Owner

You can play with https://github.com/VincentLanglet/Twig-CS-Fixer/blob/main/tests/Token/Tokenizer/TokenizerTest.php
to understand how the code is Tokenized.

For example
{{ {'foo': 42} }}
is tokenized with tokens of the type

  • Token::VAR_START_TYPE
  • Token::WHITESPACE_TYPE
  • Token::PUNCTUATION_TYPE {
  • Token::STRING_TYPE (Be aware that without the quote it's a Token::NAME_TYPE)
  • Token::PUNCTUATION_TYPE :
  • Token::WHITESPACE_TYPE
  • Token::NUMBER_TYPE
  • Token::PUNCTUATION_TYPE }
  • Token::WHITESPACE_TYPE
  • Token::VAR_END_TYPE

Notice that in case of the token } you'll have $token->getRelatedToken() returning the one which is opening {. (This is currently used only for ternary $a ? $b : $c here

$relatedToken = $token->getRelatedToken();
return null !== $relatedToken && '?' === $relatedToken->getValue() ? 1 : 0;
).

You rule will need to be configurable to know what should be used for indent (tab or space ? And how many space ?) an example can be found here https://github.com/VincentLanglet/Twig-CS-Fixer/blob/main/src/Rules/Whitespace/IndentRule.php#L14

Here you have an example of rule which works on ), } and ]

if (!$this->isTokenMatching($token, Token::PUNCTUATION_TYPE, [')', '}', ']'])) {
; I assume you'll have to

  • Create a rule on }
  • get the related token {
  • Compute the indentation of the {
  • Get every line with a hash
  • Compare reindent the lines.

But the issue you'll encounter will be

  • Case like {} inside another {} since the indentation will be different
  • And worst, hash with multilines like
{{ form_start(form, {
foo: 42
    + 42
}
}) }}

because people might expect it to be fixed to

{{ form_start(form, {
    foo: 42
         + 42
}
}) }}

My fear is the fact that putting one step in the indentation rule will end with having to indenting everything ({, (, operator, ...) which seems kinda complicated (PHP-Cs-Fixer rule is not even perfect ATM).

@ruudk
Copy link
Contributor

ruudk commented Jul 3, 2024

@lukepass Just wanted to say that I'm also very much interested in having this properly formatted. So thanks in advance for your work on this 🙌 💪

Copy link

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@github-actions github-actions bot added the Stale label Dec 31, 2024
@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Jan 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants