예제 #1
0
        public async Task <Document> OrganizeImportsAsync(Document document, OrganizeImportsOptions options, CancellationToken cancellationToken)
        {
            var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);

            var rewriter = new Rewriter(options);
            var newRoot  = rewriter.Visit(root);

            return(document.WithSyntaxRoot(newRoot));
        }
예제 #2
0
파일: Formatter.cs 프로젝트: taori/roslyn
        /// <summary>
        /// Organizes the imports in the document.
        /// </summary>
        /// <param name="document">The document to organize.</param>
        /// <param name="cancellationToken">The cancellation token that the operation will observe.</param>
        /// <returns>The document with organized imports. If the language does not support organizing imports, or if no changes were made, this method returns <paramref name="document"/>.</returns>
        public static async Task <Document> OrganizeImportsAsync(Document document, CancellationToken cancellationToken = default)
        {
            var organizeImportsService = document.GetLanguageService <IOrganizeImportsService>();

            if (organizeImportsService is null)
            {
                return(document);
            }

            var options = await OrganizeImportsOptions.FromDocumentAsync(document, cancellationToken).ConfigureAwait(false);

            return(await organizeImportsService.OrganizeImportsAsync(document, options, cancellationToken).ConfigureAwait(false));
        }
예제 #3
0
        public async Task <Document> FormatNewDocumentAsync(Document document, Document?hintDocument, CodeCleanupOptions options, CancellationToken cancellationToken)
        {
            var organizeImportsService = document.GetRequiredLanguageService <IOrganizeImportsService>();

            var organizeOptions = await OrganizeImportsOptions.FromDocumentAsync(document, cancellationToken).ConfigureAwait(false);

            var documentOptions = await document.GetOptionsAsync(cancellationToken).ConfigureAwait(false);

            var codeStyleOption = documentOptions.GetOption(CSharpCodeStyleOptions.PreferredUsingDirectivePlacement);

            var organizedDocument = await organizeImportsService.OrganizeImportsAsync(document, organizeOptions, cancellationToken).ConfigureAwait(false);

            return(await MisplacedUsingDirectivesCodeFixProvider.TransformDocumentIfRequiredAsync(organizedDocument, options.SimplifierOptions, codeStyleOption, cancellationToken).ConfigureAwait(false));
        }
예제 #4
0
        protected static async Task CheckAsync(
            string initial, string final,
            bool placeSystemNamespaceFirst = false,
            bool separateImportGroups      = false,
            string?endOfLine = null)
        {
            using var workspace = new AdhocWorkspace();
            var project  = workspace.CurrentSolution.AddProject("Project", "Project.dll", LanguageNames.CSharp);
            var document = project.AddDocument("Document", initial.ReplaceLineEndings(endOfLine ?? Environment.NewLine));

            var options = new OrganizeImportsOptions()
            {
                PlaceSystemNamespaceFirst     = placeSystemNamespaceFirst,
                SeparateImportDirectiveGroups = separateImportGroups,
                NewLine = endOfLine ?? OrganizeImportsOptions.Default.NewLine
            };

            var organizeImportsService = document.GetRequiredLanguageService <IOrganizeImportsService>();
            var newDocument            = await organizeImportsService.OrganizeImportsAsync(document, options, CancellationToken.None);

            var newRoot = await newDocument.GetRequiredSyntaxRootAsync(default);
 private OmniSharpOrganizeImportsOptionsWrapper(OrganizeImportsOptions underlyingObject)
 {
     UnderlyingObject = underlyingObject;
 }
예제 #6
0
 static void ValidateOrganizeImportsOptions(OrganizeImportsOptions options)
 {
     Assert.Equal("\r", options.NewLine);
 }
 public Rewriter(OrganizeImportsOptions options)
 {
     _placeSystemNamespaceFirst = options.PlaceSystemNamespaceFirst;
     _separateGroups            = options.SeparateImportDirectiveGroups;
     _newLineTrivia             = CSharpSyntaxGeneratorInternal.Instance.EndOfLine(options.NewLine);
 }
예제 #8
0
 public async Task <Document> OrganizeImportsAsync(Document document, OrganizeImportsOptions options, CancellationToken cancellationToken)
 {
     return(await _organizeService.OrganizeNamespacesAsync(document, options.PlaceSystemNamespaceFirst, cancellationToken).ConfigureAwait(false) ?? document);
 }