fix: detectMethodArgContext skips parens inside strings and regex literals#712
Conversation
|
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. |
…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.
605a898 to
3b160bf
Compare
|
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
left a comment
There was a problem hiding this comment.
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.
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.tsskipStringBackward()to jump past the entire string/in regex-start context triggersskipRegexBackward()to jump past the entire regexskipStringBackward()helper (tracks escape sequences)skipRegexBackward()helper (tracks escape sequences)Changes in
playgroundContextDetector.tsmaskNonCodeRegions()helper that blanks the contents of string literals, line comments (//), and block comments (/* */) while preserving string length and newline positionsdetectMethodArgContext()now passes text throughmaskNonCodeRegions()before the paren scan so only real-code parentheses are countedTest coverage
db.users.find({ note: ) , na-> correctly detectsmethod-argumentdb.users.find({ name: /foo(bar)/, ag-> correctly detectsmethod-argumentdb.users.find({ active: true /* ) */, na-> correctly detectsmethod-argument(comment parens ignored)db.users.find({ active: true // ) na-> correctly detectsmethod-argument(line comment parens ignored)Issue
Fixes #710
Verification