Skip to content

Commit

Permalink
owpythonscript: Add toggle comment action (Ctrl/CMD + /)
Browse files Browse the repository at this point in the history
  • Loading branch information
irgolic committed Aug 18, 2020
1 parent 6452e35 commit 703f65a
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 59 deletions.
60 changes: 60 additions & 0 deletions Orange/widgets/data/utils/pythoneditor/editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,9 @@ def createAction(text, shortcut, slot, iconFileName=None):

return action

# custom Orange actions
self.commentLine = createAction('Toggle comment line', 'Ctrl+/', self._onToggleCommentLine)

# scrolling
self.scrollUpAction = createAction('Scroll up', 'Ctrl+Up',
lambda: self._onShortcutScroll(down=False),
Expand Down Expand Up @@ -222,6 +225,63 @@ def createAction(text, shortcut, slot, iconFileName=None):
self._onShortcutDuplicateLine)
self.invokeCompletionAction = createAction('Invoke completion', 'Ctrl+Space',
self._onCompletion)
def _onToggleCommentLine(self):
cursor: QTextCursor = self.textCursor()
cursor.beginEditBlock()

startBlock = self.document().findBlock(cursor.selectionStart())
endBlock = self.document().findBlock(cursor.selectionEnd())

def lineIndentationLength(text):
return len(text) - len(text.lstrip())

def isHashCommentSelected(lines):
return all(not line.strip() or line.lstrip().startswith('#') for line in lines)

blocks = []
lines = []

block = startBlock
line = block.text()
if block != endBlock or line.strip():
blocks += [block]
lines += [line]
while block != endBlock:
block = block.next()
line = block.text()
if line.strip():
blocks += [block]
lines += [line]

if isHashCommentSelected(lines):
# remove the hash comment
for block, text in zip(blocks, lines):
cursor = QTextCursor(block)
cursor.setPosition(block.position() + lineIndentationLength(text))
for _ in range(lineIndentationLength(text[lineIndentationLength(text) + 1:]) + 1):
cursor.deleteChar()
else:
# add a hash comment
for block, text in zip(blocks, lines):
cursor = QTextCursor(block)
cursor.setPosition(block.position() + lineIndentationLength(text))
cursor.insertText('# ')

if endBlock == self.document().lastBlock():
if endBlock.text().strip():
cursor = QTextCursor(endBlock)
cursor.movePosition(QTextCursor.End)
self.setTextCursor(cursor)
self._insertNewBlock()
cursorBlock = endBlock.next()
else:
cursorBlock = endBlock
else:
cursorBlock = endBlock.next()
cursor = QTextCursor(cursorBlock)
cursor.movePosition(QTextCursor.EndOfBlock)
self.setTextCursor(cursor)
cursor.endEditBlock()

def _onShortcutIndent(self):
if self.textCursor().hasSelection():
Expand Down
59 changes: 0 additions & 59 deletions Orange/widgets/data/utils/pythoneditor/tests/test_actions.py

This file was deleted.

20 changes: 20 additions & 0 deletions Orange/widgets/data/utils/pythoneditor/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,26 @@ def test_2(self):
self.assertTrue(self.qpart.isComment(0, 0))


class ToggleCommentTest(_BaseTest):
def test_single_line(self):
self.qpart.text = 'a = 2'
self.qpart._onToggleCommentLine()
self.assertEqual('# a = 2\n', self.qpart.text)
self.qpart._onToggleCommentLine()
self.assertEqual('# a = 2\n', self.qpart.text)
self.qpart._selectLines(0, 0)
self.qpart._onToggleCommentLine()
self.assertEqual('a = 2\n', self.qpart.text)

def test_two_lines(self):
self.qpart.text = 'a = 2\nb = 3'
self.qpart._selectLines(0, 1)
self.qpart._onToggleCommentLine()
self.assertEqual('# a = 2\n# b = 3\n', self.qpart.text)
self.qpart.undo()
self.assertEqual('a = 2\nb = 3', self.qpart.text)


class Signals(_BaseTest):
def test_indent_width_changed(self):
newValue = [None]
Expand Down
1 change: 1 addition & 0 deletions doc/visual-programming/source/widgets/data/pythonscript.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ Editor keyboard shortcuts
- **Shift+Enter**: Run script
- **Ctrl/CMD+Z**: Undo
- **Ctrl/CMD+Y**: Redo
- **CTRL/CMD+/**: Toggle comment
- **Alt+Up**: Move line up
- **Alt+Down**: Move line down
- **CTRL/CMD+D**: Delete line
Expand Down

0 comments on commit 703f65a

Please sign in to comment.