示例#1
0
        private GeneratorDriverState ApplyPartialEdit(GeneratorDriverState state, PendingEdit edit, CancellationToken cancellationToken = default)
        {
            var initialState = state;

            // see if any generators accept this particular edit
            var stateBuilder = PooledDictionary <ISourceGenerator, GeneratorState> .GetInstance();

            for (int i = 0; i < initialState.Generators.Length; i++)
            {
                var generator      = initialState.Generators[i];
                var generatorState = initialState.GeneratorStates[i];
                if (edit.AcceptedBy(generatorState.Info))
                {
                    // attempt to apply the edit
                    var previousSources = CreateSourcesCollection();
                    previousSources.AddRange(generatorState.SourceTexts);
                    var context   = new GeneratorEditContext(previousSources, cancellationToken);
                    var succeeded = edit.TryApply(generatorState.Info, context);
                    if (!succeeded)
                    {
                        // we couldn't successfully apply this edit
                        // return the original state noting we failed
                        return(initialState.With(editsFailed: true));
                    }

                    // update the state with the new edits
                    var additionalSources = previousSources.ToImmutableAndFree();
                    state = state.With(generatorStates: state.GeneratorStates.SetItem(i, new GeneratorState(generatorState.Info, sourceTexts: additionalSources, trees: ParseAdditionalSources(generator, additionalSources, cancellationToken), diagnostics: ImmutableArray <Diagnostic> .Empty)));
                }
            }
            state = edit.Commit(state);
            return(state);
        }
示例#2
0
        private GeneratorDriverState ApplyPartialEdit(GeneratorDriverState state, PendingEdit edit, CancellationToken cancellationToken = default)
        {
            var initialState = state;

            // see if any generators accept this particular edit
            var stateBuilder = PooledDictionary <ISourceGenerator, GeneratorState> .GetInstance();

            foreach (var(generator, generatorState) in state.GeneratorStates)
            {
                if (edit.AcceptedBy(generatorState.Info))
                {
                    // attempt to apply the edit
                    var context   = new EditContext(generatorState.Sources.Keys.ToImmutableArray(), cancellationToken);
                    var succeeded = edit.TryApply(generatorState.Info, context);
                    if (!succeeded)
                    {
                        // we couldn't successfully apply this edit
                        // return the original state noting we failed
                        return(initialState.With(editsFailed: true));
                    }

                    // update the state with the new edits
                    state = state.With(generatorStates: state.GeneratorStates.SetItem(generator, generatorState.WithSources(ParseAdditionalSources(context.AdditionalSources.ToImmutableAndFree(), cancellationToken))));
                }
            }
            state = edit.Commit(state);
            return(state);
        }
示例#3
0
 internal GeneratorDriver(GeneratorDriverState state)
 {
     Debug.Assert(
         state.Generators.GroupBy(s => s.GetType()).Count() == state.Generators.Length
         ); // ensure we don't have duplicate generator types
     _state = state;
 }
示例#4
0
 public Builder(Compilation compilation, GeneratorDriverState driverState, SyntaxStore.Builder syntaxStore, CancellationToken cancellationToken = default)
 {
     Compilation        = compilation;
     DriverState        = driverState;
     _previousTable     = driverState.StateTable;
     _cancellationToken = cancellationToken;
     SyntaxStore        = syntaxStore;
 }
示例#5
0
 public Builder(Compilation compilation, GeneratorDriverState driverState, ImmutableArray <ISyntaxInputNode> syntaxInputNodes, CancellationToken cancellationToken = default)
 {
     Compilation        = compilation;
     DriverState        = driverState;
     _previousTable     = driverState.StateTable;
     _syntaxInputNodes  = syntaxInputNodes;
     _cancellationToken = cancellationToken;
 }
示例#6
0
        private static GeneratorDriverState StateWithPendingEditsApplied(GeneratorDriverState state)
        {
            if (state.Edits.Length == 0)
            {
                return(state);
            }

            foreach (var edit in state.Edits)
            {
                state = edit.Commit(state);
            }
            return(state.With(edits: ImmutableArray <PendingEdit> .Empty, editsFailed: false));
        }
示例#7
0
 internal GeneratorDriver(
     ParseOptions parseOptions,
     ImmutableArray <ISourceGenerator> generators,
     AnalyzerConfigOptionsProvider optionsProvider,
     ImmutableArray <AdditionalText> additionalTexts
     )
 {
     _state = new GeneratorDriverState(
         parseOptions,
         optionsProvider,
         generators,
         additionalTexts,
         ImmutableArray.Create(new GeneratorState[generators.Length]),
         ImmutableArray <PendingEdit> .Empty,
         editsFailed: true
         );
 }
示例#8
0
        private static Compilation RemoveGeneratedSyntaxTrees(GeneratorDriverState state, Compilation compilation)
        {
            ArrayBuilder <SyntaxTree> trees = ArrayBuilder <SyntaxTree> .GetInstance();

            foreach (var generatorState in state.GeneratorStates)
            {
                foreach (var tree in generatorState.Trees)
                {
                    if (tree is object && compilation.ContainsSyntaxTree(tree))
                    {
                        trees.Add(tree);
                    }
                }
            }

            var comp = compilation.RemoveSyntaxTrees(trees);

            trees.Free();
            return(comp);
        }
示例#9
0
 internal abstract GeneratorDriver FromState(GeneratorDriverState state);
示例#10
0
        private GeneratorDriver BuildFinalCompilation(Compilation compilation, out Compilation outputCompilation, GeneratorDriverState state, CancellationToken cancellationToken)
        {
            ArrayBuilder <SyntaxTree> trees = ArrayBuilder <SyntaxTree> .GetInstance();

            foreach (var generatorState in state.GeneratorStates)
            {
                trees.AddRange(generatorState.Trees);
            }
            outputCompilation = compilation.AddSyntaxTrees(trees);
            trees.Free();

            state = state.With(edits: ImmutableArray <PendingEdit> .Empty,
                               editsFailed: false);
            return(FromState(state));
        }
示例#11
0
 internal GeneratorDriver(ParseOptions parseOptions, ImmutableArray <ISourceGenerator> generators, AnalyzerConfigOptionsProvider optionsProvider, ImmutableArray <AdditionalText> additionalTexts, GeneratorDriverOptions driverOptions)
 {
     (var filteredGenerators, var incrementalGenerators) = GetIncrementalGenerators(generators, SourceExtension);
     _state = new GeneratorDriverState(parseOptions, optionsProvider, filteredGenerators, incrementalGenerators, additionalTexts, ImmutableArray.Create(new GeneratorState[filteredGenerators.Length]), DriverStateTable.Empty, SyntaxStore.Empty, driverOptions.DisabledOutputs, runtime: TimeSpan.Zero, driverOptions.TrackIncrementalGeneratorSteps);
 }
示例#12
0
 internal override GeneratorDriverState Commit(GeneratorDriverState state) => state.With(additionalTexts: state.AdditionalTexts.Add(this.AddedText));
示例#13
0
 internal abstract GeneratorDriverState Commit(GeneratorDriverState state);
示例#14
0
 internal GeneratorDriver(ParseOptions parseOptions, ImmutableArray <ISourceGenerator> generators, ImmutableArray <AdditionalText> additionalTexts)
 {
     _state = new GeneratorDriverState(parseOptions, generators, additionalTexts, ImmutableDictionary <ISourceGenerator, GeneratorState> .Empty, ImmutableArray <PendingEdit> .Empty, editsFailed: true);
 }
示例#15
0
 internal GeneratorDriver(GeneratorDriverState state)
 {
     _state = state;
 }
示例#16
0
 internal GeneratorDriver(ParseOptions parseOptions, ImmutableArray <ISourceGenerator> generators, AnalyzerConfigOptionsProvider optionsProvider, ImmutableArray <AdditionalText> additionalTexts, bool enableIncremental)
 {
     (var filteredGenerators, var incrementalGenerators) = GetIncrementalGenerators(generators, enableIncremental);
     _state = new GeneratorDriverState(parseOptions, optionsProvider, filteredGenerators, incrementalGenerators, additionalTexts, ImmutableArray.Create(new GeneratorState[filteredGenerators.Length]), DriverStateTable.Empty, enableIncremental);
 }