Adding custom pair finder

From Aptana Development

Adding Pair Finder for an Aptana Editor

This page describes how to add custom character pair finder for an existing Aptana Editor.

We register a pair finder for a particular content type (MIME type) by extending extension-point “com.aptana.ide.editors.pairfinder”. Most Aptana editors ( Javascript, Html and CSS editors as of milestone 0.2.8 ) look up pair character finders registered via the extension point for the content type they edit. Whenever there are modifications to editor text, first the registered pair finder for that content type are invoked.

Notes:

  • You should always register a character pair finder when adding new mime type to Aptana IDE. At least the "do nothing" implementation(com.aptana.ide.editors.unified.NullPairFinder) should be registred.
  • You should not register multiple pair finders on one mime type. Only the first registered one will be used in this case.


Instructions

As a simple hypothetical example, let us write an character pair matcher that will match curlies in css documents.

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


   <extension
         point="com.aptana.ide.editors.pairfinder">
      <pairFinder
            class="com.aptana.ide.editor.css.CSSPairFinder"
            language="text/css"
            name="CSS Pair Finder">
      </pairFinder>
   </extension>


class 
Fully qualified Java class name implementing com.aptana.ide.editors.unified.IPairFinder interface.
language 
MIME type of the language that this character pair finder can work on.
name 
Descriptive name of the character pair finder.

2. Write Java class specified in step 1 above, implementing IPairFinder interface.

The com.aptana.ide.editors.unified.IPairFinder interface has one function:



         PairMatch findPairMatch(int offset,IParseState parseState);


offset
The offset in the document where one of pair elements is situated.
parseState
The actual parse state of the document.


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




	public PairMatch findPairMatch(int offset, IParseState parseState)
	{
		LexemeList lexemeList=parseState.getLexemeList();
		Lexeme cursorLexeme = lexemeList.getLexemeFromOffset(offset);
		int loopCount = 2;
		if (cursorLexeme == null && offset > 0)
		{
			cursorLexeme = lexemeList.getLexemeFromOffset(offset - 1);
			loopCount--;
		}
		LexemeList lexemeList = parseState.getLexemeList();
		PairMatch result = null;
		while (loopCount > 0 && cursorLexeme != null)
		{
			int index = lexemeList.getLexemeIndex(cursorLexeme);
			Lexeme matchingLexeme = null;

			switch (cursorLexeme.typeIndex)
			{
				case CSSTokenTypes.LCURLY:
					matchingLexeme = UnifiedEditor.findBalancingLexeme(lexemeList, index, CSSMimeType.MimeType,
							cursorLexeme.typeIndex, CSSTokenTypes.RCURLY, 1);
					break;

				case CSSTokenTypes.RCURLY:
					matchingLexeme = UnifiedEditor.findBalancingLexeme(lexemeList, index, CSSMimeType.MimeType,
							cursorLexeme.typeIndex, CSSTokenTypes.LCURLY, -1);
					break;

				default:
			}
			if (matchingLexeme != null)
			{
				result = new PairMatch();
				result.beginStart = cursorLexeme.getStartingOffset();
				result.beginEnd = cursorLexeme.getEndingOffset();
				result.endStart = matchingLexeme.getStartingOffset();
				result.endEnd = matchingLexeme.getEndingOffset();
				// break out of loop
				loopCount = 0;
			}
			else
			{
				loopCount--;

				if (loopCount > 0 && offset > 0)
				{
					cursorLexeme = lexemeList.getLexemeFromOffset(offset - 1);
				}
				else
				{
					// break out of loop
					loopCount = 0;
				}
			}
		}
		return result;
	}

3. Verify that the pair finder thus registered is detected in Aptana. Build everything and launch IDE. Open editor and move cursor to offset at wich matching character exists. You should see the match in the editor.

If you are interested, you can also take a look at the default character pair finders supplied with Aptana:

  • com.aptana.ide.editor.js.JSPairFinder
  • com.aptana.ide.editor.css.CSSPairFinder
  • com.aptana.ide.editor.html.HTMLPairFinder