Ejemplo n.º 1
0
        private async Task <Document> RemoveSortUsingsAsync(
            Document document, OrganizeUsingsSet organizeUsingsSet, CancellationToken cancellationToken)
        {
            if (organizeUsingsSet.IsRemoveUnusedImportEnabled)
            {
                var removeUsingsService = document.GetLanguageService <IRemoveUnnecessaryImportsService>();
                if (removeUsingsService != null)
                {
                    using (Logger.LogBlock(FunctionId.CodeCleanup_RemoveUnusedImports, cancellationToken))
                    {
                        document = await removeUsingsService.RemoveUnnecessaryImportsAsync(document, cancellationToken).ConfigureAwait(false);
                    }
                }
            }

            if (organizeUsingsSet.IsSortImportsEnabled)
            {
                using (Logger.LogBlock(FunctionId.CodeCleanup_SortImports, cancellationToken))
                {
                    document = await OrganizeImportsService.OrganizeImportsAsync(document, cancellationToken).ConfigureAwait(false);
                }
            }

            return(document);
        }
Ejemplo n.º 2
0
 protected void Check(string initial, string final, bool specialCaseSystem, CSharpParseOptions options = null)
 {
     using (var workspace = CSharpWorkspaceFactory.CreateWorkspaceFromFile(initial))
     {
         var document = workspace.CurrentSolution.GetDocument(workspace.Documents.First().Id);
         var newRoot  = OrganizeImportsService.OrganizeImportsAsync(document, specialCaseSystem).Result.GetSyntaxRootAsync().Result;
         Assert.Equal(final, newRoot.ToFullString());
     }
 }
Ejemplo n.º 3
0
 protected async Task CheckAsync(string initial, string final, bool specialCaseSystem, CSharpParseOptions options = null)
 {
     using (var workspace = await CSharpWorkspaceFactory.CreateWorkspaceFromFileAsync(initial))
     {
         var document = workspace.CurrentSolution.GetDocument(workspace.Documents.First().Id);
         var newRoot  = await(await OrganizeImportsService.OrganizeImportsAsync(document, specialCaseSystem)).GetSyntaxRootAsync();
         Assert.Equal(final.NormalizeLineEndings(), newRoot.ToFullString());
     }
 }
Ejemplo n.º 4
0
 protected async Task CheckAsync(string initial, string final, bool placeSystemNamespaceFirst = false, CSharpParseOptions options = null)
 {
     using (var workspace = await TestWorkspace.CreateCSharpAsync(initial))
     {
         var document = workspace.CurrentSolution.GetDocument(workspace.Documents.First().Id);
         workspace.Options = workspace.Options.WithChangedOption(new OptionKey(GenerationOptions.PlaceSystemNamespaceFirst, document.Project.Language), placeSystemNamespaceFirst);
         var newRoot = await(await OrganizeImportsService.OrganizeImportsAsync(document)).GetSyntaxRootAsync();
         Assert.Equal(final.NormalizeLineEndings(), newRoot.ToFullString());
     }
 }
        private void SortImports(ITextBuffer subjectBuffer, CancellationToken cancellationToken)
        {
            var document = subjectBuffer.CurrentSnapshot.GetOpenDocumentInCurrentContextWithChanges();

            if (document != null)
            {
                var newDocument = OrganizeImportsService.OrganizeImportsAsync(document, subjectBuffer.GetOption(OrganizerOptions.PlaceSystemNamespaceFirst), cancellationToken).WaitAndGetResult(cancellationToken);
                if (document != newDocument)
                {
                    ApplyTextChange(document, newDocument);
                }
            }
        }
        private void SortAndRemoveUnusedImports(ITextBuffer subjectBuffer, CancellationToken cancellationToken)
        {
            var document = subjectBuffer.CurrentSnapshot.GetOpenDocumentInCurrentContextWithChanges();

            if (document != null)
            {
                var newDocument = document.GetLanguageService <IRemoveUnnecessaryImportsService>().RemoveUnnecessaryImportsAsync(document, cancellationToken).WaitAndGetResult(cancellationToken);
                newDocument = OrganizeImportsService.OrganizeImportsAsync(newDocument, subjectBuffer.GetFeatureOnOffOption(GenerationOptions.PlaceSystemNamespaceFirst), cancellationToken).WaitAndGetResult(cancellationToken);
                if (document != newDocument)
                {
                    ApplyTextChange(document, newDocument);
                }
            }
        }
        private void SortImports(ITextBuffer subjectBuffer, IUIThreadOperationContext operationContext)
        {
            var cancellationToken = operationContext.UserCancellationToken;
            var document          = subjectBuffer.CurrentSnapshot.GetOpenDocumentInCurrentContextWithChanges();

            if (document != null)
            {
                var newDocument = OrganizeImportsService.OrganizeImportsAsync(document, cancellationToken).WaitAndGetResult(cancellationToken);
                if (document != newDocument)
                {
                    ApplyTextChange(document, newDocument);
                }
            }
        }
Ejemplo n.º 8
0
        private void SortAndRemoveUnusedImports(ITextBuffer subjectBuffer, IUIThreadOperationContext operationContext)
        {
            var cancellationToken = operationContext.UserCancellationToken;
            var document          = subjectBuffer.CurrentSnapshot.GetFullyLoadedOpenDocumentInCurrentContextWithChangesAsync(operationContext).WaitAndGetResult(cancellationToken);

            if (document != null)
            {
                var newDocument = document.GetLanguageService <IRemoveUnnecessaryImportsService>().RemoveUnnecessaryImportsAsync(document, cancellationToken).WaitAndGetResult(cancellationToken);
                newDocument = OrganizeImportsService.OrganizeImportsAsync(newDocument, cancellationToken).WaitAndGetResult(cancellationToken);
                if (document != newDocument)
                {
                    ApplyTextChange(document, newDocument);
                }
            }
        }
Ejemplo n.º 9
0
        private async Task <Document> RemoveSortUsingsAsync(Document document, DocumentOptionSet docOptions, CancellationToken cancellationToken)
        {
            // remove usings
            if (docOptions.GetOption(CodeCleanupOptions.RemoveUnusedImports))
            {
                var removeUsingsService = document.GetLanguageService <IRemoveUnnecessaryImportsService>();
                if (removeUsingsService != null)
                {
                    document = await removeUsingsService.RemoveUnnecessaryImportsAsync(document, cancellationToken).ConfigureAwait(false);
                }
            }

            // sort usings
            if (docOptions.GetOption(CodeCleanupOptions.SortImports))
            {
                document = await OrganizeImportsService.OrganizeImportsAsync(document, cancellationToken).ConfigureAwait(false);
            }

            return(document);
        }
Ejemplo n.º 10
0
        static async Task SortAndRemoveUnusedImports(Document originalDocument, CancellationToken cancellationToken)
        {
            if (originalDocument == null)
            {
                return;
            }

            var workspace = originalDocument.Project.Solution.Workspace;

            var unnecessaryImportsService = originalDocument.GetLanguageService <IRemoveUnnecessaryImportsService> ();

            // Remove unnecessary imports and sort them
            var removedImportsDocument = await unnecessaryImportsService.RemoveUnnecessaryImportsAsync(originalDocument, cancellationToken);

            var resultDocument = await OrganizeImportsService.OrganizeImportsAsync(removedImportsDocument, cancellationToken);

            // Apply the document change if needed
            if (resultDocument != originalDocument)
            {
                workspace.ApplyDocumentChanges(resultDocument, cancellationToken);
            }
        }