Commit b9f5b9e
committed
Ensure @rematch and nextEmbedded can be used together in a Monarch grammar.
Similar to the repro case I created for
microsoft#90266, I created a local
playground with the following:
```js
const DEMO_LANG_ID = "demo";
const SQL_QUERY_START = "(SELECT|INSERT|UPDATE|DELETE|CREATE|REPLACE|ALTER|WITH)";
const languageConfiguration = {
tokenizer: {
root: [
[
`(\"\"\")${SQL_QUERY_START}`,
[
{
"token": "string.quote",
},
{
token: "@rematch",
next: "@endStringWithSQL",
nextEmbedded: "sql",
},
]
],
[
/(""")$/,
[
{
token: "string.quote",
next: "@maybeStringIsSQL",
},
]
],
],
maybeStringIsSQL: [
[
/(.*)/,
{
cases: {
[`${SQL_QUERY_START}\\b.*`]: {
token: "@rematch",
next: "@endStringWithSQL",
nextEmbedded: "sql",
},
"@default": {
token: "@rematch",
switchTo: "@endDblDocString",
},
}
}
],
],
endDblDocString: [
[
"[^\"]+",
"string"
],
[
"\\\\\"",
"string"
],
[
"\"\"\"",
"string",
"@popall"
],
[
"\"",
"string"
]
],
endStringWithSQL: [
[
/"""/,
{
token: "string.quote",
next: "@popall",
nextEmbedded: "@pop",
},
]
],
},
};
monaco.languages.register({
id: DEMO_LANG_ID,
extensions: ['.example'],
});
monaco.languages.setMonarchTokensProvider(DEMO_LANG_ID, languageConfiguration);
const value = `\
mysql_query("""SELECT * FROM table_name WHERE ds = '<DATEID>'""")
mysql_query("""
SELECT *
FROM table_name
WHERE ds = '<DATEID>'
""")
`;
var editor = monaco.editor.create(document.getElementById("container"), {
value,
language: DEMO_LANG_ID,
lineNumbers: "off",
roundedSelection: false,
scrollBeyondLastLine: false,
readOnly: false,
theme: "vs-dark",
});
```
Without my change to monarchLexer.ts, I get this error in the console:
> errors.ts:26 Uncaught Error: demo: cannot pop embedded mode if not inside one
But with my change, I see a Python-esque multiline string literal with
SQL syntax highlighting within it.1 parent bdd729a commit b9f5b9e
1 file changed
Lines changed: 25 additions & 15 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
740 | 740 | | |
741 | 741 | | |
742 | 742 | | |
| 743 | + | |
| 744 | + | |
| 745 | + | |
| 746 | + | |
| 747 | + | |
| 748 | + | |
| 749 | + | |
| 750 | + | |
| 751 | + | |
| 752 | + | |
| 753 | + | |
| 754 | + | |
| 755 | + | |
| 756 | + | |
| 757 | + | |
| 758 | + | |
| 759 | + | |
| 760 | + | |
743 | 761 | | |
744 | 762 | | |
745 | 763 | | |
| |||
780 | 798 | | |
781 | 799 | | |
782 | 800 | | |
| 801 | + | |
| 802 | + | |
| 803 | + | |
| 804 | + | |
| 805 | + | |
| 806 | + | |
783 | 807 | | |
784 | 808 | | |
785 | 809 | | |
| |||
810 | 834 | | |
811 | 835 | | |
812 | 836 | | |
813 | | - | |
814 | | - | |
815 | | - | |
816 | | - | |
817 | | - | |
818 | | - | |
819 | | - | |
820 | | - | |
821 | | - | |
822 | | - | |
823 | | - | |
824 | | - | |
825 | | - | |
826 | | - | |
827 | | - | |
| 837 | + | |
828 | 838 | | |
829 | 839 | | |
830 | 840 | | |
| |||
0 commit comments