Skip to content

Trim surrounding whitespace from line comment token#316763

Open
verifizieren wants to merge 1 commit into
microsoft:mainfrom
verifizieren:fix-249958-line-comment-whitespace
Open

Trim surrounding whitespace from line comment token#316763
verifizieren wants to merge 1 commit into
microsoft:mainfrom
verifizieren:fix-249958-line-comment-whitespace

Conversation

@verifizieren
Copy link
Copy Markdown

Fixes #249958

Problem

When a language configures its lineComment token with leading or trailing whitespace (e.g. " #"), Toggle Line Comment stops toggling: instead of removing an existing comment it keeps stacking new ones (# # # # #).

Root cause

In LineCommentCommand._analyzeLines, the comment is located at firstNonWhitespaceIndex(lineContent). When the configured token itself begins with whitespace, the inserted comment's leading space is that whitespace, so the detection offset points past it and the needle never matches — the command can never recognize (and therefore never remove) a comment it just inserted.

Fix

Trim the line comment token where the comment configuration is resolved (ResolvedLanguageConfiguration._handleComments). Whitespace around a comment marker is not semantically meaningful — insertion spacing is controlled separately by the editor.comments.insertSpace setting — so normalizing it at the source keeps every consumer consistent: comment toggling, the ${LINE_COMMENT} snippet variable, and notebook inline-variable detection.

This is intentionally a central, minimal fix rather than special-casing the toggle logic. The alternative (handle a whitespace-padded token inside _analyzeLines) is more code, more blast radius in hot editor logic, and would leave the snippet/notebook consumers still broken — but I'm happy to switch approaches if a maintainer prefers that.

Safety

  • All 11 distinct built-in lineComment values (//, #, --, ;, ', %, @REM, …) have no surrounding whitespace, so no built-in language is affected.
  • A whitespace-only token trims to "", which the existing if (!commentStr) guards already handle gracefully (treated as "no line comment").

Tests

Added a regression test in lineCommentCommand.test.ts asserting toggle reversibility (add, then remove) with a space-padded token.

Verified locally (headless Chromium, transpiled build):

  • Editor Contrib - Line Comment Command40 passing, including the new #249958 case, no regressions
  • Editor Contrib - Block Comment Command12 passing (related suite sharing the same comment-config resolution), no regressions

Copilot AI review requested due to automatic review settings May 16, 2026 08:58
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR normalizes configured line-comment markers by trimming surrounding whitespace at language configuration resolution time, fixing Toggle Line Comment behavior for whitespace-padded tokens.

Changes:

  • Trims string and object-form lineComment tokens in ResolvedLanguageConfiguration.
  • Adds a regression test for toggling a whitespace-padded line comment token.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
src/vs/editor/common/languages/languageConfigurationRegistry.ts Normalizes resolved line comment tokens with trim().
src/vs/editor/contrib/comment/test/browser/lineCommentCommand.test.ts Adds regression coverage for toggling a whitespace-padded token.

Comment thread src/vs/editor/common/languages/languageConfigurationRegistry.ts
A line comment token configured with leading or trailing whitespace
(e.g. " #") broke Toggle Line Comment: the inserted comment could
never be detected again, so each toggle stacked another comment
instead of removing it.

Whitespace around the marker is not semantically meaningful - spacing
on insertion is controlled by `editor.comments.insertSpace` - so the
token is now trimmed when the comment configuration is resolved. This
also keeps the `${LINE_COMMENT}` snippet variable and notebook inline
variable detection consistent. No built-in language is affected.

Fixes microsoft#249958
@verifizieren verifizieren force-pushed the fix-249958-line-comment-whitespace branch from dd9448d to 77a8508 Compare May 16, 2026 09:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Line comment toggling does not work when line comment starts with a whitespace

3 participants