Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
6 changes: 2 additions & 4 deletions src/prompt_toolkit/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -457,12 +457,10 @@ def get_word_before_cursor(
def _is_word_before_cursor_complete(
self, WORD: bool = False, pattern: Pattern[str] | None = None
) -> bool:
if not self.text_before_cursor == "" or self.text_before_cursor[-1:].isspace():
return True
if pattern:
return self.find_start_of_previous_word(WORD=WORD, pattern=pattern) is None
else:
return (
self.text_before_cursor == "" or self.text_before_cursor[-1:].isspace()
)

def find_start_of_previous_word(
self, count: int = 1, WORD: bool = False, pattern: Pattern[str] | None = None
Expand Down
11 changes: 11 additions & 0 deletions tests/test_document.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import pytest
import re

from prompt_toolkit.document import Document

Expand Down Expand Up @@ -67,3 +68,13 @@ def test_translate_index_to_position(document):
def test_is_cursor_at_the_end(document):
assert Document("hello", 5).is_cursor_at_the_end
assert not Document("hello", 4).is_cursor_at_the_end


def test_get_word_before_cursor_with_whitespace_and_pattern():
text = "foobar "
document = Document(text=text, cursor_position=len(text))

assert document.get_word_before_cursor() == ""

_FIND_WORD_RE = re.compile(r"([a-zA-Z0-9_]+|[^a-zA-Z0-9_\s]+)")
assert document.get_word_before_cursor(pattern=_FIND_WORD_RE) == ""