Inherit this type to provide source code refactorings. Remember to use ExportCodeRefactoringProviderAttribute so the host environment can offer your refactorings in a UI.
Example #1
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));
            }
        }
        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 = ArrayBuilder <CodeAction> .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?
                                                         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) ?? 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);
        }