Esempio n. 1
0
 internal static SyntaxNode?AsRootOfNewTreeWithOptionsFrom(
     this SyntaxNode?node,
     SyntaxTree oldTree
     )
 {
     return(node != null
       ? oldTree.WithRootAndOptions(node, oldTree.Options).GetRoot()
       : null);
 }
 public Microsoft.CodeAnalysis.SyntaxTree Translate(VSGraphModel graphModel, CompilationOptions options)
 {
     Profiler.BeginSample("Translation");
     Microsoft.CodeAnalysis.SyntaxTree syntaxTree = ToSyntaxTree(graphModel, options);
     Profiler.EndSample();
     if (syntaxTree is CSharpSyntaxTree cSharpSyntaxTree && syntaxTree.TryGetRoot(out var treeRoot))
     {
         var treeOptions = cSharpSyntaxTree.Options.WithLanguageVersion(LanguageVersion);
         return(syntaxTree.WithRootAndOptions(treeRoot, treeOptions));
     }
     return(syntaxTree);
 }
        internal static async Task <SyntaxTree> FixOneAsync(SyntaxTree syntaxTree, FormattingProvider formattingProvider, SyntaxFormattingOptions options, Diagnostic diagnostic, CancellationToken cancellationToken)
        {
            // The span to format is the full line(s) containing the diagnostic
            var text = await syntaxTree.GetTextAsync(cancellationToken).ConfigureAwait(false);

            var diagnosticSpan             = diagnostic.Location.SourceSpan;
            var diagnosticLinePositionSpan = text.Lines.GetLinePositionSpan(diagnosticSpan);
            var spanToFormat = TextSpan.FromBounds(
                text.Lines[diagnosticLinePositionSpan.Start.Line].Start,
                text.Lines[diagnosticLinePositionSpan.End.Line].End);

            var root = await syntaxTree.GetRootAsync(cancellationToken).ConfigureAwait(false);

            var formattedRoot = Formatter.Format(root, spanToFormat, formattingProvider, options, cancellationToken);

            return(syntaxTree.WithRootAndOptions(formattedRoot, syntaxTree.Options));
        }
Esempio n. 4
0
        internal static async Task <SyntaxTree> FixOneAsync(
            SyntaxTree syntaxTree,
            FormatterState formatterState,
            OptionSet options,
            Diagnostic diagnostic,
            CancellationToken cancellationToken
            )
        {
            // The span to format is the full line(s) containing the diagnostic
            var text = await syntaxTree.GetTextAsync(cancellationToken).ConfigureAwait(false);

            var diagnosticSpan             = diagnostic.Location.SourceSpan;
            var diagnosticLinePositionSpan = text.Lines.GetLinePositionSpan(diagnosticSpan);
            var spanToFormat = TextSpan.FromBounds(
                text.Lines[diagnosticLinePositionSpan.Start.Line].Start,
                text.Lines[diagnosticLinePositionSpan.End.Line].End
                );

            var root = await syntaxTree.GetRootAsync(cancellationToken).ConfigureAwait(false);

#if CODE_STYLE
            var formattedRoot = Formatter.Format(
                root,
                formatterState,
                new[] { spanToFormat },
                options,
                Formatter.GetDefaultFormattingRules(formatterState),
                cancellationToken
                );
#else
            var formattedRoot = Formatter.Format(
                root,
                spanToFormat,
                formatterState,
                options,
                cancellationToken
                );
#endif

            return(syntaxTree.WithRootAndOptions(formattedRoot, syntaxTree.Options));
        }
Esempio n. 5
0
 /// <summary>
 /// Attaches the node to a SyntaxTree that the same options as <paramref name="oldTree"/>
 /// </summary>
 internal static SyntaxNode AsRootOfNewTreeWithOptionsFrom(this SyntaxNode node, SyntaxTree oldTree)
 {
     return(oldTree.WithRootAndOptions(node, oldTree.Options).GetRoot());
 }