I'm trying to figure out a way to do auto-importing for Java in TextMate. I'm stuck at a couple of places and I would appreciate it if someone who knows more about TM than I do could chime in with some suggestions.
Essentially, what I'd like to be able to do is take a class name, select it, and hit Ctrl-Meta-Bucky-I and have TM add the appropriate import statement to the file. My naive implmentation would look something like:
1. Get the project CLASSPATH from a project-level config file 2. Build a list of the jar files on the CLASSPATH and grep through their contents to find matches. 3. If there's only one match, munge it appropriately and stick it in the import section of the buffer 4. If there's more than one match, pop up a dialog and let the user pick the appropriate fully-qualified class name, then stick in import section.
Does this seem to be a sensible way of approaching the problem? Is there a way of sticking the import statement where it belongs? More generally, is there any way to do arbitrary transformations on the contents of the buffer as if it were a java AST, instead of a stream of characters? I think that would probably be necessary to create commands to do source-level refactorings.
Thanks, Alex
On 14/09/2005, at 22.37, Alex Garrett wrote:
I'm trying to figure out a way to do auto-importing for Java in TextMate. I'm stuck at a couple of places and I would appreciate it if someone who knows more about TM than I do could chime in with some suggestions.
As for actually figuring out the import, I won't comment on that.
Let's just assume we have find_import.sh which takes one argument (class name) and returns the actual import statement.
Then to use it, you could do something like:
1) place caret on class name (no need to select it) 2) Automation / Start Macro Recording 3) Text / Filter Through Command a) set command to: find_import.sh "$TM_CURRENT_WORD" b) set input to: none (doesn't really matter) c) set output to: Place on clipboard 4) Move to begin of document (cmd arrow up) 5) Edit / Find / Find… a) set Find string to: ^(?=@import) or whatever you use in Java :) b) enable regular expression check box c) press next (should find first @import statement and close dialog) 6) Edit / Paste (i.e. the import line returned from the script) 7) Automation / Stop Macro Recording
Now you can save the macro and use later.
I made it a regexp search only to not have the find actually select the stuff found (by using look-ahead).
[...] is there any way to do arbitrary transformations on the contents of the buffer as if it were a java AST, instead of a stream of characters? I think that would probably be necessary to create commands to do source-level refactorings.
And would be pretty tied to Java ;) so no, that's not possible.
On 9/15/05, Allan Odgaard allan@macromates.com wrote:
Let's just assume we have find_import.sh which takes one argument (class name) and returns the actual import statement.
Then to use it, you could do something like:
- place caret on class name (no need to select it)
- Automation / Start Macro Recording
- Text / Filter Through Command a) set command to: find_import.sh "$TM_CURRENT_WORD" b) set input to: none (doesn't really matter) c) set output to: Place on clipboard
- Move to begin of document (cmd arrow up)
- Edit / Find / Find… a) set Find string to: ^(?=@import) or whatever you use in Java :) b) enable regular expression check box c) press next (should find first @import statement and close
dialog) 6) Edit / Paste (i.e. the import line returned from the script) 7) Automation / Stop Macro Recording
Now you can save the macro and use later.
Thanks, Allan, that's very helpful. I hadn't considered using a macro -- I was fixated on commands. Is there a way to put the caret back where you started?
I made it a regexp search only to not have the find actually select the stuff found (by using look-ahead).
[...] is there any way to do arbitrary transformations on the contents of the buffer as if it were a java AST, instead of a stream of characters? I think that would probably be necessary to create commands to do source-level refactorings.
And would be pretty tied to Java ;) so no, that's not possible.
I suppose I was thinking more along the lines of pulling in the contents of the buffer, antlr-izing it, doing whatever transformations, and writing it back. I agree, it doesn't make sense to have a backing AST in the way that a Java IDE like eclipse would.
BTW, I ask because I've started using TextMate for all my non-Java programming and I really love it. But, unfortunately, I'm a professional Java programmer and need a productive Java programming environment. Eclipse and Netbeans both run slowly on my powerbook, so I wind up doing Java in Windows and everything else in OS X. As I become more familiar with TextMate I plan on enhancing the Java bundle (which I'll happily contribute back to the TM community, if anyone's interested).
Let me ask you this: emacs supports a full-featured major mode for Java and XRefactory has an emacs refactoring library for emacs that does many of the things that Java IDEs do, but emacs is language agnostic in the same (or similar) way to TM. Is there any a priori reason why TM couldn't provide the same sort of functionality as a collection of macros, commands, snippets, etc?
Thanks again for your help, Alex
On 16.09.2005, at 15:36, Alex Garrett wrote:
BTW, I ask because I've started using TextMate for all my non-Java programming and I really love it. But, unfortunately, I'm a professional Java programmer and need a productive Java programming environment. Eclipse and Netbeans both run slowly on my powerbook, so I wind up doing Java in Windows and everything else in OS X. As I become more familiar with TextMate I plan on enhancing the Java bundle (which I'll happily contribute back to the TM community, if anyone's interested).
I definitely am. And I'd help out myself. I wrote a little import sorter a while ago in Ruby, so that might be of some use for others maybe ;)
Cheers, Mathias