Microsoft.R.Editor.TextViewExtensions.GetIdentifierUnderCaret C# (CSharp) Method

GetIdentifierUnderCaret() public static method

Extracts identifier sequence at the caret location. Fetches parts of 'abc$def' rather than tne entire expression. If there is selection, returns complete selected item as is.
public static GetIdentifierUnderCaret ( this textView, Span &span ) : string
textView this
span Span
return string
        public static string GetIdentifierUnderCaret(this ITextView textView, out Span span) {
            if (!textView.Caret.InVirtualSpace) {
                if (textView.Selection.Mode == TextSelectionMode.Stream) {
                    SnapshotPoint position = textView.Caret.Position.BufferPosition;
                    ITextSnapshotLine line = null;
                    int caretPosition = -1;
                    if (textView.Selection.SelectedSpans.Count > 0) {
                        span = textView.Selection.SelectedSpans[0];
                        if (span.Length > 0) {
                            return textView.TextBuffer.CurrentSnapshot.GetText(span);
                        }
                    }
                    line = line ?? position.GetContainingLine();
                    caretPosition = caretPosition >= 0 ? caretPosition : position.Position;
                    var item = GetItemAtPosition(line, caretPosition, x => x == RTokenType.Identifier, out span);
                    if (string.IsNullOrEmpty(item)) {
                        item = textView.GetItemBeforeCaret(out span, x => x == RTokenType.Identifier);
                    }
                    return item;
                }
            }
            span = Span.FromBounds(0, 0);
            return string.Empty;
        }