snippetem 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/README ADDED
@@ -0,0 +1,38 @@
1
+ = Snippitem
2
+ A DSL for constructing snipmate.vim snippets
3
+
4
+ = Install
5
+
6
+ gem install snippetem
7
+
8
+ = Usage
9
+
10
+ Create a ruby file in your ~/.vim/snippets directory. Fill it with something like the following:
11
+
12
+ # snippet_definitions.rb
13
+ require 'rubygems'
14
+ require 'snippitem'
15
+
16
+ snippets do
17
+ filetype 'ruby' do
18
+ block('each', :yields => %w(x))
19
+ block('ewi', :yields => %w(x index), :function => 'each_with_index')
20
+ block('sel', :yields => %w(x), :function => 'select', :style => :curly)
21
+ end
22
+
23
+ filetype 'rspec' do
24
+ block('desc', :params => [String], :function => 'describe')
25
+ block('it', :params => [String])
26
+ block('context', :params => [String])
27
+ block('should', :params => [String])
28
+ end
29
+ end
30
+
31
+ Run the script. It outputs each snippet it creates. Since these are generated files, you
32
+ don't want them in source control.
33
+
34
+ $ ruby snippet_definitions.rb > .gitignore
35
+
36
+ This will generate snippets! They'll be placed in a directory corresponding to the filetype.
37
+ It will clobber any existing snippets with the same name, but that shouldn't matter because
38
+ you've got them in source control, right?
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ begin
2
+ require 'jeweler'
3
+ Jeweler::Tasks.new do |gemspec|
4
+ gemspec.name = "snippetem"
5
+ gemspec.summary = "A DSL for creating snipmate.vim snippets"
6
+ gemspec.email = "contact@rhnh.net"
7
+ gemspec.homepage = "http://github.com/xaviershay/snippitem"
8
+ gemspec.authors = ["Xavier Shay"]
9
+ end
10
+ rescue LoadError
11
+ puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
12
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
@@ -0,0 +1,77 @@
1
+ require 'fileutils'
2
+
3
+ class SnippetBuilder
4
+ class FileTypeBuilder < Struct.new(:type)
5
+ def block(*args)
6
+ (@blocks ||= []) << ::SnippetBuilder::BlockSnippetBuilder.new(*args)
7
+ end
8
+
9
+ def process!
10
+ FileUtils.mkdir_p(type)
11
+
12
+ @blocks.each do |block|
13
+ file = File.join(type, block.name + '.snippet')
14
+ File.open(file, 'w') do |f|
15
+ f.write(block.to_s)
16
+ end
17
+ puts file
18
+ end
19
+ end
20
+ end
21
+
22
+ class BlockSnippetBuilder
23
+ attr_reader :name
24
+
25
+ def initialize(name, opts = {})
26
+ @name = name
27
+ @function = opts[:function] || name
28
+ @opts = opts
29
+ @var_index = 0
30
+ end
31
+
32
+ def to_s
33
+ if @opts[:style] == :curly
34
+ "#{@function} #{params}{#{yields.strip} ${#{next_var}} }"
35
+ else
36
+ <<-EOS
37
+ #{@function} #{params}do#{yields}
38
+ \t${#{next_var}}
39
+ end
40
+ EOS
41
+ end
42
+ end
43
+
44
+ def params
45
+ if @opts[:params]
46
+ @opts[:params].map do |x|
47
+ if x == String
48
+ "'${#{next_var}}'"
49
+ else
50
+ "${#{next_var}}"
51
+ end
52
+ end.join(', ') + ' '
53
+ end
54
+ end
55
+
56
+ def yields
57
+ if @opts[:yields]
58
+ params = @opts[:yields].map do |x|
59
+ "${#{next_var}:#{x}}"
60
+ end
61
+ " |%s|" % params.join(', ')
62
+ end
63
+ end
64
+
65
+ def next_var
66
+ @var_index += 1
67
+ end
68
+ end
69
+
70
+ def filetype(type, &block)
71
+ (@filetypes ||= []) << FileTypeBuilder.new(type).tap {|x| x.instance_eval(&block) }
72
+ end
73
+
74
+ def process!
75
+ @filetypes.each(&:process!)
76
+ end
77
+ end
data/lib/snippetem.rb ADDED
@@ -0,0 +1,11 @@
1
+ require 'snippet_builder'
2
+
3
+ def snippets(&block)
4
+ $snippet_builder = SnippetBuilder.new
5
+ $snippet_builder.instance_eval(&block)
6
+ $snippet_builder
7
+ end
8
+
9
+ at_exit do
10
+ $snippet_builder.process!
11
+ end
@@ -0,0 +1,69 @@
1
+ require 'spec'
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require 'snippet_builder'
4
+
5
+ BSB = SnippetBuilder::BlockSnippetBuilder
6
+ describe BSB do
7
+ describe 'block functions' do
8
+ it 'supports function different from the snippet name' do
9
+ BSB.new('e', :function => 'e').to_s.should == <<-EOS
10
+ e do
11
+ \t${1}
12
+ end
13
+ EOS
14
+ end
15
+
16
+ it 'supports no params, no yield' do
17
+ BSB.new('each').to_s.should == <<-EOS
18
+ each do
19
+ \t${1}
20
+ end
21
+ EOS
22
+ end
23
+
24
+ it 'supports string params' do
25
+ BSB.new('describe', :params => [String]).to_s.should == <<-EOS
26
+ describe '${1}' do
27
+ \t${2}
28
+ end
29
+ EOS
30
+ end
31
+
32
+ it 'supports unknown params' do
33
+ BSB.new('describe', :params => [Integer]).to_s.should == <<-EOS
34
+ describe ${1} do
35
+ \t${2}
36
+ end
37
+ EOS
38
+ end
39
+
40
+ it 'supports mixed params' do
41
+ BSB.new('describe', :params => [String, Integer]).to_s.should == <<-EOS
42
+ describe '${1}', ${2} do
43
+ \t${3}
44
+ end
45
+ EOS
46
+ end
47
+
48
+ it 'supports yielding with default parameter name, one' do
49
+ BSB.new('each', :yields => %w(x)).to_s.should == <<-EOS
50
+ each do |${1:x}|
51
+ \t${2}
52
+ end
53
+ EOS
54
+ end
55
+
56
+ it 'supports yielding with default parameter name, multiple' do
57
+ BSB.new('each_with_index', :yields => %w(x index)).to_s.should == <<-EOS
58
+ each_with_index do |${1:x}, ${2:index}|
59
+ \t${3}
60
+ end
61
+ EOS
62
+ end
63
+
64
+ it 'supports curly brackets' do
65
+ BSB.new('each', :yields => %w(x), :style => :curly).to_s.should ==
66
+ "each {|${1:x}| ${2} }"
67
+ end
68
+ end
69
+ end
metadata ADDED
@@ -0,0 +1,60 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: snippetem
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Xavier Shay
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-09-25 00:00:00 +03:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email: contact@rhnh.net
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README
24
+ files:
25
+ - README
26
+ - Rakefile
27
+ - VERSION
28
+ - lib/snippet_builder.rb
29
+ - lib/snippetem.rb
30
+ - spec/snippetem_spec.rb
31
+ has_rdoc: true
32
+ homepage: http://github.com/xaviershay/snippitem
33
+ licenses: []
34
+
35
+ post_install_message:
36
+ rdoc_options:
37
+ - --charset=UTF-8
38
+ require_paths:
39
+ - lib
40
+ required_ruby_version: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: "0"
45
+ version:
46
+ required_rubygems_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: "0"
51
+ version:
52
+ requirements: []
53
+
54
+ rubyforge_project:
55
+ rubygems_version: 1.3.4
56
+ signing_key:
57
+ specification_version: 3
58
+ summary: A DSL for creating snipmate.vim snippets
59
+ test_files:
60
+ - spec/snippetem_spec.rb