Thanks to visit codestin.com
Credit goes to github.com

Skip to content

gh-130693: Add options of the tkinter.Text.search method: -nolinestop -all -overlap -strictlimits #130848

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 20 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions Lib/test/test_tkinter/test_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,34 @@ def test_count(self):
self.assertEqual(text.count('1.3', '1.3', 'update', return_ints=True), 0)
self.assertEqual(text.count('1.3', '1.3', 'update'), None)

class TextSearchOptionsTest(AbstractTkTest, unittest.TestCase):
def setUp(self):
super().setUp()
self.text = tkinter.Text(self.root)
self.text.pack()
self.text.insert('1.0',
'This is a test. This is only a test.\n'
'Another line.\nYet another line.')

def test_nolinestop(self):
result = self.text.search('line', '1.0', 'end', nolinestop=True, regexp=True)
self.assertEqual(result, '2.8')

def test_all(self):
result = self.text.search('test', '1.0', 'end', all=True)
self.assertIsInstance(result, str)
indices = result.split()
self.assertGreaterEqual(len(indices), 2)
self.assertTrue(all(isinstance(i, str) for i in indices))

def test_overlap(self):
result = self.text.search('test', '1.0', 'end', all=True, overlap=True)
self.assertIsInstance(result, str)
self.assertIn("textindex", result)

def test_strictlimits(self):
result = self.text.search('test', '1.0', '1.20', strictlimits=True)
self.assertEqual(result, '1.10')

if __name__ == "__main__":
unittest.main()
10 changes: 8 additions & 2 deletions Lib/tkinter/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4039,8 +4039,10 @@ def scan_dragto(self, x, y):
self.tk.call(self._w, 'scan', 'dragto', x, y)

def search(self, pattern, index, stopindex=None,
forwards=None, backwards=None, exact=None,
regexp=None, nocase=None, count=None, elide=None):
forwards=None, backwards=None, exact=None,
regexp=None, nocase=None, count=None,
elide=None, nolinestop=None, all=None,
overlap=None, strictlimits=None):
"""Search PATTERN beginning from INDEX until STOPINDEX.
Return the index of the first character of a match or an
empty string."""
Expand All @@ -4052,6 +4054,10 @@ def search(self, pattern, index, stopindex=None,
if nocase: args.append('-nocase')
if elide: args.append('-elide')
if count: args.append('-count'); args.append(count)
if nolinestop: args.append('-nolinestop')
if all: args.append('-all')
if overlap: args.append('-overlap')
if strictlimits: args.append('-strictlimits')
if pattern and pattern[0] == '-': args.append('--')
args.append(pattern)
args.append(index)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add support for ``-nolinestop``, ``-all``, ``-overlap``, and ``-strictlimits`` options to ``!tkinter.Text.search``
Loading