示例#1
0
        private async Task Worker(CancellationToken cancellationToken)
        {
            while (true)
            {
                cancellationToken.ThrowIfCancellationRequested();

                await _semaphore.WaitAsync(cancellationToken).ConfigureAwait(false);

                HighlightedLine line;
                if (!_queue.TryDequeue(out line))
                {
                    continue;
                }

                var documentLine = line.DocumentLine;
                IEnumerable <ClassifiedSpan> spans;
                try
                {
                    spans = await GetClassifiedSpansAsync(documentLine).ConfigureAwait(false);
                }
                catch (Exception)
                {
                    continue;
                }

                foreach (var classifiedSpan in spans)
                {
                    if (IsOutsideLine(classifiedSpan, documentLine))
                    {
                        continue;
                    }
                    line.Sections.Add(new HighlightedSection
                    {
                        Color  = ClassificationHighlightColors.GetColor(classifiedSpan.ClassificationType),
                        Offset = classifiedSpan.TextSpan.Start,
                        Length = classifiedSpan.TextSpan.Length
                    });
                }

                OnHighlightingStateChanged(documentLine.LineNumber, documentLine.LineNumber);
            }
            // ReSharper disable once FunctionNeverReturns
        }
示例#2
0
        private HighlightedLine DoHighlightLine(IDocumentLine documentLine)
        {
            _line = new HighlightedLine(_document, documentLine);

            IEnumerable <ClassifiedSpan> spans;

            try
            {
                // TODO: check offset in Roslyn's doc
                spans = Classifier.GetClassifiedSpansAsync(_roslynHost.CurrentDocument,
                                                           new TextSpan(documentLine.Offset, documentLine.TotalLength), CancellationToken.None).GetAwaiter().GetResult();
            }
            catch (OperationCanceledException)
            {
                return(_line);
            }

            foreach (var classifiedSpan in spans)
            {
                if (classifiedSpan.TextSpan.Start > documentLine.EndOffset ||
                    classifiedSpan.TextSpan.End > documentLine.EndOffset)
                {
                    // TODO: this shouldn't happen, but the Roslyn document and AvalonEdit's somehow get out of sync
                    continue;
                }
                _line.Sections.Add(new HighlightedSection
                {
                    Color  = ClassificationHighlightColors.GetColor(classifiedSpan.ClassificationType),
                    Offset = classifiedSpan.TextSpan.Start,
                    Length = classifiedSpan.TextSpan.Length
                });
            }

            if (_cachedLines != null && _document.Version != null)
            {
                _cachedLines.Add(new CachedLine(_line, _document.Version));
            }
            return(_line);
        }