public
Description: TextMate bundle - Amplified Mate Productivity
Homepage: http://code.leadmediapartners.com/tools/rubyamp
Clone URL: git://github.com/timcharper/rubyamp.git
rubyamp / Support / lib / ruby_tm_helpers.rb
100644 106 lines (82 sloc) 2.092 kb
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
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
99
100
101
102
103
104
105
106
# TextMate helpers
# Author: Tim Harper with Lead Media Partners.
# http://code.google.com/p/productivity-bundle/
 
 
def exit_discard
  exit 200;
end
 
def exit_replace_text
  exit 201;
end
 
def exit_replace_document
  exit 202;
end
 
def exit_insert_text
  exit 203;
end
 
def exit_insert_snippet
  exit 204;
end
 
def exit_show_html
  exit 205
end
 
def exit_show_tool_tip
  exit 206;
end
 
def exit_create_new_document
  exit 207;
end
 
def tm_open(file, options = {})
  line = options[:line]
  wait = options[:wait]
  if line.nil? && /^(.+):(\d+)$/.match(file)
    file = $1
    line = $2
  end
 
  unless /^\//.match(file)
    file = File.join((ENV['TM_PROJECT_DIRECTORY'] || Dir.pwd), file)
  end
 
  args = []
  args << "-w" if wait
  args << e_sh(file)
  args << "-l #{line}" if line
  %x{"#{ENV['TM_SUPPORT_PATH']}/bin/mate" #{args * " "}}
end
 
# this method only applies when the whole document contents are sent in
def tm_expanded_selection(options = {})
  text=ENV['TM_SELECTED_TEXT'].to_s
  return text unless text.empty?
 
  options = {
    :input_type => :doc,
    :input => nil,
    :forward => /\w*/i,
    :backward => /\w*/i,
    :line_number => ENV['TM_LINE_NUMBER'].to_i,
    :col_number => ENV['TM_COLUMN_NUMBER'].to_i
  }.merge(options)
 
  col_number, line_number = options[:col_number], options[:line_number]
 
  doc = options[:input] ||= $stdin.read
 
  line =
    case options[:input_type]
    when :doc then doc.split("\n")[line_number - 1].to_s
    when :line then doc
    else
      raise "Can't handle input_type #{options[:input_type]} for tm_expanded_selection"
    end
 
  last_part = line[ (col_number - 1)..-1]
  first_part = col_number == 1 ? "" : line[ 0..col_number - 2]
 
  last_part.gsub!(/^(#{options[:forward]}){0,1}.*$/i) { $1 }
 
  first_part.reverse!
  first_part.gsub!(/^(#{options[:backward]}){0,1}.*$/i) { $1 }
  first_part.reverse!
  first_part + last_part
end
 
 
 
module Enumerable
  # TODO remove when 1.9 supports natively
  def map_with_index
    result = []
    each_with_index do |item, idx|
      result << yield(item, idx)
    end
    result
  end
end