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);
        }
예제 #2
0
        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));
        }