Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Add code refactoring to offer usings for fully qualified names#81484

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Draft
Copilot wants to merge5 commits intomain
base:main
Choose a base branch
Loading
fromcopilot/add-usings-for-qualified-types

Conversation

Copy link
Contributor

CopilotAI commentedNov 29, 2025
edited
Loading

Summary

This PR implements a new code refactoring that offers to add using directives for fully qualified type names and simplify them.

Changes

  • CreateAbstractAddImportCodeRefactoringProvider insrc/Features/Core/Portable/AddImport/
  • Implement two code actions:
    • "Add import for '{0}'" - adds using and simplifies just the current occurrence
    • "Add import for '{0}' and simplify all occurrences" - adds using and finds all types from that namespace in the file and simplifies them
  • CreateCSharpAddImportCodeRefactoringProvider insrc/Features/CSharp/Portable/AddImport/
  • Add resource strings for both code actions
  • Add comprehensive tests including:
    • Tests for simplifying just the current occurrence
    • Tests for simplifying all occurrences in the file
    • Tests for mixed scenarios with other namespaces

Behavior

  • Offers refactoring when cursor is on any part of a qualified type reference
  • Does not offer if using already exists for that namespace
  • First action simplifies only the invoked qualified name
  • Second action finds and simplifies all types from the target namespace throughout the file
Original prompt

This section details on the original issue you should resolve

<issue_title>Offer to add usings for fully qualified names</issue_title>
<issue_description>It would be nice to have a codefix that offered to add usings for a fully qualified type and then simplified the name.

so if I had this:

publicstaticasyncSystem.Threading.Tasks.Task<string>GetGoodDealsAsync(){usingHttpClientclient=newHttpClient();usingHttpResponseMessageresponse=awaitclient.GetAsync("http://good-deals.com/");usingHttpContentcontent=response.Content;stringresult=awaitcontent.ReadAsStringAsync();return$"Here are the good deals:{result}";}

it would offer to do this:

usingSystem.Threading.Tasks;publicstaticasyncTask<string>GetGoodDealsAsync(){usingHttpClientclient=newHttpClient();usingHttpResponseMessageresponse=awaitclient.GetAsync("http://good-deals.com/");usingHttpContentcontent=response.Content;stringresult=awaitcontent.ReadAsStringAsync();return$"Here are the good deals:{result}";}```</issue_description><agent_instructions>Copilot, you're going to be creating a wholenewfeature here.1. this will be arefactoring.   So you will be creating aCodeRefactoringProvider.2.This will workfor C# andVB.Butwe're just going to start by making C# work.3. You'll need to create an `AbstractAddImportCodeRefactoringProvider`in `Microsoft.CodeAnalysis.Features`.  This can sit next toAbstractAddImportCodeFixProvider.cs4. The refactoring willworkinthefollowingfashion.Therefactoring will startwith the location the useris at,and will seeif itis on some name that binds to anamespace,and has a type name accessed off of it.  Note thatthis may beas simpleas `System.Console` oras complexas `global::System.Console` or `System.Collections.Generic.List<int>` etc.  Becausethisisin the shared 'Features' layer,you should use `ISyntaxFactsService` todo the analysis here so that it will workfor VB/C#.5. If you find such anamespacename,then you will offer the user a code action to `Add import and simplify type name`.6. Whenthis code action runs,it willdo3 things:1. It willadd the `using` to the proper scope(namespace or compilation unit).You canuse `IAddImportsService` to helpwiththis.2. It will then bothadd the `Simplifier.Annotation` annotation to thenamespace(or possiblynamespace+type)name thatit wasinvoked offof.This waythe codeaction infrastructurewill attemptto removethat superfluousname afterthe `using`is added.3. It will go through the file lookingfor types or expressions that startwith some name,and will checkif that nameis a type-namein thenewnamespace it's about toadd a `using`for.  These type names may now binddifferently,or become ambiguous.  So the refactoring should attempt to bind the current name,and replace itwith `SyntaxGenerator.TypeExpression(typeSymbol,true/false)`so thatthenameisnow qualifiedappropriately.  If thenameisambiguous,this willstay.Butif itis notambiguous, the code action infrastructurewill remove that qualification that youadded.1. You will then need to makea C#specificsubclass of yourabstractcoderefactoringprovider.Thereshould be almost nothinginthis.  It will exist just to properlyexport the `[ExportCodeRefactoringProvider(...)]`with apropername and language binding.Finally,add a lot of testsforthis.Add anew `AddUsingCodeRefactoringTests.cs` that sits next to `AddUsingTests.cs`.  This should use the VerifyCS patternlike `usingVerifyCS=CSharpCodeRefactoringVerifier<...>;`pattern.Test all your logic extensively.  Importantly, test simplificationin all the interesting places a qualified type name could show upin a type-onlylocation(like an inheritance list, parameter type,return type,typeof, `new T`, etc.).   And all the placeswhere it could start an expression, and validate those.Then, we want to test extensively that the logic to qualify existing names, and have them either become unqualified(ifthereis no ambiguity) orstay qualified(ifthereis) works.</agent_instructions>##Comments onthe Issue(youare @copilotinthissection)<comments></comments>

✨ Let Copilot coding agentset things up for you — coding agent works faster and does higher quality work when set up for your repo.

…efactoringProviderCo-authored-by: CyrusNajmabadi <4564579+CyrusNajmabadi@users.noreply.github.com>
CopilotAIand others added2 commitsNovember 29, 2025 10:36
Co-authored-by: CyrusNajmabadi <4564579+CyrusNajmabadi@users.noreply.github.com>
Co-authored-by: CyrusNajmabadi <4564579+CyrusNajmabadi@users.noreply.github.com>
CopilotAI changed the title[WIP] Add code fix to offer usings for fully qualified namesAdd code refactoring to offer usings for fully qualified namesNov 29, 2025
Copilot finished work on behalf ofCyrusNajmabadiNovember 29, 2025 10:45
{
Task M() => null;
}
""");

Choose a reason for hiding this comment

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

@copilot we should add two code actions. One that just does what you've done here (adds using and simplifies the name invoked on), but a second that does an 'add using and simplify all type names' which then looks for the namespace we're adding a using for and adds the simplification annotation. So if we addusing System; it will then fix up all System.Console in the file.

Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

Added both code actions in commit1afb500:

  1. "Add import for '{0}'" - simplifies only the current occurrence
  2. "Add import for '{0}' and simplify all occurrences" - finds all types from the namespace in the file and simplifies them

…spaceCo-authored-by: CyrusNajmabadi <4564579+CyrusNajmabadi@users.noreply.github.com>
Copilot finished work on behalf ofCyrusNajmabadiNovember 29, 2025 11:15
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@CyrusNajmabadiCyrusNajmabadiAwaiting requested review from CyrusNajmabadi

At least 1 approving review is required to merge this pull request.

Labels

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Offer to add usings for fully qualified names

2 participants

@CyrusNajmabadi

[8]ページ先頭

©2009-2025 Movatter.jp