Skip to content
Kward Search API index

Class: Kward::PromptInterface::EditorIndentNavigation

Inherits:
Object
  • Object
show all
Defined in:
lib/kward/prompt_interface/editor/indent_navigation.rb

Overview

Indentation-based navigation over editor lines.

Instance Method Summary collapse

Constructor Details

#initialize(lines) ⇒ EditorIndentNavigation

Returns a new instance of EditorIndentNavigation.



7
8
9
# File 'lib/kward/prompt_interface/editor/indent_navigation.rb', line 7

def initialize(lines)
  @lines = lines
end

Instance Method Details

#empty_line?(line_index) ⇒ Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/kward/prompt_interface/editor/indent_navigation.rb', line 15

def empty_line?(line_index)
  @lines[line_index].to_s.strip.empty?
end

#indentation_level_for_line(line_index) ⇒ Object



11
12
13
# File 'lib/kward/prompt_interface/editor/indent_navigation.rb', line 11

def indentation_level_for_line(line_index)
  @lines[line_index].to_s.index(/\S/) || 0
end

#next_line(current_line, current_indentation) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/kward/prompt_interface/editor/indent_navigation.rb', line 19

def next_line(current_line, current_indentation)
  end_line = @lines.length - 1
  return nil if current_line == end_line

  next_line = current_line + 1
  jumping_over_space = indentation_level_for_line(next_line) != current_indentation || empty_line?(next_line)

  (next_line..end_line).each do |line_index|
    indentation = indentation_level_for_line(line_index)
    if jumping_over_space && indentation == current_indentation && !empty_line?(line_index)
      return line_index
    elsif !jumping_over_space && (indentation != current_indentation || empty_line?(line_index))
      return line_index - 1
    elsif !jumping_over_space && indentation == current_indentation && line_index == end_line
      return line_index
    end
  end

  nil
end

#previous_line(current_line, current_indentation) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/kward/prompt_interface/editor/indent_navigation.rb', line 40

def previous_line(current_line, current_indentation)
  return nil if current_line.zero?

  previous_line = current_line - 1
  jumping_over_space = indentation_level_for_line(previous_line) != current_indentation || empty_line?(previous_line)

  previous_line.downto(0) do |line_index|
    indentation = indentation_level_for_line(line_index)
    if jumping_over_space && indentation == current_indentation && !empty_line?(line_index)
      return line_index
    elsif !jumping_over_space && (indentation != current_indentation || empty_line?(line_index))
      return line_index + 1
    elsif !jumping_over_space && indentation == current_indentation && line_index.zero?
      return line_index
    end
  end

  nil
end