Class: SublimeDSL::TextMate::Snippet
- Inherits:
-
Object
- Object
- SublimeDSL::TextMate::Snippet
- Includes:
- CustomBaseName, SublimeDSL::Tools::StableInspect
- Defined in:
- lib/sublime_dsl/textmate/snippet.rb
Defined Under Namespace
Classes: DSLReader, DSLWriter, Importer, PListReader, XMLReader
Instance Attribute Summary collapse
-
#bundle_uuid ⇒ Object
TextMate only.
-
#content ⇒ Object
Returns the value of attribute content.
- #file_format ⇒ Object
-
#key_equivalent ⇒ Object
TextMate only.
-
#name ⇒ Object
(also: #to_s)
Returns the value of attribute name.
-
#scope ⇒ Object
Returns the value of attribute scope.
-
#semantic_class ⇒ Object
TextMate only.
-
#tab_trigger ⇒ Object
Returns the value of attribute tab_trigger.
-
#uuid ⇒ Object
TextMate only.
-
#warnings ⇒ Object
readonly
Returns the value of attribute warnings.
Class Method Summary collapse
-
.import(file) ⇒ Object
Returns a Snippet read from
file. -
.to_snake_map ⇒ Object
Hash { attributeName => attribute_name }.
Instance Method Summary collapse
-
#c(content) ⇒ Object
HACK: return <![#content">CDATA]>, except if
contentis itself <![CDATA[.… - #complete! ⇒ Object
- #export(dir) ⇒ Object
- #h(text) ⇒ Object
-
#initialize ⇒ Snippet
constructor
A new instance of Snippet.
- #to_dsl(default_scope) ⇒ Object
- #to_plist ⇒ Object
- #to_xml ⇒ Object
Methods included from SublimeDSL::Tools::StableInspect
Methods included from CustomBaseName
#basename, #basename=, #custom_basename, #dsl_file_arg
Constructor Details
#initialize ⇒ Snippet
Returns a new instance of Snippet.
30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/sublime_dsl/textmate/snippet.rb', line 30 def initialize @name = nil @content = nil @tab_trigger = nil @scope = nil @key_equivalent = nil @semantic_class = nil @uuid = nil @bundle_uuid = nil @file_format = nil @warnings = [] end |
Instance Attribute Details
#bundle_uuid ⇒ Object
TextMate only
26 27 28 |
# File 'lib/sublime_dsl/textmate/snippet.rb', line 26 def bundle_uuid @bundle_uuid end |
#content ⇒ Object
Returns the value of attribute content.
25 26 27 |
# File 'lib/sublime_dsl/textmate/snippet.rb', line 25 def content @content end |
#file_format ⇒ Object
45 46 47 |
# File 'lib/sublime_dsl/textmate/snippet.rb', line 45 def file_format @file_format || :sublime_text end |
#key_equivalent ⇒ Object
TextMate only
26 27 28 |
# File 'lib/sublime_dsl/textmate/snippet.rb', line 26 def key_equivalent @key_equivalent end |
#name ⇒ Object Also known as: to_s
Returns the value of attribute name.
25 26 27 |
# File 'lib/sublime_dsl/textmate/snippet.rb', line 25 def name @name end |
#scope ⇒ Object
Returns the value of attribute scope.
25 26 27 |
# File 'lib/sublime_dsl/textmate/snippet.rb', line 25 def scope @scope end |
#semantic_class ⇒ Object
TextMate only
26 27 28 |
# File 'lib/sublime_dsl/textmate/snippet.rb', line 26 def semantic_class @semantic_class end |
#tab_trigger ⇒ Object
Returns the value of attribute tab_trigger.
25 26 27 |
# File 'lib/sublime_dsl/textmate/snippet.rb', line 25 def tab_trigger @tab_trigger end |
#uuid ⇒ Object
TextMate only
26 27 28 |
# File 'lib/sublime_dsl/textmate/snippet.rb', line 26 def uuid @uuid end |
#warnings ⇒ Object (readonly)
Returns the value of attribute warnings.
28 29 30 |
# File 'lib/sublime_dsl/textmate/snippet.rb', line 28 def warnings @warnings end |
Class Method Details
.import(file) ⇒ Object
Returns a Snippet read from file.
9 10 11 |
# File 'lib/sublime_dsl/textmate/snippet.rb', line 9 def self.import(file) Importer.for(file).snippet end |
.to_snake_map ⇒ Object
Hash { attributeName => attribute_name }
14 15 16 17 18 19 20 21 |
# File 'lib/sublime_dsl/textmate/snippet.rb', line 14 def self.to_snake_map @to_snake_map ||= Hash[ %w( name content scope tabTrigger keyEquivalent semanticClass uuid bundleUUID ).map { |a| [a, a.snake_case] } ] end |
Instance Method Details
#c(content) ⇒ Object
HACK: return <![#content">CDATA]>, except if content is itself <![CDATA[…
152 153 154 |
# File 'lib/sublime_dsl/textmate/snippet.rb', line 152 def c(content) content =~ /<!\[CDATA\[/ ? h(content) : "<![CDATA[#{content}]]>" end |
#complete! ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/sublime_dsl/textmate/snippet.rb', line 49 def complete! # assign name from the base name if not given unless name if @basename warnings << 'name assigned from the file name' @name = basename @basename = nil else raise Error, 'the snippet name is required' end end tab_trigger || key_equivalent or warnings << 'no tab trigger nor key equivalent' warnings.each { |w| warn "snippet #{name}: #{w}" } # remove spaces on empty lines @content.gsub!(/^[ \t]+$/, '') end |
#export(dir) ⇒ Object
116 117 118 119 120 121 122 123 124 125 |
# File 'lib/sublime_dsl/textmate/snippet.rb', line 116 def export(dir) if file_format == :textmate file = "#{dir}/#{basename}.tmSnippet" content = to_plist else file = "#{dir}/#{basename}.sublime-snippet" content = to_xml end File.open(file, 'wb:utf-8') { |f| f.write content } end |
#h(text) ⇒ Object
147 148 149 |
# File 'lib/sublime_dsl/textmate/snippet.rb', line 147 def h(text) text.html_escape(false) end |
#to_dsl(default_scope) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/sublime_dsl/textmate/snippet.rb', line 76 def to_dsl(default_scope) = '' case scope when NilClass warn "scope missing for snippet #{name.to_source}, set to '#{default_scope}'" warnings << 'missing scope, will be default_scope' when default_scope else << ", scope: #{scope.to_source}" end << ", semantic_class: #{semantic_class.to_source}" if semantic_class << ", uuid: #{uuid.to_source}" if uuid << ", bundle_uuid: #{bundle_uuid.to_source}" if bundle_uuid << dsl_file_arg dsl = warnings.map { |w| " # FIXME: #{w}\n" }.join if tab_trigger start = "tab #{tab_trigger.to_source}" << ", key_equivalent: #{key_equivalent.inspect_dq}" if key_equivalent elsif key_equivalent start = "key #{key_equivalent.inspect_dq}" else start = 'key nil' end if content =~ /[ \t]$/ dsl << " #{start}, #{name.to_source}, #{content.inspect_dq}#{}\n" else # TODO: use the ruby heredoc mnemonic from default_scope (CPP, RUBY, XML, etc.) dsl << " #{start}, #{name.to_s.to_source}, <<-'TXT'#{}\n" dsl << content << "\nTXT\n" end dsl end |
#to_plist ⇒ Object
138 139 140 141 142 143 144 145 |
# File 'lib/sublime_dsl/textmate/snippet.rb', line 138 def to_plist h = {} Snippet.to_snake_map.each_pair do |camel, snake| value = send(snake) h[camel] = value if value end PList.dump(h) end |
#to_xml ⇒ Object
127 128 129 130 131 132 133 134 135 136 |
# File 'lib/sublime_dsl/textmate/snippet.rb', line 127 def to_xml <<-XML <snippet> \t<content>#{c(content)}</content> \t<tabTrigger>#{h(tab_trigger)}</tabTrigger> \t<scope>#{h(scope)}</scope> \t<description>#{h(name)}</description> </snippet> XML end |