コード例 #1
0
        internal override async Task <SourceText> FormatFileAsync(
            Document document,
            SourceText sourceText,
            OptionSet optionSet,
            AnalyzerConfigOptions analyzerConfigOptions,
            FormatOptions formatOptions,
            ILogger logger,
            CancellationToken cancellationToken)
        {
            // If we are fixing CodeStyle and the 'IDE0005' diagnostic is configured, then
            // see if we can remove unused imports.

            // If we are not saving files, do not make changes in this formatter. They will be
            // reported by their diagnostic id when the analyzer formatter runs.
            if (!formatOptions.SaveFormattedFiles)
            {
                return(sourceText);
            }

            // If diagnostics are being filtered and IDE0005 isn't specified, then make no changes.
            if (!formatOptions.Diagnostics.IsEmpty &&
                !formatOptions.Diagnostics.Contains(IDE0005))
            {
                return(sourceText);
            }

            var tree = await document.GetSyntaxTreeAsync(cancellationToken).ConfigureAwait(false);

            if (tree is null)
            {
                return(sourceText);
            }

            var severity = analyzerConfigOptions.GetDiagnosticSeverity(document.Project, tree, IDE0005, Style);

            if (severity < formatOptions.CodeStyleSeverity)
            {
                return(sourceText);
            }

            var formattedDocument = await RemoveUnnecessaryImportsHelper.RemoveUnnecessaryImportsAsync(document, cancellationToken).ConfigureAwait(false);

            if (formattedDocument is null)
            {
                return(sourceText);
            }

            var isSameVersion = await IsSameDocumentAndVersionAsync(document, formattedDocument, cancellationToken).ConfigureAwait(false);

            if (isSameVersion)
            {
                return(sourceText);
            }

            var formattedText = await formattedDocument.GetTextAsync(cancellationToken).ConfigureAwait(false);

            return(formattedText);
        }