microsoft / microsoft/monaco-editor
[Bug] monaco.editor.tokenize does not return correct results
Nobody has claimed this yet.
- Dominant language
- JavaScript
- Stars
- 46.8k
- Forks
- 4.1k
- Avg merge
- 17h 58m
- Merged PRs (30d)
- 1
Description
Reproducible in vscode.dev or in VS Code Desktop?
- Not reproducible in vscode.dev or VS Code Desktop
Reproducible in the monaco editor playground?
- Not reproducible in the monaco editor playground
Monaco Editor Playground Code
monaco.editor.create(document.getElementById('container'), {
value: "function hello() {\n\talert('Hello world!');\n}",
language: 'typescript'
});
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);
const tokensEscaped = monaco.editor.tokenize("// My awesome TypeScript function!\nexport const areEqual = (a: number, b: number): boolean => {\n\treturn a === b;\n}", "typescript");
console.log("tokensEscaped", tokensEscaped);
Reproduction Steps
- Try to pass a preformatted string to
monaco.editor.tokenize - Log out the results via
console.log
Actual (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:
tokens = monaco.editor.tokenize(`// My awesome TypeScript function!
export const areEqual = (a: number, b: number): boolean => {
return a === b;
}`, "typescript");
console.log("tokens", tokens);
tokens (4) [Array(1), Array(27), Array(9), Array(1)]
I feel like this is some sort of initialization issue... but calling monaco.editor.tokenize even 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 \n and \t issue, 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.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the monaco.editor.tokenize calls in the Monaco Editor Playground reproduction and compare the preformatted template string with the escaped single-line string. Investigate why both produce lines without tokens while the terminal example works; done means the supplied TypeScript snippet returns detected tokens consistently.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- frontend
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100