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.

Be Sociable, Share!

29 Responses to “Improved Auto Import for ActionScript 3 in TextMate”


  1. Aaron Says:

    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.


  2. Stephen Says:

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

    Thanks!


  3. Simon Says:

    @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.


  4. Junio Vitorino Says:

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


  5. Simon Says:

    @ 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 ;)


  6. localToGlobal » Blog Archive » news review -> 8th week of 2009 Says:

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


  7. Cassio Says:

    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?


  8. Martin Says:

    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.


  9. Lars Says:

    Thanks Simon!


  10. Martin Says:

    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['TMCURRENTWORD'] originalline = ENV['TMCURRENTLINE'] #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).


  11. Junio Vitorino Says:

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


  12. Tom Says:

    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.


  13. Simon Says:

    @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 TMAS3USUALSRCDIRS 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.


  14. Tom Says:

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


  15. Marek Says:

    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?


  16. Marek Says:

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


  17. Simon Says:

    @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?


  18. Marek Says:

    @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 :(


  19. Simon Says:

    @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).


  20. Marek Says:

    @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.exitreplacedocument(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


  21. Simon Says:

    @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’.


  22. Josh Says:

    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?


  23. Simon Says:

    @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.


  24. Josh Says:

    @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!


  25. Marek Says:

    @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!


  26. Robert Says:

    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


  27. Enrique_IKNITE Says:

    THANKS FOR YOUR IMPRESIVE JOB!


  28. Mikkel Says:

    Simon, the auto import works great! Thanks!

    I can’t get the documentation search working though … I downloaded the flex3documentation.zip, unzipped, renamed it docs and placed it in my flexsdk_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)


  29. Tom Morgan Says:

    Hello

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

    So… (I’m not totally sure I understand but) if you’ve accidentally selected a file in the Project Drawer then TMSELECTEDFILES 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['TMSELECTEDFILES'] = ” just above where TextMate.eachtextfile 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

Fork me on GitHub