Exemple #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);
        }
Exemple #2
0
 internal override bool TryApply(GeneratorInfo info, GeneratorEditContext context) =>
 info.EditCallback !.Invoke(context, this);
Exemple #3
0
 internal abstract bool TryApply(GeneratorInfo info, GeneratorEditContext context);