Example #1
0
        private static async Task <Document> FixOneAsync(CodeFixContext context, Diagnostic diagnostic, CancellationToken cancellationToken)
        {
            var options = await SyntaxFormattingOptions.FromDocumentAsync(context.Document, cancellationToken).ConfigureAwait(false);

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

            var formattedTree = await FormattingCodeFixHelper.FixOneAsync(tree, context.Document.Project.Solution.Workspace.Services, options, diagnostic, cancellationToken).ConfigureAwait(false);

            return(context.Document.WithSyntaxRoot(await formattedTree.GetRootAsync(cancellationToken).ConfigureAwait(false)));
        }
        public static async Task <SyntaxFormattingOptions> GetInferredFormattingOptionsAsync(this IIndentationManagerService indentationManager, Document document, bool explicitFormat, CancellationToken cancellationToken)
        {
            var text = await document.GetTextAsync(cancellationToken).ConfigureAwait(false);

            var snapshot = text.FindCorrespondingEditorTextSnapshot();

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

            if (snapshot == null)
            {
                return(options);
            }

            indentationManager.GetIndentation(snapshot.TextBuffer, explicitFormat, out var convertTabsToSpaces, out var tabSize, out var indentSize);

            return(options.With(new LineFormattingOptions(
                                    UseTabs: !convertTabsToSpaces,
                                    IndentationSize: indentSize,
                                    TabSize: tabSize,
                                    NewLine: options.NewLine)));
        }