Class: VER::View::List::Methods

Inherits:
VER::View::List show all
Defined in:
lib/ver/view/list/methods.rb

Overview

Show methods based on patterns.

TODO: Handle C and evaled methods.

Once 1.9.2 is out, use Method#parameters.

Instance Attribute Summary

Attributes inherited from VER::View::List

#callback, #entry, #frame, #list, #parent, #tag

Instance Method Summary collapse

Methods inherited from VER::View::List

#cancel, #completion, #destroy, #initialize, #line_down, #line_up, #message, #on_update, #pick, #pick_first, #pick_selection, #quit, #select_index, #setup_events, #setup_keymap, #setup_widgets, #sublist

Constructor Details

This class inherits a constructor from VER::View::List

Instance Method Details

#insert_choice(choice) ⇒ Object



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
# File 'lib/ver/view/list/methods.rb', line 35

def insert_choice(choice)
  method, score = choice.values_at(:method, :score)
  owner, name = method.owner, method.name

  list.insert :end, " #{score} #{owner}.#{name}"
  return

  owner, name, parameters = method.owner, method.name, method.parameters

  params = []

  method.parameters.each do |param|
    case param.first
    when :req
      params << param.last
    when :opt
      params << "?#{param.last}"
    when :block
      params << "&#{param.last}"
    end
  end

  params = params.join(', ')

  list.insert :end, " #{score} #{owner}.#{name}(#{params})"
end

#methods_of(namespace, seen = [], &block) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/ver/view/list/methods.rb', line 134

def methods_of(namespace, seen = [], &block)
  return if seen.include?(namespace)
  seen << namespace

  if namespace.respond_to?(:instance_methods)
    namespace.instance_methods(false).each do |name|
      yield namespace, namespace.instance_method(name)
    end
  end

  if namespace.respond_to?(:singleton_methods)
    namespace.singleton_methods(false).each do |name|
      yield namespace, namespace.method(name)
    end
  end

  if namespace.respond_to?(:constants)
    namespace.constants.each do |name|
      methods_of(namespace.const_get(name), seen, &block)
    end
  end

  nil
end

#pick_action(entry) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/ver/view/list/methods.rb', line 16

def pick_action(entry)
  index = list.curselection.first
  choice = @choices[index]

  return unless method = choice[:method]

  if location = method.source_location
    filename, line = location

    if ::File.file?(filename)
      callback.call(filename, line)
    else
      # defined by clumsy eval
    end
  else
    # C method?
  end
end

#updateObject



9
10
11
12
13
14
# File 'lib/ver/view/list/methods.rb', line 9

def update
  list.value = update_choices.map{|choice|
    method, score = choice.values_at(:method, :score)
    " #{score} #{method.owner}.#{method.name}"
  }
end

#update_choicesObject

Intelligent method grepper, tries to find matching methods for a wide range of input, by distributing scores.

Possible input might be:

Window.y
Window y
Window::y
Window#y
VER y
VER::View::File#copy
VER::View::File.copy
VER::View::File::copy

The matching is very tolerant, so you can search for methods in completely different namespaces but still get good results:

VER.delta
# => VER::Buffer.apply_delta
# => VER::Buffer.apply_deltas
# => VER::Cursor.delta

sort
# => VER::Cursor.boundary_sort

VER.ask
# => #<Class:VER>.ask
# => VER::View::File::Methods.buffer_ask
# => VER::View::File::Methods.execute_ask
# => VER::View::File::Methods.execute_ask_context
# => VER::View::File::Methods.filter_selection_ask
# => VER::View::File::Methods.goto_line_ask
# => #<Class:FFI::NCurses>.mousemask
# => VER::View::File::Methods.save_as_ask
# => VER::View::File::Methods.search_ask
# => VER::View::File::Methods.search_ask_context

We are only using string comparisions to find matches, so input is never interpreted as a regular expression and can include anything without having to worry about special meaning within a regexp.

There are some improvments possible (as usual). In particular the scoring should become more accurate for short methods, for example making adjustments when a method starts or ends with the search string.



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
# File 'lib/ver/view/list/methods.rb', line 107

def update_choices
  @choices = []

  needle = entry.value.split(/::|[#.\s]+/)
  owner = needle.join('::')
  name = needle.last.to_s

  methods_of VER do |namespace, method|
    mnamespace = namespace.to_s
    mowner, mname = method.owner.to_s, method.name.to_s
    smowner = mowner.split('::')

    score = 0
    score += (needle & smowner).size
    if mname.include?(name)
      distance = Levenshtein.distance(mname, name)
      score += ([mname, name].max_by{|n| n.size }.size - distance)
    end

    next if score == 0

    @choices << {namespace: namespace, method: method, score: score}
  end

  @choices = @choices.sort_by{|hash| [-hash[:score], hash[:method].name] }
end