/// <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)); }
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)); }