monaco-editor version: 0.11.x in the playground
Browser: Chrome 65.0.3325.181 (Official Build) (64-bit)
OS: Windows 10
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'
});
Hover over the text. Notice that the var keyword is not highlighted.
Hover over the text again. Now it is coloured properly.
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. 馃帀
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()