microsoft / microsoft/monaco-editor
[Bug] Regex without anchor `^` in the tokenizer rule matches only the beginning of the string
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 Link
Monaco Editor Playground Code
// Register a new language
monaco.languages.register({ id: "mySpecialLanguage" });
monaco.languages.setLanguageConfiguration('mySpecialLanguage', {
brackets: [
['{', '}'],
['(', ')'],
],
});
// Register a tokens provider for the language
monaco.languages.setMonarchTokensProvider("mySpecialLanguage", {
tokenizer: {
root: [
[/\{(.*?)\}\(.*?\)/, 'custom-url'],
[/[^<&]+/, ''] // text,
],
},
});
// Define a new theme that contains only rules that match this language
monaco.editor.defineTheme("myCoolTheme", {
base: "vs",
inherit: true,
rules: [
{ token: "custom-url", foreground: "EB9642" },
],
colors: {
"editor.foreground": "#000000",
},
});
monaco.editor.create(document.getElementById("container"), {
theme: "myCoolTheme",
value: getCode(),
language: "mySpecialLanguage",
});
function getCode() {
return [
"{Monaco - The Editor of the Web}(https://microsoft.github.io/monaco-editor/)",
" {Monaco - The Editor of the Web}(https://microsoft.github.io/monaco-editor/)"
].join("\n");
}
Reproduction Steps
In tokenizer, add a regular expression for parsing markdown-like links before the regular expression for plain text
Actual (Problematic) Behavior
Text matching the regular expression is highlighted with the 'cutom-url' token anywhere in the text
Expected Behavior
The text is highlighted only at the beginning of the line and is not highlighted if any characters precede the match pattern
Additional Context
[/[^<&]+/, ''] this regular expression is taken from the html syntax description embedded in monaco https://github.com/microsoft/monaco-editor/blob/main/src/basic-languages/html/html.ts#L102, since I'm doing an add-in over html. The example in playground is minimal, but the important thing is that the regular expression for markdown-like links comes first, and then at the end the regular expression for plain text
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 Playground link and the tokenizer configuration in the issue, then compare the behavior with the HTML syntax description at src/basic-languages/html/html.ts. Verify whether a tokenizer rule without a ^ anchor is intended to match later in a line, and confirm that the reproduction highlights the markdown-like link only at the line start.
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
- Clearly specified
- Newbie friendliness
- 45/100