Sublime Forum

Formatting and replacing line with Snippet

#1

I have the following snippet:

<snippet> <content><![CDATA[console.log( ${TM_CURRENT_LINE/;$/$1/m}$0);]]></content> <tabTrigger>conl</tabTrigger> <scope>text.html,source.js</scope> <description>console.log()</description> </snippet>

Which will wrap a line/selection with console.log(); replacing any ‘;’ characters in the line. This is what I want if I select the line, but If I run the snippet (via a key binding) on a line without selecting text, it will duplicate the text.

Ex:

// If I trigger the keybinding while the cursor is at the beginning of the following line, with no text selected:
exampleFunction()
// becomes...
console.log( exampleFunction()); exampleFunction();

I’d prefer it replace the whole line. Is this possible?

0 Likes

#2

@iota did you ever find an answer to this?

I’m trying to do something with creating an object method with a templated description comment block and common initialization stuff:

<snippet>
	<content><![CDATA[
/* ←— ${1/\./_/g} ——————————————→ *\
 | ${2:description}
 * ←————————————————————→ */
myNameSpace.${TM_CURRENT_LINE/(?<=[^ ]) /./}${1:myObject.myMethod} = function() {
	/* declaration/initialization of common variables etc */

	${3:/* body */}$SELECTION
}
]]></content>
	<tabTrigger>myTrigger</tabTrigger>
</snippet>

The idea is to be able to type

myModule myTrigger

Then use tab-complete to turn that into:

/* ←— myObject_myMethod ——————————————→ *\
 | ${2:description}
 * ←————————————————————→ */
myNameSpace.myModule.${1:myObject.myMethod} = function() {
	/* declaration/initialization of common variables etc */

	${3:/* body */}
}

Where typing just myTrigger on a line Would give the same thing but without the module.

When trying to create a module method, I instead get:

myModule /* ←— myObject_myMethod ——————————————→ *\
 | ${2:description}
 * ←————————————————————→ */
myNameSpace.myModule.${1:myObject.myMethod} = function() {
	/* declaration/initialization of common variables etc */

	${3:/* body */}
}
0 Likes