question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to generate the stylesheet for tokens when using both TextMate and Monarch grammars simultaneously

See original GitHub issue

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:

  • Most of the times that Monarch grammar will be the only one my users will need, so no need to load Oniguruma at all in that common scenario.
  • The Monarch grammar is most probably going to be faster, as it uses JS regexes.
  • Also importantly I already have the Monarch grammar at hand, but translating that for TextMate is going to be error prone at best and very time consuming.

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!

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
fabiospampinatocommented, Apr 22, 2021

It actually worked!

For posterity, I did the following:

  • Shifted all counters by 100 for the TextMate stylesheet, this ensures the classes for tokens from both registries won’t overlap:
css.replace ( /mtk(\d+)/gi, ( _, nr ) => `mtk${Number ( nr ) + 100}` );
  • Shifted the counters by 100 for tokens generated by TextMate grammars, this ensures both in-editor and off-editor tokens match the classes in the stylesheet:
for ( let i = 1, l = tokens.length; i < l; i += 2 ) {
  tokens[i] += 100 << 14;
}
  • Padded by 100 the color map from the Monarch/global registry and concatenated that with the color map from the TextMate registry, this ensures the right colors are displayed in the Minimap:
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 );
  • What’s left basically is just reusing the TextMate theme for Monarch grammars, I haven’t addressed that yet but that should be a bit tedious at worst, and not really needed perhaps.

I love Monaco! ❤️

0reactions
fabiospampinatocommented, Apr 22, 2021

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.

https://github.com/microsoft/vscode/blob/2f077172cb0fcbd06f5820b0a5125215e8d38435/src/vs/editor/common/modes.ts#L172

Read more comments on GitHub >

github_iconTop Results From Across the Web

Optimizations in Syntax Highlighting, a Visual Studio Code Story
Syntax Highlighting usually consists of two phases. Tokens are assigned to source code, and then they are targeted by a theme, assigned colors, ......
Read more >
Monaco Editor Monarch - Microsoft Open Source
To make an end tag only match its specific open tag, we need to dynamically generate token classes that make the braces match...
Read more >
How to tokenize a code snippet using a Textmate grammar in ...
First make sure you have Python 2.7 (not 3.X) installed on your machine and on Windows in your PATH variable. Install vscode-textmate using...
Read more >
[Bug] 0.34: bracketPairColorization does not work #3237
Idea to use grammars to help with bracket pairs: Use textmate grammar token kind to exclude unbalanced brackets from being colorized vscode# ...
Read more >
Writing a browser based editor using Monaco and ANTLR
This is a tutorial on creating a browser-based editor for a new language we are going to define. We are going to use...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found