** N.B. Official plugin now exists: https://plugins.jetbrains.com/plugin/9997-styled-components so I reccomend using that now instead :) **
This language injection configuration file gives IntelliJ based products (webstorm/pycharm/phpstorm,etc) some knowledge of styled-components, providing syntax highlighting and hints.
Tested in 2017.1 and 2017.2
- File->Settings
- Language Injections
- Import
- 'styled-components.xml'
- Rule 1 - Catches any literal within
styled(...)
orstyled.anything(...)
jsLiteralExpression().and(jsArgument("styled", 0))
- Rule 2 - Catches any literal after an expression starting with
styled`...`
includingstyled.span`...`
andstyled.anything`...`
jsLiteralExpression().withParent(psiElement().withText(string().startsWith("styled")))
- Rule 3 - Fallback case that matches any literal who's variable name starts with
Styled
as in
const StyledCoolComponent = whatever`...`
jsLiteralExpression().withParent(psiElement().withParent(psiElement().withText(string().startsWith("Styled"))))
This last rule is obviously quite workflow specific. I've left it in here in case it helps anyone else.
-
Add a rule to catch
StyledComponent.extend`...`
(the work around at the moment for me is via rule 3) -
Other API usage
- JS interpolations - The correct highlighting of them is unlikely to ever be possible via this language injection method. Judging from from their 'PSI Viewer' the editor can't peek inside the template string itself to reason about what type of language tokens may or may not be there (at least, not via this language injection interface).