private async Task AddRefactoringAsync(
            CodeRefactoringProvider provider,
            List <CodeRefactoring> allRefactorings,
            IExtensionManager extensionManager,
            CodeRefactoringContext context)
        {
            try
            {
                if (!extensionManager.IsDisabled(provider))
                {
                    using (Logger.LogBlock(FeatureId.CodeActions, FunctionId.CodeAction_AddRefactoring, codeRefactoringDescription, provider, context.Span, context.CancellationToken))
                    {
                        var actions = await provider.GetRefactoringsAsync(context).ConfigureAwait(false);

                        if (actions != null && actions.Count() > 0)
                        {
                            allRefactorings.Add(new CodeRefactoring(provider, actions));
                        }
                    }
                }
            }
            catch (OperationCanceledException)
            {
                throw;
            }
            catch (Exception e)
            {
                extensionManager.HandleException(provider, e);
            }
        }
 private async Task AddRefactoringAsync(
     CodeRefactoringProvider provider,
     List<CodeRefactoring> allRefactorings,
     IExtensionManager extensionManager,
     CodeRefactoringContext context)
 {
     try
     {
         if (!extensionManager.IsDisabled(provider))
         {
             using (Logger.LogBlock(FeatureId.CodeActions, FunctionId.CodeAction_AddRefactoring, codeRefactoringDescription, provider, context.Span, context.CancellationToken))
             {
                 var actions = await provider.GetRefactoringsAsync(context).ConfigureAwait(false);
                 if (actions != null && actions.Count() > 0)
                 {
                     allRefactorings.Add(new CodeRefactoring(provider, actions));
                 }
             }
         }
     }
     catch (OperationCanceledException)
     {
         throw;
     }
     catch (Exception e)
     {
         extensionManager.HandleException(provider, e);
     }
 }
        private async Task<CodeRefactoring> GetRefactoringFromProvider(
            CodeRefactoringProvider provider,
            IExtensionManager extensionManager,
            CodeRefactoringContext context)
        {
            context.CancellationToken.ThrowIfCancellationRequested();
            if (extensionManager.IsDisabled(provider))
            {
                return null;
            }

            try
            {
                var actions = await provider.GetRefactoringsAsync(context).ConfigureAwait(false);
                if (actions != null && actions.Count() > 0)
                {
                    return new CodeRefactoring(provider, actions);
                }
            }
            catch (OperationCanceledException)
            {
                // We don't want to catch operation canceled exceptions in the catch block 
                // below. So catch is here and rethrow it.
                throw;
            }
            catch (Exception e)
            {
                extensionManager.HandleException(provider, e);
            }

            return null;
        }
 public CodeRefactoringSuggestedAction(
     SuggestedActionsSourceProvider sourceProvider,
     Workspace workspace,
     ITextBuffer subjectBuffer,
     CodeRefactoringProvider provider,
     CodeAction codeAction)
     : base(sourceProvider, workspace, subjectBuffer, provider, codeAction)
 {
 }
 public CodeRefactoringSuggestedAction(
     Workspace workspace,
     ITextBuffer subjectBuffer,
     ICodeActionEditHandlerService editHandler,
     CodeAction codeAction,
     CodeRefactoringProvider provider)
     : base(workspace, subjectBuffer, editHandler, codeAction, provider)
 {
 }
        private async Task <CodeRefactoring> GetRefactoringFromProviderAsync(
            Document document,
            TextSpan state,
            CodeRefactoringProvider provider,
            IExtensionManager extensionManager,
            bool isBlocking,
            CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            if (extensionManager.IsDisabled(provider))
            {
                return(null);
            }

            try
            {
                var actions = ArrayBuilder <(CodeAction action, TextSpan?applicableToSpan)> .GetInstance();

                var context = new CodeRefactoringContext(document, state,

                                                         // TODO: Can we share code between similar lambdas that we pass to this API in BatchFixAllProvider.cs, CodeFixService.cs and CodeRefactoringService.cs?
                                                         (action, applicableToSpan) =>
                {
                    // Serialize access for thread safety - we don't know what thread the refactoring provider will call this delegate from.
                    lock (actions)
                    {
                        actions.Add((action, applicableToSpan));
                    }
                },
                                                         isBlocking,
                                                         cancellationToken);

                var task = provider.ComputeRefactoringsAsync(context) ?? Task.CompletedTask;
                await task.ConfigureAwait(false);

                var result = actions.Count > 0
                    ? new CodeRefactoring(provider, actions.ToImmutable())
                    : null;

                actions.Free();

                return(result);
            }
            catch (OperationCanceledException)
            {
                // We don't want to catch operation canceled exceptions in the catch block
                // below. So catch is here and rethrow it.
                throw;
            }
            catch (Exception e)
            {
                extensionManager.HandleException(provider, e);
            }

            return(null);
        }
Exemple #7
0
        public CodeRefactoring(CodeRefactoringProvider provider, ImmutableArray <CodeAction> actions)
        {
            Provider = provider;
            Actions  = actions.NullToEmpty();

            if (Actions.Length == 0)
            {
                throw new ArgumentException(FeaturesResources.Actions_can_not_be_empty, nameof(actions));
            }
        }
Exemple #8
0
        public CodeRefactoring(CodeRefactoringProvider provider, IEnumerable<CodeAction> actions)
        {
            Provider = provider;
            Actions = actions.ToImmutableArrayOrEmpty();

            if (Actions.Count == 0)
            {
                throw new ArgumentException(FeaturesResources.Actions_can_not_be_empty, nameof(actions));
            }
        }
		public CodeRefactoring(CodeRefactoringProvider provider, IEnumerable<CodeAction> actions)
		{
			_provider = provider;
			_actions = actions.ToImmutableArray();

			if (_actions.Count == 0)
			{
				throw new ArgumentException("Actions can not be empty", "actions");
			}
		}
        public CodeRefactoring(CodeRefactoringProvider provider, IEnumerable <CodeAction> actions)
        {
            Provider = provider;
            Actions  = actions.ToImmutableArrayOrEmpty();

            if (Actions.Count == 0)
            {
                throw new ArgumentException(FeaturesResources.Actions_can_not_be_empty, nameof(actions));
            }
        }
        public CodeRefactoring(CodeRefactoringProvider provider, IEnumerable<CodeAction> actions)
        {
            _provider = provider;
            _actions = actions.ToImmutableArrayOrEmpty();

            if (_actions.Count == 0)
            {
                throw new ArgumentException(FeaturesResources.ActionsCanNotBeEmpty, "actions");
            }
        }
Exemple #12
0
        public CodeRefactoring(CodeRefactoringProvider provider, IEnumerable <CodeAction> actions)
        {
            _provider = provider;
            _actions  = actions.ToImmutableArrayOrEmpty();

            if (_actions.Count == 0)
            {
                throw new ArgumentException(FeaturesResources.ActionsCanNotBeEmpty, "actions");
            }
        }
 private async Task ActionSets(TestWorkspace workspace, CodeRefactoringProvider provider)
 {
     var codeActions = new List<CodeAction>();
     RefactoringSetup(workspace, provider, codeActions, out var extensionManager, out var textBuffer);
     var suggestedAction = new CodeRefactoringSuggestedAction(
         workspace.ExportProvider.GetExportedValue<SuggestedActionsSourceProvider>(),
         workspace, textBuffer, provider, codeActions.First());
     var actionSets = await suggestedAction.GetActionSetsAsync(CancellationToken.None);
     Assert.True(extensionManager.IsDisabled(provider));
     Assert.False(extensionManager.IsIgnored(provider));
 }
 private static void RefactoringSetup(TestWorkspace workspace, CodeRefactoringProvider provider, List<CodeAction> refactorings, out ICodeActionEditHandlerService editHandler, out EditorLayerExtensionManager.ExtensionManager extensionManager, out VisualStudio.Text.ITextBuffer textBuffer)
 {
     var document = GetDocument(workspace);
     var span = document.GetSyntaxRootAsync().Result.Span;
     var context = new CodeRefactoringContext(document, span, (a) => refactorings.Add(a), CancellationToken.None);
     provider.ComputeRefactoringsAsync(context).Wait();
     var action = refactorings.Single();
     editHandler = workspace.ExportProvider.GetExportedValue<ICodeActionEditHandlerService>();
     extensionManager = document.Project.Solution.Workspace.Services.GetService<IExtensionManager>() as EditorLayerExtensionManager.ExtensionManager;
     textBuffer = document.GetTextAsync().Result.Container.GetTextBuffer();
 }
 public CodeRefactoringSuggestedAction(
     Workspace workspace,
     ITextBuffer subjectBuffer,
     ICodeActionEditHandlerService editHandler,
     IWaitIndicator waitIndicator,
     CodeAction codeAction,
     CodeRefactoringProvider provider,
     IAsynchronousOperationListener operationListener)
     : base(workspace, subjectBuffer, editHandler, waitIndicator, codeAction, provider, operationListener)
 {
 }
 private async Task<CodeRefactoring> GetCodeRefactoringAsync(
     CodeRefactoringProvider provider,
     TestWorkspace workspace)
 {
     var document = GetDocument(workspace);
     var span = workspace.Documents.Single(d => !d.IsLinkFile && d.SelectedSpans.Count == 1).SelectedSpans.Single();
     var actions = new List<CodeAction>();
     var context = new CodeRefactoringContext(document, span, (a) => actions.Add(a), CancellationToken.None);
     await provider.ComputeRefactoringsAsync(context);
     return actions.Count > 0 ? new CodeRefactoring(provider, actions) : null;
 }
 private void DisplayText(TestWorkspace workspace, CodeRefactoringProvider provider)
 {
     var codeActions = new List<CodeAction>();
     RefactoringSetup(workspace, provider, codeActions, out var extensionManager, out var textBuffer);
     var suggestedAction = new CodeRefactoringSuggestedAction(
         workspace.ExportProvider.GetExportedValue<SuggestedActionsSourceProvider>(),
         workspace, textBuffer, provider, codeActions.First());
     var text = suggestedAction.DisplayText;
     Assert.True(extensionManager.IsDisabled(provider));
     Assert.False(extensionManager.IsIgnored(provider));
 }
 private void GetPreview(TestWorkspace workspace, CodeRefactoringProvider provider)
 {
     List<CodeAction> refactorings = new List<CodeAction>();
     ICodeActionEditHandlerService editHandler;
     EditorLayerExtensionManager.ExtensionManager extensionManager;
     VisualStudio.Text.ITextBuffer textBuffer;
     RefactoringSetup(workspace, provider, refactorings, out editHandler, out extensionManager, out textBuffer);
     var suggestedAction = new CodeRefactoringSuggestedAction(workspace, textBuffer, editHandler, refactorings.First(), provider);
     suggestedAction.GetPreviewAsync(CancellationToken.None).PumpingWaitResult();
     Assert.True(extensionManager.IsDisabled(provider));
     Assert.False(extensionManager.IsIgnored(provider));
 }
 private void DisplayText(TestWorkspace workspace, CodeRefactoringProvider provider)
 {
     List<CodeAction> refactorings = new List<CodeAction>();
     ICodeActionEditHandlerService editHandler;
     EditorLayerExtensionManager.ExtensionManager extensionManager;
     VisualStudio.Text.ITextBuffer textBuffer;
     RefactoringSetup(workspace, provider, refactorings, out editHandler, out extensionManager, out textBuffer);
     var suggestedAction = new CodeRefactoringSuggestedAction(workspace, textBuffer, editHandler, new TestWaitIndicator(), refactorings.First(), provider);
     var text = suggestedAction.DisplayText;
     Assert.True(extensionManager.IsDisabled(provider));
     Assert.False(extensionManager.IsIgnored(provider));
 }
 private async Task ActionSets(TestWorkspace workspace, CodeRefactoringProvider provider)
 {
     List<CodeAction> refactorings = new List<CodeAction>();
     ICodeActionEditHandlerService editHandler;
     EditorLayerExtensionManager.ExtensionManager extensionManager;
     VisualStudio.Text.ITextBuffer textBuffer;
     RefactoringSetup(workspace, provider, refactorings, out editHandler, out extensionManager, out textBuffer);
     var suggestedAction = new CodeRefactoringSuggestedAction(workspace, textBuffer, editHandler, new TestWaitIndicator(), refactorings.First(), provider);
     var actionSets = await suggestedAction.GetActionSetsAsync(CancellationToken.None).ConfigureAwait(true);
     Assert.True(extensionManager.IsDisabled(provider));
     Assert.False(extensionManager.IsIgnored(provider));
 }
 public void VerifyRefactoringDiabled(CodeRefactoringProvider codeRefactoring)
 {
     var refactoringService = new CodeRefactorings.CodeRefactoringService(GetMetadata(codeRefactoring));
     using (var workspace = CSharpWorkspaceFactory.CreateWorkspaceFromFile(@"class Program {}"))
     {
         var project = workspace.CurrentSolution.Projects.Single();
         var document = project.Documents.Single();
         var extensionManager = document.Project.Solution.Workspace.Services.GetService<IExtensionManager>() as EditorLayerExtensionManager.ExtensionManager;
         var result = refactoringService.GetRefactoringsAsync(document, TextSpan.FromBounds(0, 0), CancellationToken.None).Result;
         Assert.True(extensionManager.IsDisabled(codeRefactoring));
         Assert.False(extensionManager.IsIgnored(codeRefactoring));
     }
 }
Exemple #22
0
        private async Task <CodeRefactoring> GetRefactoringFromProviderAsync(
            Document document,
            TextSpan state,
            CodeRefactoringProvider provider,
            IExtensionManager extensionManager,
            CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            if (extensionManager.IsDisabled(provider))
            {
                return(null);
            }

            try
            {
                var actions = new List <CodeAction>();
                var context = new CodeRefactoringContext(document, state,

                                                         // TODO: Can we share code between similar lambdas that we pass to this API in BatchFixAllProvider.cs, CodeFixService.cs and CodeRefactoringService.cs?
                                                         a =>
                {
                    // Serialize access for thread safety - we don't know what thread the refactoring provider will call this delegate from.
                    lock (actions)
                    {
                        actions.Add(a);
                    }
                },
                                                         cancellationToken);

                var task = provider.ComputeRefactoringsAsync(context) ?? SpecializedTasks.EmptyTask;
                await task.ConfigureAwait(false);

                if (actions.Count > 0)
                {
                    return(new CodeRefactoring(provider, actions));
                }
            }
            catch (OperationCanceledException)
            {
                // We don't want to catch operation canceled exceptions in the catch block
                // below. So catch is here and rethrow it.
                throw;
            }
            catch (Exception e)
            {
                extensionManager.HandleException(provider, e);
            }

            return(null);
        }
        private async Task<CodeRefactoring> GetRefactoringFromProviderAsync(
            Document document,
            TextSpan state,
            CodeRefactoringProvider provider,
            IExtensionManager extensionManager,
            CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            if (extensionManager.IsDisabled(provider))
            {
                return null;
            }

            try
            {
                var actions = new List<CodeAction>();
                var context = new CodeRefactoringContext(document, state,

                    // TODO: Can we share code between similar lambdas that we pass to this API in BatchFixAllProvider.cs, CodeFixService.cs and CodeRefactoringService.cs?
                    a =>
                    {
                        // Serialize access for thread safety - we don't know what thread the refactoring provider will call this delegate from.
                        lock (actions)
                        {
                            actions.Add(a);
                        }
                    },
                    cancellationToken);

                var task = provider.ComputeRefactoringsAsync(context) ?? SpecializedTasks.EmptyTask;
                await task.ConfigureAwait(false);
                if (actions.Count > 0)
                {
                    return new CodeRefactoring(provider, actions);
                }
            }
            catch (OperationCanceledException)
            {
                // We don't want to catch operation canceled exceptions in the catch block 
                // below. So catch is here and rethrow it.
                throw;
            }
            catch (Exception e)
            {
                extensionManager.HandleException(provider, e);
            }

            return null;
        }
		/// <summary>
		/// Gets the roslyn code action provider.
		/// </summary>
		public CodeRefactoringProvider GetProvider ()
		{
			if (instance == null)
				instance = (CodeRefactoringProvider)Activator.CreateInstance (codeActionType);
			return instance;
		}
        internal static string GetRefactoringResult(string language, CodeRefactoringProvider codeRefactoringProvider, string oldSource, TextSpan span, string equivalenceKey)
        {
            var document = CodeAnalysisHelper.CreateDocument(oldSource, language);
            var actions = new List<CodeAction>();
            var context = new CodeRefactoringContext(document, span, (a) => actions.Add(a), CancellationToken.None);
            codeRefactoringProvider.ComputeRefactoringsAsync(context).Wait();

            if (equivalenceKey != null)
            {
                document = CodeAnalysisHelper.ApplyFix(document, actions.Single(n => n.EquivalenceKey == equivalenceKey));
            }
            else
            {
                document = CodeAnalysisHelper.ApplyFix(document, actions[0]);
            }

            var newSource = CodeAnalysisHelper.GetStringFromDocument(document);
            return newSource;
        }
 internal static List<CodeAction> GetRefactoringActions(string language, CodeRefactoringProvider codeRefactoringProvider, string source, TextSpan span)
 {
     var document = CodeAnalysisHelper.CreateDocument(source, language);
     var actions = new List<CodeAction>();
     var context = new CodeRefactoringContext(document, span, (a) => actions.Add(a), CancellationToken.None);
     codeRefactoringProvider.ComputeRefactoringsAsync(context).Wait();
     return actions;
 }
        private async Task <CodeRefactoring?> GetRefactoringFromProviderAsync(
            Document document,
            TextSpan state,
            CodeRefactoringProvider provider,
            CodeChangeProviderMetadata?providerMetadata,
            IExtensionManager extensionManager,
            CodeActionOptionsProvider options,
            bool isBlocking,
            CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            if (extensionManager.IsDisabled(provider))
            {
                return(null);
            }

            try
            {
                using var _ = ArrayBuilder <(CodeAction action, TextSpan?applicableToSpan)> .GetInstance(out var actions);

                var context = new CodeRefactoringContext(document, state,

                                                         // TODO: Can we share code between similar lambdas that we pass to this API in BatchFixAllProvider.cs, CodeFixService.cs and CodeRefactoringService.cs?
                                                         (action, applicableToSpan) =>
                {
                    // Serialize access for thread safety - we don't know what thread the refactoring provider will call this delegate from.
                    lock (actions)
                    {
                        // Add the Refactoring Provider Name to the parent CodeAction's CustomTags.
                        // Always add a name even in cases of 3rd party refactorings that do not export
                        // name metadata.
                        action.AddCustomTagAndTelemetryInfo(providerMetadata, provider);

                        actions.Add((action, applicableToSpan));
                    }
                },
                                                         options,
                                                         isBlocking,
                                                         cancellationToken);

                var task = provider.ComputeRefactoringsAsync(context) ?? Task.CompletedTask;
                await task.ConfigureAwait(false);

                if (actions.Count == 0)
                {
                    return(null);
                }

                var fixAllProviderInfo = extensionManager.PerformFunction(
                    provider, () => ImmutableInterlocked.GetOrAdd(ref _fixAllProviderMap, provider, FixAllProviderInfo.Create), defaultValue: null);
                return(new CodeRefactoring(provider, actions.ToImmutable(), fixAllProviderInfo, options));
            }
            catch (OperationCanceledException)
            {
                // We don't want to catch operation canceled exceptions in the catch block
                // below. So catch is here and rethrow it.
                throw;
            }
            catch (Exception e)
            {
                extensionManager.HandleException(provider, e);
            }

            return(null);
        }