|
JSEclipse comes with code completion for most of the default libraries (document, system, etc). On top of that, as you work on the project, new libraries are built, specific for the current project, with the functions for each file, variable name and so forth.
To make it even more extendable, you can define your own code completion libraries. That is if you use a particular class in more than one project, you can define the code completion library for it. You simply have to create an XML file based on a specific format, and use the Add library dialog to integrate it with JSEclipse
To define a new code completion library you must create an XML file to describe the options to suggest and for which element. To explain it better, let's create the XML file that will add code completion for the class element which has three methods. For the method names let's use some already existing functions: getElementById, getElementsByTagName and innerHTML.
The XML file must start with the version and encoding definition. On the first line you have:
<?xml version="1.0" encoding="UTF-8"?>
Next define the name of the class top trigger code completion, and the eventual classes it extends
<completion prefix="element" extends="Object">
Note: to have a valid XML document you will have to close the <completion> tag after all code completion options have been added.
Next you must define the items to display in the code completion dialog. For the first method use:
<item repl="getElementById()" display="getElementById(String id) Element" />
The repl attribute stores the value to insert into the document
The display attribute contains the text to show in the code completion window.
Optionally you can add documentation or notes for the completion option. You must enclose it in a <[CDATA] element, as you can see below:
<![CDATA[Get an element by its ID.]]>
XML nodes for the other two methods are similar. You can decide what you display, but you should insert the correct code:
<item repl="getElementsByTagName()" display="getElementsByTagName(String name) Element[]" />
<item repl="innerHTML" display="innerHTML String" />
At last do not forget to close the <completion> tag or the XML file will not be valid.
To make the defined library available from within Eclipse, you should copy it in a folder within the workspace. The other, simpler way is to use the Add library feature added by JSEclipse. To access it, click on the JSEclipse menu > Add library.
To add a new library, simply click Browse and select it from the file system. Then click OK and the new code completion will be available from eclipse:
Note: because the element class was defined as extending the class object, the methods of the object class are suggested for completion as well.