-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Attempt to fix highlighting issue in monaco #12917
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
Conversation
Thank you for the pull request, @jjspace! ✅ We can confirm we have a CLA on file for you. |
Disclaimer: It's surprisingly hard to systematically test this. The behavior seems to change randomly. Building and reloading (with cache disabled) sometimes seems to pick up modified code, but sometimes not, and sometimes changing the 'Settings' seems to have an effect, and sometimes not, and sometimes only after reloading the page (not once but) multiple times. The settings appear to be stored in cookies (?). Maybe it's necessary to systematically delete them at certain points in the test procedure...? However, I tried it out, and it didn't resolve it for me. My strong recommendation for resolving this issue is: Remove that "Droid Sans Mono" option, remove the "Font ligatures" toggle, and call it a day. (If somebody cares, the response should be "Pull requests welcome!". How many substantially different forms of a "monospaced font" are there, after all ... ?) Rant: I'm recommending that after zooming a bit into that (with another disclaimer: I'm not a CSS/React expert, and I proudly refuse to become one). Apparently, My sincere condolences to everybody who has to deal with ... things ... like this: Back on topic: It looks like the |
f1342eb
to
0010d7a
Compare
I did some more digging into this this morning. I'm more confident now saying this is an async issue with the font's loading after Monaco tries to measure them as mentioned in this issue microsoft/monaco-editor#392. This was linked from a few other alignment related issues too. I've updated the code to revert the original ligature change but also delay setting the monaco font until it's loaded. The font loading API is not perfect and there might still be a better way of doing this. However, I have confirmed the issue is gone on my windows machine using the CI branch. @javagl can you please re-test this? Other solutions might be to try and pre-load the font files but I wanted to avoid loading unnecessary fonts that people aren't using. |
I tried it out, and despite all evil efforts of switching the fonts and toggling the ligatures, I could no longer reproduce the issue. So this seems to fix it. (Given that this apparently was some "synchronization/race-condition" thingy, it's hard to say for sure, but it seems to be fixed). I looked at the code change, and cannot say anything about that. But in the future, when someone asks "Why do IT projects often take so much longer and cost so much more than originally planned?", I can say: "This 👉". |
const fontFace = [...documentRef.current.fonts.values()].find( | ||
(font) => font.family === cssName && font.weight === "400", | ||
); | ||
if (fontFace?.status !== "loaded") { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For simplicity, could we instead just wait on document.fonts.ready
and then call monaco.editor.remeasureFonts()
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Per MDN:
Note that the font.load() returns a promise, so we could have handled the completion of font loading by chaining then afterwards. Using document.fonts.ready can be better in some circumstances, as it is only called when all fonts in the document have been resolved and layout is complete.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ggetz Unfortunately not. That promise is a one time thing and it always returned instantly when I tried. On top of that, that promise and the other onfontloaded
event handlers only trigger when the font is loaded using the fonts API and do not seem to trigger when a font is loaded because of styling changes (like how monaco updates it). I tried other arrangements of this and none worked so I settled on this as the best option.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, bummer.
Auto merge won't go until it has an approve. CI passed on the last commit so I'm going to just merge |
Description
This is an attempt to fix the highlighting issue shown below from the feedback thread comment
I can't reproduce this myself as it seems specific to Windows + Chrome.
Some investigation has pointed to potentially the ligature setting affecting this so this PR is to test that.More investigation showed this was a problem with loading fonts after Monaco tried to measure them.Issue number and link
part of #12857
Testing plan
npm run dev
from the sandcastle package ornpm run build-sandcastle
andnpm start
from the project rootThese steps reproduced the issue for me even on Linux:
Same works for me from
Cascadia -> Fira
Droid -> Cascadia
Jetbrains -> Cascadia
Toggling Ligatures on the selected font also usually fixed it for me
Example on Linux:

Author checklist
CONTRIBUTORS.md
CHANGES.md
with a short summary of my change