[TxMt] Continuing block comments - my solution

Aanand Prasad aanand.prasad at gmail.com
Tue Jul 11 11:26:09 UTC 2006


Hi peeps. I was told this was the best place to put this. I've had a
hunt round the mailing list archives and dug up an elegant solution
here or there for Vim-style comment block behaviour, but nothing
obsessive-compulsive enough to quite satisfy me. So here's mine! I
hope you find it useful.

#!/usr/bin/env ruby

# TextMate command to continue block comment
# Preserves whitespace before and after asterisk

# Save:          Nothing
# Input:         Selected Text / Line
# Output:        Insert as Snippet
# Activation:    Key Equivalent
# Scope:         comment.block

line  = ENV['TM_CURRENT_LINE']
caret = ENV['TM_LINE_INDEX'].to_i

before = (caret > 0) ? line[0..caret-1] : ''
after  = line[caret..-1]

before =~ /^(\s*)(\/)?\*(\s*)/

if $&
	spc = $2 ? ' ' : ''
	printf before + "\n#{$1}#{spc}*#{$3}$0" + after
else
	before =~ /^(\s*)/
	printf before + "\n#{$1}$0" + after
end

This is also my first ever custom command, so it might look a bit
rough. Also, it doesn't insert the space after the asterisk by
default, but by its very indentation-preserving nature will happily
add it after you type one out on the first line.

I guess if there IS a way to do this with a snippet after all, I'm
going to look a bit silly.



More information about the textmate mailing list