0

Just like I can pull in the currently selected text into a snippet using the $TM_SELECTED_TEXT, is there any way I can retrieve text from my code like the method name or class name containing the current caret position?

This'd be super useful for quickly creating useful log messages.

So, if I had some, say, JavaScript code (with | representing the cursor/caret location):

function doSomething() {
   somethingElse();
   |
}

I'd love to be able to spit out doSomething via a snippet.

Something like,

 console.log($TM_CURRENT_METHOD_NAME + "() $1");

Is something like this possible?

Scott Thiessen
  • 873
  • 7
  • 20

1 Answers1

0

In my fork of the Ember bundle, I put this script in Support/bin/camelize_filename:

#!/usr/bin/env ruby
c=%w{config helpers mixins controllers models routes templates views}.join('|')
r = %r{.*/(?:#{c})/(.*)\.js}
puts ENV['TM_FILEPATH'].sub(%r{.*/(?:#{c})/(.*)\.js},'\1').
  gsub(/(?:_|(\/)|^)([a-z\d]*)/){|s| "#{$1}#{$2.capitalize}" }.gsub('/','')

Then I use it in snippets thusly:

console.groupCollapsed("`camelize_filename`#model");

You could adapt this for other frameworks by adapting the regex to match which directory segments belong in a class's namespace and which do not, and changing gsub('/','') if the language you are interested uses a namespace delimiter like '::'.

Michael Johnston
  • 5,298
  • 1
  • 29
  • 37
  • Thanks for the response. Unfortunately, the code and your description is a bit over my head here. Is this simply doing some regex on the filename? Is any piece of this getting me closer to extracting the name of the current method and outputting it via a snippet? – Scott Thiessen Dec 09 '13 at 00:05