Class: SublimeDSL::SublimeText::KeyMap
- Inherits:
-
Object
- Object
- SublimeDSL::SublimeText::KeyMap
- Defined in:
- lib/sublime_dsl/sublime_text/keymap.rb,
lib/sublime_dsl/sublime_text/keymap/dsl_reader.rb
Defined Under Namespace
Classes: BindingReader, Context, DSLReader, KeyBinding, MethodCatcher
Instance Attribute Summary collapse
-
#bindings ⇒ Object
readonly
Returns the value of attribute bindings.
-
#keyboard ⇒ Object
readonly
Returns the value of attribute keyboard.
-
#name ⇒ Object
(also: #to_s)
readonly
Returns the value of attribute name.
-
#os ⇒ Object
readonly
Returns the value of attribute os.
Class Method Summary collapse
Instance Method Summary collapse
- #export(dir) ⇒ Object
- #for_keyboard(other_keyboard) ⇒ Object
-
#initialize(name, keyboard) ⇒ KeyMap
constructor
A new instance of KeyMap.
- #to_json ⇒ Object
- #update_fixmes ⇒ Object
- #write(dir) ⇒ Object
Constructor Details
#initialize(name, keyboard) ⇒ KeyMap
Returns a new instance of KeyMap.
26 27 28 29 30 31 32 33 34 35 36 |
# 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 |
Instance Attribute Details
#bindings ⇒ Object (readonly)
Returns the value of attribute bindings.
24 25 26 |
# File 'lib/sublime_dsl/sublime_text/keymap.rb', line 24 def bindings @bindings end |
#keyboard ⇒ Object (readonly)
Returns the value of attribute keyboard.
24 25 26 |
# File 'lib/sublime_dsl/sublime_text/keymap.rb', line 24 def keyboard @keyboard end |
#name ⇒ Object (readonly) Also known as: to_s
Returns the value of attribute name.
24 25 26 |
# File 'lib/sublime_dsl/sublime_text/keymap.rb', line 24 def name @name end |
#os ⇒ Object (readonly)
Returns the value of attribute os.
24 25 26 |
# File 'lib/sublime_dsl/sublime_text/keymap.rb', line 24 def os @os end |
Class Method Details
.import(file) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 |
# 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 |
Instance Method Details
#export(dir) ⇒ Object
72 73 74 75 |
# 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) ⇒ Object
40 41 42 43 44 45 46 47 48 49 |
# 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 ⇒ Object
77 78 79 80 81 82 |
# 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 ⇒ Object
84 85 86 87 88 89 90 91 92 |
# 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) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# 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 |