Skip to content

Commit

Permalink
fix(text area): fix mouse selection with tab characters (#5237)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomJGooding authored Nov 15, 2024
1 parent 0e9e320 commit 98172d6
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
### Fixed

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

### Added

Expand Down
4 changes: 3 additions & 1 deletion src/textual/document/_wrapped_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,9 @@ def offset_to_location(self, offset: Offset) -> Location:
if not self._width:
# No wrapping, so we directly map offset to location and clamp.
line_index = min(y, len(self._wrap_offsets) - 1)
column_index = min(x, len(self.document.get_line(line_index)))
column_index = cell_width_to_column_index(
self.document.get_line(line_index), x, self._tab_width
)
return line_index, column_index

# Find the line corresponding to the given y offset in the wrapped document.
Expand Down
19 changes: 19 additions & 0 deletions tests/text_area/test_selection.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,3 +334,22 @@ def compose(self) -> ComposeResult:

assert text_area.cursor_screen_offset == (5, 1)
assert app.cursor_position == (5, 1)


async def test_mouse_selection_with_tab_characters():
"""Regression test for https://github.com/Textualize/textual/issues/5212"""

class TextAreaTabsApp(App):
def compose(self) -> ComposeResult:
yield TextArea(soft_wrap=False, text="\t\t")

app = TextAreaTabsApp()
async with app.run_test() as pilot:
text_area = pilot.app.query_one(TextArea)
expected_selection = Selection((0, 0), (0, 0))
assert text_area.selection == expected_selection

await pilot.mouse_down(text_area, offset=(2, 1))
await pilot.hover(text_area, offset=(3, 1))

assert text_area.selection == expected_selection

0 comments on commit 98172d6

Please sign in to comment.