Hi,
I’m trying to follow the examples in CodeMirror Mixed-Language Parsing Example to create a mixed python and SQL language support. My end goal is to use this in Jupyter notebook in the following way:
some_query_str = "SELECT * FROM table where a > 1"
In this example, this is a python line which should be highlighted as python, but the content inside the string quotation is an SQL query which should be highlighted with SQL.
Could you provide any guidance?
I took the html + javascript example which seems very very similar to what I need, however I face an issue:
When switching from javascript inside html to javascript inside python the highlighting of the javascript code stops working, treated as a python string. (I changed the check to be node.name == “String”), with an example of:
some_query_str = "let a = 5"
Even though there is a “String” node, it stays colored in red as a string and not changed to SQL
The SQL package does expose a number of different dialects, so it’s potentially an issue with how you are configuring the parser and the language support that is required for highlighting.
Ah, thank you for that Playground example, very interesting!
I did some more playing around and it looks like the mixed parsing is parsing the string as JS but autocomplete and highlighting is disabled.
If I use a multiline string autocomplete will work for JS, but, the syntax highlighting is not working.
I wonder if this is because it’s not enough to simply replace the String node, perhaps it’s worth looking into using overlay nesting to try and replace within the String node itself
Glad you figured it out! Yes, it’s a bit tricky, I’ve also found myself cross-referencing a lot of different docs, examples, and threads here to solve for my own use case.
If you don’t mind, would you be able to share an example playground, snippet, or repo for future travelers (one of which may be myself )?
What you were doing was parsing the entire Python string, including the quotes, as JavaScript
Could you provide some more details on the correct approach here? The way this is written, It sounds like what the OP (and I to some extent) were trying to do.
Here’s an somewhat isolated playground that does the following:
If you don’t want the quotes to be part of the inner language’s document, use an overlay.
Non-overlay nested parses replace the entire node with the parse tree. The Script(ExpressionStatement(String)) is the JavaScript parse tree. Nothing is backwards.
In general, overlays are tricky because I haven’t found a way to get information out about what is wrong, is there diagnostic information that can be gotten out of the tree that I can use to help highlight issues like the aforementioned range issue?
@marijn Thanks, I got it working with the overlays method.
I’m using this inside JupyterLab, and it seems that everything is pretty much highlighted with the same “keyword” color. Can I customize it so builtins are highlighted differently? Currently from the DOM it seems that builtins / types are not getting any class names. I tried using styleTags but with no success
Basically im trying to override this the “styleTags” that’s defined in lang-sql. When I edit the source code I can change the highlighting for Type for example, im just trying to override it from the outside without changing the source code
This is exactly what im doing but it’s not working. Could it be because im trying to override an existing styletags? or because the tags im changing are inside the inner language?
From what I see I need to configure the inner language of the sql-lang, but since it’s readonly and the constructor is private im not sure i can access it. Any workaround?