Example #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);
        }
Example #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);
        }