Skip to main content

By clicking Submit, you agree to the developerWorks terms of use.

The first time you sign into developerWorks, a profile is created for you. Select information in your profile (name, country/region, and company) is displayed to the public and will accompany any content you post. You may update your IBM account at any time.

All information submitted is secure.

  • Close [x]

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerworks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

By clicking Submit, you agree to the developerWorks terms of use.

All information submitted is secure.

  • Close [x]

Build an Eclipse development environment for Perl, Python, and PHP

Use the Dynamic Languages Toolkit (DLTK) to create your own IDE

Matthew Scarpino, Java Developer, Eclipse Engineering, LLC
Matthew Scarpino is a project manager and Java developer at Eclipse Engineering LLC. He is the lead author of SWT/JFace in Action and made a minor but important contribution to the Standard Widget Toolkit (SWT). He enjoys Irish folk music, marathon running, the poetry of William Blake, and the Graphical Editing Framework (GEF).
(An IBM developerWorks Contributing Author)
Nathan A. Good, Senior Consultant and Freelance Developer, Freelance Developer
Nathan Good
Nathan A. Good lives in the Twin Cities area of Minnesota. Professionally, he does software development, software architecture, and systems administration. When he's not writing software, he enjoys building PCs and servers, reading about and working with new technologies, and trying to get his friends to make the move to open source software. He's written and co-written many books and articles, including Professional Red Hat Enterprise Linux 3, Regular Expression Recipes: A Problem-Solution Approach, and Foundations of PEAR: Rapid PHP Development.

Summary:  Eclipse presents a wealth of capabilities for building tools for compiled languages like C and the Java™ programming language but provides little support for scripting languages like Perl, Python, and PHP. For these and similar languages, the Eclipse Dynamic Languages Toolkit (DLTK) comes to the rescue. Walk through the process of building a DLTK-based IDE, and discover sample code for each step.

Date:  27 Oct 2011 (Published 03 Feb 2009)
Level:  Intermediate PDF:  A4 and Letter (596 KB | 38 pages)Get Adobe® Reader®

Activity:  132961 views
Comments:  

Getting started with a DLTK project

For the sake of simplicity, this tutorial integrates all the Octave IDE functionality into a single plug-in. Generally, you implement a feature using multiple plug-ins: one containing core classes, one containing UI classes, one containing debug classes, and so on. But this tutorial's code only needs one plug-in, so if you have Eclipse running, click File > New > Project, select the Plug-in Project option, and click Next to begin creating the plug-in project. Follow the steps here to create a plug-in project that includes a sample editor, which is a good starting point for your new plug-in.

  1. Enter the project name, which in this example is org.dworks.octaveide, as shown in Figure 3.

    Figure 3. Entering the project's name
    Screenshot shows the New Plug-in Project screen where the Project name is being entered as org.dworks.octaveide

  2. In the Content screen, shown in Figure 4, enter org.dworks.ocativeide as the ID if it's not already set, and click Next.

    Figure 4. The Content screen
    Screenshot shows the Content screen of the New Plug-in Project with the ID set to org.dworks.octaveide

  3. Select the Create a plug-in using one of the templates check box, select Plug-in with an editor from the list, and click Next.
  4. Enter OctiveEditor, as shown in Figure 5, enter m for the File Extension, and then click Finish to create your plug-in project.

    Figure 5. Entering the information for the editor
    Screenshot shows the Sample XML Editor screen with the File Extenion set to 'm'

Your new plug-in project is created with the plugin.xml configuration file already available in your project, and you're off to a good start. You can edit the plugin.xml configuration using the built-in editor, or you can edit the source of the file directly by clicking the editor's plugin.xml tab. This tutorial shows you the listing of the plugin.xml file itself.

Before continuing with the rest of the tutorial, you should add two dependencies that allow your plug-in code to find the DLTK classes. In the Dependencies tab of your plugin.xml editor, click Add, and add org.eclipse.dltk.core and org.eclipse.dltk.ui. The current version in this tutorial is 3.0.1.

Configure the Octave plug-in

The plugin.xml file that was started when you created your plug-in project contains information about the plug-in's extensions. These extensions tell Eclipse how the plug-in intends to contribute to the workbench. To start, the plugin.xml file needs two simple extensions: one that identifies the Octave editor and one that identifies the types of files to be edited. The first extension was already created when you created the plug-in project using the template.

Listing 1 presents the first extension, which defines characteristics of the Octave editor.


Listing 1. The Octave IDE editor extension
	
<extension point="org.eclipse.ui.editors">
   <editor
      id="org.dworks.octaveide.editor.OctaveEditor"   
      class="org.dworks.octaveide.editor.OctaveEditor"
      contributorClass="org.dworks.octaveide.actions.OctaveActionContributor"
      default="true"
      icon="icons/oct.gif"
      name="OctaveEditor">
      <contentTypeBinding
         contentTypeId="org.dworks.octaveide.octaveContentType">
      </contentTypeBinding>
   </editor>
</extension>

Thoughts on the DLTK

The more we learn about the DLTK, the more impressed we are. The DLTK creators thought of everything, and this tutorial barely scratches the surface. Whenever we find ourselves wondering whether the DLTK supports a feature we're curious about, it turns out that it does and that the DLTK creators were way ahead of us.

However, the DLTK has its shortcomings. It's a necessarily complicated system, and the documentation is sparse at best, nonexistent at worst. There are a few helpful comments in the source code, but not many. Thankfully, the DLTK examples—particularly for the Tcl editor—clear up a lot of confusion regarding how the DLTK is used in practice.

This extension contains several important attributes. The class attribute defines the primary class that embodies the editor: OctaveEditor, which you will create shortly. The contributorClass attribute identifies the class that provides actions for the editor. And the default attribute states that the OctaveEditor should be the default editor for associated files. The name attribute identifies the text that will be displayed for the editor. As defined in the plugin.properties file, the OctaveEditor.name property corresponds to "Octave Script Editor."

The last supplement in the extension, contentTypeBinding, requires explanation. Eclipse maintains a registry of content types that represent file formats and naming conventions. By adding a contentTypeBinding element to the editor's extension, you can associate the editor with file suffixes, file names, and even file aliases. But for your Octave editor, you're only interested in *.m files, so your extension of org.eclipse.core.runtime.contentTypes is simple. Listing 2 shows this second extension.


Listing 2. The Octave IDE ContentType extension
	
<extension point="org.eclipse.core.runtime.contentTypes">
   <content-type
      id="org.dworks.octaveide.octaveContentType"   
      base-type="org.eclipse.core.runtime.text"
      file-extensions="m"
      name="%octaveContentType"
      priority="high">       
   </content-type>
</extension>

Many more attributes are available for customizing content types, but this declaration identifies the org.dworks.octaveide.octaveContentType content type as being text based and associated with the file suffix .m. The priority attribute states that if a file becomes associated with multiple content types, this type should be given high priority.


Creating a DLTK language toolkit class

A principal advantage of using the DLTK is that it's simple to customize how IDEs work. This customization is made possible through DLTK's language toolkit interfaces (IDLTKCoreLanguageToolkit, IDLTKDebugUILanguageToolkit, IDLTKUILanguageToolkit, and so on). These interfaces present methods that, when implemented, configure many aspects of the IDE, such as parsing, color usage, user preferences, and accessing the interpreter.

This tutorial focuses primarily on the DLTK UI, so you'll only concern yourself with the IDLTKUILanguageToolkit. The methods in this interface perform one of two functions: They either return a class that handles editor configuration or provide the ID of a configuration object declared in plugin.xml. Seven of these methods are listed here in Table 1.


Table 1. Methods on the IDLTKUILanguageToolkit interface
Method nameDescription
getEditorId(Object elementID)Returns the editor's ID
getTextTools()Returns the ScriptTextTools object that will customize how text is presented in the editor
createSourceViewerConfiguration()Returns the ScriptSourceViewerConfiguration object that will configure the operation of the editor's source viewer
getPreferenceStore()Returns the IPreferenceStore object that contains user settings
getEditorPreferencePages()Returns the IDs of the preference pages related to editor settings
getInterpreterPreferencePage()Returns the IDs of the preference pages related to the script interpreter
getIntepreterId()Returns the ID of the script interpreter

In the example code, the OctaveUILanguageToolkit class fleshes out these methods with objects and identifiers. The first two objects discussed relate to the first two methods in the toolkit: OctaveEditor and OctaveTextTools.


Create the Octave editor and the Octave text tools

Now that you've configured the editor's extension, the first order of business is to create the OctaveEditor. Thanks to the DLTK, it won't require much coding: The DLTK's ScriptEditor class (the superclass of OctaveEditor) handles most of the editor's operations on your behalf. All you need are a few configuration methods. In the OctaveEditor, the most important of these is getTextTools(), which returns a ScriptTextTools object.

The ScriptTextTools class is convenient. This central class accesses several other classes that provide the editor's capabilities. By subclassing ScriptTextTools, an editor can customize how these capabilities are performed. The Octave editor creates a subclass called OctaveTextTools, which provides access to the important objects shown in Table 2.


Table 2. Objects made available by OctaveTextTools
ObjectDescription
OctavePartitionScannerReads the editor text and determines partition boundaries
OctaveSourceViewerConfigurationConfigures the ScriptSourceViewer and the way it responds to user-generated events

These objects become particularly important in implementing syntax coloring—that is, changing the color of text depending on the text's syntactic function in code. The next topic explains this feature in greater detail.

3 of 10 | Previous | Next

Comments



static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=Open source
ArticleID=366849
TutorialTitle=Build an Eclipse development environment for Perl, Python, and PHP
publish-date=10272011
author1-email=mattscar@yahoo.com
author1-email-cc=
author2-email=mail@nathanagood.com
author2-email-cc=