@@ -33,6 +33,8 @@ def defined? do
3333 yield
3434 ]
3535
36+ HELP_COMMAND_PREPOSING = /\A help\s +/
37+
3638 def completion_candidates ( preposing , target , postposing , bind :)
3739 raise NotImplementedError
3840 end
@@ -86,8 +88,8 @@ def retrieve_files_to_require_from_load_path
8688 )
8789 end
8890
89- def command_completions ( preposing , target )
90- if preposing . empty? && !target . empty?
91+ def command_candidates ( target )
92+ if !target . empty?
9193 IRB ::Command . command_names . select { _1 . start_with? ( target ) }
9294 else
9395 [ ]
@@ -111,8 +113,18 @@ def inspect
111113 end
112114
113115 def completion_candidates ( preposing , target , _postposing , bind :)
114- commands = command_completions ( preposing , target )
116+ # When completing the argument of `help` command, only commands should be candidates
117+ return command_candidates ( target ) if preposing . match? ( HELP_COMMAND_PREPOSING )
118+
119+ commands = if preposing . empty?
120+ command_candidates ( target )
121+ # It doesn't make sense to propose commands with other preposing
122+ else
123+ [ ]
124+ end
125+
115126 result = ReplTypeCompletor . analyze ( preposing + target , binding : bind , filename : @context . irb_path )
127+
116128 return commands unless result
117129
118130 commands | result . completion_candidates . map { target + _1 }
@@ -187,12 +199,20 @@ def complete_require_path(target, preposing, postposing)
187199 end
188200
189201 def completion_candidates ( preposing , target , postposing , bind :)
190- if preposing && postposing
191- result = complete_require_path ( target , preposing , postposing )
192- return result if result
202+ if result = complete_require_path ( target , preposing , postposing )
203+ return result
193204 end
194- commands = command_completions ( preposing || '' , target )
195- commands | retrieve_completion_data ( target , bind : bind , doc_namespace : false ) . compact . map { |i | i . encode ( Encoding . default_external ) }
205+
206+ commands = command_candidates ( target )
207+
208+ # When completing the argument of `help` command, only commands should be candidates
209+ return commands if preposing . match? ( HELP_COMMAND_PREPOSING )
210+
211+ # It doesn't make sense to propose commands with other preposing
212+ commands = [ ] unless preposing . empty?
213+
214+ completion_data = retrieve_completion_data ( target , bind : bind , doc_namespace : false ) . compact . map { |i | i . encode ( Encoding . default_external ) }
215+ commands | completion_data
196216 end
197217
198218 def doc_namespace ( _preposing , matched , _postposing , bind :)
@@ -470,7 +490,7 @@ def retrieve_completion_data(input, bind: IRB.conf[:MAIN_CONTEXT].workspace.bind
470490 end
471491 end
472492 CompletionProc = -> ( target , preposing = nil , postposing = nil ) {
473- regexp_completor . completion_candidates ( preposing , target , postposing , bind : IRB . conf [ :MAIN_CONTEXT ] . workspace . binding )
493+ regexp_completor . completion_candidates ( preposing || '' , target , postposing || '' , bind : IRB . conf [ :MAIN_CONTEXT ] . workspace . binding )
474494 }
475495 end
476496 deprecate_constant :InputCompletor
0 commit comments