Skip to content

Commit 98172d6

Browse files
authored
fix(text area): fix mouse selection with tab characters (#5237)
1 parent 0e9e320 commit 98172d6

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

Diff for: CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1010
### Fixed
1111

1212
- Fixed duplicated key displays in the help panel https://github.com/Textualize/textual/issues/5037
13+
- Fixed `TextArea` mouse selection with tab characters https://github.com/Textualize/textual/issues/5212
1314

1415
### Added
1516

Diff for: src/textual/document/_wrapped_document.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,9 @@ def offset_to_location(self, offset: Offset) -> Location:
295295
if not self._width:
296296
# No wrapping, so we directly map offset to location and clamp.
297297
line_index = min(y, len(self._wrap_offsets) - 1)
298-
column_index = min(x, len(self.document.get_line(line_index)))
298+
column_index = cell_width_to_column_index(
299+
self.document.get_line(line_index), x, self._tab_width
300+
)
299301
return line_index, column_index
300302

301303
# Find the line corresponding to the given y offset in the wrapped document.

Diff for: tests/text_area/test_selection.py

+19
Original file line numberDiff line numberDiff line change
@@ -334,3 +334,22 @@ def compose(self) -> ComposeResult:
334334

335335
assert text_area.cursor_screen_offset == (5, 1)
336336
assert app.cursor_position == (5, 1)
337+
338+
339+
async def test_mouse_selection_with_tab_characters():
340+
"""Regression test for https://github.com/Textualize/textual/issues/5212"""
341+
342+
class TextAreaTabsApp(App):
343+
def compose(self) -> ComposeResult:
344+
yield TextArea(soft_wrap=False, text="\t\t")
345+
346+
app = TextAreaTabsApp()
347+
async with app.run_test() as pilot:
348+
text_area = pilot.app.query_one(TextArea)
349+
expected_selection = Selection((0, 0), (0, 0))
350+
assert text_area.selection == expected_selection
351+
352+
await pilot.mouse_down(text_area, offset=(2, 1))
353+
await pilot.hover(text_area, offset=(3, 1))
354+
355+
assert text_area.selection == expected_selection

0 commit comments

Comments
 (0)