Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions lib/irb/command/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,46 @@ def execute(irb_context, arg)
puts e.message
end

# Returns formatted lines for display in the doc dialog popup.
def doc_dialog_content(name, width)
lines = []
lines << Color.colorize(name, [:BOLD, :BLUE]) + Color.colorize(" (command)", [:CYAN])
lines << ""
lines.concat(wrap_lines(description, width))
if help_message
lines << ""
lines.concat(wrap_lines(help_message, width))
end
lines
end

private

def highlight(text)
Color.colorize(text, [:BOLD, :BLUE])
end

def wrap_lines(text, width)
text.lines.flat_map do |line|
line = line.chomp
next [''] if line.empty?
next [line] if line.length <= width

indent = line[/\A\s*/]
parts = line.strip.split(/(\s+)/)
result = []
current = indent.dup
parts.each do |part|
if current != indent && current.length + part.length > width
result << current.rstrip
current = indent.dup
end
current << part unless current == indent && part.match?(/\A\s+\z/)
end
result << current.rstrip unless current == indent
result
end
end
end

def initialize(irb_context)
Expand Down
39 changes: 35 additions & 4 deletions lib/irb/completion.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,29 @@
require_relative 'ruby-lex'

module IRB
class DocumentTarget # :nodoc:
attr_reader :name

def initialize(name)
@name = name
end
end

class CommandDocument < DocumentTarget # :nodoc:
end

# Represents a method/class documentation target. May hold multiple names
# when the receiver is ambiguous (e.g. `{}.any?` could be Hash#any? or Proc#any?).
# The dialog popup uses only the first name; the full-screen display renders all.
class MethodDocument < DocumentTarget # :nodoc:
attr_reader :names

def initialize(*names)
super(names.first)
@names = names
end
end

class BaseCompletor # :nodoc:

# Set of reserved words used by Ruby, you should not use these for
Expand Down Expand Up @@ -76,6 +99,12 @@ def command_candidates(target)
end
end

def command_document_target(preposing, matched)
if preposing.empty? && IRB::Command.command_names.include?(matched)
CommandDocument.new(matched)
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 Down Expand Up @@ -118,8 +147,10 @@ def completion_candidates(preposing, target, _postposing, bind:)
end

def doc_namespace(preposing, matched, _postposing, bind:)
result = ReplTypeCompletor.analyze(preposing + matched, binding: bind, filename: @context.irb_path)
result&.doc_namespace('')
command_document_target(preposing, matched) || begin
result = ReplTypeCompletor.analyze(preposing + matched, binding: bind, filename: @context.irb_path)
result&.doc_namespace('')
end
end
end

Expand Down Expand Up @@ -201,8 +232,8 @@ def completion_candidates(preposing, target, postposing, bind:)
commands | completion_data
end

def doc_namespace(_preposing, matched, _postposing, bind:)
retrieve_completion_data(matched, bind: bind, doc_namespace: true)
def doc_namespace(preposing, matched, _postposing, bind:)
command_document_target(preposing, matched) || retrieve_completion_data(matched, bind: bind, doc_namespace: true)
end

def retrieve_completion_data(input, bind:, doc_namespace:)
Expand Down
Loading