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,
}

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.



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

def all_classes
  @ri_reader.full_class_names
end

#all_methodsObject

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



260
261
262
# File 'lib/fastri/ri_service.rb', line 260

def all_methods
  @ri_reader.full_method_names
end

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



226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/fastri/ri_service.rb', line 226

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.



247
248
249
# File 'lib/fastri/ri_service.rb', line 247

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::”, … ]



254
255
256
257
# File 'lib/fastri/ri_service.rb', line 254

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



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

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)
      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(", "))
    end
  end
rescue RiError
  return nil
end

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



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

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