From ec916a6a15db84146c1d2f8cdc5b21b4c2cc3c5e Mon Sep 17 00:00:00 2001
From: Maxime Jacquemin <maxime.jacquemin@cea.fr>
Date: Wed, 25 Jan 2023 17:51:32 +0100
Subject: [PATCH] [Ivette] Bug fix in text field setter

The dispatched operations order is important and caused a bug that would
prevent scrolling to a marker outside of the ASTview componant if it was
in another function than the one currently displayed. The text field
setter now dispatch the document changes before the field update. This
changes impact how the MarkerScroller ViewUpdater catches an update of
the selected marker, allowing to perform the scrolling when needed.
---
 ivette/src/dome/renderer/text/editor.tsx | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)

diff --git a/ivette/src/dome/renderer/text/editor.tsx b/ivette/src/dome/renderer/text/editor.tsx
index 5652c4638e0..9a2dda30d69 100644
--- a/ivette/src/dome/renderer/text/editor.tsx
+++ b/ivette/src/dome/renderer/text/editor.tsx
@@ -331,8 +331,8 @@ function createSelectionField(): Field<EditorSelection> {
   const cursor = EditorSelection.cursor(0);
   const field = createField<EditorSelection>(EditorSelection.create([cursor]));
   const set: Set<EditorSelection> = (view, selection) => {
-    field.set(view, selection);
     view?.dispatch({ selection });
+    field.set(view, selection);
   }
   const updater = EditorView.updateListener.of((update) => {
     if (update.selectionSet) field.set(update.view, update.state.selection);
@@ -344,16 +344,14 @@ function createSelectionField(): Field<EditorSelection> {
 export type ToString<A> = (text: A) => string;
 export function createTextField<A>(init: A, toString: ToString<A>): Field<A> {
   const field = createField<A>(init);
-  const isUpdated: IsUpdated = (u) =>
-    field.isUpdated(u) && u.startState.doc.length !== 0;
   const set: Set<A> = (view, text) => {
-    field.set(view, text);
     const selection = { anchor: 0 };
     const length = view?.state.doc.length;
     const changes = { from: 0, to: length, insert: toString(text) };
     view?.dispatch({ changes, selection });
+    field.set(view, text);
   };
-  return { ...field, set, isUpdated };
+  return { ...field, set };
 }
 
 // An extension displaying line numbers in a gutter. Does not display anything
-- 
GitLab