Beispiel #1
0
        private async Task <Document> FixDocumentAsync(Document document, FixIdContainer enabledFixIds, ProgressTracker progressTracker, CancellationToken cancellationToken)
        {
            if (document.IsGeneratedCode(cancellationToken))
            {
                return(document);
            }

            var codeCleanupService = document.GetLanguageService <ICodeCleanupService>();

            var allDiagnostics = codeCleanupService.GetAllDiagnostics();

            var enabedDiagnosticSets = ArrayBuilder <DiagnosticSet> .GetInstance();

            foreach (var diagnostic in allDiagnostics.Diagnostics)
            {
                foreach (var diagnosticId in diagnostic.DiagnosticIds)
                {
                    if (enabledFixIds.IsFixIdEnabled(diagnosticId))
                    {
                        enabedDiagnosticSets.Add(diagnostic);
                        break;
                    }
                }
            }

            var isRemoveUnusedUsingsEnabled = enabledFixIds.IsFixIdEnabled(RemoveUnusedImportsFixId);
            var isSortUsingsEnabled         = enabledFixIds.IsFixIdEnabled(SortImportsFixId);
            var enabledDiagnostics          = new EnabledDiagnosticOptions(enabedDiagnosticSets.ToImmutableArray(),
                                                                           new OrganizeUsingsSet(isRemoveUnusedUsingsEnabled, isSortUsingsEnabled));

            return(await codeCleanupService.CleanupAsync(
                       document, enabledDiagnostics, progressTracker, cancellationToken).ConfigureAwait(false));
        }
        private async Task <bool> FixTextBufferAsync(TextBufferCodeCleanUpScope textBufferScope, ICodeCleanUpExecutionContext context, CancellationToken cancellationToken)
        {
            using (var cancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(context.OperationContext.UserCancellationToken, cancellationToken))
            {
                cancellationToken = cancellationTokenSource.Token;

                var buffer = textBufferScope.SubjectBuffer;
                using (var scope = context.OperationContext.AddScope(allowCancellation: true, description: EditorFeaturesResources.Applying_changes))
                {
                    var progressTracker = new ProgressTracker((description, completed, total) =>
                    {
                        if (scope != null)
                        {
                            scope.Description = description;
                            scope.Progress.Report(new ProgressInfo(completed, total));
                        }
                    });

                    var document           = buffer.CurrentSnapshot.GetOpenDocumentInCurrentContextWithChanges();
                    var codeCleanupService = document.GetLanguageService <ICodeCleanupService>();

                    var allDiagnostics = codeCleanupService.GetAllDiagnostics();

                    var enabedDiagnosticSets = ArrayBuilder <DiagnosticSet> .GetInstance();

                    foreach (var diagnostic in allDiagnostics.Diagnostics)
                    {
                        foreach (var diagnosticId in diagnostic.DiagnosticIds)
                        {
                            if (context.EnabledFixIds.IsFixIdEnabled(diagnosticId))
                            {
                                enabedDiagnosticSets.Add(diagnostic);
                                break;
                            }
                        }
                    }

                    var isRemoveUnusedUsingsEnabled = context.EnabledFixIds.IsFixIdEnabled(RemoveUnusedImportsFixId);
                    var isSortUsingsEnabled         = context.EnabledFixIds.IsFixIdEnabled(SortImportsFixId);
                    var enabledDiagnostics          = new EnabledDiagnosticOptions(enabedDiagnosticSets.ToImmutableArray(),
                                                                                   new OrganizeUsingsSet(isRemoveUnusedUsingsEnabled, isSortUsingsEnabled));

                    var newDoc = await codeCleanupService.CleanupAsync(
                        document, enabledDiagnostics, progressTracker, cancellationToken).ConfigureAwait(true);

                    await _threadingContext.JoinableTaskFactory.SwitchToMainThreadAsync(cancellationToken);

                    return(document.Project.Solution.Workspace.TryApplyChanges(newDoc.Project.Solution, progressTracker));
                }
            }
        }
Beispiel #3
0
        public async Task <Document> CleanupAsync(
            Document document,
            EnabledDiagnosticOptions enabledDiagnostics,
            IProgressTracker progressTracker,
            CancellationToken cancellationToken)
        {
            // add one item for the 'format' action we'll do last
            if (enabledDiagnostics.FormatDocument)
            {
                progressTracker.AddItems(1);
            }

            // and one for 'remove/sort usings' if we're going to run that.
            var organizeUsings = enabledDiagnostics.OrganizeUsings.IsRemoveUnusedImportEnabled ||
                                 enabledDiagnostics.OrganizeUsings.IsSortImportsEnabled;

            if (organizeUsings)
            {
                progressTracker.AddItems(1);
            }

            if (_codeFixServiceOpt != null)
            {
                document = await ApplyCodeFixesAsync(
                    document, enabledDiagnostics.Diagnostics, progressTracker, cancellationToken).ConfigureAwait(false);
            }

            // do the remove usings after code fix, as code fix might remove some code which can results in unused usings.
            if (organizeUsings)
            {
                progressTracker.Description = CSharpFeaturesResources.Organize_Usings;
                document = await RemoveSortUsingsAsync(
                    document, enabledDiagnostics.OrganizeUsings, cancellationToken).ConfigureAwait(false);

                progressTracker.ItemCompleted();
            }

            if (enabledDiagnostics.FormatDocument)
            {
                progressTracker.Description = FeaturesResources.Formatting_document;
                using (Logger.LogBlock(FunctionId.CodeCleanup_Format, cancellationToken))
                {
                    document = await Formatter.FormatAsync(document, cancellationToken : cancellationToken).ConfigureAwait(false);

                    progressTracker.ItemCompleted();
                }
            }

            return(document);
        }