Mode appropriate comments in templates
Submitted by Wednesday, 21 July, 2010 - 13:23
on
While messing around with the Template plugin I decided it was pointless to have different templates to take into account differences in comment characters for
different languages, so I poked around to see if I could find a way around this and came up with this.
--
## template = Get mode comment char
#*
Get the line comment character for the current buffer's mode. If the mode
doesn't define a line comment character, complain and ask what prefix
string to use instead. $comment will contain the results either way.
*#
#beanshell (false)
context.put("caret", view.getTextArea().getCaretPosition());
context.put("comment", buffer.getContextSensitiveProperty(caret,"lineComment"));
#end
#if ( $comment )
#else
#prompt ( "Mode doesn't have a line comment. String prefix to use?" $comment )
#end
--
Put that in a file called "getcommentchar.vm" in your templates directory, then in other templates you can do:
--
#parse ( "getcommentchar.vm" )
${comment} Lorem ipsum
${comment} dolor sit amet, consectetur adipiscing
--
The inserted text will be preceded by whatever passes for line comments in the buffer's language, or the prefix text you gave.
Enjoy.
different languages, so I poked around to see if I could find a way around this and came up with this.
--
## template = Get mode comment char
#*
Get the line comment character for the current buffer's mode. If the mode
doesn't define a line comment character, complain and ask what prefix
string to use instead. $comment will contain the results either way.
*#
#beanshell (false)
context.put("caret", view.getTextArea().getCaretPosition());
context.put("comment", buffer.getContextSensitiveProperty(caret,"lineComment"));
#end
#if ( $comment )
#else
#prompt ( "Mode doesn't have a line comment. String prefix to use?" $comment )
#end
--
Put that in a file called "getcommentchar.vm" in your templates directory, then in other templates you can do:
--
#parse ( "getcommentchar.vm" )
${comment} Lorem ipsum
${comment} dolor sit amet, consectetur adipiscing
--
The inserted text will be preceded by whatever passes for line comments in the buffer's language, or the prefix text you gave.
Enjoy.