Class: SublimeDSL::SublimeText::KeyMap::KeyBinding

Inherits:
Object
  • Object
show all
Includes:
Tools::ValueEquality
Defined in:
lib/sublime_dsl/sublime_text/keymap.rb

Overview

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Tools::ValueEquality

#eql?, #hash

Constructor Details

#initialize(keystrokes, command, context = nil) ⇒ KeyBinding

Returns a new instance of KeyBinding.



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

def initialize(keystrokes, command, context = nil)
  @keystrokes = keystrokes
  @command = command
  @context = context
  @fixmes = []
end

Instance Attribute Details

#commandObject (readonly)

Returns the value of attribute command.



115
116
117
# File 'lib/sublime_dsl/sublime_text/keymap.rb', line 115

def command
  @command
end

#contextObject (readonly)

Returns the value of attribute context.



115
116
117
# File 'lib/sublime_dsl/sublime_text/keymap.rb', line 115

def context
  @context
end

#fixmesObject (readonly)

Returns the value of attribute fixmes.



116
117
118
# File 'lib/sublime_dsl/sublime_text/keymap.rb', line 116

def fixmes
  @fixmes
end

#keystrokesObject (readonly)

Returns the value of attribute keystrokes.



115
116
117
# File 'lib/sublime_dsl/sublime_text/keymap.rb', line 115

def keystrokes
  @keystrokes
end

#source_fileObject

Returns the value of attribute source_file.



117
118
119
# File 'lib/sublime_dsl/sublime_text/keymap.rb', line 117

def source_file
  @source_file
end

Class Method Details

.from_json(json_hash) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# 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

Instance Method Details

#add_condition(args) ⇒ Object



126
127
128
129
# 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) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# 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_dslObject Also known as: to_s



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# 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

#to_jsonObject



177
178
179
180
181
182
183
184
185
186
# 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

#value_idObject



192
193
194
# File 'lib/sublime_dsl/sublime_text/keymap.rb', line 192

def value_id
  [keystrokes, command, context]
end