class SublimeDSL::SublimeText::KeyMap::KeyBinding

A key binding: one or more keystrokes, a command and an optional context

Attributes

command[R]
context[R]
fixmes[R]
keystrokes[R]
source_file[RW]

Public Class Methods

from_json(json_hash) click to toggle source
# File lib/sublime_dsl/sublime_text/keymap.rb, line 99
def self.from_json(json_hash)
  h = json_hash.dup
  keystroke_specs = h.delete('keys') or raise Error, 'no keys: ' << json_hash.inspect
  keystrokes = keystroke_specs.map { |s| Keyboard.sublime.ensure_keystroke(s) }
  cmd = h.delete('command')  or raise Error, 'no command: ' << json_hash.inspect
  command = Command.new(cmd, h.delete('args'))
  context_hash = h.delete('context')
  context = context_hash && Context.from_json(context_hash)
  h.empty? or raise Error, 'unexpected JSON keys: ' << h.inspect
  new(keystrokes, command, context)
rescue => ex
  warn "error with binding #{json_hash.inspect}"
  warn ex.message
  raise
end
new(keystrokes, command, context = nil) click to toggle source
# File lib/sublime_dsl/sublime_text/keymap.rb, line 119
def initialize(keystrokes, command, context = nil)
  @keystrokes = keystrokes
  @command = command
  @context = context
  @fixmes = []
end

Public Instance Methods

add_condition(args) click to toggle source
# File lib/sublime_dsl/sublime_text/keymap.rb, line 126
def add_condition(args)
  @context ||= Context.new
  @context.conditions << Context::Condition.from_dsl(args)
end
for_keyboard(other_keyboard) click to toggle source
# File lib/sublime_dsl/sublime_text/keymap.rb, line 131
def for_keyboard(other_keyboard)
  if other_keyboard == Keyboard.sublime
    # the current binding is for a custom keyboard:
    # get the corresponding ST keystrokes
    other_keystrokes = keystrokes.map do |ks|
      spec = ks.key_event || ks.chr_event
      spec or raise Error, "#{ks} has no SublimeText equivalent"
      other_keyboard.ensure_keystroke(spec)
    end
  else
    # the current binding is for the sublime text keyboard:
    # its keystrokes may not exist in the target keyboard
    other_keystrokes = keystrokes.map do |ks|
      other_keyboard.keystroke_for_sublime_spec(ks.to_spec)
    end
  end
  KeyBinding.new(other_keystrokes, command, context)
end
to_dsl() click to toggle source
# File lib/sublime_dsl/sublime_text/keymap.rb, line 150
def to_dsl

  comments = fixmes.map { |f| "# FIXME: #{f}\n" }.join
  valid = true
  keystrokes.each do |ks|
    if ks.type == :null
      comments << "# FIXME: no equivalent for keystroke: #{ks.key_event}\n"
      valid = false
      next
    end
    next if ks.type == :char || ks.to_spec.length == 1
    if ks.os_action
      comments << "# FIXME: #{ks} is OS-reserved (#{ks.os_action})\n"
    end
    if ks.key_event.nil?
      comments << "# FIXME: #{ks} is not seen by Sublime Text\n"
    elsif ks.chr_event
      comments << "# FIXME: #{ks} also generates the character #{ks.chr_event.to_source}\n"
    end
  end
  spec = keystrokes.map { |ks| ks.to_spec || ks.key_event }.join(', ')
  dsl = "bind #{spec.to_source}, #{command.to_dsl}\n"
  dsl << context.to_dsl.indent(2) << "\n" if context
  dsl.gsub!(/^/, '# ') unless valid
  (comments << dsl).strip
end
Also aliased as: to_s
to_json() click to toggle source
# File lib/sublime_dsl/sublime_text/keymap.rb, line 177
def to_json
  h = { 'keys' => keystrokes.map { |ks| ks.to_spec } }
  h.merge! command.to_h
  json = '  ' << JSON.generate(h)
  return json unless context
  json = json[0..-2] << %(, "context": [\n    )
  json << context.conditions.map(&:to_json).join(",\n    ")
  json << "\n  ]}"
  json
end
to_s()
Alias for: to_dsl
value_id() click to toggle source
# File lib/sublime_dsl/sublime_text/keymap.rb, line 192
def value_id
  [keystrokes, command, context]
end