public ITagger <T> CreateTagger <T>(ITextView textView, ITextBuffer buffer) where T : ITag { //provide highlighting only on the top buffer if (textView.TextBuffer != buffer) { return(null); } // HighlightWordCommands.Instance.TextStructureNavigatorSelectorService = TextStructureNavigatorSelector; ITextStructureNavigator textStructureNavigator = TextStructureNavigatorSelector.GetTextStructureNavigator(buffer); var tagger = new HighlightWordTagger(textView, buffer, TextSearchService, textStructureNavigator); return(tagger as ITagger <T>); }
/// <summary> /// Given an IWpfTextView, find the position of the caret and report its column /// number. The column number is 0-based /// </summary> /// <param name="textView">The text view containing the caret</param> /// <returns>The column number of the caret's position. When the caret is at the /// leftmost column, the return value is zero.</returns> private static string GetCaretWord(IWpfTextView textView, ITextStructureNavigator textStructureNavigator) { var caretPosition = textView.Caret.Position; var sourceBuffer = textView.TextBuffer; SnapshotPoint?point = caretPosition.Point.GetPoint(sourceBuffer, caretPosition.Affinity); if (!point.HasValue) { return(null); } var snapshotPoint = point.Value; TextExtent word = textStructureNavigator.GetExtentOfWord(snapshotPoint); var foundWord = true; if (!HighlightWordTagger.WordExtentIsValid(snapshotPoint, word)) { //Before we retry, make sure it is worthwhile if (word.Span.Start != snapshotPoint || snapshotPoint == snapshotPoint.GetContainingLine().Start || char.IsWhiteSpace((snapshotPoint - 1).GetChar())) { foundWord = false; } else { // Try again, one character previous. //If the caret is at the end of a word, pick up the word. word = textStructureNavigator.GetExtentOfWord(snapshotPoint - 1); //If the word still isn't valid, we're done if (!HighlightWordTagger.WordExtentIsValid(snapshotPoint, word)) { foundWord = false; } } } if (!foundWord) { return(null); } return(word.Span.GetText()); }