Skip to content

fix: detectMethodArgContext skips parens inside strings and regex literals#712

Merged
tnaum-ms merged 3 commits into
microsoft:mainfrom
hanhan761:fix-710-method-arg-context-literals
Jun 22, 2026
Merged

fix: detectMethodArgContext skips parens inside strings and regex literals#712
tnaum-ms merged 3 commits into
microsoft:mainfrom
hanhan761:fix-710-method-arg-context-literals

Conversation

@hanhan761

@hanhan761 hanhan761 commented May 30, 2026

Copy link
Copy Markdown
Contributor

Summary

ShellCompletionProvider.detectMethodArgContext() now skips parentheses inside string literals and regex literals when scanning backward for the unmatched (. Similarly, playgroundContextDetector.detectMethodArgContext() masks all non-code regions (strings, line comments, block comments) before the paren scan. Previously, punctuation inside strings, regexes, or comments could shift the open/close paren depth count and cause completions to disappear.

Changes in ShellCompletionProvider.ts

  • Added string-aware backward scan: quotes trigger skipStringBackward() to jump past the entire string
  • Added regex-aware backward scan: / in regex-start context triggers skipRegexBackward() to jump past the entire regex
  • Added skipStringBackward() helper (tracks escape sequences)
  • Added skipRegexBackward() helper (tracks escape sequences)
  • Both helpers return the position before the opening delimiter

Changes in playgroundContextDetector.ts

  • Added maskNonCodeRegions() helper that blanks the contents of string literals, line comments (//), and block comments (/* */) while preserving string length and newline positions
  • detectMethodArgContext() now passes text through maskNonCodeRegions() before the paren scan so only real-code parentheses are counted

Test coverage

  • db.users.find({ note: ) , na -> correctly detects method-argument
  • db.users.find({ name: /foo(bar)/, ag -> correctly detects method-argument
  • db.users.find({ active: true /* ) */, na -> correctly detects method-argument (comment parens ignored)
  • db.users.find({ active: true // ) na -> correctly detects method-argument (line comment parens ignored)

Issue

Fixes #710

Verification

  • All 553 shell tests pass (17 suites)
  • All 89 completion provider tests pass
  • Two new TDD tests for comment-inside-call scenarios pass

Copilot AI review requested due to automatic review settings May 30, 2026 05:34
@hanhan761 hanhan761 requested a review from a team as a code owner May 30, 2026 05:34
@tnaum-ms tnaum-ms added the in-triage-queue We've seen your input and will triage it label Jun 4, 2026
@tnaum-ms

Copy link
Copy Markdown
Collaborator

Thank you again for this contribution. We are genuinely amazed by both the quality and the volume of improvements you have submitted. We also apologize for the delayed review while the 0.9.0 release was in flight. This fix improves completion correctness in a careful and practical way.

@tnaum-ms tnaum-ms removed the in-triage-queue We've seen your input and will triage it label Jun 19, 2026
@tnaum-ms tnaum-ms added this to the 0.9.1 milestone Jun 19, 2026
hanhan761 and others added 2 commits June 19, 2026 10:06
…erals

The backward scan in detectMethodArgContext now skips over parentheses
that appear inside string literals (single/double-quoted) and regex
literals, so that method-argument context detection works correctly
even when the filter object contains strings with ')' or regex
patterns with nested parens.

Adds skipStringBackward() and skipRegexBackward() helpers.

Fixes microsoft#710
Mask string and comment contents before counting parentheses so that punctuation inside line and block comments cannot corrupt the paren-depth scan.
@tnaum-ms tnaum-ms force-pushed the fix-710-method-arg-context-literals branch from 605a898 to 3b160bf Compare June 19, 2026 10:26
@tnaum-ms

Copy link
Copy Markdown
Collaborator

Small follow-up after rebasing onto main.

What was spotted: the backward scan in detectMethodArgContext counted every parenthesis it saw, including parens that live inside comments. So an input like db.users.find({ active: true /* ) */, na would see the ) in the comment, treat it as real code, and throw off the open/close paren count. The result was that method argument completion stopped working when a comment with parens appeared inside the call. The same applied to line comments such as // ).

What was fixed: before counting parentheses, the text is now passed through a small helper (maskNonCodeRegions) that blanks out the contents of string literals, line comments, and block comments while keeping the same length and newlines. The paren scan then runs on that masked copy, so only real code parens are counted. Method and collection names are still read from the original text, so nothing else changes.

Why: comments and strings should never affect how we match parentheses. The earlier version already handled strings, but comments were still being read as code. Masking both in one place keeps the logic simple and covers all three cases (strings, line comments, block comments) consistently.

Two new TDD tests cover the comment cases and now pass, along with the full suite.

@tnaum-ms tnaum-ms left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thank you again for this contribution, and for the quality and volume of the work you have been sending our way. We are genuinely amazed by it. Apologies for the delay in reviewing while we were focused on shipping 0.9.0.

Approved after rebasing onto main and a full validation pass (lint, the complete test suite, and a clean build). I pushed one small follow-up so that parentheses inside comments are ignored during method argument detection. Details are in the comment above.

@tnaum-ms tnaum-ms enabled auto-merge June 19, 2026 10:29
@tnaum-ms tnaum-ms disabled auto-merge June 22, 2026 20:56
@tnaum-ms tnaum-ms merged commit 0e58882 into microsoft:main Jun 22, 2026
5 checks passed
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.

Shell completions should ignore parentheses inside strings and regex literals

3 participants