-
Notifications
You must be signed in to change notification settings - Fork 116
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to generate the stylesheet for tokens when using both TextMate and Monarch grammars simultaneously #149
Comments
@fabiospampinato You basically need to collect all the colors used by the Monarch theme and all the colors used by the TM theme and create a single color map that contains all of them. A color map is just an array with the colors. This is used to encode/decode the color of a token as a small integer. Once you compute the color map, you can define it for
For Monarch, you can define it via
I think that would make it possible to have text models using both tokenizers at the same time. |
@alexdima Thanks for the help, it sounds doable in principle, but if ids [1..N] must have the same colors in both registries I suppose the ids themselves should also correspond to the same tokens under the hood, like if the first color is for strings in one registry it can't be for numbers on the other registry. Is it possible to enforce that somehow? 🤔 Alternatively what if I reserve the first 50 tokens or whatever for the Monarch grammar and then force the TM grammars to start from 51 for example? I suppose that should work in principle too and perhaps it would be much easier to implement? I'm thinking for that I may only need to add 50 dummy initial colors/tokens to one of the registry, compute the stylesheet for both, and make sure to insert them in the DOM in the right order basically. Does this sound reasonable/easier to you? If so I tried setting this to 100 but it didn't work, I'm guessing there's an array of colors to pre-fill somewhere or something, any pointers would be greatly appreciated. |
If that approach makes sense and it would be easier to implement it then I think I could do the following:
Does this sound reasonable? |
It looks like that contains some metadata, but not exactly which id to use for the token, I'm not sure where that's resolved or if it would be feasible to patch that. |
Actually maybe I could just increment the bits the encode the foreground color to achieve this. I'll have to try this, it sounds promising. |
It actually worked! For posterity, I did the following:
css.replace ( /mtk(\d+)/gi, ( _, nr ) => `mtk${Number ( nr ) + 100}` );
for ( let i = 1, l = tokens.length; i < l; i += 2 ) {
tokens[i] += 100 << 14;
}
const colorsMonarch = Monaco.TokenizationRegistry._colorMap.slice ( 0, 100 ),
colorsTextMate = Style.getColors ( registry ),
colorPadding = Monaco.Color.Format.CSS.parseHex ( '#00000000' ),
colors = new Array ( 100 + colorsTextMate.length ).fill ( colorPadding );
for ( let i = 0, l = colorsMonarch.length; i < l; i++ ) {
if ( !colorsMonarch[i] ) continue;
colors[i] = colorsMonarch[i];
}
for ( let i = 0, l = colorsTextMate.length; i < l; i++ ) {
if ( !colorsTextMate[i] ) continue;
colors[100 + i] = colorsTextMate[i];
}
Monaco.TokenizationRegistry.setColorMap ( colors );
I love Monaco! <3 |
I've mostly added TextMate grammars support in an app of mine, but that app still uses a Monarch grammar too, and I haven't been able to figure out how to generate the right CSS for the tokens properly, without having the tokens defined in the Monarch stylesheet override the ones in the TextMate stylesheet and viceversa.
I'm hoping I won't have to rewrite the Monarch grammar to TextMate as that would have quite a few downsides:
I'm guessing the root issue is that I basically have 2 registries for grammars that don't know about each other, but how could I merge them when the registry for TextMate only seems to handle TextMate grammars?
Any ideas on how to address this issue?
/cc @bolinfest, in case you might have already encountered this issue. Thanks for
monaco-tm
by the way!The text was updated successfully, but these errors were encountered: