Created
May 3, 2011 22:04
-
-
Save nolanw/954361 to your computer and use it in GitHub Desktop.
TextMate bundle command to nicely wrap contiguous single-line comments
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
def wrap text, sigil, indent | |
text = text.map do |line| | |
line.match(/^\s*\S (.*)$/)[1] | |
end.join ' ' | |
lines = [] | |
while text and not text.empty? | |
cut = [text.length, 78 - indent].min | |
if cut < text.length and text[cut, 1] != ' ' | |
cut.downto 1 do |i| | |
if text[i - 1, 1] == ' ' | |
cut = i | |
break | |
end | |
end | |
end | |
lines << text.slice!(0, cut).strip | |
end | |
lines.map {|line| "#{' ' * indent}#{sigil} #{line}"} | |
end | |
doc_lines = $stdin.read.split "\n" | |
start = ENV['TM_LINE_NUMBER'].to_i - 1 | |
if start > 0 | |
fin = start | |
match = doc_lines[start].match /^(\s*)(\S)/ | |
sigil = match[2] | |
indent = match[1].length | |
while doc_lines[start - 1].match /^\s*#{sigil}/ | |
start -= 1 | |
end | |
while doc_lines[fin + 1].match /^\s*#{sigil}/ | |
fin += 1 | |
end | |
doc_lines[start..fin] = wrap doc_lines[start..fin], sigil, indent | |
end | |
print doc_lines.join("\n") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment