Enable whole-word search

Add whole-word search functionality to DoFindNext
* Use stc's SearchInTarget instead of str.find/rfind.

Modify ShowPosition (override)
* Reverted the comment line.
  Folded lines need to be expanded to ensure the target line is visible.
This commit is contained in:
Kazuya O'moto
2025-07-05 21:51:06 +09:00
parent 1327ebdef9
commit ffe0166e4e
2 changed files with 19 additions and 24 deletions

View File

@@ -251,34 +251,30 @@ class EditWindow(stc.StyledTextCtrl):
def ShowPosition(self, pos):
line = self.LineFromPosition(pos)
#self.EnsureVisible(line)
self.EnsureVisible(line) # Expand the line if folded.
self.GotoLine(line)
def DoFindNext(self, findData, findDlg=None):
backward = not (findData.GetFlags() & wx.FR_DOWN)
matchcase = (findData.GetFlags() & wx.FR_MATCHCASE) != 0
end = self.GetLastPosition()
# Changed to reflect the fact that StyledTextControl is in UTF-8 encoding
textstring = self.GetRange(0, end).encode('utf-8')
findstring = findData.GetFindString().encode('utf-8')
if not matchcase:
textstring = textstring.lower()
findstring = findstring.lower()
matchcase = findData.GetFlags() & wx.FR_MATCHCASE
wholeword = findData.GetFlags() & wx.FR_WHOLEWORD
findstring = findData.GetFindString().encode()
self.SetSearchFlags(0
| (wx.stc.STC_FIND_MATCHCASE if matchcase else 0)
| (wx.stc.STC_FIND_WHOLEWORD if wholeword else 0)
)
if backward:
start = self.GetSelection()[0]
loc = textstring.rfind(findstring, 0, start)
self.TargetStart = self.GetAnchor() # backward anchor
self.TargetEnd = 0
else:
start = self.GetSelection()[1]
loc = textstring.find(findstring, start)
self.TargetStart = self.GetCurrentPos() # forward anchor
self.TargetEnd = self.TextLength
loc = self.SearchInTarget(findstring)
# if it wasn't found then restart at beginning
if loc == -1 and start != 0:
if backward:
start = end
loc = textstring.rfind(findstring, 0, start)
else:
start = 0
loc = textstring.find(findstring, start)
if loc == -1:
self.TargetStart = self.TextLength if backward else 0
loc = self.SearchInTarget(findstring)
# was it still not found?
if loc == -1:

View File

@@ -536,11 +536,10 @@ class Frame(wx.Frame):
return
win = wx.Window.FindFocus()
if self.shellName == 'PyCrust':
self.findDlg = wx.FindReplaceDialog(win, self.findData,
"Find",wx.FR_NOWHOLEWORD)
self.findDlg = wx.FindReplaceDialog(win, self.findData, "Find")
else:
self.findDlg = wx.FindReplaceDialog(win, self.findData,
"Find & Replace", wx.FR_NOWHOLEWORD|wx.FR_REPLACEDIALOG)
self.findDlg = wx.FindReplaceDialog(win, self.findData, "Find & Replace",
wx.FR_REPLACEDIALOG)
self.findDlg.Show()
def OnFindNext(self, event, backward=False):