microsoft / microsoft/monaco-editor

[Bug] The setMonarchTokensProvider rule matches incorrectly

Open
#4,423 0 comments 0 reactions 0 assignees View on GitHub

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?

https://microsoft.github.io/monaco-editor/playground.html?source=v0.47.0#XQAAAAJ3AgAAAAAAAABBqQkHQ5NjdMjwa-jY7SIQ9S7DNlzs5W-mwj0fe1ZCDRFc9ws9XQE0SJE1jc2VKxhaLFIw9vEWSxW3yscw15yhqT9RSPWJrMzrzzSucLOyj4KniHiZKetJN83iKCIna1cjwLSQRb-627e2KFeF76IibMJYXcyCAabseBrQkTTa5u03nxl7PDMek0QnoWEHXoFGAxHI7185bCqcVeGupMoN1QDNfNSR246VtJ4QRWEJxFU0o-uhd7n3nPv1k5LD3MMBeurNiqRkFmSUdzo7Zm2ctJApX0uxBtviaIB6s5RC7yguN5eLYnP7Gw_bXMpK-nK1MJqu168K-xMo5DtZv2cwEhTPmGIwXuB8JRYUatJ1IMkGLuT3NHJocARPGJK5S-AankYTobQARTCNLxbvoql1HvId9zgLeM9iYJ1AhotMnIQdwooQSMdC3z3UkFfhCGuoBaM7SUeABEdc3YPRoFQs0fn6rBxu8dKYQeHNECecVsKgnaDQjn_qgswN1z5-IOlYTNM1_vWDZaA

Monaco Editor Playground Link

I have a list of rules

const keywords = [Stop,.............]
  monaco?.languages.setMonarchTokensProvider('cpp', {
      tokenizer: {
        root: [[new RegExp(`\\b(${keywords.join('|')})\\b`), 'keyword']]
      },
      keywords: [Stop,.............]
    });

I expected all keywords to be highlighted, but now Stop is included in the keywords list, but words ending in Stop will still be highlighted

image

The effect at the beginning of Stop is correct, but the effect at the end of Stop is wrong and should be shown in black, not yellow

How do I make it highlighted as a keyword only for Stop words, it shouldn't be highlighted if there are letters or words before and after it

Monaco Editor Playground Code

monaco.languages.register({ id: "mySpecialLanguage" });

monaco.languages.setMonarchTokensProvider("mySpecialLanguage", {
	tokenizer: {
		root: [
      		[/(?<!\w)Stop(?!\w)/, 'keyword']
		],
	},
});

monaco.editor.defineTheme("myCoolTheme", {
	base: "vs",
	inherit: false,
	rules: [
		{ token: 'keyword', foreground: 'f1d710' },
	],
	colors: {
		"editor.foreground": "#000000",
	},
});


monaco.editor.create(document.getElementById("container"), {
	theme: "myCoolTheme",
	value: `Stop
QdStop
qdStop
11Stop
StopSS
Stop11
Stopdd 
	`,
	language: "mySpecialLanguage",
});

Reproduction Steps

No response

Actual (Problematic) Behavior

No response

Expected Behavior

No response

Additional Context

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the linked Monaco Editor Playground reproduction and the setMonarchTokensProvider tokenizer rule using the Stop regex. Verify how token boundaries behave for Stop, QdStop, StopSS, and similar values; done means only the standalone Stop token is highlighted as a keyword.

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.