Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Improve Python syntax highlighting #1762

Open
fabiospampinato opened this issue Jan 12, 2020 · 7 comments
Open

Improve Python syntax highlighting #1762

fabiospampinato opened this issue Jan 12, 2020 · 7 comments
Labels
feature-request Request for new features or functionality grammars help wanted Issues identified as good community contribution opportunities

Comments

@fabiospampinato
Copy link

monaco-editor version: 0.19.2
Browser: Chrome 79 maybe
OS: macOS Mojave
Playground code that reproduces the issue:

monaco.editor.create(document.getElementById("container"), {
	value: "from functions import sum, average",
	language: "python"
});

Basically some import statements in Python aren't highlighted properly.

How Monaco/Monarch highlights things:

image

How VSCode highlights things:

image

@alexdima
Copy link
Member

PR welcome. The Python tokenizer is here.

@alexdima alexdima added feature-request Request for new features or functionality grammars labels Jan 14, 2020
@blois
Copy link

blois commented Jan 21, 2020

Part of the issue is that Monaco's default theme differs from VSCode's Dark+ and Light+.

It would be great if these could be reconciled to make it possible to implement Monarch tokenization that follows the same TextMate patterns.

For example, if you try to update the Python tokenizer to support themes such as https://github.com/brijeshb42/monaco-themes/blob/master/themes/Monokai.json then it will break with the default Monaco themes.

@fabiospampinato
Copy link
Author

make it possible to implement Monarch tokenization that follows the same TextMate patterns.

My understanding is that that's not possible, at least not without sacrificing performance or platforms where WASM isn't available, TextMate uses a different regex engine and its regexes can't just be converted to JS regexes.

@blois
Copy link

blois commented Jan 21, 2020

It should be possible to implement a Monarch tokenizer which results in a token stream similar to what the TextMate grammars result in. Right now implementing a Monarch tokenizer which approximates TextMate will result in a loss of syntax highlighting as Monaco's default theme does not support all of the tokens.

I did extend the Monarch Python syntax highlighting to be closer to TextMate, but this required a number of customizations to the theme as well, would be great if I could make a PR to monaco-languages without it requiring theme customizations.

Python editor with my customizations: https://colab.research.google.com/gist/blois/00fa28cdb0433e62c8106a4b5812d024/syntax-highlighting.ipynb

@bolinfest
Copy link
Contributor

More importantly, this seems to be missing Python 3 features like recognizing async/await as keywords and supporting f-strings.

bolinfest added a commit to bolinfest/monaco-languages that referenced this issue Apr 21, 2020
This supports microsoft/monaco-editor#1762.

Note the new Python 3 keywords are:

* `async`
* `await`
* `nonlocal`

I also reorganized the list a bit because it seems like it contains
a mixture of keywords and builtins (and `self`, which is neither...),
so it is helpful to be specific to illustrate how to properly maintain
the list.
@hediet hediet added the help wanted Issues identified as good community contribution opportunities label Feb 13, 2023
@samstrohkorbatt
Copy link
Contributor

@hediet, @bpasero, @alexdima
Could any of you approve my PR to add Python F-String syntax highlighting? This is the second half of what @bolinfest asked for over 4 years ago that still isn't in Monaco Editor yet

@saiyalamarty
Copy link

Is it expected that the f-string support that was merged works in v0.49.0-dev-20240524 but not in v0.49.0 or v0.49.0-rc?

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
feature-request Request for new features or functionality grammars help wanted Issues identified as good community contribution opportunities
Projects
None yet
Development

No branches or pull requests

7 participants