Skip to content
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

Hovers don't support syntax highlighting like VS Code #802

Closed
rcjsuen opened this issue Apr 5, 2018 · 7 comments
Closed

Hovers don't support syntax highlighting like VS Code #802

rcjsuen opened this issue Apr 5, 2018 · 7 comments

Comments

@rcjsuen
Copy link
Contributor

rcjsuen commented Apr 5, 2018

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

monaco.languages.register({ id: 'typescript' });

monaco.editor.create(document.getElementById("container"), {
	value: 'var xxxx = ""\nxxxx.toString();',
	language: 'typescript'
});

If you try to hover over toString(), you'll get no syntax highlighting in the browser but you'll get something in VS Code.

VS Code 1.21.1

image

Monaco playground

image

@ryanlin1986
Copy link

This is very important code editing feature I believe.
Please consider this feature.

@rcjsuen
Copy link
Contributor Author

rcjsuen commented Apr 6, 2018

Just in case I tried writing a code block explicitly in Markdown but it didn't colour anything.

image

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: '**asdf**' },
                { value:
                        '```js\n' + 
                        'var x = blah.toString();\n' + 
                        '```\n'
                },
                { value:
                        '```JavaScript\n' + 
                        'var x = blah.toString();\n' + 
                        '```\n'
                }
            ]
        }
	}
});

monaco.editor.create(document.getElementById("container"), {
	value: 'Hover over this text',
	language: 'mySpecialLanguage'
});

@AlexTugarev
Copy link

@rcjsuen could you try with another example? the one from monaco-languageclient for instance. I looks like it's working there for me.

@rcjsuen
Copy link
Contributor Author

rcjsuen commented Apr 6, 2018

@AlexTugarev I'm not sure what case you would like tested exactly but it seems like Monaco is very selective about what "language" it supports in its Markdown code blocks. Consider the following piece of code where js and javascript works but JavaScript doesn't. Perhaps things are working on your end for the same reason.

image

monaco.languages.registerHoverProvider('javascript', {
	provideHover: function(model, position) {
        return {
            range: new monaco.Range(1, 1, model.getLineCount(), model.getLineMaxColumn(model.getLineCount())),
            contents: [
                { value:
                        '```js\n' + 
                        'var x = "string";\n' + 
                        '```\n'
                },
                { value:
                        '```javascript\n' + 
                        'var x = "string";\n' + 
                        '```\n'
                },
                { value:
                        '```JavaScript\n' + 
                        'var x = "string";\n' + 
                        '```\n'
                }
            ]
        }
	}
});

monaco.editor.create(document.getElementById("container"), {
	value: '// hover here',
	language: 'javascript'
});

@alexdima
Copy link
Member

The discrepancy is because in monaco-typescript we are not using a fenced code block in markdown: https://github.com/Microsoft/monaco-typescript/blob/5d8e7bfc6abc35387d091da2fafcc400ad28b363/src/languageFeatures.ts#L332-L334

PR welcome.

@rcjsuen
Copy link
Contributor Author

rcjsuen commented Apr 11, 2018

I tried wrapping the content in a code block but it's not quite the same as what's in VS Code.

image

@alexdima
Copy link
Member

alexdima commented Apr 11, 2018

The coloring is given by the tokenizer.

VS Code uses a TM grammar for TypeScript:

image

While the editor uses the TS lexer to generate tokens:

image

They will never match, given the source code is tokenized with two different tokenization engines.

But equal effects can be achieved if you would write a different tokenizer...

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants