Skip to content

Instantly share code, notes, and snippets.

Created December 17, 2012 18:29
Show Gist options
  • Save anonymous/4320643 to your computer and use it in GitHub Desktop.
Save anonymous/4320643 to your computer and use it in GitHub Desktop.
Get the text of an ITextViewLine as it appears in the user editable document
/// <summary>
/// This will get the text of the ITextView line as it appears in the actual user editable
/// document.
/// </summary>
public static bool TryGetText(ITextView textView, ITextViewLine textViewLine, out string text)
{
var extent = textViewLine.Extent;
var bufferGraph = textView.BufferGraph;
try
{
var collection = bufferGraph.MapDownToSnapshot(extent, SpanTrackingMode.EdgeInclusive, textView.TextSnapshot);
var span = new SnapshotSpan(collection[0].Start, collection[collection.Count - 1].End);
text = span.ToString();
return true;
}
catch
{
text = null;
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment