private GetLineByOffset ( int offset ) : |
||
offset | int | |
return |
public void CheckGetLineInEmptyDocument() { Assert.AreEqual(1, document.Lines.Count); List <DocumentLine> lines = new List <DocumentLine>(document.Lines); Assert.AreEqual(1, lines.Count); DocumentLine line = document.Lines[0]; Assert.AreSame(line, lines[0]); Assert.AreSame(line, document.GetLineByNumber(1)); Assert.AreSame(line, document.GetLineByOffset(0)); }
/// <summary> /// Gets the line containing the character at a specific offset in the document /// (first char at index 0 at the beginning of document) /// </summary> public ITextLine GetLineByOffset(int offset, out int indexOfCharInLine) { IDocumentLine documentLine = _avalonEditTextDocument.GetLineByOffset(offset); indexOfCharInLine = offset - documentLine.Offset; return(BuildTextLineFromDocumentLine(documentLine)); }
/// <summary> /// Creates a HTML fragment from a part of a document. /// </summary> /// <param name="document">The document to create HTML from.</param> /// <param name="highlighter">The highlighter used to highlight the document. <c>null</c> is valid and will create HTML without any highlighting.</param> /// <param name="segment">The part of the document to create HTML for. You can pass <c>null</c> to create HTML for the whole document.</param> /// <param name="options">The options for the HTML creation.</param> /// <returns>HTML code for the document part.</returns> public static string CreateHtmlFragment(TextDocument document, IHighlighter highlighter, ISegment segment, HtmlOptions options) { if (document == null) throw new ArgumentNullException("document"); if (options == null) throw new ArgumentNullException("options"); if (highlighter != null && highlighter.Document != document) throw new ArgumentException("Highlighter does not belong to the specified document."); if (segment == null) segment = new SimpleSegment(0, document.TextLength); StringBuilder html = new StringBuilder(); int segmentEndOffset = segment.EndOffset; DocumentLine line = document.GetLineByOffset(segment.Offset); while (line != null && line.Offset < segmentEndOffset) { HighlightedLine highlightedLine; if (highlighter != null) highlightedLine = highlighter.HighlightLine(line.LineNumber); else highlightedLine = new HighlightedLine(document, line); SimpleSegment s = segment.GetOverlap(line); if (html.Length > 0) html.AppendLine("<br>"); html.Append(highlightedLine.ToHtml(s.Offset, s.EndOffset, options)); line = line.NextLine; } return html.ToString(); }
/// <summary> /// Constructs an element at the specified offset. /// May return null if no element should be constructed. /// </summary> public override VisualLineElement ConstructElement(int offset) { //Match m = FindMatch(offset); // check whether there's a match exactly at offset //if (m.Success && m.Index == 0) { ICSharpCode.AvalonEdit.Document.TextDocument document = CurrentContext.Document; int start = document.GetLineByOffset(offset).Offset; if (frameworkElement == null) { return(null); } if (start == offset) { DocumentLine documentLine = document.GetLineByOffset(offset); int height = (int)frameworkElement.Height;// (int)CurrentContext.VisualLine.Height; // BitmapImage bitmap = bitmapImage;// LoadBitmap(m.Groups[1].Value); ////if (bitmap != null) { // Image image = new Image(); // image.Source = bitmap; // image.Width = bitmap.PixelWidth; // image.Height = height;// bitmap.PixelHeight; // // Pass the length of the match to the 'documentLength' parameter // // of InlineObjectElement. var es = new InlineObjectElementExtended(1 /*m.Length*/, frameworkElement); //es.obs = documentLine.obs; //image.Tag = es; //es.Element.MouseDown += Element_MouseDown; return(es); } //} return(null); }
void SearchDefinition(TextDocument document, BracketSearchResult result) { if (document.GetCharAt(result.OpeningOffset) != '{') return; // get line var documentLine = document.GetLineByOffset(result.OpeningOffset); while (documentLine != null && IsBracketOnly(document, documentLine)) documentLine = documentLine.PreviousLine; if (documentLine != null) { result.DefinitionHeaderOffset = documentLine.Offset; result.DefinitionHeaderLength = documentLine.Length; } }
/// <summary> /// Skips any comments that start at the current offset. /// </summary> /// <param name="document">The document.</param> /// <param name="offset">The offset.</param> /// <returns>The index of the next character after the comments.</returns> private static int SkipComment(TextDocument document, int offset) { if (offset >= document.TextLength - 1) return offset + 1; char current = document.GetCharAt(offset); char next = document.GetCharAt(offset + 1); if (current == '/' && next == '/') { // Skip line comment "//" var line = document.GetLineByOffset(offset); int offsetOfNextLine = line.Offset + line.TotalLength; return offsetOfNextLine; } if (current == '/' && next == '*') { // Skip block comment "/* ... */" offset += 2; while (offset + 1 < document.TextLength) { if (document.GetCharAt(offset) == '*' && document.GetCharAt(offset + 1) == '/') { offset = offset + 2; break; } offset++; } return offset; } return offset + 1; }
/// <summary> /// Marks the region that starts at the given offset. /// </summary> /// <param name="document">The document.</param> /// <param name="offset">The offset.</param> /// <param name="folds">The fold markers.</param> /// <returns>The index of the next character after the region.</returns> private static int MarkRegion(TextDocument document, int offset, List<NewFolding> folds) { if (offset >= document.TextLength) return offset; if (document.GetCharAt(offset) == '#') { int startOffset = offset; offset++; string word = TextUtilities.GetIdentifierAt(document, offset); if (word == "region") { offset += "region".Length; // Find label var line = document.GetLineByOffset(offset); int lineEnd = line.Offset + line.Length; int labelLength = lineEnd - offset; string label = document.GetText(offset, labelLength); label = label.Trim(); if (label.Length == 0) label = "#region"; // Find and mark subregions offset = FindAndMarkRegions(document, lineEnd, folds); if (offset <= document.TextLength) { AddFold(document, folds, startOffset, offset, label); offset++; } } } else { offset++; } return offset; }
/// <summary> /// Gets whether the selection is multi-line. /// </summary> public virtual bool IsMultiline(TextDocument document) { if (document == null) throw new ArgumentNullException("document"); ISegment surroundingSegment = this.SurroundingSegment; if (surroundingSegment == null) return false; int start = surroundingSegment.Offset; int end = start + surroundingSegment.Length; return document.GetLineByOffset(start) != document.GetLineByOffset(end); }