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 Feb 17, 2024
1 parent 17bf7c5 commit 9d33fe1
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
17 changes: 14 additions & 3 deletions lib/irb/completion.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,14 @@ def retrieve_files_to_require_from_load_path
)
end

def command_completions(preposing, target)
if preposing.empty? && !target.empty?
IRB::ExtendCommandBundle.command_names.select { _1.start_with?(target) }
else
[]
end
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 +111,11 @@ 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 @@ -181,7 +191,8 @@ def completion_candidates(preposing, target, postposing, bind:)
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 = command_completions(preposing || '', target)
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/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 9d33fe1

Please sign in to comment.