Class: SublimeDSL::TextMate::Preference

Inherits:
Object
  • Object
show all
Includes:
CustomBaseName
Defined in:
lib/sublime_dsl/textmate/preference.rb

Overview

A named set of preferences.

Defined Under Namespace

Classes: DSLReader, PListWriter

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from CustomBaseName

#basename, #basename=, #custom_basename, #dsl_file_arg

Constructor Details

#initializePreference

Returns a new instance of Preference.



48
49
50
# File 'lib/sublime_dsl/textmate/preference.rb', line 48

def initialize
  @settings = {}
end

Instance Attribute Details

#bundle_uuidObject

Returns the value of attribute bundle_uuid.



46
47
48
# File 'lib/sublime_dsl/textmate/preference.rb', line 46

def bundle_uuid
  @bundle_uuid
end

#nameObject Also known as: to_s

Returns the value of attribute name.



42
43
44
# File 'lib/sublime_dsl/textmate/preference.rb', line 42

def name
  @name
end

#scopeObject

Returns the value of attribute scope.



43
44
45
# File 'lib/sublime_dsl/textmate/preference.rb', line 43

def scope
  @scope
end

#settingsObject

Returns the value of attribute settings.



44
45
46
# File 'lib/sublime_dsl/textmate/preference.rb', line 44

def settings
  @settings
end

#uuidObject

Returns the value of attribute uuid.



45
46
47
# File 'lib/sublime_dsl/textmate/preference.rb', line 45

def uuid
  @uuid
end

Class Method Details

.import(file) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# 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

Instance Method Details

#export(dir) ⇒ Object



100
101
102
103
# 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) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# 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_dslObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# 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