         let string = String(mentionsTextView.text[startIndex ..< endIndex])
 
         var textBeforeTrigger = " "
-
         let rangeTuple = string.range(of: triggers, options: .backwards)
-
         let location = rangeTuple.range.location
+
+        mentionEnabled = false
         let trigger = rangeTuple.foundString
 
-        mentionEnabled = false
-
         if location != NSNotFound {
-            mentionEnabled = location == 0
-
-            if location > 0 {
-                // Determine whether or not a space exists before the triggter.
-                // (in the case of an @ trigger this avoids showing the mention list for an email address)
-                let startIndex = mentionsTextView.text.index(mentionsTextView.text.startIndex, offsetBy: location - 1)
-                let endIndex = mentionsTextView.text.index(startIndex, offsetBy: 1)
-                textBeforeTrigger = String(mentionsTextView.text[startIndex ..< endIndex])
-                mentionEnabled = textBeforeTrigger == " " || textBeforeTrigger == "\n"
-            }
+            (mentionEnabled, textBeforeTrigger) = mentionsTextView.text.isMentionEnabledAt(location)
         }
 
         if mentionEnabled {
             var mentionString: String = ""
             if searchSpaces {
                 let startIndex = mentionsTextView.text.index(mentionsTextView.text.startIndex, offsetBy: location)
-                let endIndex = mentionsTextView.text.index(startIndex, offsetBy: textView.selectedRange.location - location + textView.selectedRange.length)
+                let endIndex = mentionsTextView.text.index(startIndex, offsetBy: NSMaxRange(textView.selectedRange) - location)
                 mentionString = String(mentionsTextView.text[startIndex ..< endIndex])
             } else if let stringBeingTyped = string.components(separatedBy: textBeforeTrigger).last,
                 let stringForMention = stringBeingTyped.components(separatedBy: " ").last,

                                                             options: .backwards,
                                                             range: NSRange(location: 0,
                                                                            length: NSMaxRange(mentionsTextView.selectedRange)))
                guard rangeTuple.range.location != NSNotFound else { return }

                let location: Int = rangeTuple.range.location

                if location + 1 >= mentionsTextView.text.utf16.count { return }

                let startIndex = mentionsTextView.text.index(mentionsTextView.text.startIndex, offsetBy: location)
                let endIndex = mentionsTextView.text.index(startIndex, offsetBy: 1)

    private func clearMention() -> (Mention?) -> Bool {
        return { mention in
            guard let mention = mention else { return false }
            self.mentions = self.mentions |> remove([mention])
            self.mentionsTextView.attributedText
                |> apply(self.defaultTextAttributes, range: mention.range)

            return true
        }
