Improved Auto Import for ActionScript 3 in TextMate

Feb 18, 2009

The ActionScript 3 bundles had the ability to auto import a class for a long time, but the functionality has been limited to the area of the document between the package and class declarations. There have been some interesting solutions to improve this workflow, but ultimately it needed a more elegant solution. If you grab the most recent version of the bundle you’ll now find that ⇧⌘I can

be invoked from a wider scope and will present you with a list of possible classes to import based on the current word. Select one and an import statement for the class will be generated and injected into the document (or a tooltip saying the class is already imported). I ought to stress that the importing is improved, but needs more work. The command is scoped to work within a public class, so any private classes hiding beneath your package block won’t benefit. You’re also likely, if you’re tidy, to want to organise the imports statements later on. But what is great about this is it let’s you skip a repetitive task and focus on higher level logic.


Comments:

Simon - Apr 3, 2009

@Marek - Sounds strange to me. You could try altering the command itself, via Bundle Editor > ActionScript 3 > Import Class. I’d try seeing if the same thing happens when output is set to ‘Create New Document’, or commenting out the last 5 lines (where the output is forked).


Tom - Mar 1, 2009

Thanks Simon, that’s certainly enough for me to get something working. Cheers.


Martin - Mar 4, 2009

Hey. I’m thrilled that you’ve come upon a better solution than my slightly half-assed one (which you linked above). Although I still love dragging classes from QuickSilver (just feels soo good) I’m most definitely going to give your updated bundle a go. Once again; Thanks a bunch for your great work on this bundle.


Cassio - Feb 5, 2009

I know this is not the best post to put this comment, but in the new AS3 bundle is there a way to make the autocomplete work automatically instead of having o press option+esc?


Junio Vitorino - Mar 6, 2009

I install the new bundle (from git) but, i can’t enjoy of the new features, i should make more some configuration for work?


Simon - Mar 0, 2009

@Cassio - I’m afraid there’s no easy way to do what you’re after. @Martin - Many thanks, your changes have been added :) @Junio - There isn’t anything you need to do configuration wise, if you could drop me a line with a specific problem I should be able to help. @Tom - The bundles designed to recognise source that’s contained within directories named src, source, test, lib and AS3Lib. These are used because they are (mostly) common conventions. In your case there’s nothing to recognise as the source directory, hence the paths you see. It is possible to add custom names to the dir list by modifying the env var TM_AS3_USUAL_SRC_DIRS in the bundles ‘Settings’ preference item. I’m not sure that it helps in this instance as it’s not something that generically solves the problem. A solution may be to detect what class is being compiled and use that as a base path, but this wouldn’t be a trivial thing to do for the bundle at the moment.


Simon - Apr 4, 2009

@Marek - It looks as though I’ve confused myself here. Tthe last 5 lines I mentioned before are actually only present in my working branch (here). You’re actually using this. The extra lines reposition the caret but are experimental and I was testing them for a while before adding it to the official branch. So the only thing that sounds wrong with what you’ve described is that the command output was set to ‘Create New Document’ - it shouldn’t be, try setting it to ‘Discard’.


Martin - Mar 0, 2009

Indeed a very nice improvement. I added two lines (in my own copy) to make the completion change the word you invoked the command from as well. word = ENV['TM_CURRENT_WORD'] originalline = ENV['TM_CURRENT_LINE'] #added this line doc = STDIN.read.strip and further down pre, imps, post = [], [], [] a = pre doc[originalline] = doc[originalline].gsub(word, import.split(".")[-1]); #added last line Now I’m just trying to figure out how to move the caret so it ends up after the line where the completion waas invoked (without resolving to macros again).


Enrique_IKNITE - May 6, 2009

THANKS FOR YOUR IMPRESIVE JOB!


Robert - Apr 3, 2009

Simon! Did I already told you that I love you ???!?!!! Well this works so good that I do! I love you! :))) thanks man cheers Robert


Marek - Apr 2, 2009

Hi Simon, Thanks for this one! I use the import all the time and it’s a real time saver. However when I use the shortcut  + Shift + i to import something it deletes all my textmate-bookmarks (+F2) I have set. Is this normal?


Marek - Apr 4, 2009

@Simon: I tried to change the shortcut with the same result as before. The output was already set to ‘Create New Document’ (should I expect something else?). I commented out these 2 lines: imps[safe] = “#{imps[safe]}\n#{indent}import #{import};\n” TextMate.exit_replace_document(pre.join("\n”)+”\n"+imps.join("\n”)+”\n"+post.join("\n”)) but then of course the import wasn’t functional (just a new empty window opened and my other window stayed the same: means the bookmarks were still there) I will try to reinstall textmate this weekend when I have time for that. Thanks for your help. -Marek


Tom - Mar 3, 2009

Hello, This is fantastic and (along with the rest of the bundle) making my AS3 experience infinitely more fun to code. However, I’ve noticed that when importing a class which isn’t from a default flash package (i.e. one which I’ve placed in a folder relative to the Document Class) the import statement defines the path to the class from a lower level than I need. i.e. I’m working in a folder /my/work/ so the document class is /my/work/Main.as and the classes I’m importing are in a folder /my/work/com/mydomain/bits/ and so when I import I want this: import com.mydomain.bit.Class; but I get import my.work.com.mydomain.bits.Class; It’s so close but it’s not right, I’m wondering whether there’s some ENV variable I have set wrong or something. Sadly I don’t understand the ruby etc enough to delve into this command more.


localToGlobal » Blog Archive » news review -> 8th week of 2009 - Feb 3, 2009

[…] > Simon Gregory » Improved Auto Import for ActionScript 3 in TextMate […]


Simon - Feb 5, 2009

@ Junio - it does… but I’m keeping quiet about it until I find the time to write a post and explain it’s limitations. Hope you’re enjoying it ;)


Junio Vitorino - Feb 5, 2009

Firstly, congratulations for your work. Simon the bundle situated at svn TextMate repository already has the features shown in *AS3 Autocompletion in TextMate*?


Marek - Apr 3, 2009

@simon thanks for your quick reply. The import works properly(at least I think so): the code ‘flashes’ for a blink of a second when I use the shortcut and the import statement is added below the last import statement. the bookmarks are gone and not functional anymore after that. But strangely one bookmarks is added to the last line of my code (which works) but I didn’t set it there. Maybe I will try to reinstall textmate? But sigh*.. I then have to customize everything from the beginning :(


Simon - Feb 4, 2009

@Aaron - Thanks for the heads up on the import issue. I’ll add it to my todo list. @Stephen - Allan moved the TextMate repository to http://svn.textmate.org/ so you’ll find the most recent bundle there.


Stephen - Feb 4, 2009

The last update to the bundle was Oct. 13, 2008 at 12:43. Is this the version that has the update? Thanks!


Aaron - Feb 3, 2009

Hi Simon, I really appreciate all the public work you’re doing improving TextMate. It’s nice to see a fellow AS3+OSX+TM user that hasn’t jumped ship to something like FDT. I’ve given the auto import command a run through and it works great. The only issue I’ve run into is when there is a class doc (/** */) containing imports it sometimes gets confused and adds the import line inside the comment block. (I know you stressed “improved”, but just wanted to let you know about this quirk incase you didn’t already.) I look forward to future improvements to auto complete and auto importing commands and am thankful for the work you’ve put in so far. Cheers.


Lars - Mar 5, 2009

Thanks Simon!


Josh - Apr 5, 2009

I’m having trouble locating the AS3 bundle… I can’t find it in the repository at http://svn.textmate.org/ or with the GetBundle TM bundle (ha!). What am I doing wrong?


Simon - Apr 5, 2009

@Josh - GetBundle has been superseded by GetBundles which can be found here, along with the ActionScript 3 bundle. If you prefer git then there’s a mirror of the bundle on Github here.


Josh - Apr 6, 2009

@Simon - Thanks for the heads up on GetBundles! It’s much better than the previous version! I was able to get your ActionScript 3 bundle to work great, code-hinting and all! The only other thing on my wish list is for syntax highlighting (and code hinting) outside of a package (like in the official ActionScript bundle that comes with TextMate). This is really good stuff, great job and thanks for all the work you’ve put into this awesome bundle!


Marek - Apr 6, 2009

@Simon …set to ‘Create New Document’ - it shouldn’t be, try setting it to ‘Discard’. I completly reinstalled textmate and without installing anything more I got the problem again. I then installed getBundles and updated to the latest revision from april the 10th 2009 . The Output of ‘Import Class’ was set to ‘Replace Document’ and input was set to ‘Entire Document’ by default. The problem still occurs. When I set the output to ‘Discard’ nothing happens when I invoke the shortcut. When I set the output to ‘Replace selected text’ (I know this is nonsense) the bookmarks are still there!


Mikkel - Jun 4, 2009

Simon, the auto import works great! Thanks! I can’t get the documentation search working though … I downloaded the flex3_documentation.zip, unzipped, renamed it docs and placed it in my flex_sdk_3 folder The only thing I see no matter what I search for is a link to livedocs (and the livedocs search returns nothing either)


Marek - Apr 2, 2009

when I use the auto import shortcut my textmate-bookmarks are erased. Is this normal?


Simon - Apr 2, 2009

@Marek - I’ve just tried to recreate what you’ve described and my bookmarks aren’t deleted. There’s nothing in the Bookmarks section of the TextMate manual to suggest that ⇧⌘I removes bookmarks. Does the import work properly? And are your bookmarks functional - even if they’re not visible?


Tom Morgan - Dec 5, 2010

Hello

I’ve found a problem with source_tools.rb which sometimes prevents the Import function working. It seems like sometimes when it’s searching through the project TextMate.each_text_file can only search within the files listed in TM_SELECTED_FILES.

So… (I’m not totally sure I understand but) if you’ve accidentally selected a file in the Project Drawer then TM_SELECTED_FILES only contains that file and no Classes (other than that one) will be searched. For me this usually results in a [Class not found] tooltip.

Anyway with a bit of Googling I managed to find that if I add ENV[‘TM_SELECTED_FILES’] = '’ just above where TextMate.each_text_file does it’s stuff, it wipes that value and forces Textmate to search the entire project.

It seems to work for me, and it definitely wasn’t before, does it make any sense? I don’t like hacking up your bundle so I thought I’d ask if this was right or if there was some reason I shouldn’t do it.

Cheers Tom