File size: 5,148 Bytes
5b76e0f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
from __future__ import annotations

from dataclasses import dataclass


@dataclass
class TextEditorBackend:
    """Represents a text editor (some text and a cursor)"""

    content: str = ""
    cursor_index: int = 0

    def set_content(self, text: str) -> None:
        """Set the content of the editor

        Args:
            text (str): The text to set as the content
        """
        self.content = text

    def delete_back(self) -> bool:
        """Delete the character behind the cursor and moves cursor back. If the
        cursor is at the start of the content, does nothing other than immediately
        return False.

        Returns:
            bool: True if the text content was modified. False otherwise.
        """
        if self.cursor_index == 0:
            return False

        new_text = (
            self.content[: self.cursor_index - 1] + self.content[self.cursor_index :]
        )
        self.content = new_text
        self.cursor_index = max(0, self.cursor_index - 1)
        return True

    def delete_forward(self) -> bool:
        """Delete the character in front of the cursor without moving the cursor.

        Returns:
            bool: True if the text content was modified. False otherwise.
        """
        if self.cursor_index == len(self.content):
            return False

        new_text = (
            self.content[: self.cursor_index] + self.content[self.cursor_index + 1 :]
        )
        self.content = new_text
        return True

    def cursor_left(self) -> bool:
        """Move the cursor 1 character left in the text. Is a noop if cursor is at start.

        Returns:
            bool: True if the cursor moved. False otherwise.
        """
        previous_index = self.cursor_index
        new_index = max(0, previous_index - 1)
        self.cursor_index = new_index
        return previous_index != new_index

    def cursor_right(self) -> bool:
        """Move the cursor 1 character right in the text. Is a noop if the cursor is at end.

        Returns:
            bool: True if the cursor moved. False otherwise.
        """
        previous_index = self.cursor_index
        new_index = min(len(self.content), previous_index + 1)
        self.cursor_index = new_index
        return previous_index != new_index

    def query_cursor_left(self) -> bool:
        """Check if the cursor can move 1 codepoint left in the text.

        Returns:
            bool: True if the cursor can move left. False otherwise.
        """
        previous_index = self.cursor_index
        new_index = max(0, previous_index - 1)
        return previous_index != new_index

    def query_cursor_right(self) -> str | None:
        """Check if the cursor can move right (we can't move right if we're at the end)
        and return the codepoint to the right of the cursor if it exists. If it doesn't
        exist (e.g. we're at the end), then return None

        Returns:
            str: The codepoint to the right of the cursor if it exists, otherwise None.
        """
        previous_index = self.cursor_index
        new_index = min(len(self.content), previous_index + 1)
        if new_index == len(self.content):
            return None
        elif previous_index != new_index:
            return self.content[new_index]
        return None

    def cursor_text_start(self) -> bool:
        """Move the cursor to the start of the text

        Returns:
            bool: True if the cursor moved. False otherwise.
        """
        if self.cursor_index == 0:
            return False

        self.cursor_index = 0
        return True

    def cursor_text_end(self) -> bool:
        """Move the cursor to the end of the text

        Returns:
            bool: True if the cursor moved. False otherwise.
        """
        text_length = len(self.content)
        if self.cursor_index == text_length:
            return False

        self.cursor_index = text_length
        return True

    def insert(self, text: str) -> bool:
        """Insert some text at the cursor position, and move the cursor
        to the end of the newly inserted text.

        Args:
            text: The text to insert

        Returns:
            bool: Always returns True since text should be insertable regardless of cursor location
        """
        new_text = (
            self.content[: self.cursor_index] + text + self.content[self.cursor_index :]
        )
        self.content = new_text
        self.cursor_index = min(len(self.content), self.cursor_index + len(text))
        return True

    def get_range(self, start: int, end: int) -> str:
        """Return the text between 2 indices. Useful for previews/views into
        a subset of the content e.g. scrollable single-line input fields

        Args:
            start (int): The starting index to return text from (inclusive)
            end (int): The index to return text up to (exclusive)

        Returns:
            str: The sliced string between start and end.
        """
        return self.content[start:end]

    @property
    def cursor_at_end(self):
        return self.cursor_index == len(self.content)