コード例 #1
0
        public static async Task <ConversionResult> ConvertTextAsync <TLanguageConversion>(string text, TextConversionOptions conversionOptions, IProgress <ConversionProgress> progress = null, CancellationToken cancellationToken = default) where TLanguageConversion : ILanguageConversion, new()
        {
            progress ??= new Progress <ConversionProgress>();
            using var roslynEntryPoint = await RoslynEntryPointAsync(progress);

            var languageConversion = new TLanguageConversion {
                ConversionOptions = conversionOptions
            };
            var syntaxTree = languageConversion.MakeFullCompilationUnit(text, out var textSpan);

            if (conversionOptions.SourceFilePath != null)
            {
                syntaxTree = syntaxTree.WithFilePath(conversionOptions.SourceFilePath);
            }
            if (textSpan.HasValue)
            {
                conversionOptions.SelectedTextSpan = textSpan.Value;
            }
            var document = await languageConversion.CreateProjectDocumentFromTreeAsync(syntaxTree, conversionOptions.References);

            return(await ConvertSingleAsync <TLanguageConversion>(document, conversionOptions, progress, cancellationToken));
        }