Mono.TextEditor.TextEditorData.GetMarkup C# (CSharp) Method

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
		public string GetMarkup (int offset, int length, bool removeIndent, bool useColors = true, bool replaceTabs = true)
		{
			ISyntaxMode mode = Document.SyntaxMode;
			var style = ColorStyle;

			if (style == null) {
				var str = Document.GetTextAt (offset, length);
				if (removeIndent)
					str = str.TrimStart (' ', '\t');
				return ConvertToPangoMarkup (str, replaceTabs);
			}

			int indentLength = SyntaxMode.GetIndentLength (Document, offset, length, false);
			int curOffset = offset;

			StringBuilder result = new StringBuilder ();
			while (curOffset < offset + length && curOffset < Document.TextLength) {
				DocumentLine line = Document.GetLineByOffset (curOffset);
				int toOffset = System.Math.Min (line.Offset + line.Length, offset + length);
				var styleStack = new Stack<ChunkStyle> ();

				foreach (var chunk in mode.GetChunks (style, line, curOffset, toOffset - curOffset)) {
					var chunkStyle = style.GetChunkStyle (chunk);
					bool setBold = (styleStack.Count > 0 && styleStack.Peek ().FontWeight != chunkStyle.FontWeight) || 
						chunkStyle.FontWeight != FontWeight.Normal;
					bool setItalic = (styleStack.Count > 0 && styleStack.Peek ().FontStyle != chunkStyle.FontStyle) || 
						chunkStyle.FontStyle != FontStyle.Normal;
					bool setUnderline = chunkStyle.Underline && (styleStack.Count == 0 || !styleStack.Peek ().Underline) ||
							!chunkStyle.Underline && (styleStack.Count == 0 || styleStack.Peek ().Underline);
					bool setColor = styleStack.Count == 0 || TextViewMargin.GetPixel (styleStack.Peek ().Foreground) != TextViewMargin.GetPixel (chunkStyle.Foreground);
					if (setColor || setBold || setItalic || setUnderline) {
						if (styleStack.Count > 0) {
							result.Append ("</span>");
							styleStack.Pop ();
						}
						result.Append ("<span");
						if (useColors) {
							result.Append (" foreground=\"");
							result.Append (SyntaxMode.ColorToPangoMarkup (chunkStyle.Foreground));
							result.Append ("\"");
						}
						if (chunkStyle.FontWeight != Xwt.Drawing.FontWeight.Normal)
							result.Append (" weight=\"" + chunkStyle.FontWeight + "\"");
						if (chunkStyle.FontStyle != Xwt.Drawing.FontStyle.Normal)
							result.Append (" style=\"" + chunkStyle.FontStyle + "\"");
						if (chunkStyle.Underline)
							result.Append (" underline=\"single\"");
						result.Append (">");
						styleStack.Push (chunkStyle);
					}
					result.Append (ConvertToPangoMarkup (Document.GetTextBetween (chunk.Offset, System.Math.Min (chunk.EndOffset, Document.TextLength)), replaceTabs));
				}
				while (styleStack.Count > 0) {
					result.Append ("</span>");
					styleStack.Pop ();
				}

				curOffset = line.EndOffsetIncludingDelimiter;
				if (removeIndent)
					curOffset += indentLength;
				if (result.Length > 0 && curOffset < offset + length)
					result.AppendLine ();
			}
			return result.ToString ();
		}

Usage Example

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);
            }
        }
All Usage Examples Of Mono.TextEditor.TextEditorData::GetMarkup