Class: TTY2::Reader::Completer Private

Inherits:
Object
  • Object
show all
Defined in:
lib/tty2/reader/completer.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Responsible for word completion

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(handler: nil, suffix: "", cycling: true) ⇒ Completer

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Create a Completer instance



29
30
31
32
33
34
35
36
37
# File 'lib/tty2/reader/completer.rb', line 29

def initialize(handler: nil, suffix: "", cycling: true)
  @handler = handler
  @suffix = suffix
  @cycling = cycling
  @completions = Completions.new
  @show_initial = false
  @word = ""
  @completed = false
end

Instance Attribute Details

#completionsObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The completion suggestions



12
13
14
# File 'lib/tty2/reader/completer.rb', line 12

def completions
  @completions
end

#cyclingObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Enable / Disable Cycling



21
22
23
# File 'lib/tty2/reader/completer.rb', line 21

def cycling
  @cycling
end

#handlerObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The handler for finding word completion suggestions



15
16
17
# File 'lib/tty2/reader/completer.rb', line 15

def handler
  @handler
end

#suffixObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The suffix to add to suggested word completion



18
19
20
# File 'lib/tty2/reader/completer.rb', line 18

def suffix
  @suffix
end

#wordObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The word to complete



24
25
26
# File 'lib/tty2/reader/completer.rb', line 24

def word
  @word
end

Instance Method Details

#cancel(line) ⇒ Object

Cancel completion suggestion and revert to the initial word

Parameters:

  • line (Line)

    the line to cancel word completion in



182
183
184
185
186
187
188
# File 'lib/tty2/reader/completer.rb', line 182

def cancel(line)
  return if completions.empty?

  completed_word = @show_initial ? word : completions.get
  line.remove(completed_word.length)
  line.insert(word)
end

#complete(line, direction: :next, initial: false) ⇒ String?

Find a suggestion to complete a word

Parameters:

  • line (Line)

    the line to complete a word in

  • direction (Symbol) (defaults to: :next)

    the direction in which to cycle through completions

  • initial (Boolean) (defaults to: false)

    whether to find initial or next completion suggestion

Returns:

  • (String, nil)

    the completed word or nil when no suggestion is found



52
53
54
55
56
57
58
# File 'lib/tty2/reader/completer.rb', line 52

def complete(line, direction: :next, initial: false)
  if initial || @completed
    complete_initial(line, direction: direction)
  elsif @cycling
    complete_next(line, direction: direction)
  end
end

#complete_initial(line, direction: :next) ⇒ String?

Complete the initial word

Parameters:

  • line (Line)

    the line to complete a word in

  • direction (Symbol) (defaults to: :next)

    the direction in which to cycle through completions

Returns:

  • (String, nil)

    the completed word or nil when no suggestion is found



71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/tty2/reader/completer.rb', line 71

def complete_initial(line, direction: :next)
  @completed = false
  completed_word = complete_word(line)
  return if @completions.empty?
  if @cycling && completions.size > 1
    @word = line.word_to_complete
    position = word.length
    completions.previous if direction == :previous
    completed_word = completions.get
    line.insert(completed_word[position..-1])
  end
  completed_word
end

#complete_next(line, direction: :next) ⇒ String?

Complete a word with the next suggestion from completions

Parameters:

  • line (Line)

    the line to complete a word in

  • direction (Symbol) (defaults to: :next)

    the direction in which to cycle through completions

Returns:

  • (String, nil)

    the completed word or nil when no suggestion is found



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
123
124
125
# File 'lib/tty2/reader/completer.rb', line 96

def complete_next(line, direction: :next)
  return if completions.size < 2

  previous_suggestion = completions.get
  first_or_last = direction == :previous ? :first? : :last?
  if completions.send(first_or_last) && !@show_initial
    @show_initial = true
    completed_word = word
  else
    if @show_initial
      @show_initial = false
      previous_suggestion = word
    end
    completions.send(direction)
    completed_word = completions.get
  end

  length_to_remove = previous_suggestion.length

  line.remove(length_to_remove)
  #Due to a suspected timeing issue, verify all chars have actually been removed
  if line.word_to_complete.length == 0
    line.insert(completed_word)
    completed_word
  else
    completions.previous
    return
  end

end

#complete_word(line) ⇒ String

Find suggestions and complete the current word as far as it is unambigous

Parameters:

  • line (Line)

    the line to complete a word in

Returns:

  • (String)

    the completed word



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/tty2/reader/completer.rb', line 152

def complete_word(line)
  load_completions(line)

  return if completions.empty?

  position = @word.length
  if completions.size > 1
    char = completions.first[position]
    while completions.all? { |candidate| candidate[position] == char }
      line.insert(char)
      @word = line.word_to_complete
      position = @word.length
      char = completions.first[position]
    end
    completed_word = word
  elsif completions.size == 1
    completed_word = completions.first
    line.insert(completions.first[position..-1] + suffix)
    @completed = true
  end

  completed_word
end

#load_completions(line) ⇒ Object

Get suggestions and populate completions

Parameters:

  • line (Line)

    the line to complete a word in



133
134
135
136
137
138
139
140
141
# File 'lib/tty2/reader/completer.rb', line 133

def load_completions(line)
  @word = line.word_to_complete
  context = line.subtext[0, line.word_start_pos]
  suggestions = handler.(word, context)
  suggestions = suggestions.grep(/^#{Regexp.escape(@word)}/)
  completions.clear
  return if suggestions.empty?
  completions.concat(suggestions)
end