override fun findWordAtOrFollowingCursor()

in vim-engine/src/main/kotlin/com/maddyhome/idea/vim/api/VimSearchHelperBase.kt [112:161]


  override fun findWordAtOrFollowingCursor(editor: VimEditor, offset: Int, isBigWord: Boolean): TextRange? {
    val chars = editor.text()
    val line = editor.offsetToBufferPosition(offset).line
    val stop = editor.getLineEndOffset(line, true)

    val pos = offset

    if (chars.isEmpty() || chars.length <= pos) return null

    var start = pos
    val types = arrayOf(
      CharacterHelper.CharacterType.KEYWORD,
      CharacterHelper.CharacterType.PUNCTUATION
    )
    for (i in 0..1) {
      start = pos
      val type = charType(editor, chars[start], isBigWord)
      if (type == types[i]) {
        // Search back for start of word
        while (start > 0 && charType(editor, chars[start - 1], isBigWord) == types[i]) {
          start--
        }
      } else {
        // Search forward for start of word
        while (start < stop && charType(editor, chars[start], isBigWord) != types[i]) {
          start++
        }
      }

      if (start != stop) {
        break
      }
    }

    if (start == stop) {
      return null
    }

    // Special case 1 character words because 'findNextWordEnd' returns one to many chars
    val end = if (start < stop &&
      (start >= chars.length - 1 ||
        charType(editor, chars[start + 1], isBigWord) != CharacterHelper.CharacterType.KEYWORD)
    ) {
      start + 1
    } else {
      injector.searchHelper.findNextWordEnd(editor, start, 1, isBigWord, false) + 1
    }

    return TextRange(start, end)
  }