summaryrefslogtreecommitdiff
path: root/bin/ri
blob: 49d88499747951192b243d9a22c9fccc2a2183cd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
#!/usr/bin/env ruby
# usage:
#
#   ri  name...
#
# where name can be 
#
#   Class | Class::method | Class#method | Class.method | method
#
# All names may be abbreviated to their minimum unbiguous form. If a name
# _is_ ambiguous, all valid options will be listed.
#
# The form '.' method matches either class or instance methods, while 
# #method matches only instance and ::method matches only class methods.

require 'rdoc/ri/ri_paths'
require 'rdoc/ri/ri_cache'
require 'rdoc/ri/ri_util'
require 'rdoc/ri/ri_reader'
require 'rdoc/ri/ri_formatter'
require 'rdoc/ri/ri_options'


######################################################################

class  RiDisplay

  def initialize
    @options = RI::Options.instance
    @options.parse
    paths = @options.paths || RI::Paths::PATH
    if paths.empty?
      $stderr.puts "No ri documentation found in:"
      [ RI::Paths::SYSDIR, RI::Paths::SITEDIR, RI::Paths::HOMEDIR].each do |d|
        $stderr.puts "     #{d}"
      end
      $stderr.puts "\nWas rdoc run to create documentation?"
      exit 1                   
    end
    @ri_reader = RI::RiReader.new(RI::RiCache.new(paths))
    @formatter = @options.formatter.new(@options, "     ")
  end    
  
  
  ######################################################################
  
  def display_usage
    setup_pager
    RI::Options::OptionList.usage(short_form=true)
    page_output
  end

  ######################################################################

  def setup_pager
    unless @options.use_stdout
      require 'tempfile'

      @save_stdout = STDOUT.clone
      STDOUT.reopen(Tempfile.new("ri_"))
    end
  end

  ######################################################################

  def page_output
    unless @options.use_stdout
      path = STDOUT.path
      STDOUT.reopen(@save_stdout)
      @save_stdout = nil
      paged = false
      for pager in [ ENV['PAGER'], "less", "more <", 'pager' ].compact.uniq
        if system("#{pager} #{path}")
          paged = true
          break
        end
      end
      if !paged
        @options.use_stdout = true
        puts File.read(path)
      end
    end
  end

  ######################################################################
  
  def display_params(method)

    params = method.params

    if params[0,1] == "("
      if method.is_singleton
        params = method.full_name + params
      else
        params = method.name + params
      end
    end
    params.split(/\n/).each {|p| @formatter.wrap(p) }
  end

  ######################################################################
  
  def display_flow(flow)
    if !flow || flow.empty?
      @formatter.wrap("(no description...)")
    else
      @formatter.display_flow(flow)
    end
  end

  ######################################################################
  
  def display_method_info(method_entry)
    method = @ri_reader.get_method(method_entry)
    @formatter.draw_line(method.full_name)
    display_params(method)
    @formatter.draw_line
    display_flow(method.comment)
    if method.aliases && !method.aliases.empty?
      @formatter.blankline
      aka = "(also known as "
      aka << method.aliases.map {|a| a.name }.join(", ") 
      aka << ")"
      @formatter.wrap(aka)
    end
  end
  
  ######################################################################
  
  def display_class_info(class_entry)
    klass = @ri_reader.get_class(class_entry)
    superclass = klass.superclass_string
    
    if superclass
      superclass = " < " + superclass
    else
      superclass = ""
    end
    
    @formatter.draw_line(klass.display_name + ": " +
                         klass.full_name + superclass)
    
    display_flow(klass.comment)
    @formatter.draw_line
    
    unless klass.includes.empty?
      @formatter.blankline
      @formatter.display_heading("Includes:", 2, "")
      incs = []
      klass.includes.each do |inc|
        inc_desc = @ri_reader.find_class_by_name(inc.name)
        if inc_desc
          str = inc.name + "("
          str << inc_desc.instance_methods.map{|m| m.name}.join(", ")
          str << ")"
          incs << str
        else
          incs << inc.name
        end
      end
    @formatter.wrap(incs.sort.join(', '))
    end
    
    unless klass.constants.empty?
      @formatter.blankline
      @formatter.display_heading("Constants:", 2, "")
      len = 0
      klass.constants.each { |c| len = c.name.length if c.name.length > len }
      len += 2
      klass.constants.each do |c|
        @formatter.wrap(c.value, 
                        @formatter.indent+((c.name+":").ljust(len)))
      end 
    end
    
    unless klass.class_methods.empty?
      @formatter.blankline
      @formatter.display_heading("Class methods:", 2, "")
      @formatter.wrap(klass.class_methods.map{|m| m.name}.sort.join(', '))
    end
    
    unless klass.instance_methods.empty?
      @formatter.blankline
      @formatter.display_heading("Instance methods:", 2, "")
      @formatter.wrap(klass.instance_methods.map{|m| m.name}.sort.join(', '))
    end
    
    unless klass.attributes.empty?
      @formatter.blankline
      @formatter.wrap("Attributes:", "")
      @formatter.wrap(klass.attributes.map{|a| a.name}.sort.join(', '))
    end
  end
  
  ######################################################################
  
  # If the list of matching methods contains exactly one entry, or
  # if it contains an entry that exactly matches the requested method,
  # then display that entry, otherwise display the list of
  # matching method names
  
  def report_method_stuff(requested_method_name, methods)
    if methods.size == 1
      display_method_info(methods[0])
    else
      entries = methods.find_all {|m| m.name == requested_method_name}
      if entries.size == 1
        display_method_info(entries[0])
      else
        puts "More than one method matched your request. You can refine"
        puts "your search by asking for information on one of:\n\n"
        @formatter.wrap(methods.map {|m| m.full_name} .join(", "))
      end
    end
  end
  
  ######################################################################
  
  def report_class_stuff(requested_class_name, namespaces)
    if namespaces.size == 1
      display_class_info(namespaces[0])
    else 
      entries = namespaces.find_all {|m| m.full_name == requested_class_name}
      if entries.size == 1
        display_class_info(entries[0])
      else
        puts "More than one class or module matched your request. You can refine"
        puts "your search by asking for information on one of:\n\n"
        @formatter.wrap(namespaces.map {|m| m.full_name}.join(", "))
      end
    end
  end
  
  ######################################################################
  
  
  def display_info_for(arg)
    desc = NameDescriptor.new(arg)
    
    namespaces = @ri_reader.top_level_namespace
    
    for class_name in desc.class_names
      namespaces = @ri_reader.lookup_namespace_in(class_name, namespaces)
      if namespaces.empty?
        raise RiError.new("Nothing known about #{arg}")
      end
    end
    
    setup_pager
    
    begin
      if desc.method_name.nil?
        report_class_stuff(desc.class_names.join('::'), namespaces)
      else
        methods = @ri_reader.find_methods(desc.method_name, 
                                          desc.is_class_method,
                                          namespaces)
        
        if methods.empty?
          raise RiError.new("Nothing known about #{arg}")
        else
          report_method_stuff(desc.method_name, methods)
        end
      end
      
      page_output
    ensure
      STDOUT.reopen(@save_stdout) if @save_stdout
    end
    
  end

  ######################################################################

  def process_args
    if @options.list_classes
      display_class_list
    else
      if ARGV.size.zero?
        display_usage
      else
        begin
          ARGV.each do |arg|
            display_info_for(arg)
          end
        rescue RiError => e
          $stderr.puts(e.message)
          exit(1)
        end
      end
    end
  end

  ######################################################################

  def display_class_list
    classes = @ri_reader.class_names
    if classes.empty?
      puts "Before using ri, you need to generate documentation"
      puts "using 'rdoc' with the --ri option"
    else
      setup_pager
      @formatter.draw_line("Known classes and modules")
      @formatter.blankline
      @formatter.wrap(@ri_reader.class_names.sort.join(", "))
      page_output
    end
  end

end  # class RiDisplay

######################################################################

ri = RiDisplay.new
ri.process_args