private static ExtractMethodResult TryWithoutMakingValueTypesRef(
            Document document, NormalizedSnapshotSpanCollection spans, ExtractMethodResult result, CancellationToken cancellationToken)
        {
            OptionSet options = document.Project.Solution.Options;

            if (options.GetOption(ExtractMethodOptions.DontPutOutOrRefOnStruct, document.Project.Language) || !result.Reasons.IsSingle())
            {
                return(null);
            }

            var reason = result.Reasons.FirstOrDefault();
            var length = FeaturesResources.Asynchronous_method_cannot_have_ref_out_parameters_colon_bracket_0_bracket.IndexOf(':');

            if (reason != null && length > 0 && reason.IndexOf(FeaturesResources.Asynchronous_method_cannot_have_ref_out_parameters_colon_bracket_0_bracket.Substring(0, length), 0, length, StringComparison.Ordinal) >= 0)
            {
                options = options.WithChangedOption(ExtractMethodOptions.DontPutOutOrRefOnStruct, document.Project.Language, true);
                var newResult = ExtractMethodService.ExtractMethodAsync(
                    document, spans.Single().Span.ToTextSpan(), options, cancellationToken).WaitAndGetResult(cancellationToken);

                // retry succeeded, return new result
                if (newResult.Succeeded || newResult.SucceededWithSuggestion)
                {
                    return(newResult);
                }
            }

            return(null);
        }
        /// <returns>
        /// True: if a failure notification was displayed or the user did not want to proceed in a best effort scenario.
        ///       Extract Method does not proceed further and is done.
        /// False: the user proceeded to a best effort scenario.
        /// </returns>
        private bool TryNotifyFailureToUser(Document document, ExtractMethodResult result)
        {
            var notificationService = document.Project.Solution.Workspace.Services.GetService <INotificationService>();

            // see whether we will allow best effort extraction and if it is possible.
            if (!document.Project.Solution.Workspace.Options.GetOption(ExtractMethodOptions.AllowBestEffort, document.Project.Language) ||
                !result.Status.HasBestEffort() || result.Document == null)
            {
                if (notificationService != null)
                {
                    notificationService.SendNotification(
                        string.Join(Environment.NewLine, result.Reasons),
                        EditorFeaturesResources.ExtractMethodFailedReasons, NotificationSeverity.Error);
                }

                return(true);
            }

            // okay, best effort is turned on, let user know it is an best effort
            if (notificationService != null)
            {
                if (!notificationService.ConfirmMessageBox(
                        string.Join(Environment.NewLine, result.Reasons) + Environment.NewLine + Environment.NewLine +
                        EditorFeaturesResources.ExtractMethodStillGenerateCode,
                        EditorFeaturesResources.ExtractMethodFailedReasons, NotificationSeverity.Error))
                {
                    return(true);
                }
            }

            return(false);
        }
Exemple #3
0
        private void ApplyChangesToBuffer(ExtractMethodResult extractMethodResult, ITextBuffer subjectBuffer, CancellationToken cancellationToken)
        {
            using var undoTransaction = _undoManager.GetTextBufferUndoManager(subjectBuffer).TextBufferUndoHistory.CreateTransaction("Extract Method");

            // apply extract method code to buffer
            var(document, _) = extractMethodResult.GetFormattedDocumentAsync(cancellationToken).WaitAndGetResult(cancellationToken);
            document.Project.Solution.Workspace.ApplyDocumentChanges(document, cancellationToken);

            // apply changes
            undoTransaction.Complete();
        }
        /// <summary>
        /// Applies an ExtractMethodResult to the editor.
        /// </summary>
        private void ApplyChangesToBuffer(ExtractMethodResult extractMethodResult, ITextBuffer subjectBuffer, CancellationToken cancellationToken)
        {
            using (var undoTransaction = _undoManager.GetTextBufferUndoManager(subjectBuffer).TextBufferUndoHistory.CreateTransaction("Extract Method"))
            {
                // apply extract method code to buffer
                var document = extractMethodResult.Document;
                document.Project.Solution.Workspace.ApplyDocumentChanges(document, cancellationToken);

                // apply changes
                undoTransaction.Complete();
            }
        }
Exemple #5
0
        /// <returns>
        /// True: if a failure notification was displayed or the user did not want to proceed in a best effort scenario.
        ///       Extract Method does not proceed further and is done.
        /// False: the user proceeded to a best effort scenario.
        /// </returns>
        private bool TryNotifyFailureToUser(Document document, ExtractMethodResult result, IUIThreadOperationContext waitContext)
        {
            // We are about to show a modal UI dialog so we should take over the command execution
            // wait context. That means the command system won't attempt to show its own wait dialog
            // and also will take it into consideration when measuring command handling duration.
            waitContext.TakeOwnership();
            var project             = document.Project;
            var solution            = project.Solution;
            var notificationService = solution.Workspace.Services.GetService <INotificationService>();

            // see whether we will allow best effort extraction and if it is possible.
            if (!solution.Options.GetOption(ExtractMethodOptions.AllowBestEffort, project.Language) ||
                !result.Status.HasBestEffort() ||
                result.Document == null)
            {
                if (notificationService != null)
                {
                    notificationService.SendNotification(
                        EditorFeaturesResources.Extract_method_encountered_the_following_issues + Environment.NewLine +
                        string.Join("", result.Reasons.Select(r => Environment.NewLine + "  " + r)),
                        title: EditorFeaturesResources.Extract_Method,
                        severity: NotificationSeverity.Error);
                }

                return(true);
            }

            // okay, best effort is turned on, let user know it is an best effort
            if (notificationService != null)
            {
                if (!notificationService.ConfirmMessageBox(
                        EditorFeaturesResources.Extract_method_encountered_the_following_issues + Environment.NewLine +
                        string.Join("", result.Reasons.Select(r => Environment.NewLine + "  " + r)) + Environment.NewLine + Environment.NewLine +
                        EditorFeaturesResources.Do_you_still_want_to_proceed_This_may_produce_broken_code,
                        title: EditorFeaturesResources.Extract_Method,
                        severity: NotificationSeverity.Warning))
                {
                    return(true);
                }
            }

            return(false);
        }
Exemple #6
0
        private static ExtractMethodResult TryWithoutMakingValueTypesRef(
            Document document, NormalizedSnapshotSpanCollection spans, ExtractMethodResult result, ExtractMethodOptions options, CancellationToken cancellationToken)
        {
            if (options.DontPutOutOrRefOnStruct || !result.Reasons.IsSingle())
            {
                return(null);
            }

            var reason = result.Reasons.FirstOrDefault();
            var length = FeaturesResources.Asynchronous_method_cannot_have_ref_out_parameters_colon_bracket_0_bracket.IndexOf(':');

            if (reason != null && length > 0 && reason.IndexOf(FeaturesResources.Asynchronous_method_cannot_have_ref_out_parameters_colon_bracket_0_bracket.Substring(0, length), 0, length, StringComparison.Ordinal) >= 0)
            {
                var newResult = ExtractMethodService.ExtractMethodAsync(
                    document,
                    spans.Single().Span.ToTextSpan(),
                    localFunction: false,
                    options with {
                    DontPutOutOrRefOnStruct = true
                },
        /// <returns>
        /// True: if a failure notification was displayed or the user did not want to proceed in a best effort scenario.
        ///       Extract Method does not proceed further and is done.
        /// False: the user proceeded to a best effort scenario.
        /// </returns>
        private bool TryNotifyFailureToUser(Document document, ExtractMethodResult result)
        {
            var notificationService = document.Project.Solution.Workspace.Services.GetService <INotificationService>();

            // see whether we will allow best effort extraction and if it is possible.
            if (!document.Project.Solution.Options.GetOption(ExtractMethodOptions.AllowBestEffort, document.Project.Language) ||
                !result.Status.HasBestEffort() || result.Document == null)
            {
                if (notificationService != null)
                {
                    notificationService.SendNotification(
                        EditorFeaturesResources.Extract_method_failed_with_following_reasons_colon + Environment.NewLine + Environment.NewLine +
                        string.Join(Environment.NewLine, result.Reasons),
                        title: EditorFeaturesResources.Extract_Method,
                        severity: NotificationSeverity.Error);
                }

                return(true);
            }

            // okay, best effort is turned on, let user know it is an best effort
            if (notificationService != null)
            {
                if (!notificationService.ConfirmMessageBox(
                        EditorFeaturesResources.Extract_method_failed_with_following_reasons_colon + Environment.NewLine + Environment.NewLine +
                        string.Join(Environment.NewLine, result.Reasons) + Environment.NewLine + Environment.NewLine +
                        EditorFeaturesResources.Do_you_still_want_to_proceed_it_will_generate_broken_code,
                        title: EditorFeaturesResources.Extract_Method,
                        severity: NotificationSeverity.Error))
                {
                    return(true);
                }
            }

            return(false);
        }