fix: prevent comment continuation when '//' appears inside strings#307798
Open
yogeshwaran-c wants to merge 1 commit intomicrosoft:mainfrom
Open
fix: prevent comment continuation when '//' appears inside strings#307798yogeshwaran-c wants to merge 1 commit intomicrosoft:mainfrom
yogeshwaran-c wants to merge 1 commit intomicrosoft:mainfrom
Conversation
When pressing Enter on a line containing '//' inside a string literal (e.g. '//this is a string' or "http://localhost"), VS Code incorrectly treated it as a line comment and added '//' on the next line. This is because the indentation context processor only stripped brackets from string/regex tokens but not comment markers, allowing the onEnter comment continuation rule to match '//' within strings. Strip comment markers (line comment token, block comment start/end) from string and regex tokens during indentation context processing, alongside the existing bracket stripping. This prevents any comment-like sequences inside strings from triggering comment continuation or other comment-aware onEnter rules. Closes microsoft#241802
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What kind of change does this PR introduce?
Bug fix
What is the current behavior?
When pressing Enter on a line containing
//inside a string literal (e.g.'//this is a string'or"http://localhost"), VS Code incorrectly treats it as a line comment and adds//on the next line.For example:
Similarly with URLs:
Closes #241802
What is the new behavior?
Comment markers (
//,/*,*/) are now stripped from string and regex tokens during indentation context processing, just like brackets already are. This prevents the onEnter comment continuation rule from matching//that appears inside string literals, while preserving correct comment continuation behavior for actual line comments.Additional context
Root cause: The
IndentationLineProcessor.getProcessedTokensmethod already removes brackets from string/regex/comment tokens to prevent bracket-based indentation rules from being triggered by brackets inside strings. However, it did not remove comment markers from string/regex tokens, so the regex-based onEnter comment continuation rule could match//that appeared inside string content.Fix approach: Added comment marker stripping (line comment token, block comment start/end tokens from the language configuration) for string and regex tokens, alongside the existing bracket stripping. This is a minimal, targeted fix that follows the same architectural pattern already used for brackets.
Files changed:
src/vs/editor/common/languages/supports/indentationLineProcessor.ts— Strip comment markers from string/regex tokenssrc/vs/editor/contrib/indentation/test/browser/indentationLineProcessor.test.ts— Added tests for the fix