private static void MakeDocumentChanges( ISolution solution, List <FileGeneratorOutput> fileGeneratorOutputs, List <InPlaceGeneratorOutput> inPlaceGeneratorOutputs) { foreach (FileGeneratorOutput output in fileGeneratorOutputs) { IDocument document = output.GeneratedProjectFile.GetDocument(); document.ReplaceText(document.DocumentRange, output.GeneratedText); ExecutionHelpers.OrginizeUsingsAndFormatFile(solution, document); } foreach (InPlaceGeneratorOutput output in inPlaceGeneratorOutputs) { IDocument document = output.ProjectFile.GetDocument(); if (!output.RangeToDelete.IsEmpty) { document.DeleteText(output.RangeToDelete); } document.InsertText(output.PositionToInsert, output.GeneratedText); var treeTextRange = TreeTextRange.FromLength(new TreeOffset(output.PositionToInsert), output.GeneratedText.Length); ExecutionHelpers.FormatFileRangeAndAddUsingDirectives(solution, document, treeTextRange, output.MissingUsingDirectives); } }
protected override bool TryExecute(IInPlaceGenerator generator, GeneratorExecutionHost executionHost) { IDocument document = generator.DataProvider.Document; bool executed = ExecutionHelpers.TryExecuteGenerator( generator, out string generatedText, out TextRange rangeToDelete, out int positionToInsert, out IReadOnlyCollection <IUsingDirective> missingUsingDirectives ); if (!executed) { return(false); } ISolution solution = generator.DataProvider.Solution; using (var progressIndicator = NullProgressIndicator.Create()) { IProjectModelTransactionCookie transaction = solution.CreateTransactionCookie(DefaultAction.Rollback, "T4 Generating file", progressIndicator); using (transaction) { if (!rangeToDelete.IsEmpty) { document.DeleteText(rangeToDelete); } document.InsertText(positionToInsert, generatedText); var treeTextRange = TreeTextRange.FromLength(new TreeOffset(positionToInsert), generatedText.Length); ExecutionHelpers.FormatFileRangeAndAddUsingDirectives(solution, document, treeTextRange, missingUsingDirectives); transaction.Commit(progressIndicator); } } return(true); }