Skip to content

Commit

Permalink
Drop prompt list cache when num of lines is changed
Browse files Browse the repository at this point in the history
  • Loading branch information
aycabta committed Nov 20, 2020
1 parent c54d6a5 commit 1959e22
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
15 changes: 15 additions & 0 deletions bin/multiline_repl
@@ -1,8 +1,23 @@
#!/usr/bin/env ruby

require 'reline'
require 'optparse'
require_relative 'termination_checker'

opt = OptionParser.new
opt.on('--prompt-list-cache-timeout VAL') { |v|
Reline::LineEditor.__send__(:remove_const, :PROMPT_LIST_CACHE_TIMEOUT)
Reline::LineEditor::PROMPT_LIST_CACHE_TIMEOUT = v.to_f
}
opt.on('--dynamic-prompt') {
Reline.prompt_proc = proc { |lines|
lines.each_with_index.map { |l, i|
'[%04d]> ' % i
}
}
}
opt.parse!(ARGV)

begin
stty_save = `stty -g`.chomp
rescue
Expand Down
4 changes: 3 additions & 1 deletion lib/reline/line_editor.rb
Expand Up @@ -50,6 +50,8 @@ module CompletionState
CompletionJourneyData = Struct.new('CompletionJourneyData', :preposing, :postposing, :list, :pointer)
MenuInfo = Struct.new('MenuInfo', :target, :list)

PROMPT_LIST_CACHE_TIMEOUT = 0.5

def initialize(config, encoding)
@config = config
@completion_append_character = ''
Expand Down Expand Up @@ -78,7 +80,7 @@ def simplified_rendering?
end
return [prompt, calculate_width(prompt, true), [prompt] * buffer.size] if simplified_rendering?
if @prompt_proc
if @cached_prompt_list and Time.now.to_f < (@prompt_cache_time + 0.5)
if @cached_prompt_list and Time.now.to_f < (@prompt_cache_time + PROMPT_LIST_CACHE_TIMEOUT) and buffer.size == @cached_prompt_list.size
prompt_list = @cached_prompt_list
else
prompt_list = @cached_prompt_list = @prompt_proc.(buffer)
Expand Down
12 changes: 12 additions & 0 deletions test/reline/yamatanooroti/test_rendering.rb
Expand Up @@ -398,6 +398,18 @@ def test_binding_for_vi_movement_mode
EOC
end

def test_prompt_list_caching
start_terminal(5, 30, %W{ruby -I#{@pwd}/lib #{@pwd}/bin/multiline_repl --prompt-list-cache-timeout 10 --dynamic-prompt}, startup_message: 'Multiline REPL.')
write("def hoge\n 3\nend")
close
assert_screen(<<~EOC)
Multiline REPL.
[0000]> def hoge
[0001]> 3
[0002]> end
EOC
end

private def write_inputrc(content)
File.open(@inputrc_file, 'w') do |f|
f.write content
Expand Down

0 comments on commit 1959e22

Please sign in to comment.