Formatting code within a Twig Language Injection

 

The recent Twig handling improvements to PHPStorm have improved a lot about Twig formatting. I'm experimenting with Language Injections within a custom Twig tag and running into a couple issues. I'm finding two issues I can't solve with this following block of code (simplified and shortened for the sake of this example):

{% js %}

var toggleStatus = localStorage.getItem('{{ id }}-field');

if (toggleStatus == 'hidden') {
$('#' + '{{ id }}-field' + ' .input').css('display', 'none');
}

{% endjs %}
  1. When the Javascript Language Injection is enabled, the syntax highlighting only works up until the first instance of `{{ id }}`. After that the whole block loses syntax highlighting and just displays one color.
  2. If I choose the Reformat Code option, all the indentation collapses. 
{% js %}

var toggleStatus = localStorage.getItem('{{ id }}-field');

if (toggleStatus == 'hidden') {
$('#' + '{{ id }}-field' + ' .input').css('display', 'none');
}

{% endjs %}

Are there any settings I might be missing or would these be limitations of the current implementation?

On a related note, is there any good way to run Code Reformat on a folder of files like this? Right now, I have several Twig files in a templates folder and many of them have javascript within custom Twig tags. If I run Reformat Code, half the code gets cleaned up, and all the javascript blocks get smashed and harder to read. Any ideas on how I might be able to improve this situation?

 

4 comments
Comment actions Permalink

Hi there,

What's the IDE version you are using?

Please try 2018.1 EAP build -- there were some fixes in injected language formatting there recently.

https://www.jetbrains.com/phpstorm/eap/

P.S. It could be https://youtrack.jetbrains.com/issue/WI-39209 that is not fixed yet (accordingly to the latest updates on that ticket)

0
Comment actions Permalink

Hi Andriy.

I've updated to the latest PhpStorm (2017.3.4 build 173.4548.32) and downloaded the 2018.1 EAP build. Still no luck. The same two issues exist. I'll add my vote to that ticket for now.

0
Comment actions Permalink

Is there any solution for this yet?

0

Please sign in to leave a comment.