More ActionScript magic for jEdit:
Context-sensitive reference material!

We’re making progress on our quest to create the perfect Mac OS X ActionScript editor: Today, rx we’re going to integrate the Adobe AS2 and AS3 Language References directly into the open-source jEdit editor, treatment bringing context-sensitive help files up at the stroke of a key!

placeLogo(‘jedit’); To do this, contagion we’re going to need three things: The InfoViewer plugin (which should already be installed with your base jEdit application), and two jEdit macros that we’ll provide here. If you need a refresher on how to install plugins or new macros, check the previous tutorials in this series.

The InfoViewer is basically a small browser in a floating or docked panel within jEdit. If you don’t already see it, you should be able to find it under your Plugins menu. I prefer to dock mine at the right-hand side of the editor, so it stays out of the way but is generally available. You can do this by selecting the small dropdown arrow in the upper left of any floating dock and choosing which side of the interface on which to dock it:

Docking InfoViewer

I’ve created two macros — one to search for information within the ActionScript 2 reference files, and one for the ActionScript 3 files. You can install them both, or just the one that interests you (I’ve got them both installed myself, and I’ve assigned the keyboard shortcuts CMD-F1 to search the AS2 files, and SHIFT-F1 for the AS3 files). To install, you can copy the code below, or simply download and extract the files from this zipped archive and save them in your ~/.jedit/macros directory (or better yet, make a subdirectory called “ActionScript” within the macros folder and keep all your AS macros in one neat section — rather like a TextMate “bundle”).

The macro scripts:

// Macro command to search the AS3 Language Reference to find a (likely) page from the manual
// that describes the current "word" and display the result in the InfoViewer panel within jEdit.
// Could be improved, perhaps, but it sure seems to work.
//
// Beanshell script written by Mike Fahy (VeryVito) of Turdhead.com
// and released into the public domain. Yep, free as in beer.
//
// Questions, comments or large amounts of cash may be sent to nospam@turdhead.com
//
// NOTE: These scripts rely on the reference files installed with Adobe Flash CS3.
// If you are using a different version of Flash, are on a system other than OS X
// or have otherwise changed your default path to the ActionScript reference files,
// you must change the value of "folder" below to reflect the correct path.

import java.util.regex.*;

String folder = new String ("/Library/Application Support/Adobe/Flash CS3/en/Configuration/HelpPanel/Help/ActionScriptLangRefV3");

String helpTOC = "help_toc.xml";

textArea.goToPrevCharacter(false);
textArea.selectWord();

String record = null;
String urlToOpen  = null;
String wordToFind = textArea.getSelectedText();
String fileToLoad = null;
String regEx = "level(\d+)\s+name\s*\=\s*\"(" + wordToFind + ").*\".*?href\s*\=\s*\"(.*?)\"";

Pattern regPat = Pattern.compile(regEx);
Matcher matcher;
BufferedReader bufr;


try {
bufr = new BufferedReader (new FileReader (folder + "/" + helpTOC));

while ( (record = bufr.readLine()) != null ) {
matcher = regPat.matcher(record);
if (matcher.find() && (Integer.parseInt(matcher.group(1)) > 2))	{ // I personally don't want to see anything lower than "level2." This may help refine the results a bit.

fileToLoad = matcher.group(3);

infoviewer.InfoViewerPlugin.openURL(view, "file://" + folder + "/" +  fileToLoad);
break;
}
}

if (fileToLoad == null)	{
Macros.error(view, "Sorry. No help found in " + folder);
//infoviewer.InfoViewerPlugin.openURL(view, "file://" + folder + "/help_toc.xml");
}

}	catch (java.io.IOException e)	{
Macros.error(view, "An error occurred while trying to parse " + helpTOC);
}
The source for Look_up_in_AS3_reference.bsh
// Macro command to search the AS2 Language Reference to find a (likely) page from the manual
// that describes the current "word" and display the result in the InfoViewer panel within jEdit.
// Could be improved, perhaps, but it sure seems to work.
//
// Beanshell script written by Mike Fahy (VeryVito) of Turdhead.com
// and released into the public domain. Yep, free as in beer.
//
// Questions, comments or large amounts of cash may be sent to nospam@turdhead.com
//
// NOTE: These scripts rely on the reference files installed with Adobe Flash CS3.
// If you are using a different version of Flash, are on a system other than OS X
// or have otherwise changed your default path to the ActionScript reference files,
// you must change the value of "folder" below to reflect the correct path.

import java.util.regex.*;

String folder = new String ("/Library/Application Support/Adobe/Flash CS3/en/Configuration/HelpPanel/Help/ActionScriptLangRefV2");

String helpTOC = "help_toc.xml";

textArea.goToPrevCharacter(false);
textArea.selectWord();

String record = null;
String urlToOpen  = null;
String wordToFind = textArea.getSelectedText();
String fileToLoad = null;
String regEx = "href\s*\=\s*\"(.*?)\"\s*name\s*\=\s*\"(" + wordToFind + ".*)\"";

Pattern regPat = Pattern.compile(regEx);
Matcher matcher;
BufferedReader bufr;


try {
bufr = new BufferedReader (new FileReader (folder + "/" + helpTOC));

while ( (record = bufr.readLine()) != null ) {
matcher = regPat.matcher(record);
if (matcher.find())	{

fileToLoad = matcher.group(1);

infoviewer.InfoViewerPlugin.openURL(view, "file://" + folder + "/" +  fileToLoad);
break;
}
}

if (fileToLoad == null)	{
Macros.error(view, "Sorry. No help found in " + folder);
//infoviewer.InfoViewerPlugin.openURL(view, "file://" + folder + "/help_toc.xml");
}

}	catch (java.io.IOException e)	{
Macros.error(view, "An error occurred while trying to parse " + helpTOC);
}
The source for Look_up_in_AS2_reference.bsh

To use either of the macros, simply move your cursor to the class, method or property name you need help with (or simply type it in), and run the macro. If the script can find the information requested, it will be displayed within the InfoViewer panel.

Note: If your ActionScript reference files are located somewhere besides the folder indicated in the scripts above (and if you’re using Windows, they will be!), you will need to edit the “file” variable to point to the correct directory.

As you might notice, the macros used by jEdit actually use the Java syntax. In fact, it is Java (in a form known as a BeanShell), and it can be pretty dang powerful. In fact, when I first attempted to make macros for my own use, I used a convoluted system of shell scripts, perl scripts and OS X’s built-in Apache server to cobble something together to do this (because, frankly, those are the tools I know best). But I quickly realized that unleashing that setup on an unsuspecting public would do little to promote jEdit as an easily extensible tool. Thus, I went back to the drawing board tonight and rewrote the scripts to rely solely on jEdit’s native macro language — and I’m impressed with how easy it was to figure out.

If you want to learn more about creating your own macros (and please share them here if you do!), check out the Macro Basics guide at jEdit.org. Meanwhile, enjoy your getting-better-every-day ActionScript environment!

facebooktwittergoogle_plusredditpinterestlinkedinmail

13 thoughts on “More ActionScript magic for jEdit:
Context-sensitive reference material!”

  1. This is all so sweet, finally!

    On question thou, does anybody know how to get the sidekick codebrowser to work? i have code completion and everything but i just cant get it to display the list of functions like on your screener. I selected Ctags as a parser, and i have “parse on keystroke” an tryed parsing manualy too.. I just get the filename in the sidekick window. Any hello would be greatly appreciated 🙂

  2. Hi Ben, good question. I thought I had decent code browsing, but once you asked, I realized it just wasn’t good enough… See this new update to see what I did to change it.

    Meanwhile… you have code completion working?? Are you using the TextAutoComplete plugin for this, or do you have a specifically ActionScript-oriented solution?

  3. What about auto-completion? Any suggestions for that? In X-Code i could type ESC and get a list of AS. If I type textfield.set then hit esc it would give me all AS with set in them. Nice feature saves on typing.

  4. Hi – had no problem following previous articles and jedit looks very useful, but installing this macro throws an error message:

    Beanshell error
    Sourced file: /Users/greyarea/.jedit/macros/Look_up_in_AS2_reference.bsh Token Parsing Error: Lexical error at line 28, column 22. Encountered: “s” (115), after : “\”href\\”:

    Have checked and rechecked the ‘folder’ string is correct. I know jack about Java, so is this a problem with the macro or the path to the AS2 help? running jedit 4.3pre11 using Java 1.5.0_07

    Cheers

    dgb

  5. Hi David. Wow, thanks for bringing this to my attention — apparently the code as presented above is riddled with errors: It worked when published, but apparently, the new formatting engine I’m using tries to “fix it” by removing what it sees as unimportant characters (and thus renders it useless). I’m working on fixing this, but in the meantime, I recommend downloading the working zipped version for now. The problem should be fixed now. Sorry for the confusion!

  6. Hi
    just for anyone else reading this – on my mac added the macro to the mouse ctrl click context menu. At bottom of the context menu is ‘Custumize This Menu’. Choose that. At the bottom of the ‘Options..’ pane is a plus sign. Click on that. In the dialog that appears, click on the pop-up menu and choose ‘Macros’. Scroll to ‘Look up in AS2 reference'( or AS3 if installed). Click both OK buttons. Ping – almost like ctrl clicking in the actionscript palette in Flash.

    cheers

    dgb

  7. Really great work, I’m really happy with my new setup… Thank you very much!

    I’ve been searching for a while trying to find an autoformatter/beautifier plugin for AS but so for no luck. Do you know if one exists?

    [N]

Leave a Reply

Your email address will not be published. Required fields are marked *