public WaitIndicatorResult <T> Run <T>(string title, string message, bool allowCancel, Func <IWaitContext, Task <T> > asyncMethod, int totalSteps = 0) { _joinableTaskContext.VerifyIsOnMainThread(); using IWaitContext waitContext = new VisualStudioWaitContext(_waitDialogFactoryService.Value, title, message, allowCancel, totalSteps); try { #pragma warning disable VSTHRD102 // Deliberate usage T result = _joinableTaskContext.Factory.Run(() => asyncMethod(waitContext)); #pragma warning restore VSTHRD102 return(WaitIndicatorResult <T> .FromResult(result)); } catch (OperationCanceledException) { return(WaitIndicatorResult <T> .Cancelled); } catch (AggregateException aggregate) when(aggregate.InnerExceptions.All(e => e is OperationCanceledException)) { return(WaitIndicatorResult <T> .Cancelled); } }
public override async Task RenameAsync(IProjectTreeActionHandlerContext context, IProjectTree node, string value) { Requires.NotNull(context, nameof(Context)); Requires.NotNull(node, nameof(node)); Requires.NotNullOrEmpty(value, nameof(value)); string?oldFilePath = node.FilePath; string oldName = Path.GetFileNameWithoutExtension(oldFilePath); string newFileWithExtension = value; CodeAnalysis.Project?project = GetCurrentProject(); // Rename the file await CPSRenameAsync(context, node, value); if (await IsAutomationFunctionAsync() || node.IsFolder || _vsOnlineServices.ConnectedToVSOnline || FileChangedExtension(oldFilePath, newFileWithExtension)) { // Do not display rename Prompt return; } if (project is null) { return; } string newName = Path.GetFileNameWithoutExtension(newFileWithExtension); if (!await CanRenameTypeAsync(project, oldName, newName)) { return; } (bool result, Renamer.RenameDocumentActionSet? documentRenameResult) = await GetRenameSymbolsActionsAsync(project, oldFilePath, newFileWithExtension); if (!result || documentRenameResult == null) { return; } // Ask if the user wants to rename the symbol bool userWantsToRenameSymbol = await CheckUserConfirmationAsync(oldName); if (!userWantsToRenameSymbol) { return; } _threadingService.RunAndForget(async() => { Solution currentSolution = await PublishLatestSolutionAsync(); string renameOperationName = string.Format(CultureInfo.CurrentCulture, VSResources.Renaming_Type_from_0_to_1, oldName, value); WaitIndicatorResult <Solution> indicatorResult = _waitService.Run( title: VSResources.Renaming_Type, message: renameOperationName, allowCancel: true, token => documentRenameResult.UpdateSolutionAsync(currentSolution, token)); // Do not warn the user if the rename was cancelled by the user if (indicatorResult.IsCancelled) { return; } await _projectVsServices.ThreadingService.SwitchToUIThread(); if (_roslynServices.ApplyChangesToSolution(currentSolution.Workspace, indicatorResult.Result)) { return; } string failureMessage = string.Format(CultureInfo.CurrentCulture, VSResources.RenameSymbolFailed, oldName); _userNotificationServices.ShowWarning(failureMessage); }, _unconfiguredProject); }
public static bool WasCanceled(this WaitIndicatorResult result) => result == WaitIndicatorResult.Canceled;