Module: VER::ModeResolving

Includes:
Keymap::Results
Included in:
MajorMode, MinorMode
Defined in:
lib/ver/mode_resolving.rb

Instance Method Summary collapse

Instance Method Details

#resolve(pattern, parents) ⇒ Object

recursively try to find the pattern in the major mode and its parent modes.



7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/ver/mode_resolving.rb', line 7

def resolve(pattern, parents)
  keymap_result = keymap[pattern]

  case keymap_result
  when Incomplete # try to find a full or longer match in parents
    resolve_incomplete(pattern, parents, keymap_result)
  when Impossible # anything better than that.
    resolve_impossible(pattern, parents, keymap_result)
  else
    # full match from us, so get the action out.
    return keymap_result
  end
end

#resolve_impossible(pattern, parents, result) ⇒ Object



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
# File 'lib/ver/mode_resolving.rb', line 40

def resolve_impossible(pattern, parents, result)
  incomplete = nil
  fallback = nil

  parents.each do |parent|
    next if parent == self

    parent_result = parent.resolve(pattern)

    case parent_result
    when Incomplete # better than us, keep it for later
      if incomplete
        incomplete.merge!(parent_result)
      else
        incomplete = parent_result
      end
    when Impossible # just as bad, ignore
    when Fallback # better than us, keep for later
      fallback ||= parent_result
    else
      # full match, whaoh!
      return parent_result
    end
  end

  return incomplete if incomplete
  return fallback_action if fallback_action
  return fallback if fallback
  return result
end

#resolve_incomplete(pattern, parents, result) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/ver/mode_resolving.rb', line 21

def resolve_incomplete(pattern, parents, result)
  parents.each do |parent|
    next if parent == self

    parent_result = parent.resolve(pattern)

    case parent_result
    when Incomplete # merge and see if there's something better
      result.merge!(parent_result)
    when Impossible # even worse than us, skip
    when Fallback # no match, we are incomplete already
    else # full match, yay!
      return parent_result
    end
  end

  return result
end