class SublimeDSL::SublimeText::KeyMap

Attributes

bindings[R]
keyboard[R]
name[R]
os[R]
to_s[R]

Public Class Methods

import(file) click to toggle source
# File lib/sublime_dsl/sublime_text/keymap.rb, line 10
def self.import(file)
  name = File.basename(file, File.extname(file))
  kb = Keyboard.sublime
  map = new(name, kb)
  list = JSON[File.read(file, encoding: 'utf-8')]
  begin
    map.bindings.concat list.map { |h| KeyBinding.from_json(h) }
  rescue => ex
    Console.error "file: #{file}"
    raise ex
  end
  map
end
new(name, keyboard) click to toggle source
# File lib/sublime_dsl/sublime_text/keymap.rb, line 26
def initialize(name, keyboard)
  @name = name
  if name =~ /\((Windows|OSX|Linux)\)/
    @os = $1
  else
    @os = nil
  end

  @keyboard = keyboard
  @bindings = []
end

Public Instance Methods

export(dir) click to toggle source
# File lib/sublime_dsl/sublime_text/keymap.rb, line 72
def export(dir)
  file = "#{dir}/#{name}.sublime-keymap"
  File.open(file, 'wb:utf-8') { |f| f.write to_json }
end
for_keyboard(other_keyboard) click to toggle source
# File lib/sublime_dsl/sublime_text/keymap.rb, line 40
def for_keyboard(other_keyboard)
  return self if keyboard == other_keyboard
  # do not convert if the OS do not match
  return self if os && other_keyboard.os && os != other_keyboard.os
  KeyMap.new(name, other_keyboard).tap do |map|
    bindings.each do |b|
      map.bindings << b.for_keyboard(other_keyboard)
    end
  end
end
to_json() click to toggle source
# File lib/sublime_dsl/sublime_text/keymap.rb, line 77
def to_json
  st_bindings = for_keyboard(Keyboard.sublime).bindings
  "[\n" <<
    st_bindings.map { |b| b.to_json }.join(",\n") <<
  "\n]"
end
update_fixmes() click to toggle source
# File lib/sublime_dsl/sublime_text/keymap.rb, line 84
def update_fixmes
  by_keystrokes_and_context = bindings.group_by { |b| [b.keystrokes, b.context] }
  by_keystrokes_and_context.each_value do |binding_list|
    next if binding_list.length == 1
    binding_list.each do |b|
      b.fixmes << "assigned #{binding_list.length} times in this keymap"
    end
  end
end
write(dir) click to toggle source
# File lib/sublime_dsl/sublime_text/keymap.rb, line 51
def write(dir)
  update_fixmes
  file = "#{dir}/#{name}.keymap.rb"
  File.open(file, 'wb:utf-8') do |f|
    f.puts "\nkeymap #{name.to_source} do"
    f.puts
    f.puts "  keyboard #{keyboard.name.to_source}" unless keyboard == Keyboard.sublime
    f.puts "  conditionals if: 'si', and: 'et', or: 'ou'"
    f.puts
    bindings.each do |b|
      begin
        f.puts '  ' << b.to_dsl.gsub("\n", "\n  ")
      rescue => ex
        Console.error "file: #{file}\nbinding: #{b.keystrokes}"
        raise ex
      end
    end
    f.puts "\nend"
  end
end