Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added delete_selection #1942

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
26 changes: 26 additions & 0 deletions src/prompt_toolkit/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -1036,6 +1036,32 @@ def cut_selection(self) -> tuple[Document, ClipboardData]:
else:
return self, ClipboardData("")

def delete_selection(self) -> Document:
"""
Returns :class:`.Document` where the
document represents the new document when the selection is deleted.
"""
if self.selection:
# cut_parts = []
remaining_parts = []
new_cursor_position = self.cursor_position

last_to = 0
for from_, to in self.selection_ranges():
if last_to == 0:
new_cursor_position = from_

remaining_parts.append(self.text[last_to:from_])
last_to = to

remaining_parts.append(self.text[last_to:])

remaining_text = "".join(remaining_parts)

return Document(text=remaining_text, cursor_position=new_cursor_position)
else:
return self

def paste_clipboard_data(
self,
data: ClipboardData,
Expand Down
Loading