Class: VER::HoverCompletion

Inherits:
Object
  • Object
show all
Defined in:
lib/ver/hover_completion.rb

Overview

TODO: the positioning behaviour is still annoying, for some reason I just can’t get a handle on a changed position. Even using the tk-internal Expose event works only sometimes.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent, options = {}, &completer) ⇒ HoverCompletion

Returns a new instance of HoverCompletion.



9
10
11
12
13
14
15
# File 'lib/ver/hover_completion.rb', line 9

def initialize(parent, options = {}, &completer)
  @parent, @options, @completer = parent, options, completer
  setup_widgets
  setup_keymap
  setup_events
  update
end

Instance Attribute Details

#choicesObject

Returns the value of attribute choices.



7
8
9
# File 'lib/ver/hover_completion.rb', line 7

def choices
  @choices
end

#completerObject

Returns the value of attribute completer.



7
8
9
# File 'lib/ver/hover_completion.rb', line 7

def completer
  @completer
end

#fromObject

Returns the value of attribute from.



7
8
9
# File 'lib/ver/hover_completion.rb', line 7

def from
  @from
end

#listObject (readonly)

Returns the value of attribute list.



6
7
8
# File 'lib/ver/hover_completion.rb', line 6

def list
  @list
end

#optionsObject

Returns the value of attribute options.



7
8
9
# File 'lib/ver/hover_completion.rb', line 7

def options
  @options
end

#parentObject (readonly)

Returns the value of attribute parent.



6
7
8
# File 'lib/ver/hover_completion.rb', line 6

def parent
  @parent
end

#toObject

Returns the value of attribute to.



7
8
9
# File 'lib/ver/hover_completion.rb', line 7

def to
  @to
end

Instance Method Details

#cancelObject



124
125
126
127
# File 'lib/ver/hover_completion.rb', line 124

def cancel
  list.destroy
  parent.focus
end

#continue_completionObject



50
51
52
53
# File 'lib/ver/hover_completion.rb', line 50

def continue_completion
  pick
  options[:continue] ? update : cancel
end

#go_downObject



33
34
35
36
37
38
39
40
# File 'lib/ver/hover_completion.rb', line 33

def go_down
  index = list.curselection.first + 1
  max = list.size

  return unless index < max

  select index
end

#go_upObject



42
43
44
45
46
47
48
# File 'lib/ver/hover_completion.rb', line 42

def go_up
  index = list.curselection.first - 1

  return unless index >= 0

  select index
end

#layoutObject



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/ver/hover_completion.rb', line 88

def layout
  return unless choices && choices.size > 0

  x, y, caret_height =
    parent.tk_caret.values_at(:x, :y, :height)

  height, width= parent.winfo_height, parent.winfo_width
  height -= parent.status.winfo_height


  # use side with most space, east or west
  if x > (width / 2)
    side = 'e'
  else
    side = 'w'
  end

  # use hemisphere with most space
  if y < (height / 3)
    hemisphere = 'n'
    y += caret_height
    height -= y
  elsif y < (height / 2)
    hemisphere = ''
    height -= ((height - y) / 2)
  else
    hemisphere = 's'
    height -= (height - y)
  end

  list.configure height: -1 # fit as many into the list as possible

  # let's place it so we can see how big it is.
  list.place x: x, y: y, height: height, in: parent, anchor: "#{hemisphere}#{side}"
end

#pickObject



60
61
62
63
64
# File 'lib/ver/hover_completion.rb', line 60

def pick
  index = list.curselection.first
  replacement = list.get(index)
  parent.replace(from, to, replacement)
end

#select(index) ⇒ Object



80
81
82
83
84
85
86
# File 'lib/ver/hover_completion.rb', line 80

def select(index)
  list.selection_clear(0, :end)
  list.selection_set(index)
  list.see(index)

  Tk::Event.generate(list, '<<ListboxSelect>>')
end

#setup_eventsObject



28
29
30
31
# File 'lib/ver/hover_completion.rb', line 28

def setup_events
  list.bind('<<ListboxSelect>>'){ layout }
  list.bind('<Expose>'){ layout }
end

#setup_keymapObject



22
23
24
25
26
# File 'lib/ver/hover_completion.rb', line 22

def setup_keymap
  keymap_name = VER.options.keymap
  @list_keymap = Keymap.get(name: keymap_name, receiver: self,
                            widget: list, mode: :hover_completion)
end

#setup_widgetsObject



17
18
19
20
# File 'lib/ver/hover_completion.rb', line 17

def setup_widgets
  @list = Tk::Listbox.new(parent, borderwidth: 0, selectmode: :single)
  @list.focus
end

#submitObject



55
56
57
58
# File 'lib/ver/hover_completion.rb', line 55

def submit
  pick
  cancel
end

#updateObject



66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/ver/hover_completion.rb', line 66

def update
  self.from, self.to, self.choices = completer.call
  # @longest_choice = choices.map{|choice| choice.size }.max

  if choices && choices.size > 0
    list.value = choices
    select 0

    submit if choices.size == 1
  else
    cancel
  end
end