class SublimeDSL::SublimeText::Macro

Attributes

commands[R]
name[R]
to_s[R]

Public Class Methods

import(file) click to toggle source
# File lib/sublime_dsl/sublime_text/macro.rb, line 7
def self.import(file)
  # Delete to Hard EOL.sublime-macro:
  # [
  #     {"command": "move_to", "args": {"to": "hardeol", "extend": true}},
  #     {"command": "add_to_kill_ring", "args": {"forward": true}},
  #     {"command": "right_delete"}
  # ]
  macro = new(File.basename(file, '.sublime-macro'))
  JSON[File.read(file, encoding: 'utf-8')].each do |command_hash|
    cmd = command_hash.delete('command')
    cmd or raise Error, "no 'command' key in '#{file}'"
    args = command_hash.delete('args')
    command_hash.empty? or raise Error, 'unknown sublime-macro keys: ' << command_hash.inspect
    macro.commands << Command.new(cmd, args)
  end

  macro
end
new(name) click to toggle source
# File lib/sublime_dsl/sublime_text/macro.rb, line 29
def initialize(name)
  @name = name
  @commands = []
end

Public Instance Methods

export(dir) click to toggle source
# File lib/sublime_dsl/sublime_text/macro.rb, line 47
def export(dir)
  file = "#{dir}/#{name}.sublime-macro"
  File.open(file, 'wb:utf-8') { |f| f.write to_json }
end
to_dsl() click to toggle source
# File lib/sublime_dsl/sublime_text/macro.rb, line 36
def to_dsl
  # macro 'Delete to Hard EOL' do
  #   move_to "hardeol", extend: true
  #   add_to_kill_ring forward: true
  #   right_delete
  # end
  dsl = "macro #{name.inspect} do\n"
  commands.each { |c| dsl << "  #{c.to_dsl(true)}\n" }
  dsl << "end"
end
to_json() click to toggle source
# File lib/sublime_dsl/sublime_text/macro.rb, line 52
def to_json
  # JSON.pretty_generate(commands.map(&:to_h))
  "[\n" <<
    commands.map { |c| JSON.generate(c.to_h) }.join(",\n").indent(2) <<
  "\n]"
end