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); }
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); }
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; }
public Builder(Compilation compilation, GeneratorDriverState driverState, SyntaxStore.Builder syntaxStore, CancellationToken cancellationToken = default) { Compilation = compilation; DriverState = driverState; _previousTable = driverState.StateTable; _cancellationToken = cancellationToken; SyntaxStore = syntaxStore; }
public Builder(Compilation compilation, GeneratorDriverState driverState, ImmutableArray <ISyntaxInputNode> syntaxInputNodes, CancellationToken cancellationToken = default) { Compilation = compilation; DriverState = driverState; _previousTable = driverState.StateTable; _syntaxInputNodes = syntaxInputNodes; _cancellationToken = cancellationToken; }
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)); }
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 ); }
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); }
internal abstract GeneratorDriver FromState(GeneratorDriverState state);
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)); }
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); }
internal override GeneratorDriverState Commit(GeneratorDriverState state) => state.With(additionalTexts: state.AdditionalTexts.Add(this.AddedText));
internal abstract GeneratorDriverState Commit(GeneratorDriverState state);
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); }
internal GeneratorDriver(GeneratorDriverState state) { _state = state; }
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); }