-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
Add usings on paste #48501
Add usings on paste #48501
Conversation
thread await dialog to pop up to a user if it is taking a while with a "cancel" button
If there are more than one namespace containing the same class name, will it add some random using? or it will ignore it? |
Right now if there are conflicting usings it doesn't add any. This follows the design of the lightbulb command that does the same thing. IMO We should pursue fixes in this area separately as we find them, but I'm open to discussion on it. |
This is the behavior I'm expecting ❤️ |
Looking into Integration Test failures, but I think this behavior represents final design. |
src/VisualStudio/IntegrationTest/IntegrationTests/CSharp/CSharpAddMissingUsingsOnPaste.cs
Outdated
Show resolved
Hide resolved
using (var telemetry = VisualStudio.EnableTestTelemetryChannel()) | ||
{ | ||
VisualStudio.Editor.Paste(@" | ||
Task DoThingAsync() => Task.CompletedTask;"); |
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.
I would not expect any using directives to be added in this case, because the code was not originally copied from source code inside Visual Studio.
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.
That was discussed as a design, but ultimately from #45853 we indicated that timeboxing was more appropriate. If the code is originally copied from Visual Studio we could speed up the process by knowing the usings without having to calculate. That work is not covered in this PR, and out of scope for #45853
src/EditorFeatures/Core/AddUsings/IAutomaticallyAddMissingUsings.cs
Outdated
Show resolved
Hide resolved
src/VisualStudio/Core/Def/Implementation/AddUsingsOnPaste/VisualStudioAddUsingsOnPaste.cs
Outdated
Show resolved
Hide resolved
src/VisualStudio/Core/Def/Implementation/AddUsingsOnPaste/VisualStudioAddUsingsOnPaste.cs
Outdated
Show resolved
Hide resolved
src/VisualStudio/Core/Def/Implementation/AddUsingsOnPaste/VisualStudioAddUsingsOnPaste.cs
Outdated
Show resolved
Hide resolved
src/VisualStudio/Core/Def/Implementation/AddUsingsOnPaste/VisualStudioAddUsingsOnPaste.cs
Outdated
Show resolved
Hide resolved
...Features/Core/Portable/CodeRefactorings/AddMissingImports/AddMissingImportsAnalysisResult.cs
Outdated
Show resolved
Hide resolved
Design questions:
-- Major concerns: copy/pastes can be large. i.e. full multi-thousand line methods/classes. What is our perf on somehting like that? |
src/EditorFeatures/Core/AddUsings/IAutomaticallyAddMissingImportsService.cs
Outdated
Show resolved
Hide resolved
src/EditorFeatures/Core/Implementation/PasteTracking/PasteTrackingPasteCommandHandler.cs
Outdated
Show resolved
Hide resolved
src/EditorFeatures/Core/Implementation/PasteTracking/PasteTrackingPasteCommandHandler.cs
Outdated
Show resolved
Hide resolved
I'm going to hold off on reviewing until we discuss the design/implications here :) |
We already have that. This request was made specifically to automatically do it.
Would you consider this blocking this checkin, or blocking the feature complete story as a whole? I'd like to file a follow up item for a disable checkbox.
I agree this would help in cases where we are copying from VS, but we want to cover scopes where we are not. I'm happy to do a follow up item to improve performance for operations only done in VS
Likely slow, but I also think that's okay. Cancel should work immediately and allow the user to unblock if they wish. |
…AddMissingImportsAnalysisResult.cs Co-authored-by: Joey Robichaud <joseph.robichaud@microsoft.com>
src/EditorFeatures/Core/Implementation/AddImports/AbstractAddImportsPasteCommandHandler.cs
Outdated
Show resolved
Hide resolved
|
||
#pragma warning disable VSTHRD102 // Implement internal logic asynchronously | ||
var updatedDocument = _threadingContext.JoinableTaskFactory.Run(() => addMissingImportsService.AddMissingImportsAsync(document, textSpan, cancellationToken)); | ||
#pragma warning restore VSTHRD102 // Implement internal logic asynchronously |
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.
is this what we do elsewhere? I'm not super familiar with .OperationContext. but if this is our regular pattern then 👍
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.
OperationContext just requires that we have a disposable scope, which will block the UI interaction. Since command handlers are synchronous, this is the only way to take the async work and do it all in a synchronous fashion. If we go full async, we no longer will need an OperationContext or the disposable scope.
src/EditorFeatures/VisualBasic/AddImports/VisualBasicAddImportsOnPasteCommandHandler.vb
Outdated
Show resolved
Hide resolved
src/EditorFeatures/VisualBasic/AddImports/VisualBasicAddImportsOnPasteCommandHandler.vb
Outdated
Show resolved
Hide resolved
} | ||
|
||
public async Task<Project> AddMissingImportsAsync(Document document, TextSpan textSpan, CancellationToken cancellationToken) | ||
/// <inheritdoc/> | ||
public async Task<AddMissingImportsAnalysisResult> AnalyzeAsync(Document document, TextSpan textSpan, CancellationToken cancellationToken) |
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.
AddMissingImportsAnalysisResult is an internal type right?
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.
Correct
.../Core/Portable/CodeRefactorings/AddMissingImports/AbstractAddMissingImportsFeatureService.cs
Outdated
Show resolved
Hide resolved
src/VisualStudio/CSharp/Impl/Options/AdvancedOptionPageControl.xaml
Outdated
Show resolved
Hide resolved
src/VisualStudio/VisualBasic/Impl/Options/AdvancedOptionPageControl.xaml
Outdated
Show resolved
Hide resolved
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.
Signing off on the conditional the final review feedback is addressed.
Awesome work! This will be very nice to get in. Hopefully we can also get some good perf numbers and then enable by default :)
src/VisualStudio/VisualBasic/Impl/Options/AdvancedOptionPageControl.xaml
Outdated
Show resolved
Hide resolved
Co-authored-by: CyrusNajmabadi <cyrus.najmabadi@gmail.com>
Hello @ryzngard! Because this pull request has the p.s. you can customize the way I help with merging this pull request, such as holding this pull request until a specific person approves. Simply @mention me (
|
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.
Auto-approval
* upstream/master: (47 commits) Make compiler server logging explicit (dotnet#48557) [master] Update dependencies from dotnet/arcade (dotnet#48274) Handle removed project in ExternalErrorDiagnosticUpdateSource Report an error for ref-returning auto-properties declared in an interface. (dotnet#49510) Add usings on paste (dotnet#48501) Determinism test needs to use bootstrap compiler (dotnet#49483) Simplify and reduce amount of work performed by SourcePropertySymbolBase constructor. (dotnet#49360) Updates test Simplify Split Document/Workspace handler into only handling open/closed documents respectively. only report watson once. Additional usage of a PooledHashset. (dotnet#49459) Loc checkin Update src/Features/CSharp/Portable/CodeRefactorings/ConvertLocalFunctionToMethod/CSharpConvertLocalFunctionToMethodCodeRefactoringProvider.cs Preserve annotation on comment trivia when performing formatting. Validate arguments to IAsyncCompletionSource methods Determinism fixes for AnonymousTypes in VB (dotnet#49467) Collect nfw information for a crash we're seeing. Make sure to not discard text changes when no reference changes are present Create an unsafe method from a local function when necessary (dotnet#49389) ...
Addresses #45853
Hook up paste command to also automatically add usings. Operation uses thread await dialog to pop up to a user if it is taking a while with a "cancel" button. The dialog will not always show, but follows the IUIThreadOperationContext scope rules
Edit:
This is disabled by default. The following followup issues were filed
#49442
#49443
#49444
Example GIF
note that the string has been changed
Undo Behavior
Currently undo works to undo added usings, and then added pasted code (two undos)
Failure Behavior
If the operation fails, the code is still pasted but no usings are added. No dialog or exception is currently provided to the user.