Skip to content

Commit

Permalink
Implement non-method command name completion
Browse files Browse the repository at this point in the history
  • Loading branch information
tompng committed Dec 27, 2023
1 parent 2b67566 commit 66303ef
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
13 changes: 10 additions & 3 deletions lib/irb/completion.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ def retrieve_files_to_require_from_load_path
)
end

def command_completions(preposing, target)
return [] unless preposing && preposing.match?(/\A\s*\z/)
IRB::ExtendCommandBundle.command_names.select { _1.start_with?(target) }
end

def retrieve_files_to_require_relative_from_current_dir
@files_from_current_dir ||= Dir.glob("**/*.{rb,#{RbConfig::CONFIG['DLEXT']}}", base: '.').map { |path|
path.sub(/\.(rb|#{RbConfig::CONFIG['DLEXT']})\z/, '')
Expand All @@ -103,9 +108,10 @@ def inspect
end

def completion_candidates(preposing, target, _postposing, bind:)
commands = command_completions(preposing, target)
result = ReplTypeCompletor.analyze(preposing + target, binding: bind, filename: @context.irb_path)
return [] unless result
result.completion_candidates.map { target + _1 }
return commands unless result
commands | result.completion_candidates.map { target + _1 }
end

def doc_namespace(preposing, matched, _postposing, bind:)
Expand Down Expand Up @@ -177,11 +183,12 @@ def complete_require_path(target, preposing, postposing)
end

def completion_candidates(preposing, target, postposing, bind:)
commands = command_completions(preposing, target)
if preposing && postposing
result = complete_require_path(target, preposing, postposing)
return result if result
end
retrieve_completion_data(target, bind: bind, doc_namespace: false).compact.map{ |i| i.encode(Encoding.default_external) }
commands | retrieve_completion_data(target, bind: bind, doc_namespace: false).compact.map{ |i| i.encode(Encoding.default_external) }
end

def doc_namespace(_preposing, matched, _postposing, bind:)
Expand Down
7 changes: 7 additions & 0 deletions test/irb/test_completion.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ def doc_namespace(target, bind)
IRB::RegexpCompletor.new.doc_namespace('', target, '', bind: bind)
end

class CommandCompletionTest < CompletionTest
def test_command_completion
assert_include(IRB::RegexpCompletor.new.completion_candidates(' ', 'show_s', '', bind: binding), 'show_source')
assert_not_include(IRB::RegexpCompletor.new.completion_candidates(';', 'show_s', '', bind: binding), 'show_source')
end
end

class MethodCompletionTest < CompletionTest
def test_complete_string
assert_include(completion_candidates("'foo'.up", binding), "'foo'.upcase")
Expand Down
7 changes: 7 additions & 0 deletions test/irb/test_type_completor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
return
end

require 'irb/context'
require 'irb/extend-command'
require 'irb/completion'
require 'tempfile'
require_relative './helper'
Expand Down Expand Up @@ -54,6 +56,11 @@ def test_empty_completion
assert_equal [], candidates
assert_doc_namespace('(', ')', nil)
end

def test_command_completion
assert_include(@completor.completion_candidates(' ', 'show_s', '', bind: binding), 'show_source')
assert_not_include(@completor.completion_candidates(';', 'show_s', '', bind: binding), 'show_source')
end
end

class TypeCompletorIntegrationTest < IntegrationTestCase
Expand Down

0 comments on commit 66303ef

Please sign in to comment.