Skip to content
Kward Search API index

Module: Kward::TextBoundary

Defined in:
lib/kward/text_boundary.rb

Overview

Small text navigation helpers shared by composer and editor buffers.

Class Method Summary collapse

Class Method Details

.next_word_boundary(text, index) ⇒ Object



14
15
16
17
18
19
# File 'lib/kward/text_boundary.rb', line 14

def next_word_boundary(text, index)
  cursor = index.to_i
  cursor += 1 while cursor < text.length && word_separator?(text[cursor])
  cursor += 1 while cursor < text.length && !word_separator?(text[cursor])
  cursor
end

.previous_word_boundary(text, index) ⇒ Object



7
8
9
10
11
12
# File 'lib/kward/text_boundary.rb', line 7

def previous_word_boundary(text, index)
  cursor = index.to_i
  cursor -= 1 while cursor.positive? && word_separator?(text[cursor - 1])
  cursor -= 1 while cursor.positive? && !word_separator?(text[cursor - 1])
  cursor
end

.word_separator?(char) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/kward/text_boundary.rb', line 21

def word_separator?(char)
  char.to_s.match?(/\s/)
end