Live Templates in JavaScript/ActionScript/Flex

Alexander Doroshko

Writing JavaScript/ActionScript/Flex code becomes easier with IntelliJ IDEA 9.0.2. New set of live templates (see Settings dialog (Ctrl+Alt+S), Live Templates, JavaScript/ActionScript group), allows to avoid tedious typing when you need to define a function, variable or constant, to loop over an Array, Vector or anything else. Luckily, some abbreviations are the same as with Java: iter, itar, ritar. Here’s the list of currently available live templates:

Abbreviation Description
iter Iterate (for each..in)
itin Iterate (for..in)
itar Iterate elements of array
ritar Iterate elements of array in reverse order
pf public function
prf private function
psf public static function
prsf private static function
pv public var
prv private var
psv public static var
prsv private static var
pc public const
prc private const
psc public static const
prsc private static const

To use a live template, just type its abbreviation anywhere in your code and press Tab. Here are some examples:

Ctrl+J shortcut shows you all live templates available for current context.

Also you may add your own live templates at Settings (Ctrl+Alt+S), Live Templates page.

Comments below can no longer be edited.

17 Responses to Live Templates in JavaScript/ActionScript/Flex

  1. nick99 says:

    February 5, 2010

    Are you planning to provide an ability to generate class setters that use method chaining technique?

    http://en.wikipedia.org/wiki/Method_chaining

    Thanks!

  2. Alexander Doroshko says:

    February 5, 2010

    nick99, I’m afraid I don’t understand your question in context of Flex iterations live templates. Flex setters return void by definition.
    You may discuss your question at our forum: http://www.jetbrains.net/devnet/community/idea/ideacommunity

  3. Sindisil says:

    February 24, 2010

    Because of the lack of block scope in ActionScript 3.0, it’d be nice if the iterator abbreviations put variable declaration at the top of the containing function, since that’s where they will first come into scope.

    Otherwise, the second use in a function will cause compile errors.

    Another option – and perhaps it’s in there – I haven’t actually started eval of IDEA for ActionScript yet – would be to have a refactor option to hoist a variable declaration.

  4. Alexander Doroshko says:

    February 26, 2010

    Sindisil,
    the second local variable declaration causes only compiler warning, but compilation succeeds. It is very easy to manually tune live templates to better match your code style: see Settings (Ctrl+Alt+S), Live Templates node, JavaScript/ActionScript group. Though the logic of placing variable definition as the first statement in current function and only if not yet defined is not achievable by live templates paradigm.

    Ctrl+Shift+Up/Down arrows move statements up and down the code.

  5. Joa Ebert says:

    February 28, 2010

    Are you starting the support for live templates that comes close to Java? Like finding the variable for a certain type etc.?

  6. Alexander Doroshko says:

    March 1, 2010

    We’ve added a number of live templates like
    pv: public var
    prv: private var
    psv: public static var
    prsv: private static var
    and similar for functions and constants.
    Please open YouTrack request for more templates that you would like to have.

  7. Kai says:

    May 31, 2010

    The live templates can’t be modified. Every time I modify them the modification exists until I restart the IDE.
    Can you acknowledge this?

  8. Anne says:

    November 6, 2011

    On the mac/pc where can I find the complete list of all live templates and expansions? It is too tedious to navigate to the help and I can’t learn them by doing that. Thank you.

  9. Alexander Doroshko says:

    November 7, 2011

    Anne, all templates available in context are shown by Ctrl+J (Meta+J on Mac). To see all templates, configure them and create own ones see File | Settings | Live Templates.

  10. Pete says:

    December 1, 2011

    Live templates seem quite buggy in 11 Beta. +1 for Joa’s suggestion

  11. Alexander Doroshko says:

    December 1, 2011

    Pete, what are bugs? I don’t see any.
    Requests for more live templates submitted to http://youtrack.jetbrains.net have good chances to be implemented.

  12. Pete says:

    December 1, 2011

    I have a template that just isn’t recognised at all (all contexts ticked) whilst I have another that works.

  13. Pete says:

    December 1, 2011

    Alexander CMD+J does show it and it’s selectable from there. But typing it inline doesn’t work. It’s called “tSilence” if that helps at all.

    Thanks
    Pete

  14. Alexander Doroshko says:

    December 1, 2011

    May be abbreviation conflict. Try to change abbreviation of your custom template. Is it listed in Ctrl+J popup in any context?

  15. Alexander Doroshko says:

    December 1, 2011

    Please make sure that you haven’t occasionally changed ‘Expand with’ key in respective combo in right bottom corner of Live Templates settings

  16. Pete says:

    December 2, 2011

    Changing abbreviation doesn’t help. Expand with is the same as the others that do work “default (Tab)”. I have a couple that don’t work inline but show up under CMD+J.

  17. Alexander Doroshko says:

    December 2, 2011

    @Pete Can you please open YouTrack issue and attach zipped content of your [IDEA config folder]/templates/ (standard location of [IDEA config folder] on Mac is ~/Library/Preferences/IntelliJIdeaXX).

Subscribe

Subscribe for updates