Skip to content

Commit 0423f2b

Browse files
Added delete_selection
1 parent a2a1230 commit 0423f2b

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

src/prompt_toolkit/document.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1036,6 +1036,32 @@ def cut_selection(self) -> tuple[Document, ClipboardData]:
10361036
else:
10371037
return self, ClipboardData("")
10381038

1039+
def delete_selection(self) -> Document:
1040+
"""
1041+
Returns :class:`.Document` where the
1042+
document represents the new document when the selection is deleted.
1043+
"""
1044+
if self.selection:
1045+
# cut_parts = []
1046+
remaining_parts = []
1047+
new_cursor_position = self.cursor_position
1048+
1049+
last_to = 0
1050+
for from_, to in self.selection_ranges():
1051+
if last_to == 0:
1052+
new_cursor_position = from_
1053+
1054+
remaining_parts.append(self.text[last_to:from_])
1055+
last_to = to
1056+
1057+
remaining_parts.append(self.text[last_to:])
1058+
1059+
remaining_text = "".join(remaining_parts)
1060+
1061+
return Document(text=remaining_text, cursor_position=new_cursor_position)
1062+
else:
1063+
return self
1064+
10391065
def paste_clipboard_data(
10401066
self,
10411067
data: ClipboardData,

0 commit comments

Comments
 (0)