I’m attempting to make a plugin that will highlight javascript template strings within a file in another syntax. so the segment below would be highlighted as glsl.
const vs = /*glsl*/ `#version 300 es
precision mediump float;
in vec4 position;
void main(){
}
`;
So far I haven’t been successful, although I find the correct regions with regex, I’m not able to get visible syntax highlighting. I admit I used chatgpt’s suggestion on how to do it so I might have been mislead, but any help is appreciated. the code is below:
import sublime
import sublime_plugin
import re
class TemplateSyntaxHighlighter(sublime_plugin.ViewEventListener):
@classmethod
def is_applicable(cls, settings):
syntax = settings.get('syntax')
return syntax and "Packages/JavaScript" in syntax
def on_modified_async(self):
self.highlight_template_strings()
def on_activated_async(self):
self.highlight_template_strings()
def highlight_template_strings(self):
content = self.view.substr(sublime.Region(0, self.view.size()))
pattern = re.compile(r"/\*\s*glsl\s*\*/\s*`([\s\S]*?)`")
regions = []
for match in pattern.finditer(content):
if match.group(1):
start = match.start(1)
end = match.end(1)
regions.append(sublime.Region(start, end))
self.view.set_status("glsl_highlight_info", f"GLSL strings: {len(regions)}")
if regions:
self.view.add_regions(
"glsl_highlight",
regions,
"source.glsl",
"",
flags=sublime.DRAW_NO_FILL | sublime.DRAW_NO_OUTLINE
)
else:
self.view.erase_regions("glsl_highlight")