diff options
Diffstat (limited to 'lib/rdoc/ri/driver.rb')
-rw-r--r-- | lib/rdoc/ri/driver.rb | 123 |
1 files changed, 32 insertions, 91 deletions
diff --git a/lib/rdoc/ri/driver.rb b/lib/rdoc/ri/driver.rb index a4b70b8f56..64783dc163 100644 --- a/lib/rdoc/ri/driver.rb +++ b/lib/rdoc/ri/driver.rb @@ -1,23 +1,10 @@ # frozen_string_literal: true -require 'abbrev' require 'optparse' -begin - require 'readline' -rescue LoadError -end - -begin - require 'win32console' -rescue LoadError -end - -require 'rdoc' - -## -# For RubyGems backwards compatibility +require_relative '../../rdoc' -require_relative 'formatter' +require_relative 'formatter' # For RubyGems backwards compatibility +# TODO: Fix weird documentation with `require_relative` ## # The RI driver implements the command-line ri tool. @@ -47,9 +34,9 @@ class RDoc::RI::Driver class NotFoundError < Error - def initialize(klass, suggestions = nil) # :nodoc: + def initialize(klass, suggestion_proc = nil) # :nodoc: @klass = klass - @suggestions = suggestions + @suggestion_proc = suggestion_proc end ## @@ -61,8 +48,9 @@ class RDoc::RI::Driver def message # :nodoc: str = "Nothing known about #{@klass}" - if @suggestions and !@suggestions.empty? - str += "\nDid you mean? #{@suggestions.join("\n ")}" + suggestions = @suggestion_proc&.call + if suggestions and !suggestions.empty? + str += "\nDid you mean? #{suggestions.join("\n ")}" end str end @@ -142,6 +130,8 @@ Where name can be: gem_name: | gem_name:README | gem_name:History + ruby: | ruby:NEWS | ruby:globals + All class names may be abbreviated to their minimum unambiguous form. If a name is ambiguous, all valid options will be listed. @@ -153,6 +143,10 @@ they're contained in. If the gem name is followed by a ':' all files in the gem will be shown. The file name extension may be omitted where it is unambiguous. +'ruby' can be used as a pseudo gem name to display files from the Ruby +core documentation. Use 'ruby:' by itself to get a list of all available +core documentation files. + For example: #{opt.program_name} Fil @@ -160,6 +154,7 @@ For example: #{opt.program_name} File.new #{opt.program_name} zip #{opt.program_name} rdoc:README + #{opt.program_name} ruby:comments Note that shell quoting or escaping may be required for method names containing punctuation: @@ -426,9 +421,6 @@ or the PAGER environment variable. @use_stdout = options[:use_stdout] @show_all = options[:show_all] @width = options[:width] - - # pager process for jruby - @jruby_pager_process = nil end ## @@ -609,11 +601,11 @@ or the PAGER environment variable. stores = classes[current] - break unless stores and not stores.empty? + next unless stores and not stores.empty? - klasses = stores.map do |store| - store.ancestors[current] - end.flatten.uniq + klasses = stores.flat_map do |store| + store.ancestors[current] || [] + end.uniq klasses = klasses - seen @@ -957,8 +949,8 @@ or the PAGER environment variable. ary = class_names.grep(Regexp.new("\\A#{klass.gsub(/(?=::|\z)/, '[^:]*')}\\z")) if ary.length != 1 && ary.first != klass if check_did_you_mean - suggestions = DidYouMean::SpellChecker.new(dictionary: class_names).correct(klass) - raise NotFoundError.new(klass, suggestions) + suggestion_proc = -> { DidYouMean::SpellChecker.new(dictionary: class_names).correct(klass) } + raise NotFoundError.new(klass, suggestion_proc) else raise NotFoundError, klass end @@ -1045,36 +1037,6 @@ or the PAGER environment variable. end ## - # Finds the given +pager+ for jruby. Returns an IO if +pager+ was found. - # - # Returns false if +pager+ does not exist. - # - # Returns nil if the jruby JVM doesn't support ProcessBuilder redirection - # (1.6 and older). - - def find_pager_jruby pager - require 'java' - require 'shellwords' - - return nil unless java.lang.ProcessBuilder.constants.include? :Redirect - - pager = Shellwords.split pager - - pb = java.lang.ProcessBuilder.new(*pager) - pb = pb.redirect_output java.lang.ProcessBuilder::Redirect::INHERIT - - @jruby_pager_process = pb.start - - input = @jruby_pager_process.output_stream - - io = input.to_io - io.sync = true - io - rescue java.io.IOException - false - end - - ## # Finds a store that matches +name+ which can be the name of a gem, "ruby", # "home" or "site". # @@ -1113,6 +1075,10 @@ or the PAGER environment variable. def interactive puts "\nEnter the method name you want to look up." + begin + require 'readline' + rescue LoadError + end if defined? Readline then Readline.completion_proc = method :complete puts "You can use tab to autocomplete." @@ -1122,7 +1088,7 @@ or the PAGER environment variable. loop do name = if defined? Readline then - Readline.readline ">> " + Readline.readline ">> ", true else print ">> " $stdin.gets @@ -1142,17 +1108,6 @@ or the PAGER environment variable. end ## - # Is +file+ in ENV['PATH']? - - def in_path? file - return true if file =~ %r%\A/% and File.exist? file - - ENV['PATH'].split(File::PATH_SEPARATOR).any? do |path| - File.exist? File.join(path, file) - end - end - - ## # Lists classes known to ri starting with +names+. If +names+ is empty all # known classes are shown. @@ -1283,8 +1238,8 @@ or the PAGER environment variable. methods.push(*store.instance_methods[klass]) if [:instance, :both].include? types end methods = methods.uniq - suggestions = DidYouMean::SpellChecker.new(dictionary: methods).correct(method_name) - raise NotFoundError.new(name, suggestions) + suggestion_proc = -> { DidYouMean::SpellChecker.new(dictionary: methods).correct(method_name) } + raise NotFoundError.new(name, suggestion_proc) else raise NotFoundError, name end @@ -1346,7 +1301,6 @@ or the PAGER environment variable. yield pager ensure pager.close - @jruby_pager_process.wait_for if @jruby_pager_process end else yield $stdout @@ -1514,27 +1468,14 @@ or the PAGER environment variable. def setup_pager return if @use_stdout - jruby = RUBY_ENGINE == 'jruby' - pagers = [ENV['RI_PAGER'], ENV['PAGER'], 'pager', 'less', 'more'] + require 'shellwords' pagers.compact.uniq.each do |pager| - next unless pager - - pager_cmd = pager.split(' ').first - - next unless in_path? pager_cmd - - if jruby then - case io = find_pager_jruby(pager) - when nil then break - when false then next - else io - end - else - io = IO.popen(pager, 'w') rescue next - end + pager = Shellwords.split(pager) + next if pager.empty? + io = IO.popen(pager, 'w') rescue next next if $? and $?.pid == io.pid and $?.exited? # pager didn't work @paging = true |