-
Notifications
You must be signed in to change notification settings - Fork 6
Description
In PyCharm it's possible to write # language=html before a string, and have that string be syntax highlighted as html (or any other supported language). It's not supported by any other editor. It's clunky because it's not obvious what happens if the comment is on the same line as other code, if there's a newline in between, and so on.
What if there was a way to tag a string, so the code editor knows how to syntax highlight that string. Oh wait, that's what you're working on! :)
My use-case
A component in my library is a combination of python code, html, css and javascript. Currently I glue things together with a python file, where you put the paths to the html, css and javascript. When run, it brings all of the files together into a component. But for small components, having to juggle four different files around is cumbersome, so I've started to look for a way to put everything related to the component in the same file. This makes it much easier to work on, understand, and with fewer places to make path errors.
Example:
class Calendar(component.Component):
template_string = '<span class="calendar"></span>'
css_string = '.calendar { background: pink }'
js_string = 'document.getElementsByClassName("calendar)[0].onclick = function() { alert("click!") }'
Seems simple enough, right? The problem is: There's no syntax highlighting in my code editor for the three other languages. This makes for a horrible developer experience, where you constantly have to hunt for characters inside of strings. You saw the missing quote in js_string right? :)
If I instead use separate files, I get syntax highlighting and auto-completion for each file, because editors set language based on file type. But should I really have to choose?
Proposal
Would it be compatible with your work, to recommend editors to syntax highlight strings in python that has markers that correspond to this list of language identifiers? https://code.visualstudio.com/docs/languages/identifiers#_known-language-identifiers
This would make the developer experience even better for developers, and make your example tags in this repo even more powerful. html'<span class="calendar">{content}</span>'
would be highlighted as proper HTML (because the name html was used).
Advantages:
- Gives code editors a great API to standardize around. # language=html is not it...
- Makes it easier to spot errors inside those tagged strings
- Using standard names makes the feature discoverable for people that did not read the docs. "Oh, I get syntax highlighting when I use this tag, nice!"
- Everything just works for developers, no config, no installing stuff.
Even without my suggestion above, I really like what you're doing here.
Let me know what you think!