Monaco-editor: Hover won't render Markdown code blocks with styling on the first popup

Created on 11 Apr 2018  路  2Comments  路  Source: microsoft/monaco-editor

monaco-editor version: 0.11.x in the playground
Browser: Chrome 65.0.3325.181 (Official Build) (64-bit)
OS: Windows 10

  1. Run the following code in the playground.

monaco.languages.register({ id: 'mySpecialLanguage' });
monaco.languages.registerHoverProvider('mySpecialLanguage', {
    provideHover: function(model, position) {
        return {
            range: new monaco.Range(1, 1, model.getLineCount(), model.getLineMaxColumn(model.getLineCount())),
            contents: [
                { value: '```js\nvar x = y.toString();\n```' }
            ]
        };
    }
});

monaco.editor.create(document.getElementById("container"), {
    value: '\n\nHover over this text',
    language: 'mySpecialLanguage'
});
  1. Hover over the text. Notice that the var keyword is not highlighted.

  2. Hover over the text again. Now it is coloured properly.

bug

Most helpful comment

A possible workaround would be to manually load the secondary language that appears only in hovers.

e.g. monaco.editor.createModel('', 'javascript').dispose()

All 2 comments

A possible workaround would be to manually load the secondary language that appears only in hovers.

e.g. monaco.editor.createModel('', 'javascript').dispose()

This is slightly different, but for anyone reading this while implementing their own language provider based on e.g. monaco-typescript: make sure to replace the language prefix in the quick info adapter. Specifically: if in xyz.contribution.ts you call registerLanguage({ id: "xyz" }), the corresponding patch for QuickInfoAdapter.provideHover would be:

  return {
      range: this._textSpanToRange(resource, info.textSpan),
      contents: [{
-          value: '```js\n' + contents + '\n```\n'
+          value: '```xyz\n' + contents + '\n```\n'
      }, {
          value: documentation + (tags ? '\n\n' + tags : '')
      }]
  };

If you keep js, it won't have the typescript lexer loaded the first time the hover popup is shown and there won't be any syntax highlighting, which is the same problem discussed above. (Apart from that, if your syntax is significantly different from typescript, this is a good idea anyway.)

After changing the prefix to your own language id, syntax highlighting works the first time the hover popup is shown and you won't need to use @alexandrudima's workaround anymore. 馃帀

Was this page helpful?
0 / 5 - 0 ratings

Related issues

andreymarchenko picture andreymarchenko  路  3Comments

chengtie picture chengtie  路  3Comments

akosyakov picture akosyakov  路  3Comments

galyech picture galyech  路  3Comments

galusben picture galusben  路  3Comments