Skip to content

Commit 1959e22

Browse files
committed
Drop prompt list cache when num of lines is changed
1 parent c54d6a5 commit 1959e22

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

bin/multiline_repl

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,23 @@
11
#!/usr/bin/env ruby
22

33
require 'reline'
4+
require 'optparse'
45
require_relative 'termination_checker'
56

7+
opt = OptionParser.new
8+
opt.on('--prompt-list-cache-timeout VAL') { |v|
9+
Reline::LineEditor.__send__(:remove_const, :PROMPT_LIST_CACHE_TIMEOUT)
10+
Reline::LineEditor::PROMPT_LIST_CACHE_TIMEOUT = v.to_f
11+
}
12+
opt.on('--dynamic-prompt') {
13+
Reline.prompt_proc = proc { |lines|
14+
lines.each_with_index.map { |l, i|
15+
'[%04d]> ' % i
16+
}
17+
}
18+
}
19+
opt.parse!(ARGV)
20+
621
begin
722
stty_save = `stty -g`.chomp
823
rescue

lib/reline/line_editor.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ module CompletionState
5050
CompletionJourneyData = Struct.new('CompletionJourneyData', :preposing, :postposing, :list, :pointer)
5151
MenuInfo = Struct.new('MenuInfo', :target, :list)
5252

53+
PROMPT_LIST_CACHE_TIMEOUT = 0.5
54+
5355
def initialize(config, encoding)
5456
@config = config
5557
@completion_append_character = ''
@@ -78,7 +80,7 @@ def simplified_rendering?
7880
end
7981
return [prompt, calculate_width(prompt, true), [prompt] * buffer.size] if simplified_rendering?
8082
if @prompt_proc
81-
if @cached_prompt_list and Time.now.to_f < (@prompt_cache_time + 0.5)
83+
if @cached_prompt_list and Time.now.to_f < (@prompt_cache_time + PROMPT_LIST_CACHE_TIMEOUT) and buffer.size == @cached_prompt_list.size
8284
prompt_list = @cached_prompt_list
8385
else
8486
prompt_list = @cached_prompt_list = @prompt_proc.(buffer)

test/reline/yamatanooroti/test_rendering.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,18 @@ def test_binding_for_vi_movement_mode
398398
EOC
399399
end
400400

401+
def test_prompt_list_caching
402+
start_terminal(5, 30, %W{ruby -I#{@pwd}/lib #{@pwd}/bin/multiline_repl --prompt-list-cache-timeout 10 --dynamic-prompt}, startup_message: 'Multiline REPL.')
403+
write("def hoge\n 3\nend")
404+
close
405+
assert_screen(<<~EOC)
406+
Multiline REPL.
407+
[0000]> def hoge
408+
[0001]> 3
409+
[0002]> end
410+
EOC
411+
end
412+
401413
private def write_inputrc(content)
402414
File.open(@inputrc_file, 'w') do |f|
403415
f.write content

0 commit comments

Comments
 (0)