IDE: Optimization of modification events processing in text editors.
authorEdouard Tisserant
Thu, 17 Nov 2022 11:08:36 +0100
changeset 3682 c613afdab571
parent 3681 3da9a6e6fb19
child 3683 bbcbb1bba9f1
IDE: Optimization of modification events processing in text editors.

Too many modifications types where registered, and then too many events were fired.
Also, in case of uninterrupted sequence of events, updates to the model is deferred to the end of that sequence (wx.Callafter).
editors/TextViewer.py
--- a/editors/TextViewer.py	Tue Nov 15 09:29:48 2022 +0100
+++ b/editors/TextViewer.py	Thu Nov 17 11:08:36 2022 +0100
@@ -130,8 +130,7 @@
         self.Editor.SetUseTabs(0)
 
         self.Editor.SetModEventMask(wx.stc.STC_MOD_BEFOREINSERT |
-                                    wx.stc.STC_MOD_BEFOREDELETE |
-                                    wx.stc.STC_PERFORMED_USER)
+                                    wx.stc.STC_MOD_BEFOREDELETE)
 
         self.Bind(wx.stc.EVT_STC_STYLENEEDED, self.OnStyleNeeded, self.Editor)
         self.Editor.Bind(wx.stc.EVT_STC_MARGINCLICK, self.OnMarginClick)
@@ -213,25 +212,24 @@
         self.SearchResults = None
         self.CurrentFindHighlight = None
 
+    Buffering = "Off"
     def OnModification(self, event):
         if not self.DisableEvents:
             mod_type = event.GetModificationType()
             if mod_type & wx.stc.STC_MOD_BEFOREINSERT:
                 if self.CurrentAction is None:
-                    self.StartBuffering()
+                    self.Buffering = "ShouldStart"
                 elif self.CurrentAction[0] != "Add" or self.CurrentAction[1] != event.GetPosition() - 1:
-                    self.Controler.EndBuffering()
-                    self.StartBuffering()
+                    self.Buffering = "ShouldRestart"
                 self.CurrentAction = ("Add", event.GetPosition())
-                wx.CallAfter(self.RefreshModel)
+                self.RefreshModelAfter()
             elif mod_type & wx.stc.STC_MOD_BEFOREDELETE:
                 if self.CurrentAction is None:
-                    self.StartBuffering()
+                    self.Buffering = "ShouldStart"
                 elif self.CurrentAction[0] != "Delete" or self.CurrentAction[1] != event.GetPosition() + 1:
-                    self.Controler.EndBuffering()
-                    self.StartBuffering()
+                    self.Buffering = "ShouldRestart"
                 self.CurrentAction = ("Delete", event.GetPosition())
-                wx.CallAfter(self.RefreshModel)
+                self.RefreshModelAfter()
         event.Skip()
 
     def OnDoDrop(self, event):
@@ -379,7 +377,7 @@
             elif values[3] == self.TagName:
                 self.ResetBuffer()
                 event.SetDragText(values[0])
-                wx.CallAfter(self.RefreshModel)
+                self.RefreshModelAfter()
             else:
                 message = _("Variable don't belong to this POU!")
             if message is not None:
@@ -429,10 +427,14 @@
             self.ParentWindow.RefreshFileMenu()
             self.ParentWindow.RefreshEditMenu()
 
+    def EndBuffering(self):
+        self.Controler.EndBuffering()
+
     def ResetBuffer(self):
         if self.CurrentAction is not None:
-            self.Controler.EndBuffering()
+            self.EndBuffering()
             self.CurrentAction = None
+            self.Buffering == "Off"
 
     def GetBufferState(self):
         if not self.Debug and self.TextSyntax != "ALL":
@@ -834,12 +836,29 @@
                 self.RemoveHighlight(*self.CurrentFindHighlight)
             self.CurrentFindHighlight = None
 
+    pending_model_refresh=False
     def RefreshModel(self):
+        self.pending_model_refresh=False
         self.RefreshJumpList()
         self.Colourise(0, -1)
+
+        if self.Buffering == "ShouldStart":
+            self.StartBuffering()
+            self.Buffering == "On"
+        elif self.Buffering == "ShouldRestart":
+            self.EndBuffering()
+            self.StartBuffering()
+            self.Buffering == "On"
+
         self.Controler.SetEditedElementText(self.TagName, self.GetText())
         self.ResetSearchResults()
 
+    def RefreshModelAfter(self):
+        if self.pending_model_refresh:
+            return
+        self.pending_model_refresh=True
+        wx.CallAfter(self.RefreshModel)
+
     def OnKeyDown(self, event):
         key = event.GetKeyCode()
         if self.Controler is not None: