public async Task<Document> RemoveUnnecessaryRegions(DocumentConditionalRegionInfo info, CancellationToken cancellationToken = default(CancellationToken))
        {
            if (info == null)
            {
                throw new ArgumentNullException("info");
            }

            var changes = CalculateTextChanges(info.Chains);
            if (changes == null || changes.Count == 0)
            {
                return info.Document;
            }

            var newText = await info.Document.GetTextAsync(cancellationToken);

            try
            {
                newText = newText.WithChanges(changes);
            }
            catch (Exception)
            {
                var changesString = new StringBuilder();
                var syntaxTree = info.Document.GetSyntaxTreeAsync(cancellationToken).Result;

                foreach (var change in changes)
                {
                    var lineSpan = Location.Create(syntaxTree, change.Span).GetLineSpan();
                    changesString.AppendFormat("({0}-{1}): {2}", lineSpan.StartLinePosition.Line, lineSpan.EndLinePosition.Line, newText.GetSubText(change.Span).ToString());
                }

                Logger.WriteErrorLine(string.Format("Failed to remove regions from document '{0}':{1}{2}", info.Document.FilePath, Environment.NewLine, changesString.ToString()));
                return info.Document;
            }

            return info.Document.WithText(newText);
        }
Example #2
0
        private static async Task OnDocumentAnalyzed(AnalysisEngine engine, Options options, DocumentConditionalRegionInfo info, CancellationToken cancellationToken)
        {
            if (options.Edit)
            {
                var fileInfo = new FileInfo(info.Document.FilePath);
                if (fileInfo.IsReadOnly || !fileInfo.Exists)
                {
                    Console.WriteLine("warning: skipping document '{0}' because it {1}.",
                                      info.Document.FilePath, fileInfo.IsReadOnly ? "is read-only" : "does not exist");
                    return;
                }

                var document = await engine.RemoveUnnecessaryRegions(info, cancellationToken);

                document = await engine.SimplifyVaryingPreprocessorExpressions(document, cancellationToken);

                var text = await document.GetTextAsync(cancellationToken);

                using (var file = File.Open(document.FilePath, FileMode.Truncate, FileAccess.Write))
                {
                    var writer = new StreamWriter(file, text.Encoding);
                    text.Write(writer, cancellationToken);
                    await writer.FlushAsync();
                }
            }
        }
        private async Task<DocumentConditionalRegionInfo> GetConditionalRegionInfo(Document document, CancellationToken cancellationToken)
        {
            var chains = await GetConditionalRegionChains(document, cancellationToken);

            var info = new DocumentConditionalRegionInfo(document, chains);
            if (DocumentAnalyzed != null)
            {
                await DocumentAnalyzed(this, info, cancellationToken);
            }

            return info;
        }
        private static async Task OnDocumentAnalyzed(AnalysisEngine engine, Options options, DocumentConditionalRegionInfo info, CancellationToken cancellationToken)
        {
            if (options.Edit)
            {
                var fileInfo = new FileInfo(info.Document.FilePath);
                if (fileInfo.IsReadOnly || !fileInfo.Exists)
                {
                    Console.WriteLine("warning: skipping document '{0}' because it {1}.",
                        info.Document.FilePath, fileInfo.IsReadOnly ? "is read-only" : "does not exist");
                    return;
                }

                var document = await engine.RemoveUnnecessaryRegions(info, cancellationToken);
                document = await engine.SimplifyVaryingPreprocessorExpressions(document, cancellationToken);

                var text = await document.GetTextAsync(cancellationToken);
                using (var file = File.Open(document.FilePath, FileMode.Truncate, FileAccess.Write))
                {
                    var writer = new StreamWriter(file, text.Encoding);
                    text.Write(writer, cancellationToken);
                    await writer.FlushAsync();
                }
            }
        }