dotnet/roslyn

Search for instances of Primary Constructor usage

Open

#70.436 aperta il 18 ott 2023

Vedi su GitHub
 (8 commenti) (2 reazioni) (0 assegnatari)C# (4257 fork)batch import
0 - BacklogArea-IDECodeLenshelp wanted

Metriche repository

Star
 (20.414 star)
Metriche merge PR
 (Merge medio 6g 17h) (256 PR mergiate in 30 g)

Descrizione

Summary

Locating references to Primary constructors can be challenging for types that are commonly referenced.

Background and Motivation

C# 12 includes a new feature called Primary constructors. For example:

public readonly struct Distance(double dx, double dy)
{
    public readonly double Magnitude { get; } = Math.Sqrt(dx * dx + dy * dy);
    public readonly double Direction { get; } = Math.Atan2(dy, dx);
}

This would previously have been written something like this:

public readonly struct Distance
{
    private readonly double dx;
    private readonly double dy;

    public readonly double Magnitude { get; }
    public readonly double Direction { get; }

    public Distance(double dx, double dy)
    {
        this.dx = dx;
        this.dy = dy;

        this.Magnitude = Math.Sqrt(dx * dx + dy * dy);
        this.Direction = Math.Atan2(dy, dx);
    }
}

In Visual Studio and Visual Studio Code, you can use the "Find References" feature to find references to a particular item.

Using the example without Primary constructors, I can use "Find References" to find the references to the constructor:

VS Code 1.83.1 w/C# DevKit 1.0.4: image

Visual Studio 17.8 Preview 4.0: image

With the use of Primary constructors, you can no longer get a list that only contains references to the constructor. You instead get a list of type references. These type references include constructor references, but that list can't be filtered to only include the constructor references.

VS Code 1.83.1 w/C# DevKit 1.0.4: image

Visual Studio 17.8 Preview 4.0: image

In this example, it isn't too bad to sort through and find the constructor references, but for a type that is commonly used in a codebase, the list of references can become quite noisy making this task more difficult.

I find the constructor references helpful to determine if an instance of a constructor is used at all and to evaluate the impact of a change to that constructor.

Proposed Feature

This is mostly intended as feedback, but here are a few ideas:

  • For types with Primary constructors, ask the user if they want to find references to the type or the constructor.

  • For reference links in the editor on types with Primary constructors, perhaps you could instead show something like the following:

    3 references | 1 constructor reference
    public readonly struct Distance(double dx, double dy)
    {
        public readonly double Magnitude { get; } = Math.Sqrt(dx * dx + dy * dy);
        public readonly double Direction { get; } = Math.Atan2(dy, dx);
    }
    
  • Add UI to enable filtering the references list when querying type references.

Alternative Designs

None

Guida contributor