Adding folding support

From Aptana Development

Adding folding support for an Aptana Editor

This page describes how to add folding support for an existing Aptana Editor.

We register a folding support for a particular content type (MIME type) by extending extension-point “com.aptana.ide.editors.folding”. Most Aptana editors ( Javascript, Html, XML and PHP editors as of milestone 0.2.9 ) look up folding configuration registered via the extension point for the content type they edit. Whenever there are modifications to editor text, last folding configuration for that content type is used.

Notes:

  • You should not register multiple folding configuration on one mime type. Only the last registered one will be used in this case currently.
  • You should remember that folding support in Aptana is AST based, and its required to have parser which will provide correct AST structure, to make folding work.


Instructions

In this example, let us write show how folding support for JavaScript language is installed.

1. Add <folding> entry to “com.aptana.ide.editors.folding” extension point for editor in question, in this case, javascript editor ( com.aptana.ide.editor.js).


   <extension point="com.aptana.ide.editors.folding" >
   		<folding language="text/javascript" label="JavaScript" foldAllParents="false">
   			<type name="function"/>
   			<type name="JSCOMMENT"/>
   			<type name="SDCOMMENT"/>
   		</folding>
   </extension>


language 
MIME type of the language that this character pair finder can work on.
label 
Descriptive name of folding configuration.
foldAllParents 
Does parents should be folded.
<type name="function">
This means that AST nodes with name "function" may be folded.


2. Register preference page which will allow user to configure automatic foldings.



   <extension  point="org.eclipse.ui.preferencePages">
        <page
          category="com.aptana.ide.editor.js.preferences.GeneralPreferencePage"
          class="com.aptana.ide.editor.js.preferences.FoldingPreferencePage"
          id="com.aptana.ide.editor.js.preferences.FoldingPreferencePage"
          name="%foldingPreferencePage"/>
   </extension>

3. Implement registred preference page.


For the example scenario at hand, we have a simple implementation as below.



public class FoldingPreferencePage extends com.aptana.ide.editors.preferences.FoldingPreferencePage
{

	/**
	 * @see com.aptana.ide.editors.preferences.FoldingPreferencePage#addInitialFoldingFields()
	 */
	public void addInitialFoldingFields()
	{
		addInitialFoldingField(JSMimeType.MimeType, "function", Messages.FoldingPreferencePage_FoldFunctions); //$NON-NLS-1$
		addInitialFoldingField(JSMimeType.MimeType, "JSCOMMENT", Messages.FoldingPreferencePage_FoldBlockComments); //$NON-NLS-1$
		addInitialFoldingField(JSMimeType.MimeType, "SDCOMMENT", Messages.FoldingPreferencePage_FoldScriptDocComments); //$NON-NLS-1$
	}

	/**
	 * @see com.aptana.ide.editors.preferences.FoldingPreferencePage#getLanguage()
	 */
	public String getLanguage()
	{
		return JSMimeType.MimeType;
	}

}

3. Verify that the folding configuration and folding preference page thus registered is detected in Aptana. Build everything and launch IDE. Go to Window > Preferences > Aptana > Editors > (The editor for which you registered folding e.g. JavaScript Editor) > Folding. You should see current configuration for automatic folding. Open editor. You should see folding nodes in the editor.