/
githubmirror
/
roslyn
Обзор
Документация
Войти
/
githubmirror
/
roslyn
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
src/Workspaces/CoreTestUtilities/GenerateFileForEachAdditionalFileWithContentsCommented.cs
39 строк
2 KB
Cyrus Najmabadi
Use file scoped namespaces. (#77854)
27 мар 2025, 09:55
Не верифицирован
27 мар 2025, 09:55
98d41b8
Код
Авторство
О чём код?
// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. using System.IO; using System.Text; using System.Threading; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.Text; using Roslyn.Utilities; namespace Roslyn.Test.Utilities; internal sealed class GenerateFileForEachAdditionalFileWithContentsCommented : IIncrementalGenerator { public const string StepName = "AdditionalTextsAsComments"; public void Initialize(IncrementalGeneratorInitializationContext context) { context.RegisterSourceOutput(context.AdditionalTextsProvider.Select((t, ct) => t).WithTrackingName(StepName), (context, additionalText) => context.AddSource( GetGeneratedFileName(additionalText.Path), GenerateSourceForAdditionalFile(additionalText, context.CancellationToken))); } private static SourceText GenerateSourceForAdditionalFile(AdditionalText file, CancellationToken cancellationToken) { // We're going to "comment" out the contents of the file when generating this var sourceText = file.GetText(cancellationToken); Contract.ThrowIfNull(sourceText, "Failed to fetch the text of an additional file."); var changes = sourceText.Lines.SelectAsArray(l => new TextChange(new TextSpan(l.Start, length: 0), "// ")); var generatedText = sourceText.WithChanges(changes); return SourceText.From(generatedText.ToString(), encoding: Encoding.UTF8); } private static string GetGeneratedFileName(string path) => $"{Path.GetFileNameWithoutExtension(path)}.generated"; }