Search code examples
ace-editor

Is there an option to add equals and parenthesis when using autocomplete in an XML document?


I've been working in an XML editor and I'm also working on adding XML autocomplete support in Ace editor.

I'm trying to find if there's an option to automatically add the equals sign and parenthesis and then set the cursor inside the parenthesis when using autocomplete suggestion. I've seen this feature in the other XML editor.

For example, if I'm typing the following word, "lab":

<s:Button lab|   />

and then press CTRL+space it will invoke suggestions. Then if I choose "label" from the suggestions list the suggested word, "label" is written for me, an equals sign and parenthesis' are placed after it and the cursor is placed inside as shown below:

<s:Button label="|"   />

Is there a way to do this?


Solution

  • I have to set the type property to "snippet" and then set the snippet property to what I want to autocomplete:

    var autoCompleteObject = new Object();
    autoCompleteObject.value = "Greetings";
    autoCompleteObject.type = "snippet";
    autoCompleteObject.snippet = "Hello ${1:World}. Welcome to ${2:Earth}";
    

    When your editor uses the object shown above then when the user is using your editor and selects, "Greetings" from the auto complete menu, the text "Hello World. Welcome to Earth" is inserted into the document.

    The word "World" is selected and the user can change it or leave it as is. When the user presses tab again the word, "Earth" is selected and shklhee has the same option as before. You can use an empty object to just place the cursor "${3}".

    The numbering is the order that the tokens are selected in.