public HighlightedLine HighlightLine(int lineNumber) { var documentLine = Document.GetLineByNumber(lineNumber); var currentVersion = Document.Version; EnlargeList(cachedLines, lineNumber + 1); var cachedLine = cachedLines[lineNumber]; if (cachedLine != null && currentVersion != null && cachedLine.Version.CompareAge(currentVersion) == 0 && currentVersion.BelongsToSameDocumentAs(cachedLine.Version)) { return(cachedLine); } cachedLines[lineNumber]?.Cancel(); var newLine = new VersionedHighlightedLine(Document, documentLine, Document.Version, cachedLine); cachedLines[lineNumber] = newLine; UpdateHighlightLineAsync(newLine); foreach (var line in cachedLines.ToArray().Reverse()) { if (!line?.DocumentLine?.IsDeleted != false) { break; } cachedLines.Remove(line); } return(newLine); }
private async void UpdateHighlightLineAsync(VersionedHighlightedLine line) { try { await Task.Run(async() => { await initialDelayTask.ConfigureAwait(false); line.CancellationToken.ThrowIfCancellationRequested(); var documentLine = line.DocumentLine; var currentVersion = Document.Version; if (line.Version == null || !currentVersion.BelongsToSameDocumentAs(line.Version) || currentVersion.CompareAge(line.Version) != 0) { return; } var spans = await GetClassifiedSpansAsync(documentLine, line.CancellationToken).ConfigureAwait(false); line.CancellationToken.ThrowIfCancellationRequested(); await TaskHelper.Run(() => { if (line.CancellationToken.IsCancellationRequested) { return; } var newLineSections = new List <HighlightedSection>(); foreach (var classifiedSpan in spans) { if (IsOutsideLine(documentLine, classifiedSpan.TextSpan.Start, classifiedSpan.TextSpan.Length)) { continue; } newLineSections.Add(new HighlightedSection { Color = CodeHighlightColors.GetHighlightingColor(classifiedSpan.ClassificationType), Offset = classifiedSpan.TextSpan.Start, Length = classifiedSpan.TextSpan.Length }); } if (!line.Sections.SequenceEqual(newLineSections, HighlightedSectionComparer.Default)) { line.Sections.Clear(); foreach (var newSection in newLineSections) { line.Sections.Add(newSection); } HighlightingStateChanged?.Invoke(documentLine.LineNumber, documentLine.LineNumber); } }, uiTaskScheduler).ConfigureAwait(false); }, line.CancellationToken); } catch (OperationCanceledException) { } }
private static bool CancelUpdate(IDocument document, VersionedHighlightedLine line) { var currentVersion = document.Version; return(line.CancellationToken.IsCancellationRequested || line.Version == null || !currentVersion.BelongsToSameDocumentAs(line.Version) || currentVersion.CompareAge(line.Version) != 0); }
public VersionedHighlightedLine(IDocument document, IDocumentLine documentLine, ITextSourceVersion version, VersionedHighlightedLine oldVersion) : base(document, documentLine) { Version = version; cancellationTokenSource = new CancellationTokenSource(); CancellationToken = cancellationTokenSource.Token; if (oldVersion != null) { foreach (var oldSection in oldVersion.Sections) { if (IsOutsideLine(documentLine, oldSection.Offset, oldSection.Length)) { continue; } Sections.Add(new HighlightedSection { Color = oldSection.Color, Offset = oldSection.Offset, Length = oldSection.Length }); } } }