Skip to content

Instantly share code, notes, and snippets.

@jlecour
Created August 29, 2012 16:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jlecour/3515210 to your computer and use it in GitHub Desktop.
Save jlecour/3515210 to your computer and use it in GitHub Desktop.
Textmate variable extraction from source code

In a situation like this (pseudo code)

assert a_method(with, many, arguments)

I'd like to select the a_method(...) part, launch a macro/command/whatever and get that :

variable = a_method(with, many, arguments)
assert variable

The tricky part is to have variable as a selected default value that I could type over to change the current variable name on both lines.

I've seen Jim Weirich (within Emacs) and Gary Bernhardt (within Vim) do this many times, but I can't figure how to do this in Textmate.

Thanks for any help on this.


Thanks to James Edward Gray II, I've made some progress :

here is my command :

#!/usr/bin/env ruby -wKU

require File.join(ENV["TM_SUPPORT_PATH"], "lib/exit_codes.rb")
require File.join(ENV["TM_SUPPORT_PATH"], "lib/escape.rb")

if ENV['TM_SELECTED_TEXT'] != nil
  col = ENV['TM_LINE_INDEX'].to_i - ENV['TM_SELECTED_TEXT'].length
  TextMate.exit_insert_snippet("${1:variable} = " + e_snp(ENV['TM_SELECTED_TEXT']) + "\n" + ENV['TM_CURRENT_LINE'][0...col] + "${1/\s.*//}")
end

but from this :

assert a_method(with, many, arguments)

I get this

assert variable = a_method(with, many, arguments)
assert variable

The fact is that I don't know how to replace the entire line instead of just inserting some the snippet.

@JEG2
Copy link

JEG2 commented Aug 29, 2012

The more I think about it, the more I think you do need to "Replace Document" as the command's output.

@jlecour
Copy link
Author

jlecour commented Aug 30, 2012

@JEG2 I think you're right, but I would need to insert a snippet back and I don't know how to do this within a macro, preserving the other parts of the line of code (including indentation, …)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment