Inspired by the customization screencast [1], I decided to write a reflow command for us Java users. It's designed for reflowing JavaDoc comments. Based heavily on Allan's original code, it works well except for one problem: The reflowed text is never indented, even though I've set the output to "Insert as Snippet." Anyone know how to fix this?
Here's the command:
#!/usr/bin/env ruby # Grab comment text txt = STDIN.read # Remove beginning and ending slashes txt = txt.gsub(%r{\A[ \t]*/|\s**/\s*\z}, '') # Remove leading bullets txt = txt.gsub(/^[ \t]**+[ \t]*/, '') # Escape single quotes txt = txt.gsub(/'/, "'\\''") # Use fmt command to reflow text txt = %x{ fmt <<< '#{txt}' }.chomp # Put leading bullets back in txt = txt.to_a.join(' * ') # Escape special snippet characters txt = txt.gsub(/[$`\]/, '\\\0') # Put cursor marker on last line txt = txt.sub(/\n?\z/, '$0\0') # Put beginning and ending slashes back in txt = "/**" + txt + "\n */\n" print txt
I've set the input to "Selected Text or Scope" and the scope to "comment.block.documentation.java".
Thanks,
Trevor
[1] http://macromates.com/blog/archives/2006/04/12/customization- screencast/
On 13. Apr 2007, at 02:09, Trevor Harmon wrote:
Inspired by the customization screencast [1], I decided to write a reflow command for us Java users. It's designed for reflowing JavaDoc comments. Based heavily on Allan's original code, it works well except for one problem: The reflowed text is never indented, even though I've set the output to "Insert as Snippet." Anyone know how to fix this?
Looking at the Java grammar, it will scope all whitespace in front of the ‘/**’ marker as comment.block.documentation (when the line is otherwise empty).
Try press ⌃⌥B in a doc-comment to “Select Scope” to see what your command is given as input. If you remove this block you will see that the caret is at column zero, and thus, your scripts output will be indented to column zero (i.e. no indent).
So either we need to “fix” the Java grammar to not make the whitespace in front of ‘/**’ comment.block.documentation [1] or your script just needs to grab the indent from the first line and apply that to the result, before returning it to TextMate.
[1] The reason this is done is so that setting the background color for comment.block will look nicer even for indented comments -- we have long wanted to introduce a special scope though for leading/ trailing whitespace for block constructs, which themes would use, rather than have the scope of the block extend to the whitespace (which is sometimes resulting in incorrect behavior)