Beispiel #1
0
 /// <summary>
 /// 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.
 /// </summary>
 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);
 }