Ejemplo n.º 1
0
        public static async Task <T> PerformFunctionAsync <T>(
            this IExtensionManager extensionManager,
            object extension,
            Func <Task <T>?> function,
            T defaultValue)
        {
            if (extensionManager.IsDisabled(extension))
            {
                return(defaultValue);
            }

            try
            {
                var task = function();
                if (task != null)
                {
                    return(await task.ConfigureAwait(false));
                }
            }
            catch (Exception e) when(!(e is OperationCanceledException) && extensionManager.CanHandleException(extension, e))
            {
                extensionManager.HandleException(extension, e);
            }

            return(defaultValue);
        }
Ejemplo n.º 2
0
        public static async Task <T> PerformFunctionAsync <T>(
            this IExtensionManager extensionManager,
            object extension,
            Func <Task <T> > function,
            T defaultValue)
        {
            try
            {
                if (!extensionManager.IsDisabled(extension))
                {
                    var task = function() ?? SpecializedTasks.Default <T>();
                    return(await task.ConfigureAwait(false));
                }
            }
            catch (OperationCanceledException)
            {
                throw;
            }
            catch (Exception e) when(extensionManager.CanHandleException(extension, e))
            {
                extensionManager.HandleException(extension, e);
            }

            return(defaultValue);
        }
Ejemplo n.º 3
0
        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);
        }
        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);
            }
        }
Ejemplo n.º 5
0
        public static T PerformFunction <T>(
            this IExtensionManager extensionManager,
            object extension,
            Func <T> function,
            T defaultValue
            )
        {
            try
            {
                if (!extensionManager.IsDisabled(extension))
                {
                    return(function());
                }
            }
            catch (OperationCanceledException)
            {
                throw;
            }
            catch (Exception e) when(extensionManager.CanHandleException(extension, e))
            {
                extensionManager.HandleException(extension, e);
            }

            return(defaultValue);
        }
 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);
     }
 }
Ejemplo n.º 7
0
        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);
        }
Ejemplo n.º 8
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 void ClassifyNode(SyntaxNode syntax)
            {
                foreach (var classifier in getNodeClassifiers(syntax))
                {
                    cancellationToken.ThrowIfCancellationRequested();

                    try
                    {
                        if (!extensionManager.IsDisabled(classifier))
                        {
                            var classifications = classifier.ClassifyNode(syntax, this.semanticModel, cancellationToken);
                            AddClassifications(classifications);
                        }
                    }
                    catch (OperationCanceledException)
                    {
                        throw;
                    }
                    catch (Exception e)
                    {
                        extensionManager.HandleException(classifier, e);
                    }
                }
            }
Ejemplo n.º 10
0
 public static void PerformAction(this IExtensionManager extensionManager, object extension, Action action)
 {
     try
     {
         if (!extensionManager.IsDisabled(extension))
         {
             action();
         }
     }
     catch (OperationCanceledException)
     {
         throw;
     }
     catch (Exception e) when(extensionManager.CanHandleException(extension, e))
     {
         extensionManager.HandleException(extension, e);
     }
 }
Ejemplo n.º 11
0
 public static async Task PerformActionAsync(
     this IExtensionManager extensionManager,
     object extension,
     Func <Task> function)
 {
     try
     {
         if (!extensionManager.IsDisabled(extension))
         {
             var task = function() ?? SpecializedTasks.EmptyTask;
             await task.ConfigureAwait(false);
         }
     }
     catch (OperationCanceledException)
     {
         throw;
     }
     catch (Exception e) when(extensionManager.CanHandleException(extension, e))
     {
         extensionManager.HandleException(extension, e);
     }
 }
Ejemplo n.º 12
0
        public static async Task <T> PerformFunctionAsync <T>(
            this IExtensionManager extensionManager,
            object extension,
            Func <Task <T> > function,
            T defaultValue = default(T))
        {
            try
            {
                if (!extensionManager.IsDisabled(extension))
                {
                    return(await function().ConfigureAwait(false));
                }
            }
            catch (OperationCanceledException)
            {
                throw;
            }
            catch (Exception e)
            {
                extensionManager.HandleException(extension, e);
            }

            return(defaultValue);
        }
Ejemplo n.º 13
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;
        }
Ejemplo n.º 14
0
        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;
        }
        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);
        }