Skip to content

Instantly share code, notes, and snippets.

@chbrown
Created June 29, 2010 14:04
Show Gist options
  • Save chbrown/457255 to your computer and use it in GitHub Desktop.
Save chbrown/457255 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require ENV['TM_SUPPORT_PATH'] + '/lib/ui'
lines = STDIN.readlines()
selected_text = ENV.member?("TM_SELECTED_TEXT")
block_top = 1
block_bottom = lines.length
orig_search_term = TextMate::UI.request_string(:title => "Alignment term", :prompt => "Please enter the word to align by.")
if !orig_search_term.nil?
orig_search_term.strip!
if !orig_search_term.empty?
search_term = orig_search_term.gsub('{', '\{').gsub('}', '\}')
search_regex = Regexp.new('\s*' + search_term)
match_indices = []
block_top.upto(block_bottom) do |number|
line = lines[number - 1]
match_index = line =~ search_regex
match_indices << match_index if match_index
end
max_match = match_indices.max
block_top.upto(block_bottom) do |number|
match_index = lines[number - 1] =~ search_regex
if match_index then
lines[number - 1] = $` + ' '*((max_match-match_index)+1) + orig_search_term + $'
end
end
lines.each do |line|
puts line
end
end
end
#!/usr/bin/env ruby
lines = STDIN.readlines()
search_regex = /^\s+/
lines.each do |line|
line =~ search_regex
# $` and $' hold the text in the subject string to the left and to the right of the regex match.
# $& is the match, which here will be the indentation
puts $& + $'.gsub(/\s+/, ' ').gsub(/\s([,;)\]])/, '\1')
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment