Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Dec 17, 2025

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.IsMatch instead of Regex.IsMatch(input, "multi\nline\npattern")
  • new Regex instead of new Regex("multi\nline\npattern")
  • new instead of new("multi\nline\npattern") for target-typed new

Changes:

  • Added GetLocationBeforeArguments helper that creates a span from expression start to just before the argument list's opening parenthesis
  • Updated the code fixer to walk up the syntax tree to find the containing InvocationExpressionSyntax, ObjectCreationExpressionSyntax, or ImplicitObjectCreationExpressionSyntax since FindNode now returns a nested node with the narrower span
  • Added support for ImplicitObjectCreationExpressionSyntax (target-typed new) in both the analyzer and code fixer
  • Updated all 116 analyzer tests to expect the narrower diagnostic spans
  • Added a new test case for target-typed new syntax

Customer 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 UpgradeToGeneratedRegexAnalyzer tests 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

This section details on the original issue you should resolve

<issue_title>Show only a single suggestion adornment for a multi-line string</issue_title>
<issue_description>Having the dots adornment on every single line of the string is cluttering the editor.
image</issue_description>

<agent_instructions>Following the final suggestion in the issue, change the span the analyzer flags to be just the part of the syntax before the pattern so that a large multiline pattern doesn't end up with many adornments.</agent_instructions>

Comments on the Issue (you are @copilot in this section)

@CyrusNajmabadi no clue what this is. will have to see if this even is roslyn... @CyrusNajmabadi @stephentoub do you know who owns this analyzer? @ Tagging subscribers to this area: @dotnet/area-system-text-regularexpressions See info in [area-owners.md](https://github.com/dotnet/runtime/blob/main/docs/area-owners.md) if you want to be subscribed.
Issue Details

Having the dots adornment on every single line of the string is cluttering the editor.
image

Author: tmat
Assignees: CyrusNajmabadi
Labels:

area-System.Text.RegularExpressions, untriaged, code-analyzer

Milestone: -
@stephentoub > @stephentoub do you know who owns this analyzer?

Probably me at this point.

Is this something the analyzer is doing incorrectly?</comment_new>
<comment_new>@tmat
If the analyzer is reporting a single location span then my guess is that it's the editor/LSP client, who creates the "..." tags for the diagnostic.</comment_new>
<comment_new>@stephentoub

If the analyzer is reporting a single location span then my guess is that it's the editor/LSP client, who creates the "..." tags for the diagnostic.

In that case, @CyrusNajmabadi, why move this to runtime? </comment_new>
<comment_new>@CyrusNajmabadi
The desired change here would be too have the analyzer select a smaller span. Perhaps from the start of a string to the end of the line. </comment_new>


💡 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.

…-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]>
@stephentoub
Copy link
Member

@copilot, let's narrow the span further to go up to just before the opening parenthesis, eg in Regex.IsMatch(input, pattern) to just Regex.IsMatch

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]>
Copy link
Contributor Author

Copilot AI commented Dec 17, 2025

@copilot, let's narrow the span further to go up to just before the opening parenthesis, eg in Regex.IsMatch(input, pattern) to just Regex.IsMatch

Done in 562a75e. The diagnostic span now ends just before the opening parenthesis, e.g., Regex.IsMatch instead of Regex.IsMatch(input, pattern). Also updated the code fixer to walk up the syntax tree to find the containing invocation/object creation expression.

…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]>
@stephentoub stephentoub marked this pull request as ready for review December 17, 2025 15:32
Copilot AI review requested due to automatic review settings December 17, 2025 15:32
@dotnet-policy-service
Copy link
Contributor

Tagging subscribers to this area: @dotnet/area-system-text-regularexpressions
See info in area-owners.md if you want to be subscribed.

Copy link
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 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 new expressions (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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Show only a single suggestion adornment for a multi-line string

2 participants