FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-94521: Copy or cut current line while no selection by znsoooo · Pull Request #145134 · python/cpython · GitHub

Repository navigation

Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (2) .rst  (2) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
4 changes: 4 additions & 0 deletions Doc/library/idle.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,11 @@ Select All

Cut
Copy selection into the system-wide clipboard; then delete the selection.
If there is no selection, cut the current line instead.

Copy
Copy selection into the system-wide clipboard.
If there is no selection, copy the current line instead.

Paste
Insert contents of the system-wide clipboard into the current window.
Expand Down Expand Up @@ -371,9 +373,11 @@ Context menus have the standard clipboard functions also on the Edit menu.

Cut
Copy selection into the system-wide clipboard; then delete the selection.
If there is no selection, cut the current line instead.

Copy
Copy selection into the system-wide clipboard.
If there is no selection, copy the current line instead.

Paste
Insert contents of the system-wide clipboard into the current window.
Expand Down
17 changes: 12 additions & 5 deletions Lib/idlelib/editor.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -610,14 +610,21 @@ def python_docs(self, event=None):
return "break"

def cut(self,event):
self.text.event_generate("<<Cut>>")
if not self.text.tag_ranges('sel'):
self.copy(event)
self.text.delete('insert', 'insert+1l')
else:
self.text.event_generate("<<Cut>>")
return "break"

def copy(self,event):
if not self.text.tag_ranges("sel"):
# There is no selection, so do nothing and maybe interrupt.
return None
self.text.event_generate("<<Copy>>")
if not self.text.tag_ranges('sel'):
self.text.mark_set('insert', 'insert linestart')
line = self.text.get('insert', 'insert+1l')
self.text.clipboard_clear()
self.text.clipboard_append(line)
else:
self.text.event_generate("<<Copy>>")
return "break"

def paste(self,event):
Expand Down
46 changes: 46 additions & 0 deletions Lib/idlelib/idle_test/test_editor.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -401,5 +401,51 @@ def test_ignore_choice_clears_mtime(self):
stub.set_saved.assert_not_called()


class CopyCutLineTest(unittest.TestCase):

@classmethod
def setUpClass(cls):
requires('gui')
cls.root = Tk()
cls.root.withdraw()
cls.window = Editor(root=cls.root)

@classmethod
def tearDownClass(cls):
cls.root.destroy()
del cls.root

def test_copy_no_selection(self):
eq = self.assertEqual
text = self.window.text

insert(text, 'hello\nworld')
text.mark_set('insert', '1.2')
self.window.copy(event=None)

# Copy current line without being selected
eq(text.clipboard_get(), 'hello\n')
eq(text.tag_ranges('sel'), ())

# Cursor move to start of the line
eq(text.index('insert'), '1.0')

def test_cut_no_selection(self):
eq = self.assertEqual
text = self.window.text

insert(text, 'hello\nworld')
text.mark_set('insert', '1.2')
self.window.cut(event=None)

# Copy current line and remove it
eq(text.clipboard_get(), 'hello\n')
eq(text.get('1.0', 'end'), 'world\n')
eq(text.tag_ranges('sel'), ())

# Cursor move to start of the line
eq(text.index('insert'), '1.0')


if __name__ == '__main__':
unittest.main(verbosity=2)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Copy or cut current line while no selection. Patch by Shixian Li.
Loading

Back | FazBrowse Home | New Git URL