Fixed search highlight bug in ST and Code text editors
authorLaurent Bessard
Tue, 28 May 2013 10:03:41 +0200
changeset 1178 3e2aebc9c7c0
parent 1177 4cbbc58b91b4
child 1179 3e7bd88fcff7
Fixed search highlight bug in ST and Code text editors
editors/CodeFileEditor.py
editors/TextViewer.py
--- a/editors/CodeFileEditor.py	Mon May 27 13:10:32 2013 +0200
+++ b/editors/CodeFileEditor.py	Tue May 28 10:03:41 2013 +0200
@@ -100,10 +100,7 @@
         self.DisableEvents = True
         self.CurrentAction = None
         
-        self.Highlights = []
-        self.SearchParams = None
-        self.SearchResults = None
-        self.CurrentFindHighlight = None
+        self.ResetSearchResults()
         
         self.RefreshHighlightsTimer = wx.Timer(self, -1)
         self.Bind(wx.EVT_TIMER, self.OnRefreshHighlightsTimer, self.RefreshHighlightsTimer)
@@ -140,6 +137,12 @@
     def SetCodeLexer(self):
         pass
     
+    def ResetSearchResults(self):
+        self.Highlights = []
+        self.SearchParams = None
+        self.SearchResults = None
+        self.CurrentFindHighlight = None
+    
     def OnModification(self, event):
         if not self.DisableEvents:
             mod_type = event.GetModificationType()
@@ -241,7 +244,7 @@
         self.RefreshSectionStyling()
         
         self.ShowHighlights()
-
+        
     def RefreshSectionStyling(self):
         self.Colourise(0, -1)
         
@@ -274,7 +277,8 @@
             else:
                 parts[section] = ""
         self.Controler.SetTextParts(parts)
-
+        self.ResetSearchResults()
+    
     def OnKeyPressed(self, event):
         if self.CallTipActive():
             self.CallTipCancel()
@@ -503,8 +507,21 @@
                     self.CurrentFindHighlight = self.SearchResults[idx]
                     self.AddHighlight(*self.CurrentFindHighlight)
             else:
-                self.CurrentFindHighlight = self.SearchResults[0]
-                self.AddHighlight(*self.CurrentFindHighlight)
+                caret_pos = self.GetCurrentPos()
+                if self.SearchParams["wrap"]:
+                    self.CurrentFindHighlight = self.SearchResults[0]
+                else:
+                    self.CurrentFindHighlight = None
+                for start, end, highlight_type in self.SearchResults:
+                    if start[0] == 0:
+                        highlight_start_pos = start[1]
+                    else:
+                        highlight_start_pos = self.GetLineEndPosition(start[0] - 1) + start[1] + 1
+                    if highlight_start_pos >= caret_pos:
+                        self.CurrentFindHighlight = (start, end, highlight_type)
+                        break
+                if self.CurrentFindHighlight is not None:
+                    self.AddHighlight(*self.CurrentFindHighlight)
             
             self.ScrollToLine(self.CurrentFindHighlight[0][0])
             
@@ -552,12 +569,12 @@
             else:
                 highlight_start_pos = self.GetLineEndPosition(start[0] - 1) + start[1] + 1
             if end[0] == 0:
-                highlight_end_pos = end[1] - indent + 1
+                highlight_end_pos = end[1] + 1
             else:
                 highlight_end_pos = self.GetLineEndPosition(end[0] - 1) + end[1] + 2
             self.StartStyling(highlight_start_pos, 0xff)
             self.SetStyling(highlight_end_pos - highlight_start_pos, highlight_type)
-            self.StartStyling(highlight_start_pos, 0x00)
+            self.StartStyling(highlight_end_pos, 0x00)
             self.SetStyling(len(self.GetText()) - highlight_end_pos, stc.STC_STYLE_DEFAULT)
 
 
--- a/editors/TextViewer.py	Mon May 27 13:10:32 2013 +0200
+++ b/editors/TextViewer.py	Tue May 28 10:03:41 2013 +0200
@@ -163,14 +163,13 @@
         self.DisableEvents = True
         self.TextSyntax = None
         self.CurrentAction = None
-        self.Highlights = []
-        self.SearchParams = None
-        self.SearchResults = None
-        self.CurrentFindHighlight = None
+        
         self.InstancePath = instancepath
         self.ContextStack = []
         self.CallStack = []
         
+        self.ResetSearchResults()
+        
         self.RefreshHighlightsTimer = wx.Timer(self, -1)
         self.Bind(wx.EVT_TIMER, self.OnRefreshHighlightsTimer, self.RefreshHighlightsTimer)
         
@@ -214,6 +213,12 @@
     def GetCurrentPos(self):
         return self.Editor.GetCurrentPos()
     
+    def ResetSearchResults(self):
+        self.Highlights = []
+        self.SearchParams = None
+        self.SearchResults = None
+        self.CurrentFindHighlight = None
+    
     def OnModification(self, event):
         if not self.DisableEvents:
             mod_type = event.GetModificationType()
@@ -786,6 +791,7 @@
     def RefreshModel(self):
         self.RefreshJumpList()
         self.Controler.SetEditedElementText(self.TagName, self.GetText())
+        self.ResetSearchResults()
     
     def OnKeyDown(self, event):
         if self.Controler is not None: