Skip to content

Commit

Permalink
Fix issues with editing/deleting a folded scope
Browse files Browse the repository at this point in the history
  • Loading branch information
ColinDuquesnoy committed Jun 27, 2015
1 parent bb2dedc commit fdec93f
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 26 deletions.
2 changes: 1 addition & 1 deletion pyqode/core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@
widget, i.e. pyqode.core is a generic code editor widget.
"""

__version__ = '2.6.5'
__version__ = '2.6.6.dev1'
3 changes: 3 additions & 0 deletions pyqode/core/api/folding.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,3 +387,6 @@ def find_parent_scope(block):
if counter < limit:
return block
return None

def __repr__(self):
return 'FoldScope(start=%r, end=%d)' % self.get_range()
3 changes: 1 addition & 2 deletions pyqode/core/api/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,7 @@ def goto_line(self, line, column=0, move=True):
pass
else:
from pyqode.core.api.folding import FoldScope
if not block.isVisible() or TextBlockHelper.is_fold_trigger(
block):
if not block.isVisible():
block = FoldScope.find_parent_scope(block)
if TextBlockHelper.is_collapsed(block):
folding_panel.toggle_fold_trigger(block)
Expand Down
50 changes: 27 additions & 23 deletions pyqode/core/panels/folding.py
Original file line number Diff line number Diff line change
Expand Up @@ -640,35 +640,39 @@ def on_state_changed(self, state):
self._block_nbr = -1
self.editor.new_text_set.disconnect(self._clear_block_deco)

def _select_scope(self, block, c):
"""
Select the content of a scope
"""
start_block = block
_, end = FoldScope(block).get_range()
end_block = self.editor.document().findBlockByNumber(end)
c.beginEditBlock()
c.setPosition(start_block.position())
c.setPosition(end_block.position(), c.KeepAnchor)
c.deleteChar()
c.endEditBlock()

def _on_key_pressed(self, event):
"""
Override key press to select the current scope if the user wants
to deleted a folded scope (without selecting it).
"""
if event.text() or event.key() in [QtCore.Qt.Key_Backspace,
QtCore.Qt.Key_Delete,
QtCore.Qt.Key_Return]:
delete_request = event.key() in [QtCore.Qt.Key_Backspace,
QtCore.Qt.Key_Delete]
if event.text() or delete_request:
cursor = self.editor.textCursor()
block = self.editor.document().findBlock(cursor.position())
th = TextBlockHelper()
if th.is_fold_trigger(block) and th.is_collapsed(block):
self.toggle_fold_trigger(block)
if event.key() == QtCore.Qt.Key_Return:
# don't process return, just expand tab
event.setAccepted(True)
if cursor.hasSelection():
# change selection to encompass the whole scope.
positions_to_check = cursor.selectionStart(), cursor.selectionEnd()
else:
positions_to_check = (cursor.position(), )
for pos in positions_to_check:
block = self.editor.document().findBlock(pos)
th = TextBlockHelper()
if th.is_fold_trigger(block) and th.is_collapsed(block):
self.toggle_fold_trigger(block)
if delete_request and cursor.hasSelection():
scope = FoldScope(self.find_parent_scope(block))
tc = TextHelper(self.editor).select_lines(*scope.get_range())
if tc.selectionStart() > cursor.selectionStart():
start = cursor.selectionStart()
else:
start = tc.selectionStart()
if tc.selectionEnd() < cursor.selectionEnd():
end = cursor.selectionEnd()
else:
end = tc.selectionEnd()
tc.setPosition(start)
tc.setPosition(end, tc.KeepAnchor)
self.editor.setTextCursor(tc)

@staticmethod
def _show_previous_blank_lines(block):
Expand Down

0 comments on commit fdec93f

Please sign in to comment.