Class: FastRI::RiService

Inherits:
Object
  • Object
show all
Defined in:
lib/fastri/ri_service.rb

Defined Under Namespace

Classes: MatchFinder, Options

Constant Summary collapse

DEFAULT_OBTAIN_ENTRIES_OPTIONS =
{
  :lookup_order => [
            :exact, :exact_ci, :nested, :nested_ci, :partial, :partial_ci, 
            :nested_partial, :nested_partial_ci,
  ],
}
DEFAULT_INFO_OPTIONS =
{
  :formatter => :ansi,
  :width     => 72,
  :extended  => false,
  :expand_choices => false,
}

Instance Method Summary collapse

Constructor Details

#initialize(ri_reader) ⇒ RiService

Returns a new instance of RiService.



112
113
114
# File 'lib/fastri/ri_service.rb', line 112

def initialize(ri_reader)
  @ri_reader = ri_reader
end

Instance Method Details

#all_classesObject

Return array of strings with the names of all known classes.



276
277
278
# File 'lib/fastri/ri_service.rb', line 276

def all_classes
  @ri_reader.full_class_names
end

#all_methodsObject

Return array of strings with the names of all known methods.



271
272
273
# File 'lib/fastri/ri_service.rb', line 271

def all_methods
  @ri_reader.full_method_names
end

#args(keyword, options = {}) ⇒ Object



237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/fastri/ri_service.rb', line 237

def args(keyword, options = {})
  options = DEFAULT_INFO_OPTIONS.merge(options)
  return nil if keyword.strip.empty?
  descriptor = NameDescriptor.new(keyword)
  entries = obtain_entries(descriptor, options)
  return nil if entries.empty? || RiIndex::ClassEntry === entries[0]

  params_text = ""
  entries.each do |entry|
    desc = @ri_reader.get_method(entry)
    params_text << capture_stdout(display(options)) do |display|
      display.full_params(desc)
    end
  end
  params_text
rescue RiError
  return nil
end

#class_list(keyword) ⇒ Object

Returns a list with the names of the modules/classes that define the given method, or nil.



258
259
260
# File 'lib/fastri/ri_service.rb', line 258

def class_list(keyword)
  _class_list(keyword, '\1')
end

#class_list_with_flag(keyword) ⇒ Object

Returns a list with the names of the modules/classes that define the given method, followed by a flag (#|::), or nil. e.g. [“Array#”, “IO#”, “IO::”, … ]



265
266
267
268
# File 'lib/fastri/ri_service.rb', line 265

def class_list_with_flag(keyword)
  r = _class_list(keyword, '\1\2')
  r ? r.map{|x| x.gsub(/\./, "::")} : nil
end

#completion_list(keyw) ⇒ Object



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
# File 'lib/fastri/ri_service.rb', line 141

def completion_list(keyw)
  return @ri_reader.full_class_names if keyw == ""

  descriptor = NameDescriptor.new(keyw)

  if descriptor.class_names.empty?
    # try partial matches
    meths = @ri_reader.methods_under_matching("", /(#|\.)#{descriptor.method_name}/, true)
    ret = meths.map{|x| x.name}.uniq.sort
    return ret.empty? ? nil : ret
  end

  # if we're here, some namespace was given
  full_ns_name = descriptor.class_names.join("::")
  if descriptor.method_name == nil && ! [?#, ?:, ?.].include?(keyw[-1])
    # partial match
    namespaces = @ri_reader.namespaces_under_matching("", /^#{full_ns_name}/, false)
    ret = namespaces.map{|x| x.full_name}.uniq.sort
    return ret.empty? ? nil : ret
  else
    if [?#, ?:, ?.].include?(keyw[-1])
      seps = case keyw[-1]
        when ?#; %w[#]
        when ?:; %w[.]
        when ?.; %w[. #]
      end
    else  # both namespace and method
      seps = separators(descriptor.is_class_method)
    end
    sep_re = "(" + seps.map{|x| Regexp.escape(x)}.join("|") + ")"
    # partial
    methods = @ri_reader.methods_under_matching(full_ns_name, /#{sep_re}#{descriptor.method_name}/, false)
    ret = methods.map{|x| x.full_name}.uniq.sort
    return ret.empty? ? nil : ret
  end
rescue RiError
  return nil
end

#info(keyw, options = {}) ⇒ Object



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
# File 'lib/fastri/ri_service.rb', line 197

def info(keyw, options = {})
  options = DEFAULT_INFO_OPTIONS.merge(options)
  return nil if keyw.strip.empty?
  descriptor = NameDescriptor.new(keyw)
  entries = obtain_entries(descriptor, options)

  case entries.size
  when 0; nil
  when 1
    case entries[0].type
    when :namespace
      capture_stdout(display(options)) do |display|
        display.display_class_info(@ri_reader.get_class(entries[0]), @ri_reader)
        if options[:extended]
          methods = @ri_reader.methods_under(entries[0], true)
          methods.each do |meth_entry|
            display.display_method_info(@ri_reader.get_method(meth_entry))
          end
        end
      end
    when :method
      capture_stdout(display(options)) do |display|
        display.display_method_info(@ri_reader.get_method(entries[0]))
      end
    end
  else
    capture_stdout(display(options)) do |display|
      formatter = display.formatter
      formatter.draw_line("Multiple choices:")
      formatter.blankline
      formatter.wrap(entries.map{|x| x.full_name}.join(", "))
      entries.each do |entry|
        display.display_method_info(@ri_reader.get_method(entry)) if entry.type == :method
      end if options[:expand_choices]
    end
  end
rescue RiError
  return nil
end

#matches(keyword, options = {}) ⇒ Object



187
188
189
190
191
192
193
194
195
# File 'lib/fastri/ri_service.rb', line 187

def matches(keyword, options = {})
  options = DEFAULT_INFO_OPTIONS.merge(options)
  return nil if keyword.strip.empty?
  descriptor = NameDescriptor.new(keyword)
  ret = obtain_entries(descriptor, options).map{|x| x.full_name}
  ret ? ret : nil
rescue RiError
  return nil
end

#obtain_entries(descriptor, options = {}) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/fastri/ri_service.rb', line 122

def obtain_entries(descriptor, options = {})
  options = DEFAULT_OBTAIN_ENTRIES_OPTIONS.merge(options)
  if descriptor.class_names.empty?
    seps = separators(descriptor.is_class_method)
    return obtain_unqualified_method_entries(descriptor.method_name, seps,
                                             options[:lookup_order])
  end

  # if we're here, some namespace was given
  full_ns_name = descriptor.class_names.join("::")
  if descriptor.method_name == nil
    return obtain_namespace_entries(full_ns_name, options[:lookup_order])
  else  # both namespace and method
    seps = separators(descriptor.is_class_method)
    return obtain_qualified_method_entries(full_ns_name, descriptor.method_name, 
                                           seps, options[:lookup_order])
  end
end