public void TestMarekReportedBug() { var result = PangoHelper.ColorMarkupBackground( "<span foreground=\"#000000\">ec.Report.Error (</span><span foreground=\"#A40000\">29</span><span foreground=\"#000000\">, loc, </span><span foreground=\"#A40000\">\"Cannot implicitly convert type `{0}' to `{1}'\"</span><span foreground=\"#000000\">,</span>", 16, 20, new Mono.TextEditor.HslColor(1d, 1d, 1d) ); Assert.AreEqual("<span foreground=\"#000000\">ec.Report.Error <span background=\"#FFFFFF\">(</span></span><span foreground=\"#A40000\"><span background=\"#FFFFFF\">29</span></span><span foreground=\"#000000\"><span background=\"#FFFFFF\">,</span> loc, </span><span foreground=\"#A40000\">\"Cannot implicitly convert type `{0}' to `{1}'\"</span><span foreground=\"#000000\">,</span>", result); }
public void TestBug25836() { var result = PangoHelper.ColorMarkupBackground( "<span foreground=\"#000000\">List<RevisionPath> foo;</span>", 5, 17, new Mono.TextEditor.HslColor(1d, 1d, 1d) ); Assert.AreEqual("<span foreground=\"#000000\">List<<span background=\"#FFFFFF\">RevisionPath</span>> foo;</span>", result); }
public void TestBug25110() { var result = PangoHelper.ColorMarkupBackground( "<span foreground=\"#000000\">Console.WriteLine (</span><span foreground=\"#3364A4\">base</span><span foreground=\"#000000\">.ToString());</span>", 19, 24, new Mono.TextEditor.HslColor(1d, 1d, 1d) ); Assert.AreEqual("<span foreground=\"#000000\">Console.WriteLine (</span><span foreground=\"#3364A4\"><span background=\"#FFFFFF\">base</span></span><span foreground=\"#000000\"><span background=\"#FFFFFF\">.</span>ToString());</span>", result); }
public void TestSimple() { var result = PangoHelper.ColorMarkupBackground( "simple", 1, 5, new Mono.TextEditor.HslColor(1d, 1d, 1d) ); Assert.AreEqual("s<span background=\"#FFFFFF\">impl</span>e", result); }
public void TestSpanClosingIssue() { var result = PangoHelper.ColorMarkupBackground( "<span foreground=\"#000000\"><</span><span foreground=\"#204987\">Begin</span><span foreground=\"#000000\">>/*</</span><span foreground=\"#204987\">Begin</span><span foreground=\"#000000\">></span>", 0, 17, new HslColor(1d, 1d, 1d) ); Assert.AreEqual("<span foreground=\"#000000\"><span background=\"#FFFFFF\"><</span></span><span foreground=\"#204987\"><span background=\"#FFFFFF\">Begin</span></span><span foreground=\"#000000\"><span background=\"#FFFFFF\">>/*</</span></span><span foreground=\"#204987\"><span background=\"#FFFFFF\">Begin</span></span><span foreground=\"#000000\"><span background=\"#FFFFFF\">></span></span>", result); }
void ResultTextDataFunc(TreeViewColumn column, CellRenderer cell, TreeModel model, TreeIter iter) { if (TreeIter.Zero.Equals(iter)) { return; } var textRenderer = (CellRendererText)cell; var searchResult = (SearchResult)store.GetValue(iter, SearchResultColumn); if (searchResult == null || searchResult.Offset < 0) { textRenderer.Markup = "Invalid search result"; return; } string textMarkup = markupCache.FirstOrDefault(t => t.Item1 == searchResult)?.Item2; bool isSelected = treeviewSearchResults.Selection.IterIsSelected(iter); if (isSelected) { textMarkup = null; } if (textMarkup == null) { var doc = GetDocument(searchResult); if (doc == null) { textMarkup = "Can't create document for:" + searchResult.FileName; goto end; } int lineNumber, startIndex = 0, endIndex = 0; try { lineNumber = doc.OffsetToLineNumber(searchResult.Offset); } catch (ArgumentOutOfRangeException) { lineNumber = -1; textMarkup = "Invalid search result offset"; goto end; } var line = doc.GetLine(lineNumber); if (line == null) { textMarkup = "Invalid line number " + lineNumber + " from offset: " + searchResult.Offset; goto end; } int indent = line.GetIndentation(doc).Length; var lineText = doc.GetTextAt(line.Offset + indent, line.Length - indent); int col = searchResult.Offset - line.Offset - indent; // search result contained part of the indent. if (col + searchResult.Length < lineText.Length) { lineText = doc.GetTextAt(line.Offset, line.Length); } string markup; if (isSelected) { markup = Ambience.EscapeText(doc.GetTextAt(line.Offset + indent, line.Length - indent)); } else { markup = doc.GetMarkup(line.Offset + indent, line.Length - indent, new MarkupOptions(MarkupFormat.Pango)); markup = AdjustColors(markup); } if (col >= 0) { uint start; uint end; try { start = (uint)TranslateIndexToUTF8(lineText, col); end = (uint)TranslateIndexToUTF8(lineText, Math.Min(lineText.Length, col + searchResult.Length)); } catch (Exception e) { LoggingService.LogError("Exception while translating index to utf8 (column was:" + col + " search result length:" + searchResult.Length + " line text:" + lineText + ")", e); return; } startIndex = (int)start; endIndex = (int)end; } try { textMarkup = markup; if (!isSelected) { var searchColor = searchResult.GetBackgroundMarkerColor(highlightStyle); double b1 = HslColor.Brightness(searchColor); double b2 = HslColor.Brightness(AdjustColor(Style.Base(StateType.Normal), SyntaxHighlightingService.GetColor(highlightStyle, EditorThemeColors.Foreground))); double delta = Math.Abs(b1 - b2); if (delta < 0.1) { var color1 = SyntaxHighlightingService.GetColor(highlightStyle, EditorThemeColors.FindHighlight); if (color1.L + 0.5 > 1.0) { color1.L -= 0.5; } else { color1.L += 0.5; } searchColor = color1; } if (startIndex != endIndex) { textMarkup = PangoHelper.ColorMarkupBackground(textMarkup, (int)startIndex, (int)endIndex, searchColor); } } else { var searchColor = this.treeviewSearchResults.Style.Base(StateType.Selected); searchColor = searchColor.AddLight(-0.2); textMarkup = PangoHelper.ColorMarkupBackground(textMarkup, (int)startIndex, (int)endIndex, searchColor); } } catch (Exception e) { LoggingService.LogError("Error whil setting the text renderer markup to: " + markup, e); } end: textMarkup = textMarkup.Replace("\t", new string (' ', doc.Options.TabSize)); if (!isSelected) { markupCache.Add(Tuple.Create(searchResult, textMarkup)); if (markupCache.Count > 100) { markupCache.RemoveAt(0); } } } textRenderer.Markup = textMarkup; }
async Task <string> CreateMarkupAsync(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); string newMarkup; using (var markupTimeoutSource = new CancellationTokenSource(150)) { newMarkup = await doc.GetMarkupAsync(line.Offset + markupStartOffset + indent, length, new MarkupOptions (MarkupFormat.Pango), markupTimeoutSource.Token).ConfigureAwait(false); 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); } return(FormatMarkup(newMarkup, trimStart, trimEnd, tabSize)); }
void ResultTextDataFunc(TreeViewColumn column, CellRenderer cell, TreeModel model, TreeIter iter) { if (TreeIter.Zero.Equals(iter)) { return; } var textRenderer = (CellRendererText)cell; var searchResult = (SearchResult)store.GetValue(iter, SearchResultColumn); if (searchResult == null || searchResult.Offset < 0) { textRenderer.Markup = "Invalid search result"; return; } string textMarkup = searchResult.TextMarkup; if (textMarkup == null) { var doc = GetDocument(searchResult); if (doc == null) { textMarkup = "Can't create document for:" + searchResult.FileName; goto end; } bool isSelected = treeviewSearchResults.Selection.IterIsSelected(iter); if (searchResult.Markup == null) { if (searchResult.LineNumber <= 0) { searchResult.LineNumber = doc.OffsetToLineNumber(searchResult.Offset); } DocumentLine line = doc.GetLine(searchResult.LineNumber); if (line == null) { textMarkup = "Invalid line number " + searchResult.LineNumber + " from offset: " + searchResult.Offset; goto end; } int indent = line.GetIndentation(doc).Length; var data = new Mono.TextEditor.TextEditorData(doc); data.ColorStyle = highlightStyle; var lineText = doc.GetTextAt(line.Offset + indent, line.Length - indent); int col = searchResult.Offset - line.Offset - indent; // search result contained part of the indent. if (col + searchResult.Length < lineText.Length) { lineText = doc.GetTextAt(line.Offset, line.Length); } var markup = doc.SyntaxMode != null? data.GetMarkup(line.Offset + indent, line.Length - indent, true, !isSelected, false) : GLib.Markup.EscapeText(lineText); searchResult.Markup = AdjustColors(markup.Replace("\t", new string (' ', TextEditorOptions.DefaultOptions.TabSize))); if (col >= 0) { uint start; uint end; try { start = (uint)TextViewMargin.TranslateIndexToUTF8(lineText, col); end = (uint)TextViewMargin.TranslateIndexToUTF8(lineText, Math.Min(lineText.Length, col + searchResult.Length)); } catch (Exception e) { LoggingService.LogError("Exception while translating index to utf8 (column was:" + col + " search result length:" + searchResult.Length + " line text:" + lineText + ")", e); return; } searchResult.StartIndex = start; searchResult.EndIndex = end; } } try { textMarkup = searchResult.Markup; if (!isSelected) { var searchColor = searchResult.GetBackgroundMarkerColor(highlightStyle).Color; double b1 = Mono.TextEditor.HslColor.Brightness(searchColor); double b2 = Mono.TextEditor.HslColor.Brightness(AdjustColor(Style.Base(StateType.Normal), (Mono.TextEditor.HslColor)highlightStyle.PlainText.Foreground)); double delta = Math.Abs(b1 - b2); if (delta < 0.1) { Mono.TextEditor.HslColor color1 = highlightStyle.SearchResult.Color; if (color1.L + 0.5 > 1.0) { color1.L -= 0.5; } else { color1.L += 0.5; } searchColor = color1; } if (searchResult.StartIndex != searchResult.EndIndex) { textMarkup = PangoHelper.ColorMarkupBackground(textMarkup, (int)searchResult.StartIndex, (int)searchResult.EndIndex, searchColor); } } } catch (Exception e) { LoggingService.LogError("Error whil setting the text renderer markup to: " + searchResult.Markup, e); } end: searchResult.TextMarkup = textMarkup; } textRenderer.Markup = textMarkup; }
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; if (col < indent) { trimEnd = line.Length > maximumMarkupLength; // search result contained part of the indent. lineText = doc.GetTextAt(line.Offset, Math.Min(maximumMarkupLength, line.Length)); markup = doc.GetMarkup(line.Offset, Math.Min(maximumMarkupLength, line.Length), new MarkupOptions(MarkupFormat.Pango)); } else { // if not crop the indent var 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); markup = doc.GetMarkup(line.Offset + markupStartOffset + indent, length, new MarkupOptions(MarkupFormat.Pango)); col -= indent; } selectedMarkup = Ambience.EscapeText(lineText); markup = widget.AdjustColors(markup); 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; } try { var searchColor = GetBackgroundMarkerColor(widget.HighlightStyle); double b1 = HslColor.Brightness(searchColor); double b2 = HslColor.Brightness(SearchResultWidget.AdjustColor(widget.Style.Base(Gtk.StateType.Normal), SyntaxHighlightingService.GetColor(widget.HighlightStyle, EditorThemeColors.Foreground))); 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) { markup = PangoHelper.ColorMarkupBackground(markup, (int)startIndex, (int)endIndex, searchColor); } // selected var selectedSearchColor = widget.Style.Base(Gtk.StateType.Selected); selectedSearchColor = searchColor.AddLight(-0.2); selectedMarkup = PangoHelper.ColorMarkupBackground(selectedMarkup, (int)startIndex, (int)endIndex, selectedSearchColor); } catch (Exception e) { LoggingService.LogError("Error while setting the text renderer markup to: " + markup, e); } markup = markup.Replace("\t", new string (' ', doc.Options.TabSize)); selectedMarkup = selectedMarkup.Replace("\t", new string (' ', doc.Options.TabSize)); if (trimStart) { markup = "…" + markup; selectedMarkup = "…" + selectedMarkup; } if (trimEnd) { markup += "…"; selectedMarkup += "…"; } }