async Task FillCache(SearchResultWidget widget) { FillLineCache(widget); copyData = ""; markup = selectedMarkup = ""; try { var doc = GetDocument(); if (doc == null) { return; } var lineNr = location.Value.Line; var line = doc.GetLine(lineNr); if (line != null) { location = new DocumentLocation(lineNr, Offset - line.Offset + 1); copyData = $"{FileName} ({location.Value.Line}, {location.Value.Column}):{doc.GetTextAt (line.Offset, line.Length)}"; markup = await CreateMarkupAsync(widget, doc, line); widget.QueueDraw(); } } catch (Exception e) { LoggingService.LogError("Error while getting search result", e); } }
async void CreateMarkup(SearchResultWidget widget, TextEditor doc, Editor.IDocumentLine line) { int startIndex = 0, endIndex = 0; int indent = line.GetIndentation(doc).Length; string lineText; int col = Offset - line.Offset; int markupStartOffset = 0; bool trimStart = false, trimEnd = false; int length; if (col < indent) { trimEnd = line.Length > maximumMarkupLength; length = Math.Min(maximumMarkupLength, line.Length); // search result contained part of the indent. lineText = doc.GetTextAt(line.Offset, length); } else { // if not crop the indent length = line.Length - indent; if (length > maximumMarkupLength) { markupStartOffset = Math.Min(Math.Max(0, col - indent - maximumMarkupLength / 2), line.Length - maximumMarkupLength); trimEnd = markupStartOffset + maximumMarkupLength < line.Length; trimStart = markupStartOffset > 0; length = maximumMarkupLength; } lineText = doc.GetTextAt(line.Offset + markupStartOffset + indent, length); col -= indent; } if (col >= 0) { uint start; uint end; try { start = (uint)TranslateIndexToUTF8(lineText, col - markupStartOffset); end = (uint)TranslateIndexToUTF8(lineText, Math.Min(lineText.Length, col - markupStartOffset + Length)); } catch (Exception e) { LoggingService.LogError("Exception while translating index to utf8 (column was:" + col + " search result length:" + Length + " line text:" + lineText + ")", e); return; } startIndex = (int)start; endIndex = (int)end; } var tabSize = doc.Options.TabSize; this.markup = this.selectedMarkup = markup = Ambience.EscapeText(lineText); var searchColor = GetBackgroundMarkerColor(widget.HighlightStyle); var selectedSearchColor = widget.Style.Base(Gtk.StateType.Selected); selectedSearchColor = searchColor.AddLight(-0.2); double b1 = HslColor.Brightness(searchColor); double b2 = HslColor.Brightness(SearchResultWidget.AdjustColor(widget.Style.Base(Gtk.StateType.Normal), SyntaxHighlightingService.GetColor(widget.HighlightStyle, EditorThemeColors.Foreground))); // selected markup = FormatMarkup(PangoHelper.ColorMarkupBackground(selectedMarkup, (int)startIndex, (int)endIndex, searchColor), trimStart, trimEnd, tabSize); selectedMarkup = FormatMarkup(PangoHelper.ColorMarkupBackground(selectedMarkup, (int)startIndex, (int)endIndex, selectedSearchColor), trimStart, trimEnd, tabSize); var newMarkup = await doc.GetMarkupAsync(line.Offset + markupStartOffset + indent, length, new MarkupOptions (MarkupFormat.Pango)); newMarkup = widget.AdjustColors(newMarkup); try { double delta = Math.Abs(b1 - b2); if (delta < 0.1) { var color1 = SyntaxHighlightingService.GetColor(widget.HighlightStyle, EditorThemeColors.FindHighlight); if (color1.L + 0.5 > 1.0) { color1.L -= 0.5; } else { color1.L += 0.5; } searchColor = color1; } if (startIndex != endIndex) { newMarkup = PangoHelper.ColorMarkupBackground(newMarkup, (int)startIndex, (int)endIndex, searchColor); } } catch (Exception e) { LoggingService.LogError("Error while setting the text renderer markup to: " + newMarkup, e); } newMarkup = FormatMarkup(newMarkup, trimStart, trimEnd, tabSize); this.markup = newMarkup; widget.QueueDraw(); }