For future readers - the way I ended up formatting code while persisting the cursor is by translating my diff into changes to be passed to state.update. example below
so when AKclown mentions
The cursor jumps are caused by using the insertCompletionText
he means insertCompletionText
is not the method we should use to format code since it messes with cursor positioning
example
state.update({ changes: Change[] })
/* where Change has the shape
{
from: char position (number)
to: char position (number)
insert: text (string)
}
and where char position represents the position of the original
unchanged text i.e. char position shouldn't update based off
previous changes in the array
*/