Skip to content

Commit 75eca87

Browse files
alexprabhat99Alex Prabhat Bara
andauthored
Fix get_word_before_cursor behavior (#2025)
Co-authored-by: Alex Prabhat Bara <[email protected]>
1 parent d8adbe9 commit 75eca87

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

src/prompt_toolkit/document.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -457,12 +457,10 @@ def get_word_before_cursor(
457457
def _is_word_before_cursor_complete(
458458
self, WORD: bool = False, pattern: Pattern[str] | None = None
459459
) -> bool:
460+
if not self.text_before_cursor == "" or self.text_before_cursor[-1:].isspace():
461+
return True
460462
if pattern:
461463
return self.find_start_of_previous_word(WORD=WORD, pattern=pattern) is None
462-
else:
463-
return (
464-
self.text_before_cursor == "" or self.text_before_cursor[-1:].isspace()
465-
)
466464

467465
def find_start_of_previous_word(
468466
self, count: int = 1, WORD: bool = False, pattern: Pattern[str] | None = None

tests/test_document.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import annotations
22

33
import pytest
4+
import re
45

56
from prompt_toolkit.document import Document
67

@@ -67,3 +68,13 @@ def test_translate_index_to_position(document):
6768
def test_is_cursor_at_the_end(document):
6869
assert Document("hello", 5).is_cursor_at_the_end
6970
assert not Document("hello", 4).is_cursor_at_the_end
71+
72+
73+
def test_get_word_before_cursor_with_whitespace_and_pattern():
74+
text = "foobar "
75+
document = Document(text=text, cursor_position=len(text))
76+
77+
assert document.get_word_before_cursor() == ""
78+
79+
_FIND_WORD_RE = re.compile(r"([a-zA-Z0-9_]+|[^a-zA-Z0-9_\s]+)")
80+
assert document.get_word_before_cursor(pattern=_FIND_WORD_RE) == ""

0 commit comments

Comments
 (0)