I've implemented the following code to automatically close an XML tag when typing the '>' of an opening tag.
Everything is working smoothly so far, however, I am trying to position the cursor between the tags instead of at the end of the closing tag.
<myTag>[cursor should be here]</myTag> [cursor is here]
monaco.languages.registerOnTypeFormattingEditProvider('xml', {
autoFormatTriggerCharacters: ['>'],
provideOnTypeFormattingEdits: (model: any, position: any, ch: any) => {
if (ch === '>') {
const codePre = model.getValueInRange({
startLineNumber: position.lineNumber,
startColumn: 1,
endLineNumber: position.lineNumber,
endColumn: position.column,
});
const match = codePre.match(/.*<(\w+)>$/);
if (match) {
const tag = match[1];
const closingTag = `</${tag}>`;
const edit = {
range: {
startLineNumber: position.lineNumber,
startColumn: position.column,
endLineNumber: position.lineNumber,
endColumn: position.column,
},
text: `${closingTag}`,
};
return [edit];
}
}
return [];
},
});