Skip to content
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

Move usings on paste off the UI thread #61092

Merged
merged 3 commits into from
May 3, 2022

Conversation

ryzngard
Copy link
Contributor

@ryzngard ryzngard commented May 2, 2022

Fixes AB#1370350

Gif below had an artificial wait added to showcase the feature

4b821712-1fbb-43f4-b47a-185422b2017f

@ryzngard ryzngard requested a review from a team as a code owner May 2, 2022 20:46
using var _ = executionContext.OperationContext.AddScope(allowCancellation: true, DialogText);
var cancellationToken = executionContext.OperationContext.UserCancellationToken;
var indicatorFactory = document.Project.Solution.Workspace.Services.GetRequiredService<IBackgroundWorkIndicatorFactory>();
var backgroundWorkContext = indicatorFactory.Create(
Copy link
Member

Choose a reason for hiding this comment

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

why not put this in a using block?

see the code in GoToDefinitionCommandHandler for a suitable pattern on how to do the sync->async jump that does all this without the need for a TAsk.Run

@dibarbet
Copy link
Member

dibarbet commented May 2, 2022

nice! Does typing also cancel, or only escape?

@ryzngard
Copy link
Contributor Author

ryzngard commented May 2, 2022

nice! Does typing also cancel, or only escape?

Typing and loss of focus also dismisses and stops the action.

{
_threadingContext = threadingContext;
_globalOptions = globalOptions;
_listener = listenerProvider.GetListener(FeatureAttribute.GoToDefinition);
Copy link
Member

Choose a reason for hiding this comment

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

not the right listener :) there should be a listener for AddImportsOnPaste (and, if not, you can add one).

Copy link
Member

Choose a reason for hiding this comment

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

we shoudl also def have an integration test added to validate this works

Copy link
Contributor Author

Choose a reason for hiding this comment

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

woops :) copy-paste failure

}

private async Task ExecuteAsync(Document document, SnapshotSpan snapshotSpan, ITextView textView)
{
Copy link
Member

Choose a reason for hiding this comment

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

nit. consider adding: _threadingContext.ThrowIfNotOnUIThread();

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Interesting. Do we need this work to be on the UI thread to start?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

oh... will the indicator factory fail?

Copy link
Member

Choose a reason for hiding this comment

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

It may be fine (I think I wrote things to be fine either way). However, this would be helpful for documenting how things bounce across threads. It's totally optional on your part of you want to do this here.

@CyrusNajmabadi
Copy link
Member

Logic looks good! Can we do an integration tests? It shoudln't be hard right? The benefit of hte async listener stuff is that it allowed you to do the paste, then say "i want to wait for all the async worked kicked of for feature 'X'" then validate the final state once that async work is done. I imagine we could easily validate this by like pasing CancellationToken and hten seeing that System.Threading.Wahtever is added.

Copy link
Member

@CyrusNajmabadi CyrusNajmabadi left a comment

Choose a reason for hiding this comment

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

Approved with integration test forthcoming :)

@ryzngard
Copy link
Contributor Author

ryzngard commented May 3, 2022

Approved with integration test forthcoming :)

Will update the existing test :) Wanted to check logic was good before mucking around too much with it

@@ -160,9 +161,14 @@ static void Main(string[] args)

private async Task PasteAsync(string text, CancellationToken cancellationToken)
{
var provider = await TestServices.Shell.GetComponentModelServiceAsync<IAsynchronousOperationListenerProvider>(HangMitigatingCancellationToken);
var waiter = (IAsynchronousOperationWaiter)provider.GetListener(FeatureAttribute.AddImportsOnPaste);
Copy link
Member

Choose a reason for hiding this comment

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

note: i don't think we shoudl do this in paste. paste should just send the paste through. however, the test that wants to validate add-import-on-paste can itself then wait on this async work.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

All of the tests need to validate this. Even if no work is added the waiter should be there to validate that it's not being triggered right? Otherwise we could be incorrectly testing the negative. We could accidentally have it enabled and not wait on the work being done, so the test would still pass for broken code.

Copy link
Member

Choose a reason for hiding this comment

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

All of the tests need to validate this. Even if no work is added the waiter should be there to validate that it's not being triggered right? Otherwise we could be incorrectly testing the negative. We could accidentally have it enabled and not wait on the work being done, so the test would still pass for broken code.

That's a compelling argument. in that case, i woudl say that we're not waiting on AddImportsOnPaste, we're effectivley waiting on any paste feature. so i would call this FeatureAttribute.Paste :)

(doesn't ahve to happen now).

@ryzngard ryzngard merged commit d50be05 into dotnet:main May 3, 2022
@ryzngard ryzngard deleted the issues/async_usings_on_paste branch May 3, 2022 18:50
@ghost ghost added this to the Next milestone May 3, 2022
333fred added a commit that referenced this pull request May 4, 2022
…ures/required-members

* upstream/main: (368 commits)
  Use new helpers
  Cleanup
  Update src/EditorFeatures/CSharpTest/StringCopyPaste/StringCopyPasteCommandHandlerTests.cs
  Restore
  Add docs
  Fix
  Update Tools|Options code ordering to match waht is in the UI
  Use correct directory for loghub logs (#61104)
  Move usings on paste off the UI thread (#61092)
  Create shared helper to handle finding extensions in analyzer references
  Code style to convert byte arrays to UTF8 strings (#60647)
  Fixup
  Enable semantic token LSP tests + re-enable quick info tests (#61098)
  Simplify initialization logic
  Simplify common CWT pattern
  Simplify common CWT pattern
  Simplify common CWT pattern
  Simplify common CWT pattern
  Simplify common CWT pattern
  Simplify common CWT pattern
  ...
333fred added a commit to 333fred/roslyn that referenced this pull request May 13, 2022
…o poison

* upstream/features/required-members: (369 commits)
  Update tests after merge
  Use new helpers
  Cleanup
  Update src/EditorFeatures/CSharpTest/StringCopyPaste/StringCopyPasteCommandHandlerTests.cs
  Restore
  Add docs
  Fix
  Update Tools|Options code ordering to match waht is in the UI
  Use correct directory for loghub logs (dotnet#61104)
  Move usings on paste off the UI thread (dotnet#61092)
  Create shared helper to handle finding extensions in analyzer references
  Code style to convert byte arrays to UTF8 strings (dotnet#60647)
  Fixup
  Enable semantic token LSP tests + re-enable quick info tests (dotnet#61098)
  Simplify initialization logic
  Simplify common CWT pattern
  Simplify common CWT pattern
  Simplify common CWT pattern
  Simplify common CWT pattern
  Simplify common CWT pattern
  ...
@Cosifne Cosifne modified the milestones: Next, 17.3 P2 May 31, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants