-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Show only a single suggestion adornment for multi-line regex patterns #122601
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
…-line regex patterns
Changed the diagnostic span to only cover the part of the syntax before the
pattern argument (e.g., "new Regex(" instead of "new Regex(multiline pattern)").
This prevents the editor from showing suggestion adornments on every line of
multi-line regex strings.
The change affects both:
- Object creation expressions (new Regex(...))
- Static method invocations (Regex.IsMatch(input, pattern))
Updated all related tests to expect the new narrower diagnostic spans.
Co-authored-by: stephentoub <[email protected]>
|
@copilot, let's narrow the span further to go up to just before the opening parenthesis, eg in |
...System.Text.RegularExpressions/tests/FunctionalTests/UpgradeToGeneratedRegexAnalyzerTests.cs
Outdated
Show resolved
Hide resolved
Per feedback, further narrowed the diagnostic span to end just before the opening parenthesis. For example: - `Regex.IsMatch` instead of `Regex.IsMatch(input, ` - `new Regex` instead of `new Regex(` This provides a cleaner visual indicator in the editor. Also updated the code fixer to walk up the syntax tree to find the containing InvocationExpressionSyntax or ObjectCreationExpressionSyntax since FindNode now returns a nested node with the narrower span. Co-authored-by: stephentoub <[email protected]>
Done in 562a75e. The diagnostic span now ends just before the opening parenthesis, e.g., |
Co-authored-by: stephentoub <[email protected]>
…ssionSyntax
Added a test case for target-typed new syntax (e.g., `Regex r = new("abc")`).
Updated the analyzer and code fixer to handle ImplicitObjectCreationExpressionSyntax
so the diagnostic span correctly ends before the opening parenthesis.
Co-authored-by: stephentoub <[email protected]>
|
Tagging subscribers to this area: @dotnet/area-system-text-regularexpressions |
There was a problem hiding this 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 addresses a UI clutter issue where multi-line regex patterns displayed suggestion adornments ("...") on every line. The fix narrows the diagnostic span to cover only the method/constructor name before the opening parenthesis, excluding the argument list containing potentially multi-line patterns.
Key changes:
- Modified diagnostic location reporting to exclude argument lists, preventing adornments on multi-line patterns
- Updated code fixer to walk the syntax tree to find containing expressions since the diagnostic span is now narrower
- Added support for target-typed
newexpressions (ImplicitObjectCreationExpressionSyntax)
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
UpgradeToGeneratedRegexAnalyzer.cs |
Added GetLocationBeforeArguments helper method that creates narrower diagnostic spans ending before the opening parenthesis; updated diagnostic reporting calls to use the new location; added support for ImplicitObjectCreationExpressionSyntax |
UpgradeToGeneratedRegexCodeFixer.cs |
Modified to walk up the syntax tree using AncestorsAndSelf() to find the containing invocation or object creation expression, since FindNode now returns a nested node with the narrower span; added support for ImplicitObjectCreationExpressionSyntax |
UpgradeToGeneratedRegexAnalyzerTests.cs |
Updated all 116 existing tests to expect narrower diagnostic spans that exclude argument lists; added ConstructRegexInvocationWithDiagnostic helper method to generate test strings with correct diagnostic markers; added new test case TestTargetTypedNew for target-typed new syntax |
Description
The SYSLIB1045 analyzer was reporting diagnostics that spanned entire multi-line regex patterns, causing the editor to show "..." suggestion adornments on every line of the pattern string.
Changed the diagnostic span to only cover the syntax before the opening parenthesis:
Regex.IsMatchinstead ofRegex.IsMatch(input, "multi\nline\npattern")new Regexinstead ofnew Regex("multi\nline\npattern")newinstead ofnew("multi\nline\npattern")for target-typed newChanges:
GetLocationBeforeArgumentshelper that creates a span from expression start to just before the argument list's opening parenthesisInvocationExpressionSyntax,ObjectCreationExpressionSyntax, orImplicitObjectCreationExpressionSyntaxsinceFindNodenow returns a nested node with the narrower spanImplicitObjectCreationExpressionSyntax(target-typed new) in both the analyzer and code fixerCustomer Impact
Multi-line regex patterns show cluttered UI with suggestion dots on every line, making code harder to read.
Regression
No, this is a UX improvement.
Testing
All 117
UpgradeToGeneratedRegexAnalyzertests pass with updated span expectations, including the new test for target-typed new.Risk
Low. The change only affects diagnostic location reporting—the code fixer still works correctly because it walks up the syntax tree to find the containing expression.
Original prompt
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.