Skip to content

Commit

Permalink
Check RegexpCompletor then InputCompletor
Browse files Browse the repository at this point in the history
  • Loading branch information
tompng committed Oct 14, 2023
1 parent 8d7b130 commit f729d54
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 19 deletions.
26 changes: 13 additions & 13 deletions lib/katakata_irb/completor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ def self.setup
KatakataIrb::Completor.prev_analyze_result = result
candidates = case result
in [:require | :require_relative => method, name]
if IRB.const_defined? :InputCompletor # IRB::VERSION <= 1.8.1
path_completor = IRB::InputCompletor
elsif IRB.const_defined? :RegexpCompletor # IRB::VERSION >= 1.8.2
if IRB.const_defined? :RegexpCompletor # IRB::VERSION >= 1.8.2
path_completor = IRB::RegexpCompletor.new
elsif IRB.const_defined? :InputCompletor # IRB::VERSION <= 1.8.1
path_completor = IRB::InputCompletor
end
if !path_completor
[]
Expand Down Expand Up @@ -103,7 +103,16 @@ def self.setup
end
end

if IRB.const_defined? :InputCompletor # IRB::VERSION <= 1.8.1
if IRB.const_defined? :RegexpCompletor # IRB::VERSION >= 1.8.2
IRB::RegexpCompletor.class_eval do
define_method :completion_candidates do |preposing, target, postposing, bind:|
completion_proc.call(target, preposing, postposing)
end
define_method :doc_namespace do |_preposing, matched, _postposing, bind:|
doc_namespace_proc.call matched
end
end
elsif IRB.const_defined? :InputCompletor # IRB::VERSION <= 1.8.1
IRB::InputCompletor::CompletionProc.define_singleton_method :call do |*args|
completion_proc.call(*args)
end
Expand All @@ -115,15 +124,6 @@ def self.setup
end
end
)
elsif IRB.const_defined? :RegexpCompletor # IRB::VERSION >= 1.8.2
IRB::RegexpCompletor.class_eval do
define_method :completion_candidates do |preposing, target, postposing, bind:|
completion_proc.call(target, preposing, postposing)
end
define_method :doc_namespace do |_preposing, matched, _postposing, bind:|
doc_namespace_proc.call matched
end
end
else
puts 'Cannot activate katakata_irb'
end
Expand Down
23 changes: 17 additions & 6 deletions test/test_katakata_irb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,23 @@ def output.puts(*)
end

def test_irb_input_completor_compatibility
completion = IRB::InputCompletor.retrieve_completion_data 'at_exi', bind: binding, doc_namespace: false
assert_equal ['at_exit'], completion

KatakataIrb::Completor.prev_analyze_result = KatakataIrb::Completor.analyze 'a = 1.to_c; a.abs', binding
document = IRB::InputCompletor.retrieve_completion_data 'a.abs', bind: binding, doc_namespace: true
assert_equal 'Complex#abs', document
if IRB.const_defined? :RegexpCompletor # IRB::VERSION >= 1.8.2
context = Object.new
def context.workspace; self; end
def context.binding; Kernel.binding; end
IRB.conf[:MAIN_CONTEXT] = context
completion = IRB::RegexpCompletor.new.completion_candidates '', 'at_exi', '', bind: binding
assert_equal ['at_exit'], completion
KatakataIrb::Completor.prev_analyze_result = KatakataIrb::Completor.analyze 'a = 1.to_c; a.abs', binding
document = IRB::RegexpCompletor.new.doc_namespace 'a=1.to_c; ', 'a.abs', '', bind: binding
assert_equal 'Complex#abs', document
elsif IRB.const_defined? :InputCompletor # IRB::VERSION <= 1.8.1
completion = IRB::InputCompletor.retrieve_completion_data 'at_exi', bind: binding, doc_namespace: false
assert_equal ['at_exit'], completion
KatakataIrb::Completor.prev_analyze_result = KatakataIrb::Completor.analyze 'a = 1.to_c; a.abs', binding
document = IRB::InputCompletor.retrieve_completion_data 'a.abs', bind: binding, doc_namespace: true
assert_equal 'Complex#abs', document
end
end

SYNTAX_TEST_CODE_3_1_PLUS = <<~'RUBY'
Expand Down

0 comments on commit f729d54

Please sign in to comment.