var text = txt.GetText();
var tag = item + '></' + item + '>';
var index = text.lastIndexOf( "<" );
var diff = text.length - index;
text = text.substr( 0, index + 1 );
//I write the touched item to the textedit
txt.SetText( text + tag );
//Update cursor position
var pos = txt.GetCursorPos();
var newPos = pos + item.length + 1 - diff + 1;
txt.SetCursorPos( newPos );
Note that this is really a proof of concept.
It only allow the usage of autocompletion if "<" is the very last character of the text field.
A real implementation would allow to open autocompletion each time you set your cursor after a "<", regardless of it's position.
But it would require a different approach and some additional work.
I hope this samples will inspire you to do achieve what you're looking for.