Skip to content

Commit 46aa269

Browse files
committed
Cut out the autocomplete callback
1 parent 458cb8a commit 46aa269

File tree

1 file changed

+33
-31
lines changed

1 file changed

+33
-31
lines changed

lib/reline.rb

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -177,41 +177,43 @@ def get_screen_size
177177
Reline::IOGate.get_screen_size
178178
end
179179

180+
DEFAULT_DIALOG_PROC_AUTOCOMPLETE = ->() {
181+
# autocomplete
182+
if just_cursor_moving and completion_journey_data.nil?
183+
# Auto complete starts only when edited
184+
return nil
185+
end
186+
pre, target, post= retrieve_completion_block(true)
187+
if target.nil? or target.empty?# or target.size <= 3
188+
return nil
189+
end
190+
if completion_journey_data and completion_journey_data.list
191+
result = completion_journey_data.list.dup
192+
result.shift
193+
pointer = completion_journey_data.pointer - 1
194+
else
195+
result = call_completion_proc_with_checking_args(pre, target, post)
196+
pointer = nil
197+
end
198+
if result and result.size == 1 and result[0] == target
199+
result = nil
200+
end
201+
target_width = Reline::Unicode.calculate_width(target)
202+
x = cursor_pos.x - target_width
203+
if x < 0
204+
x = screen_width + x
205+
y = -1
206+
else
207+
y = 0
208+
end
209+
[Reline::CursorPos.new(x, y), result, pointer]
210+
}
211+
180212
def readmultiline(prompt = '', add_hist = false, &confirm_multiline_termination)
181213
unless confirm_multiline_termination
182214
raise ArgumentError.new('#readmultiline needs block to confirm multiline termination')
183215
end
184-
@dialog_proc = ->() {
185-
# autocomplete
186-
if just_cursor_moving and completion_journey_data.nil?
187-
# Auto complete starts only when edited
188-
return nil
189-
end
190-
pre, target, post= retrieve_completion_block(true)
191-
if target.nil? or target.empty?
192-
return nil
193-
end
194-
if completion_journey_data and completion_journey_data.list
195-
result = completion_journey_data.list.dup
196-
result.shift
197-
pointer = completion_journey_data.pointer - 1
198-
else
199-
result = call_completion_proc_with_checking_args(pre, target, post)
200-
pointer = nil
201-
end
202-
if result and result.size == 1 and result[0] == target
203-
result = nil
204-
end
205-
target_width = Reline::Unicode.calculate_width(target)
206-
x = cursor_pos.x - target_width
207-
if x < 0
208-
x = screen_width + x
209-
y = -1
210-
else
211-
y = 0
212-
end
213-
[Reline::CursorPos.new(x, y), result, pointer]
214-
}
216+
@dialog_proc = DEFAULT_DIALOG_PROC_AUTOCOMPLETE
215217
inner_readline(prompt, add_hist, true, &confirm_multiline_termination)
216218

217219
whole_buffer = line_editor.whole_buffer.dup

0 commit comments

Comments
 (0)