Search code examples
javascriptace-editorcloud9

Is it possible to update the value inside the Ace Editor without changing the position of the cursor?


I want to do something like a background worker. It should automatically update some code (at a different place than the cursor is) without moving the cursor to the last changed word. Is this possible with editor.session.replace or .insert?


Solution

  • One way of doing this is, Store the present cursor position, Insert the data and set the cursor position to the initial point.

    //Get the Current Positon
    var currentPosition = editor.selection.getCursor();
    
    //Insert data into the editor
    editor.setValue(data);
    editor.clearSelection();
    
    //Set the cursor to the Initial Point
    editor.gotoLine(currentPosition.row, currentPosition.column);