GetMarkup() public method

public GetMarkup ( int offset, int length, bool removeIndent, bool useColors = true, bool replaceTabs = true ) : string
offset int
length int
removeIndent bool
useColors bool
replaceTabs bool
return string
Example #1
0
//		void ResultLineDataFunc (TreeViewColumn column, CellRenderer cell, TreeModel model, TreeIter iter)
//		{
//			if (TreeIter.Zero.Equals (iter))
//				return;
//			var lineRenderer = (CellRendererText)cell;
//			var searchResult = (SearchResult)store.GetValue (iter, SearchResultColumn);
//			if (searchResult == null)
//				return;
//
//			Document doc = GetDocument (searchResult);
//			int lineNr = doc.OffsetToLineNumber (searchResult.Offset) + 1;
//			bool didRead = (bool)store.GetValue (iter, DidReadColumn);
//			lineRenderer.Markup = MarkupText (lineNr.ToString (), didRead);
//		}
//
        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;
            }

            var doc = GetDocument(searchResult);

            if (doc == null)
            {
                textRenderer.Markup = "Can't create document for:" + searchResult.FileName;
                return;
            }
            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)
                {
                    textRenderer.Markup = "Invalid line number " + searchResult.LineNumber + " from offset: " + searchResult.Offset;
                    return;
                }
                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)));

                uint start;
                uint end;
                try {
                    start = (uint)TextViewMargin.TranslateIndexToUTF8(lineText, col);
                    end   = (uint)TextViewMargin.TranslateIndexToUTF8(lineText, 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 {
                textRenderer.Markup = searchResult.Markup;

                if (!isSelected)
                {
                    var    searchColor = highlightStyle.SearchResult.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;
                    }
                    var attr = new Pango.AttrBackground((ushort)(searchColor.R * ushort.MaxValue), (ushort)(searchColor.G * ushort.MaxValue), (ushort)(searchColor.B * ushort.MaxValue));
                    attr.StartIndex = searchResult.StartIndex;
                    attr.EndIndex   = searchResult.EndIndex;

                    using (var list = textRenderer.Attributes.Copy()) {
                        list.Insert(attr);
                        textRenderer.Attributes = list;
                    }
                }
            } catch (Exception e) {
                LoggingService.LogError("Error whil setting the text renderer markup to: " + searchResult.Markup, e);
            }
        }
//		void ResultLineDataFunc (TreeViewColumn column, CellRenderer cell, TreeModel model, TreeIter iter)
//		{
//			if (TreeIter.Zero.Equals (iter))
//				return;
//			var lineRenderer = (CellRendererText)cell;
//			var searchResult = (SearchResult)store.GetValue (iter, SearchResultColumn);
//			if (searchResult == null)
//				return;
//			
//			Document doc = GetDocument (searchResult);
//			int lineNr = doc.OffsetToLineNumber (searchResult.Offset) + 1;
//			bool didRead = (bool)store.GetValue (iter, DidReadColumn);
//			lineRenderer.Markup = MarkupText (lineNr.ToString (), didRead);
//		}
//		
		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;
			}
			
			var doc = GetDocument (searchResult);
			if (doc == null) {
				textRenderer.Markup = "Can't create document for:" + searchResult.FileName;
				return;
			}
			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) {
					textRenderer.Markup = "Invalid line number " + searchResult.LineNumber + " from offset: " + searchResult.Offset;
					return;
				}
				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)));

				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 {
				textRenderer.Markup = 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;
					}
					var attr = new Pango.AttrBackground ((ushort)(searchColor.R * ushort.MaxValue), (ushort)(searchColor.G * ushort.MaxValue), (ushort)(searchColor.B * ushort.MaxValue));
					attr.StartIndex = searchResult.StartIndex;
					attr.EndIndex = searchResult.EndIndex;

					using (var list = textRenderer.Attributes.Copy ()) {
						list.Insert (attr);
						textRenderer.Attributes = list;
					}
				}
			} catch (Exception e) {
				LoggingService.LogError ("Error whil setting the text renderer markup to: " + searchResult.Markup, e);
			}
		}
//		void ResultLineDataFunc (TreeViewColumn column, CellRenderer cell, TreeModel model, TreeIter iter)
//		{
//			if (TreeIter.Zero.Equals (iter))
//				return;
//			var lineRenderer = (CellRendererText)cell;
//			var searchResult = (SearchResult)store.GetValue (iter, SearchResultColumn);
//			if (searchResult == null)
//				return;
//			
//			Document doc = GetDocument (searchResult);
//			int lineNr = doc.OffsetToLineNumber (searchResult.Offset) + 1;
//			bool didRead = (bool)store.GetValue (iter, DidReadColumn);
//			lineRenderer.Markup = MarkupText (lineNr.ToString (), didRead);
//		}
//		
		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;
			}
			
			var doc = GetDocument (searchResult);
			if (doc == null) {
				textRenderer.Markup = "Can't create document for:" + searchResult.FileName;
				return;
			}
			int lineNr = doc.OffsetToLineNumber (searchResult.Offset);
			DocumentLine line = doc.GetLine (lineNr);
			if (line == null) {
				textRenderer.Markup = "Invalid line number " + lineNr + " from offset: " + searchResult.Offset;
				return;
			}
			bool isSelected = treeviewSearchResults.Selection.IterIsSelected (iter);
			int indent = line.GetIndentation (doc).Length;
			var data = new Mono.TextEditor.TextEditorData (doc);
			data.ColorStyle = highlightStyle;
			string markup = doc.SyntaxMode != null ?
				data.GetMarkup (line.Offset + indent, line.Length - indent, true, !isSelected, false) :
				GLib.Markup.EscapeText (doc.GetTextAt (line.Offset, line.Length));
			
			if (!isSelected) {
				int col = searchResult.Offset - line.Offset - indent;
				string tag;
				int pos1 = FindPosition (markup, col, out tag);
				int pos2 = FindPosition (markup, col + searchResult.Length, out tag);
				if (pos1 >= 0 && pos2 >= 0) {
					markup = tag.StartsWith ("span") ? markup.Insert (pos2, "</span></span><" + tag + ">") : markup.Insert (pos2, "</span>");
					Color searchColor = Mono.TextEditor.Highlighting.ColorScheme.ToGdkColor (highlightStyle.SearchTextBg);
					double b1 = Mono.TextEditor.HslColor.Brightness (searchColor);
					double b2 = Mono.TextEditor.HslColor.Brightness (AdjustColor (Style.Base (StateType.Normal), highlightStyle.Default.Color));
					double delta = Math.Abs (b1 - b2);
					if (delta < 0.1) {
						Mono.TextEditor.HslColor color1 = highlightStyle.SearchTextBg;
						if (color1.L + 0.5 > 1.0) {
							color1.L -= 0.5;
						} else {
							color1.L += 0.5;
						}
						searchColor = color1;
					}
					markup = markup.Insert (pos1, "<span background=\"" + SyntaxMode.ColorToPangoMarkup (searchColor) + "\">");
				}
			}
			string markupText = AdjustColors (markup.Replace ("\t", new string (' ', TextEditorOptions.DefaultOptions.TabSize)));
			try {
				textRenderer.Markup = markupText;
			} catch (Exception e) {
				LoggingService.LogError ("Error whil setting the text renderer markup to: " + markup, e);
			}
		}
Example #4
0
        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)
                        {
                            var  markupBuilder = new StringBuilder();
                            bool inMarkup = false, inEntity = false, closed = false;
                            int  i = 0;
                            for (int j = 0; j < textMarkup.Length; j++)
                            {
                                var ch = textMarkup [j];
                                if (inEntity)
                                {
                                    if (ch == ';')
                                    {
                                        inEntity = false;
                                    }
                                    markupBuilder.Append(ch);
                                    continue;
                                }
                                if (inMarkup)
                                {
                                    if (ch == '>')
                                    {
                                        inMarkup = false;
                                    }
                                    markupBuilder.Append(ch);
                                    continue;
                                }
                                if (i == searchResult.EndIndex)
                                {
                                    markupBuilder.Append("</span>");
                                    markupBuilder.Append(textMarkup.Substring(j));
                                    closed = true;
                                    break;
                                }
                                if (ch == '&')
                                {
                                    inEntity = true;
                                    markupBuilder.Append(ch);
                                    continue;
                                }
                                if (ch == '<')
                                {
                                    inMarkup = true;
                                    markupBuilder.Append(ch);
                                    continue;
                                }
                                if (i == searchResult.StartIndex)
                                {
                                    markupBuilder.Append("<span background=\"" + ColorToPangoMarkup((HslColor)searchColor) + "\">");
                                }
                                markupBuilder.Append(ch);
                                i++;
                            }
                            if (!closed)
                            {
                                markupBuilder.Append("</span>");
                            }
                            textMarkup = markupBuilder.ToString();
                        }
                    }
                } catch (Exception e) {
                    LoggingService.LogError("Error whil setting the text renderer markup to: " + searchResult.Markup, e);
                }
end:
                searchResult.TextMarkup = textMarkup;
            }
            textRenderer.Markup = textMarkup;
        }
Example #5
0
//		void ResultLineDataFunc (TreeViewColumn column, CellRenderer cell, TreeModel model, TreeIter iter)
//		{
//			if (TreeIter.Zero.Equals (iter))
//				return;
//			var lineRenderer = (CellRendererText)cell;
//			var searchResult = (SearchResult)store.GetValue (iter, SearchResultColumn);
//			if (searchResult == null)
//				return;
//
//			Document doc = GetDocument (searchResult);
//			int lineNr = doc.OffsetToLineNumber (searchResult.Offset) + 1;
//			bool didRead = (bool)store.GetValue (iter, DidReadColumn);
//			lineRenderer.Markup = MarkupText (lineNr.ToString (), didRead);
//		}
//
        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;
            }

            var doc = GetDocument(searchResult);

            if (doc == null)
            {
                textRenderer.Markup = "Can't create document for:" + searchResult.FileName;
                return;
            }
            int          lineNr = doc.OffsetToLineNumber(searchResult.Offset);
            DocumentLine line   = doc.GetLine(lineNr);

            if (line == null)
            {
                textRenderer.Markup = "Invalid line number " + lineNr + " from offset: " + searchResult.Offset;
                return;
            }
            bool isSelected = treeviewSearchResults.Selection.IterIsSelected(iter);
            int  indent     = line.GetIndentation(doc).Length;
            var  data       = new Mono.TextEditor.TextEditorData(doc);

            data.ColorStyle = highlightStyle;
            string markup = doc.SyntaxMode != null?
                            data.GetMarkup(line.Offset + indent, line.Length - indent, true, !isSelected, false) :
                                GLib.Markup.EscapeText(doc.GetTextAt(line.Offset, line.Length));

            if (!isSelected)
            {
                int    col = searchResult.Offset - line.Offset - indent;
                string tag;
                int    pos1 = FindPosition(markup, col, out tag);
                int    pos2 = FindPosition(markup, col + searchResult.Length, out tag);
                if (pos1 >= 0 && pos2 >= 0)
                {
                    markup = tag.StartsWith("span") ? markup.Insert(pos2, "</span></span><" + tag + ">") : markup.Insert(pos2, "</span>");
                    Color  searchColor = Mono.TextEditor.Highlighting.ColorScheme.ToGdkColor(highlightStyle.SearchTextBg);
                    double b1          = Mono.TextEditor.HslColor.Brightness(searchColor);
                    double b2          = Mono.TextEditor.HslColor.Brightness(AdjustColor(Style.Base(StateType.Normal), highlightStyle.Default.Color));
                    double delta       = Math.Abs(b1 - b2);
                    if (delta < 0.1)
                    {
                        Mono.TextEditor.HslColor color1 = highlightStyle.SearchTextBg;
                        if (color1.L + 0.5 > 1.0)
                        {
                            color1.L -= 0.5;
                        }
                        else
                        {
                            color1.L += 0.5;
                        }
                        searchColor = color1;
                    }
                    markup = markup.Insert(pos1, "<span background=\"" + SyntaxMode.ColorToPangoMarkup(searchColor) + "\">");
                }
            }
            string markupText = AdjustColors(markup.Replace("\t", new string (' ', TextEditorOptions.DefaultOptions.TabSize)));

            try {
                textRenderer.Markup = markupText;
            } catch (Exception e) {
                LoggingService.LogError("Error whil setting the text renderer markup to: " + markup, e);
            }
        }