class SublimeDSL::SublimeText::KeyMap::BindingReader

DSL interpreter for bindings.

Public Class Methods

new(file) click to toggle source
# 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

Public Instance Methods

_and(*args, &block) click to toggle source
# 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
_bindings() click to toggle source
# File lib/sublime_dsl/sublime_text/keymap/dsl_reader.rb, line 89
def _bindings
  @bindings
end
_catchers_hash() click to toggle source
# File lib/sublime_dsl/sublime_text/keymap/dsl_reader.rb, line 97
def _catchers_hash
  @catchers_hash
end
_conditionals() click to toggle source
# 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) click to toggle source
# 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
_keyboard() click to toggle source
# File lib/sublime_dsl/sublime_text/keymap/dsl_reader.rb, line 93
def _keyboard
  @keyboard
end
_or(*args, &block) click to toggle source
# 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) click to toggle source
# 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 = {}) click to toggle source
# 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) click to toggle source
# 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
method_missing(sym, *args) click to toggle source
# 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

Private Instance Methods

consumed_catcher(c) click to toggle source
# File lib/sublime_dsl/sublime_text/keymap/dsl_reader.rb, line 186
def consumed_catcher(c)
  @catchers_hash.delete c.object_id
end
flatten_catchers(object) click to toggle source
# File lib/sublime_dsl/sublime_text/keymap/dsl_reader.rb, line 170
def flatten_catchers(object)
  if object.is_a?(MethodCatcher)
    consumed_catcher object
    array = [
      flatten_catchers(object._object),
      flatten_catchers(object._method)
    ]
    if object._args && !object._args.empty?
        array.concat object._args.map { |a| flatten_catchers a }
    end
    array
  else
    object
  end
end
get_command(arg) click to toggle source
# File lib/sublime_dsl/sublime_text/keymap/dsl_reader.rb, line 159
def get_command(arg)
  arg.is_a?(MethodCatcher) or
    return Command.new(nil, nil, "expected a sublime text command: #{arg.inspect}")
  consumed_catcher arg
  Command.from_method_missing(arg._method, arg._args)
end
get_condition(args) click to toggle source
# File lib/sublime_dsl/sublime_text/keymap/dsl_reader.rb, line 166
def get_condition(args)
  args.map { |e| flatten_catchers(e) }.flatten.compact
end