icecoder/ICEcoder

Prettier needs to use replaceRange not setValue

Closed this issue · 3 comments

When Prettier updates the editor contents it does this with setValue. This wipes out the cursors, selections and code folds.

From a quick search on the CodeMirror forum it seems the better (and more performant) solution would be to diff compare previous code and new code and use replaceRange to patch up the areas changed and so keep the cursors, selections and code folds.

See also prettier.formatWithCursor in the API:
https://prettier.io/docs/en/api.html

Best approach to handling this would be:

  • Selections, ie more than 1 - if a selection in any updated range, clear them all
  • Selection, ie just 1, try and reimplement if in an updated range
  • Folds, let them open for an updated range
  • Scroll position, let it move to give an indication of line changes, tho could also get current lines position from bottom of viewport and scroll to that again

Have implemented this now in 76d5b24 and seems to work really well. I'm letting CodeMirror handle selections, cursors, folds etc which it seems to do with natural behavior. Working well and so closing.