class SublimeDSL::TextMate::Grammar::PListWriter

Creates the PList for a grammar.

Attributes

grammar[R]
root[R]

Public Class Methods

new(grammar) click to toggle source
# File lib/sublime_dsl/textmate/grammar/plist_writer.rb, line 15
def initialize(grammar)
  @grammar = grammar
  @root = {}
  convert_grammar
end

Public Instance Methods

export(file) click to toggle source
# File lib/sublime_dsl/textmate/grammar/plist_writer.rb, line 21
def export(file)
  PList.export(root, file)
end

Private Instance Methods

cleanup_array(list) click to toggle source
# File lib/sublime_dsl/textmate/grammar/plist_writer.rb, line 125
def cleanup_array(list)
  list.each { |o| cleanup_object o }
  list.reject! { |o| empty(o) }
end
cleanup_hash(h) click to toggle source
# File lib/sublime_dsl/textmate/grammar/plist_writer.rb, line 130
def cleanup_hash(h)
  h.keys.each do |key|
    value = h[key]
    cleanup_object value
    if empty(value)
      # HACK: an empty 'begin' or 'end' matches anything (used by C & Tcl grammars)
      if key != 'end' && key != 'begin'
        h.delete key
      else
        h[key] = ''
      end
    end
  end
end
cleanup_object(o) click to toggle source
# File lib/sublime_dsl/textmate/grammar/plist_writer.rb, line 118
def cleanup_object(o)
  case o
  when Array; cleanup_array o
  when Hash; cleanup_hash o
  end
end
convert_array(list) click to toggle source
# File lib/sublime_dsl/textmate/grammar/plist_writer.rb, line 51
def convert_array(list)
  list.map { |o| convert_object o }
end
convert_begin_end_rule(rule) click to toggle source
# File lib/sublime_dsl/textmate/grammar/plist_writer.rb, line 80
def convert_begin_end_rule(rule)
  h = convert_rule_start(rule)
  h['contentName'] = rule.content_scope
  h.merge! convert_match(rule.from, 'begin', 'beginCaptures')
  h.merge! convert_match(rule.to, 'end', 'endCaptures')
  h.merge! convert_captures('captures', rule.captures)
  h['applyEndPatternLast'] = convert_object(rule.to_last)
  h['patterns'] = convert_array(rule.patterns)
  h
end
convert_captures(captures_key, captures) click to toggle source
# File lib/sublime_dsl/textmate/grammar/plist_writer.rb, line 104
def convert_captures(captures_key, captures)
  h = {}
  return h if captures.empty?
  captures.each_pair do |number, scope|
    h[number.to_s] = { 'name' => scope }
  end
  { captures_key => h }
end
convert_false_class(v) click to toggle source
# File lib/sublime_dsl/textmate/grammar/plist_writer.rb, line 116
def convert_false_class(v); 0; end
convert_fragment(f) click to toggle source
# File lib/sublime_dsl/textmate/grammar/plist_writer.rb, line 55
def convert_fragment(f)
  unless f.used
    warn "grammar '#{grammar.name}': fragment '#{f.name}' never used, not output"
    return nil
  end
  a = convert_array(f.patterns)
  a.length > 1 ? { 'patterns' => a } : a.first
end
convert_grammar() click to toggle source
# File lib/sublime_dsl/textmate/grammar/plist_writer.rb, line 27
def convert_grammar

  root['name'] = grammar.name
  root['scopeName'] = grammar.scope

  %w(fileTypes firstLineMatch foldingStartMarker foldingStopMarker keyEquivalent uuid bundleUUID).each do |att|
    root[att] = convert_object(grammar.send(att.snake_case))
  end

  root['patterns'] = convert_array(grammar.patterns)

  frags = {}
  grammar.fragments.each { |f| frags[f.name] = convert_fragment(f) }
  root['repository'] = frags

  cleanup_hash root

end
convert_include(inc) click to toggle source
# File lib/sublime_dsl/textmate/grammar/plist_writer.rb, line 64
def convert_include(inc)
  { 'include' => inc.fragment_name }
end
convert_match(match, regexp_key, captures_key) click to toggle source
# File lib/sublime_dsl/textmate/grammar/plist_writer.rb, line 95
def convert_match(match, regexp_key, captures_key)
  { regexp_key => convert_regexp_wannabe(match.regexp) }
    .merge convert_captures(captures_key, match.captures)
end
convert_match_rule(rule) click to toggle source
# File lib/sublime_dsl/textmate/grammar/plist_writer.rb, line 75
def convert_match_rule(rule)
  h = convert_rule_start(rule)
  h.merge! convert_match(rule.match, 'match', 'captures')
end
convert_nil_class(v) click to toggle source
# File lib/sublime_dsl/textmate/grammar/plist_writer.rb, line 114
def convert_nil_class(v); v; end
convert_no_match_rule(rule) click to toggle source
# File lib/sublime_dsl/textmate/grammar/plist_writer.rb, line 68
def convert_no_match_rule(rule)
  return nil if rule.empty?
  h = convert_rule_start(rule)
  h['patterns'] = convert_array(rule.patterns)
  h
end
convert_object(object) click to toggle source
# File lib/sublime_dsl/textmate/grammar/plist_writer.rb, line 46
def convert_object(object)
  k = object.class.name.split('::').last.snake_case
  send "convert_#{k}", object
end
convert_regexp_wannabe(re) click to toggle source
# File lib/sublime_dsl/textmate/grammar/plist_writer.rb, line 100
def convert_regexp_wannabe(re)
  re.to_s
end
convert_rule_start(rule) click to toggle source
# File lib/sublime_dsl/textmate/grammar/plist_writer.rb, line 91
def convert_rule_start(rule)
  { 'name' => rule.scope, 'disabled' => convert_object(rule.disabled) }
end
convert_string(v) click to toggle source
# File lib/sublime_dsl/textmate/grammar/plist_writer.rb, line 113
def convert_string(v); v; end
convert_true_class(v) click to toggle source
# File lib/sublime_dsl/textmate/grammar/plist_writer.rb, line 115
def convert_true_class(v); 1; end
empty(o) click to toggle source
# File lib/sublime_dsl/textmate/grammar/plist_writer.rb, line 145
def empty(o)
  o.nil? || o.respond_to?(:empty?) && o.empty?
end