forked from textmate/ruby.tmbundle
/
Toggle ERb Tags.tmCommand
59 lines (53 loc) · 1.74 KB
/
Toggle ERb Tags.tmCommand
1
2
3
4
5
6
7
8
9
10
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>beforeRunningCommand</key>
<string>nop</string>
<key>command</key>
<string>#!/usr/bin/env ruby -w
require "#{ENV["TM_SUPPORT_PATH"]}/lib/escape"
require "enumerator"
TAGS = %w[<%= <%# <%- <%].freeze
# locate caret (Allan's code)
line = ENV['TM_LINE_NUMBER'].to_i - ENV['TM_INPUT_START_LINE'].to_i
col = ENV['TM_LINE_INDEX'].to_i
if ENV['TM_LINE_NUMBER'].to_i == ENV['TM_INPUT_START_LINE'].to_i
col -= ENV['TM_INPUT_START_LINE_INDEX'].to_i
end
# read input
input = $stdin.read
# snippetize output
lines = RUBY_VERSION < "1.9" ? input.to_a : input.lines.to_a
lines[line] = e_sn(lines[line][0...col]) + "${0}" + e_sn(lines[line][col..-1])
enum = RUBY_VERSION < "1.9" ? lines.enum_with_index :
lines.each.with_index
output = enum.inject(String.new) do |out, (l, i)|
i == line ? out + l : out + e_sn(l)
end
# swap ERb tags
result = output.sub(/\A<%[-#=]?/) { |match| TAGS[TAGS.index(match) - 1] }
if result[2] == ?-
result.sub!(/%>\Z/, "-%>")
else
result.sub!(/-%>\Z/, "%>")
end
print result</string>
<key>disableOutputAutoIndent</key>
<true/>
<key>fallbackInput</key>
<string>scope</string>
<key>input</key>
<string>selection</string>
<key>keyEquivalent</key>
<string>^></string>
<key>name</key>
<string>Toggle ERb Tags</string>
<key>output</key>
<string>insertAsSnippet</string>
<key>scope</key>
<string>source.ruby.embedded, source.ruby.rails.embedded, comment.block.erb, meta.erb</string>
<key>uuid</key>
<string>835FAAC6-5431-436C-998B-241F7226B99B</string>
</dict>
</plist>