コード例 #1
0
        public static async Task <ConversionResult> ConvertSingleAsync <TLanguageConversion>(Document document, SingleConversionOptions conversionOptions, IProgress <ConversionProgress> progress = null, CancellationToken cancellationToken = default) where TLanguageConversion : ILanguageConversion, new()
        {
            if (conversionOptions.SelectedTextSpan is { Length : > 0 } span)
            {
                document = await WithAnnotatedSelectionAsync(document, span);
            }
            var conversionResults = await ConvertDocumentsAsync <TLanguageConversion>(new[] { document }, conversionOptions, progress, cancellationToken).ToArrayAsync(cancellationToken);

            var codeResult = conversionResults.First(r => r.SourcePathOrNull == document.FilePath);

            codeResult.Exceptions = conversionResults.SelectMany(x => x.Exceptions).ToArray();
            return(codeResult);
        }
コード例 #2
0
        public static async Task <ConversionResult> ConvertSingle <TLanguageConversion>(Document document, SingleConversionOptions conversionOptions, IProgress <ConversionProgress> progress = null, CancellationToken cancellationToken = default) where TLanguageConversion : ILanguageConversion, new()
        {
            progress = progress ?? new Progress <ConversionProgress>();
            using var roslynEntryPoint = await RoslynEntryPoint(progress);

            var languageConversion = new TLanguageConversion {
                ConversionOptions = conversionOptions
            };

            bool returnSelectedNode = conversionOptions.SelectedTextSpan.Length > 0;

            if (returnSelectedNode)
            {
                document = await WithAnnotatedSelection(document, conversionOptions.SelectedTextSpan);
            }

            var projectContentsConverter = await languageConversion.CreateProjectContentsConverter(document.Project, progress, cancellationToken);

            document = projectContentsConverter.Project.GetDocument(document.Id);

            var conversion        = new ProjectConversion(projectContentsConverter, new[] { document }, Enumerable.Empty <TextDocument>(), languageConversion, cancellationToken, conversionOptions.ShowCompilationErrors, returnSelectedNode);
            var conversionResults = await conversion.Convert(progress).ToArrayAsync();

            return(GetSingleResultForDocument(conversionResults, document, progress));
        }
コード例 #3
0
        public static async Task <ConversionResult> ConvertSingle <TLanguageConversion>(Document document, SingleConversionOptions conversionOptions, IProgress <ConversionProgress> progress = null, CancellationToken cancellationToken = default) where TLanguageConversion : ILanguageConversion, new()
        {
            progress = progress ?? new Progress <ConversionProgress>();
            using var roslynEntryPoint = await RoslynEntryPoint(progress);

            var languageConversion = new TLanguageConversion {
                ConversionOptions = conversionOptions
            };

            bool returnSelectedNode = conversionOptions.SelectedTextSpan.Length > 0;

            if (returnSelectedNode)
            {
                document = await WithAnnotatedSelection(document, conversionOptions.SelectedTextSpan);
            }

            var projectContentsConverter = await languageConversion.CreateProjectContentsConverter(document.Project, progress, cancellationToken);

            document = projectContentsConverter.Project.GetDocument(document.Id);

            var conversion        = new ProjectConversion(projectContentsConverter, new[] { document }, languageConversion, cancellationToken, conversionOptions.ShowCompilationErrors, returnSelectedNode);
            var conversionResults = await conversion.Convert(progress).ToArrayAsync();

            var codeResult = conversionResults.SingleOrDefault(x => !string.IsNullOrWhiteSpace(x.ConvertedCode))
                             ?? conversionResults.First();

            codeResult.Exceptions = conversionResults.SelectMany(x => x.Exceptions).ToArray();
            return(codeResult);
        }