Class: SublimeDSL::SublimeText::KeyMap::BindingReader

Inherits:
MethodCatcher show all
Defined in:
lib/sublime_dsl/sublime_text/keymap/dsl_reader.rb

Overview

DSL interpreter for bindings.

Constant Summary

Constants inherited from Tools::BlankSlate

Tools::BlankSlate::KEPT_METHODS

Instance Method Summary collapse

Methods inherited from MethodCatcher

#_args, #_method, #_object, #inspect, #is_a?

Constructor Details

#initialize(file) ⇒ BindingReader

Returns a new instance of BindingReader.



80
81
82
83
84
85
86
87
# File 'lib/sublime_dsl/sublime_text/keymap/dsl_reader.rb', line 80

def initialize(file)
  super(nil, nil, nil)
  @file = file
  @bindings = []
  @keyboard = Keyboard.sublime
  @conditionals = nil
  @catchers_hash = {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args) ⇒ Object



105
106
107
108
109
# File 'lib/sublime_dsl/sublime_text/keymap/dsl_reader.rb', line 105

def method_missing(sym, *args)
  catcher = super(sym, *args)
  @catchers_hash[catcher.object_id] = catcher
  catcher
end

Instance Method Details

#_and(*args, &block) ⇒ Object



143
144
145
146
147
# File 'lib/sublime_dsl/sublime_text/keymap/dsl_reader.rb', line 143

def _and(*args, &block)
  b = @bindings.last or
    raise Error, "'#{_conditionals[:and]}' without a previous '#{_conditionals[:if]}'"
  b.add_condition get_condition(args)
end

#_bindingsObject



89
90
91
# File 'lib/sublime_dsl/sublime_text/keymap/dsl_reader.rb', line 89

def _bindings
  @bindings
end

#_catchers_hashObject



97
98
99
# File 'lib/sublime_dsl/sublime_text/keymap/dsl_reader.rb', line 97

def _catchers_hash
  @catchers_hash
end

#_conditionalsObject



101
102
103
# File 'lib/sublime_dsl/sublime_text/keymap/dsl_reader.rb', line 101

def _conditionals
  @conditionals ||= { if: '_if', and: '_and', or: '_or' }
end

#_if(*args, &block) ⇒ Object



137
138
139
140
141
# File 'lib/sublime_dsl/sublime_text/keymap/dsl_reader.rb', line 137

def _if(*args, &block)
  b = @bindings.last or
    raise Error, "'#{_conditionals[:if]}' without a previous 'bind'"
  b.add_condition get_condition(args)
end

#_keyboardObject



93
94
95
# File 'lib/sublime_dsl/sublime_text/keymap/dsl_reader.rb', line 93

def _keyboard
  @keyboard
end

#_or(*args, &block) ⇒ Object



149
150
151
152
153
154
155
# File 'lib/sublime_dsl/sublime_text/keymap/dsl_reader.rb', line 149

def _or(*args, &block)
  b = @bindings.last or
    raise Error, "'#{_conditionals[:or]}' without a previous '#{_conditionals[:if]}'"
  b = KeyBinding.new(b.keystrokes, b.command)
  @bindings << b
  b.add_condition get_condition(args)
end

#bind(spec, arg, &block) ⇒ Object



129
130
131
132
133
134
135
# File 'lib/sublime_dsl/sublime_text/keymap/dsl_reader.rb', line 129

def bind(spec, arg, &block)
  ks = spec.split(/,\s+/).map { |s| @keyboard.ensure_keystroke(s) }
  cmd = get_command(arg)
  cmd.error and raise Error, "binding '#{spec}': #{cmd.error}"
  b = KeyBinding.new(ks, cmd)
  @bindings << b
end

#conditionals(options = {}) ⇒ Object



119
120
121
122
123
124
125
126
127
# File 'lib/sublime_dsl/sublime_text/keymap/dsl_reader.rb', line 119

def conditionals(options = {})
  @conditionals = options.dup
  [:if, :and, :or].each do |key|
    method = options.delete(key) or raise Error, "no method name for #{key.inspect}"
    define_singleton_method method.to_sym, self.method("_#{key}".to_sym)
  end
  options.empty? or
    warn "unknown 'conditionals' arguments ignored: #{options.inspect}"
end

#keyboard(name) ⇒ Object



111
112
113
114
115
116
117
# File 'lib/sublime_dsl/sublime_text/keymap/dsl_reader.rb', line 111

def keyboard(name)
  # FIXME: this is dirty
  # assumes the root is the directory above the one containing
  # the current file
  dir = File.dirname(@file)
  @keyboard = Keyboard.get(name, "#{dir}/..")
end