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
[Bug] monaco.editor.tokenize does not return correct results #3448
Comments
|
I looked into this further, this is almost definitely an initialization or asynchronous related bug. When I do this: setTimeout(() => {
const tokens = monaco.editor.tokenize(`// My awesome TypeScript function!
export const areEqual = (a: number, b: number): boolean => {
return a === b;
}`, "typescript");
console.log("tokens", tokens);
}, 5000)It works! Perhaps |
|
You can do this before calling tokenize: await languages.TokenizationRegistry.getOrCreate(languageId); |
I can't find this anywhere in the Monaco documentation, nor is import { languages } from "monaco-editor/esm/vs/editor/editor.api";
// ...
await languages.TokenizationRegistry.getOrCreate("typescript");
Unless I'm mistaken and this Therefore I would like a bit more clarification on this issue before it is closed. |
|
Ah, it is only exposed by |
|
@hediet - is this a separate GitHub package that can be installed? Is it compatible with the browser? |
|
For anyone who may find this later, I currently have a fairly reliable workaround. essentially I access the global In code, the workaround looks like this: setTimeout(() => {
(window as any).monaco.editor.tokenize(
`export const dummyFunction = () => {
console.log('hello world')
}`,
"typescript"
);
// any call to (window as any).monaco.editor.tokenize beyond this point works
}, 1000);You can alternatively promisify this setTimeout and do something like: const initializeMonaco = new Promise((res) =>
setTimeout(() => {
(window as any).monaco.editor.tokenize(
`export const dummy = () => {
console.log('hello world')
}`,
"typescript"
);
res("");
}, 1000)
);
await initializeMonaco;
// now (window as any).monaco.editor.tokenize() will workobviously, I'd like to not use this hack, but it gets the job done for now. |
Reproducible in vscode.dev or in VS Code Desktop?
Reproducible in the monaco editor playground?
Monaco Editor Playground Code
Reproduction Steps
monaco.editor.tokenizeconsole.logActual (Problematic) Behavior
Tokens are not detected (though lines are)
Expected Behavior
Tokens to be detected!
Additional Context
The strange thing is that if you paste the following into the terminal, it works:
I feel like this is some sort of initialization issue... but calling
monaco.editor.tokenizeeven multiple times within my code doesn't seem to solve the problem, I still get empty token lines... very strange.As you see I thought it was an escaping of
\nand\tissue, but even the escaped single line string doesn't work....Perhaps this is something small, but I can't see what is going wrong here. Need another set of eyes.
The text was updated successfully, but these errors were encountered: