Class: Textbringer::CompletionPopup

Inherits:
Object
  • Object
show all
Defined in:
lib/textbringer/completion_popup.rb

Constant Summary collapse

MAX_VISIBLE_ITEMS =
10
MIN_WIDTH =
20
MAX_WIDTH =
60

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCompletionPopup

Returns a new instance of CompletionPopup.



13
14
15
16
17
18
19
# File 'lib/textbringer/completion_popup.rb', line 13

def initialize
  @floating_window = nil
  @items = []
  @selected_index = 0
  @start_point = nil
  @prefix = ""
end

Instance Attribute Details

#itemsObject (readonly)

Returns the value of attribute items.



7
8
9
# File 'lib/textbringer/completion_popup.rb', line 7

def items
  @items
end

#selected_indexObject (readonly)

Returns the value of attribute selected_index.



7
8
9
# File 'lib/textbringer/completion_popup.rb', line 7

def selected_index
  @selected_index
end

#start_pointObject (readonly)

Returns the value of attribute start_point.



7
8
9
# File 'lib/textbringer/completion_popup.rb', line 7

def start_point
  @start_point
end

Class Method Details

.instanceObject



9
10
11
# File 'lib/textbringer/completion_popup.rb', line 9

def self.instance
  @instance ||= new
end

Instance Method Details

#acceptObject



67
68
69
70
71
72
# File 'lib/textbringer/completion_popup.rb', line 67

def accept
  return nil unless visible? && !@items.empty?
  item = current_item
  close
  item
end

#cancelObject



74
75
76
77
# File 'lib/textbringer/completion_popup.rb', line 74

def cancel
  close
  nil
end

#closeObject



38
39
40
41
42
43
44
45
46
47
# File 'lib/textbringer/completion_popup.rb', line 38

def close
  if @floating_window
    @floating_window.close
    @floating_window = nil
  end
  @items = []
  @selected_index = 0
  @start_point = nil
  @prefix = ""
end

#current_itemObject



79
80
81
82
# File 'lib/textbringer/completion_popup.rb', line 79

def current_item
  return nil if @items.empty?
  @items[@selected_index]
end

#hideObject



34
35
36
# File 'lib/textbringer/completion_popup.rb', line 34

def hide
  @floating_window&.hide
end

#select_nextObject



53
54
55
56
57
58
# File 'lib/textbringer/completion_popup.rb', line 53

def select_next
  return unless visible? && !@items.empty?
  @selected_index = (@selected_index + 1) % @items.size
  render
  @floating_window.redisplay
end

#select_previousObject



60
61
62
63
64
65
# File 'lib/textbringer/completion_popup.rb', line 60

def select_previous
  return unless visible? && !@items.empty?
  @selected_index = (@selected_index - 1) % @items.size
  render
  @floating_window.redisplay
end

#show(items:, start_point:, prefix: "") ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/textbringer/completion_popup.rb', line 21

def show(items:, start_point:, prefix: "")
  @items = items
  @start_point = start_point
  @prefix = prefix
  @selected_index = 0

  return if @items.empty?

  create_or_update_window
  render
  @floating_window.show
end

#visible?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/textbringer/completion_popup.rb', line 49

def visible?
  @floating_window&.visible? || false
end