Skip to content

Commit

Permalink
Improve method completion for string and regexp that includes word br…
Browse files Browse the repository at this point in the history
…eak characters (#523)

* Improve method completion for string and regexp that includes word break characters

* Remove completion-test's assert_not_include because candidates no longer include every possible methods

* Add comment about string's method completion regexp

Co-authored-by: Stan Lo <stan001212@gmail.com>

* Add comment about regexp's method completion regexp

Co-authored-by: Stan Lo <stan001212@gmail.com>

---------

Co-authored-by: Stan Lo <stan001212@gmail.com>
  • Loading branch information
tompng and st0012 authored Mar 6, 2023
1 parent 78c74d2 commit aa8128c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
10 changes: 7 additions & 3 deletions lib/irb/completion.rb
Original file line number Diff line number Diff line change
Expand Up @@ -166,10 +166,12 @@ def self.retrieve_files_to_require_relative_from_current_dir

def self.retrieve_completion_data(input, bind: IRB.conf[:MAIN_CONTEXT].workspace.binding, doc_namespace: false)
case input
when /^((["'`]).*\2)\.([^.]*)$/
# this regexp only matches the closing character because of irb's Reline.completer_quote_characters setting
# details are described in: https://github.com/ruby/irb/pull/523
when /^(.*["'`])\.([^.]*)$/
# String
receiver = $1
message = $3
message = $2

if doc_namespace
"String.#{message}"
Expand All @@ -178,7 +180,9 @@ def self.retrieve_completion_data(input, bind: IRB.conf[:MAIN_CONTEXT].workspace
select_message(receiver, message, candidates)
end

when /^(\/[^\/]*\/)\.([^.]*)$/
# this regexp only matches the closing character because of irb's Reline.completer_quote_characters setting
# details are described in: https://github.com/ruby/irb/pull/523
when /^(.*\/)\.([^.]*)$/
# Regexp
receiver = $1
message = $2
Expand Down
4 changes: 4 additions & 0 deletions test/irb/test_completion.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,15 @@ def setup
class TestMethodCompletion < TestCompletion
def test_complete_string
assert_include(IRB::InputCompletor.retrieve_completion_data("'foo'.up", bind: binding), "'foo'.upcase")
# completing 'foo bar'.up
assert_include(IRB::InputCompletor.retrieve_completion_data("bar'.up", bind: binding), "bar'.upcase")
assert_equal("String.upcase", IRB::InputCompletor.retrieve_completion_data("'foo'.upcase", bind: binding, doc_namespace: true))
end

def test_complete_regexp
assert_include(IRB::InputCompletor.retrieve_completion_data("/foo/.ma", bind: binding), "/foo/.match")
# completing /foo bar/.ma
assert_include(IRB::InputCompletor.retrieve_completion_data("bar/.ma", bind: binding), "bar/.match")
assert_equal("Regexp.match", IRB::InputCompletor.retrieve_completion_data("/foo/.match", bind: binding, doc_namespace: true))
end

Expand Down

0 comments on commit aa8128c

Please sign in to comment.