A more robust solution would be to use API Selection instead of position
var selection = editor.getSelection(); var range = new monaco.Range(selection.startLineNumber, selection.startColumn, selection.endLineNumber, selection.endColumn); var id = { major: 1, minor: 1 }; var text = "XXX"; var op = {identifier: id, range: range, text: text, forceMoveMarkers: true}; editor.executeEdits("my-source", [op]);
If the editor already has the selected text, the insert will replace it, which, in my opinion, is the correct behavior.
source share