# Buggy code
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).
    """
    delete_request = event.key() in [Qt.Key_Backspace, Qt.Key_Delete]
    if event.text() or delete_request:
        cursor = self.editor.textCursor()
        self._key_pressed = 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)
            start_line = block.blockNumber() + 1
            if start_line in self.folding_regions and self.folding_status[start_line]:
                end_line = self.folding_regions[start_line]
                self.toggle_fold_trigger(block.next())
                if delete_request and cursor.hasSelection():
                    tc = TextHelper(self.editor).select_lines(start_line, end_line)
                    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)
                    self.folding_regions.pop(start_line)
                    self.folding_status.pop(start_line)
        self._key_pressed = False


# Traceback (KeyError)
File "C:\Users\marco\Anaconda3\lib\site-packages\spyder\plugins\editor\utils\editor.py", line 161, in _exec_requested_job
self._job(*self._args, **self._kwargs)
File "C:\Users\marco\Anaconda3\lib\site-packages\spyder\plugins\editor\panels\codefolding.py", line 471, in _highlight_block
end_line = self.folding_regions[block_line]
KeyError: 155

# Fixed code
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).
    """
    delete_request = event.key() in {Qt.Key_Delete, Qt.Key_Backspace}
    cursor = self.editor.textCursor()
    if cursor.hasSelection():
        if event.key() == Qt.Key_Return:
            delete_request = True

    if event.text() or delete_request:
        self._key_pressed = 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)
            start_line = block.blockNumber() + 2
            if start_line in self.folding_regions and self.folding_status[start_line]:
                end_line = self.folding_regions[start_line]
                if delete_request and cursor.hasSelection():
                    tc = TextHelper(self.editor).select_lines(start_line, end_line)
                    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)
        self._key_pressed = False