class SublimeDSL::TextMate::Preference

A named set of preferences.

Attributes

bundle_uuid[RW]
name[RW]
scope[RW]
settings[RW]
to_s[RW]
uuid[RW]

Public Class Methods

import(file) click to toggle source
# File lib/sublime_dsl/textmate/preference.rb, line 11
def self.import(file)
  root = PList.import(file)
  pref = new.tap do |p|
    p.name = root.delete('name')
    p.scope = root.delete('scope')
    p.settings = root.delete('settings')
    p.uuid = root.delete('uuid')
    p.bundle_uuid = root.delete('bundleUUID')
  end
  root.empty? or raise Error, "unexpected keys: #{root.keys.inspect}"
  base = File.basename(file, File.extname(file))
  pref.name ||= base
  pref.basename = base
  pref.settings.each_pair do |key, value|
    case key
    when /showInSymbolList|disableDefaultCompletion|spellChecking/
      pref.settings[key] = value.to_s == '1'
    when /completion(s|Command)/i
      # leave as is
    when /pattern|completion|folding(Start|Stop)Marker|foldingIndentedBlock(Start|Ignore)/i
      pref.settings[key] = Tools::RegexpWannabe.new(value)
    when /symbolTransformation|completionCommand/
      pref.settings[key] = value.strip.lines.map(&:strip).join("\n")
    end
  end

  pref
end
new() click to toggle source
# File lib/sublime_dsl/textmate/preference.rb, line 48
def initialize
  @settings = {}
end

Public Instance Methods

export(dir) click to toggle source
# File lib/sublime_dsl/textmate/preference.rb, line 100
def export(dir)
  file = "#{dir}/#{basename}.tmPreferences"
  PListWriter.new(self).export(file)
end
output_pairs(name, array, out) click to toggle source
# File lib/sublime_dsl/textmate/preference.rb, line 105
def output_pairs(name, array, out)
  simple, complex = array.partition { |o,c| o.length == 1 && c.length == 1 }
  unless simple.empty? && !complex.empty?
    simple = simple.map { |o,c| o+c }.join(' ')
    out.puts "  #{name.snake_case} #{simple.inspect_dq}"
  end
  return if complex.empty?
  name = name[0..-2]  # remove the 's'
  complex.each do |o,c|
    o =~ %r'^/(.+)/$' or
      raise Error, "unexpected value for a #{name}: #{o.inspect}"
    o = Tools::RegexpWannabe.new($1)
    c =~ %r'^/(.+)/$' or
      raise Error, "unexpected value for a #{name}: #{c.inspect}"
    c = Tools::RegexpWannabe.new($1)
    out.print o.fixme_comments
    out.print c.fixme_comments
    out.puts "  #{name.snake_case} #{o.inspect(true)}, #{c.inspect}"
  end
end
to_dsl() click to toggle source
# File lib/sublime_dsl/textmate/preference.rb, line 54
def to_dsl
  out = StringIO.new('', 'wb:utf-8')
  out.puts "# FIXME: no scope" unless scope
  args = "#{name.to_source} => #{scope ? scope.to_source : 'nil'}"
  args << dsl_file_arg
  out.puts "preferences #{args} do"
  settings.each_pair do |key, value|
    if value.is_a?(Array)
      case key
      when 'completions'
        out.puts '  completions %w('
        out.puts "#{value.join(' ').wrap.indent(4)}"
        out.puts '  )'
      when 'shellVariables'
        value.each do |h|
          out.puts "  shell_variable #{h['name'].inspect_sq}, #{h['value'].inspect_sq}"
        end
      when 'smartTypingPairs', 'highlightPairs'
        output_pairs key, value, out
      else
        raise Error, "unexpected Array setting: #{key}"
      end
    elsif value.is_a?(Hash)
      key == 'indentedSoftWrap' or raise Error, "unexpected Hash setting: #{key}"
      value.keys.sort == %w(format match) or
        raise Error, "unexpected argument for indentedSoftWrap: #{value.inspect}"
      m = Tools::RegexpWannabe.new(value['match'])
      out.print m.fixme_comments
      out.puts '  indented_soft_wrap match: ' << m.inspect << ', format: ' << value['format'].to_source
    elsif value.is_a?(Tools::RegexpWannabe)
      out.print value.fixme_comments
      out.puts '  ' << key.snake_case << ' ' << value.inspect(true)
    elsif key == 'comment'
      value.each_line { |l| out.puts '  # ' + l.rstrip }
    elsif key =~ /symbolTransformation|completionCommand/ && value.include?("\n")
      out.puts '  ' << key.snake_case << " <<-'TXT'\n#{value.indent(4)}\n  TXT"
    else
      out.puts '  ' << key.snake_case << ' ' << (value.is_a?(String) ? value.to_source(true) : value.inspect)
    end
  end
  out.puts "  uuid #{uuid.inspect_dq}" if uuid
  out.puts "  bundle_uuid #{bundle_uuid.inspect_dq}" if bundle_uuid
  out.puts 'end'
  out.string
end