Skip to content

Make the extract-method command async (using the background work indicator).#61375

Merged
CyrusNajmabadi merged 8 commits into
dotnet:mainfrom
CyrusNajmabadi:extractMethodAsync
May 18, 2022
Merged

Make the extract-method command async (using the background work indicator).#61375
CyrusNajmabadi merged 8 commits into
dotnet:mainfrom
CyrusNajmabadi:extractMethodAsync

Conversation

@CyrusNajmabadi

Copy link
Copy Markdown
Contributor

No description provided.

@CyrusNajmabadi CyrusNajmabadi requested a review from a team as a code owner May 18, 2022 04:36
@ghost ghost added the Area-IDE label May 18, 2022
[ContentType(ContentTypeNames.VisualBasicContentType)]
[Name(PredefinedCommandHandlerNames.ExtractMethod)]
[Order(After = PredefinedCommandHandlerNames.DocumentationComments)]
internal sealed class ExtractMethodCommandHandler : ICommandHandler<ExtractMethodCommandArgs>

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

no need for abstract base class. There was no lang specific code in the subclasses. so just merged up into a single concrete command handler exported for C# and VB.


var document = args.SubjectBuffer.CurrentSnapshot.GetOpenDocumentInCurrentContextWithChanges();
if (document is null)
return false;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

moved bail out logic up.

var snapshotAfterFormatting = textBuffer.CurrentSnapshot;
var documentAfterFormatting = snapshotAfterFormatting.GetOpenDocumentInCurrentContextWithChanges();
_renameService.StartInlineSession(documentAfterFormatting, methodNameAtInvocation.Span, cancellationToken);
await _threadingContext.JoinableTaskFactory.SwitchToMainThreadAsync(cancellationToken);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

it makes sense to me, but just wanted to confirm the intention that changes could be applied, then the indicator cancelled before the rename starts.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

actually... you raise a good point. we want the indicator dismissed once we start inline-rename. so i need to change this logic.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

oh no. this is correct. inline rename will start, and all this code will return and we'll dismiss the BWI.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

it makes sense to me, but just wanted to confirm the intention that changes could be applied, then the indicator cancelled before the rename starts.

technically it should not be possible due to there being no opportunity for the user to do anything that would cause the cancellation. but technically, yes. if somehow some path canceled that token (again, shouldn't be possible) then we'd apply teh extract method, but not start the inline rename session.


// We're about to make an edit ourselves. so disable the cancellation that happens on editing.
waitContext.CancelOnEdit = false;
formattedDocument.Project.Solution.Workspace.ApplyDocumentChanges(formattedDocument, cancellationToken);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

  1. what happens if we cancel in the middle of the undo transaction? Do we need to explicitly complete it / throw it away if we cancel?
  2. Is it possible to partially apply changes in ApplyDocumentChanges? Or if it cancels does it do all or nothing

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

so we're on the UI thread now. so it shouldn't be possible to have a cancel happen at this point since only UI affinitized ops can cause cancellation. it's a bit of an odd duck here i admit as we could also effectively have the same effect by passing CancellationToken.None here :-/

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

interesting - but we still need the cancelOnEdit = false so that it doesn't immediately cancel after we finish applying changes, switch off the UI thread and cancel?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

yes, we still need the cancelOnEdit false. but not because of any switching. because if we don't, we'll make the edit, and the cancellation token will get signaled. ANd then any further work we do (on any thread) that checks the token will think we are cancled and will abort.

@CyrusNajmabadi CyrusNajmabadi requested a review from dibarbet May 18, 2022 18:29
Comment thread src/EditorFeatures/Core/ExtractMethod/ExtractMethodCommandHandler.cs Outdated
@CyrusNajmabadi CyrusNajmabadi enabled auto-merge May 18, 2022 19:24
@CyrusNajmabadi CyrusNajmabadi merged commit 67e035e into dotnet:main May 18, 2022
@ghost ghost added this to the Next milestone May 18, 2022
@Cosifne Cosifne modified the milestones: Next, 17.3 P2 May 31, 2022
@CyrusNajmabadi CyrusNajmabadi deleted the extractMethodAsync branch July 1, 2022 16:28
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.

3 participants