How can I have PhpStorm see code as Javascript?

For complicated reasons, I have to wrap my Javascript like this:

<script type="text/plain" data-cookiescript="accepted">
....valid and normal javascript code here...
</script>

Unfortunately because of this, PhpStorm doesn't see it as JS any more since it isn't text/javascript. Is there a setting to override this behavior and tell it that, yeah, really, there is javascript in here? ;-)

9 comments
Comment actions Permalink

Hi there,

Custom Language Injection should do the job:

https://www.jetbrains.com/help/phpstorm/using-language-injections.html#configure-injection-rules

https://www.jetbrains.com/help/phpstorm/language-injections-settings.html

P.S. "text/plain" type is too general (can be used in other places/project to be an actual plain text/other language).

0
Comment actions Permalink

Yeah, I don't love text/plain either, but it's what I'm forced to use right now. I am not using it for plain text anyhwre in my project so I figure I can override it at a project level and I should be okay.

Anyhow - I went into the config boxes and they are super confusing. ;-) Can you help me figure out what to put in here? The documentation didn't give any examples and I couldn't really figure it out from the existing injections. I guess I need something in the "Places Patterns" to indicate the opening <script type="text/plain" tag?

0
Comment actions Permalink

Look at the bundled rule that injects CSS into <style> tag.

That's the same logic behind it, so should do the job (unless there is some hardcoded override/special handling for script with text/plain)

0
Comment actions Permalink

Where can I find that bundled rule?

This is all I have here-

0
Comment actions Permalink

At the bottom of the list

0
Comment actions Permalink

I think? I'm closer -- here's what I have. It's not working but it seems like it should... ?!

 

0
Comment actions Permalink

1) Try with empty Namespace

2) If it does not work super well (looks like it has limited code completion on my setup) .. try this:

  • Remove that newly created rule and accept settings
  • Place a caret anywhere inside such script tag content
  • Alt+Enter and choose "Inject Language or reference"
  • Choose appropriate language
0
Comment actions Permalink

Hey @Andriy, thank you, this is almost there.

I removed the "Namespace" setting and now the code highlights correct, which is great. However, it still shows a background color in the code that is used for plan/text.

It also doesn't seem that the IDE is fully treating the code as Javascript. There is no code completion, and when I use code reformat, it doesn't reformat this JS code.

As soon as I change the type to "text/javascript" then it definitely knows it's JS, the code reformatting works, etc.

I looked to see if there was perhaps another Language Injection for plain/text that was overriding, but it's not there.

Oh, and when I highlight the code and click the lightbulb, an option is "Edit this plain text block."

I tried your 2nd idea, to try to inject language inside the text/plain block... but I don't have that option available. 🤷🏻‍♂️

 

0
Comment actions Permalink

It must be due to changes in new IDE versions there (I must have been using 2018.3.x back then) as I was able to inject custom language there before...

If you change type="text/plain" to be type="text/plainZZ" .. then you will be able to inject custom language .. but that would be useless as it would be a JS anyway.

For me this means that IDE now has special treatment for "text/plain". That's my best explanation for you so far.

Maybe Elena Pogorelova can help here (she is from WebStorm support team). Try posting new thread in WebStorm forum if you will get no better answer here: https://intellij-support.jetbrains.com/hc/en-us/community/topics/200367229-WebStorm

0

Please sign in to leave a comment.