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

Fix smart backspace and auto indent modes #12

Merged
merged 2 commits into from
Jan 28, 2025
Merged
Show file tree
Hide file tree
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
23 changes: 7 additions & 16 deletions pyqodeng/core/modes/autoindent.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,10 @@ def _on_key_pressed(self, event):
Auto indent if the released key is the return key.
:param event: the key event
"""
if not event.isAccepted():
if event.key() == Qt.Key_Return or event.key() == Qt.Key_Enter:
cursor = self.editor.textCursor()
pre, post = self._get_indent(cursor)
cursor.beginEditBlock()
cursor.insertText("%s\n%s" % (pre, post))

# eats possible whitespaces
cursor.movePosition(QtGui.QTextCursor.WordRight, QtGui.QTextCursor.KeepAnchor)
txt = cursor.selectedText()
if txt.startswith(' '):
new_txt = txt.replace(" ", '')
if len(txt) > len(new_txt):
cursor.insertText(new_txt)
cursor.endEditBlock()
event.accept()
if not event.isAccepted() and (event.key() == Qt.Key_Return or event.key() == Qt.Key_Enter):
cursor = self.editor.textCursor()
pre, post = self._get_indent(cursor)
cursor.beginEditBlock()
cursor.insertText(f"{pre}\n{post}")
cursor.endEditBlock()
event.accept()
9 changes: 4 additions & 5 deletions pyqodeng/core/modes/backspace.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""
This module contains the smart backspace mode
"""
from qtpy import QtCore, QtGui
from qtpy.QtGui import QTextCursor
from pyqodeng.core.api import Mode


Expand All @@ -20,8 +20,7 @@ def on_state_changed(self, state):
self.editor.key_pressed.disconnect(self._on_key_pressed)

def _on_key_pressed(self, event):
no_modifiers = int(event.modifiers()) == QtCore.Qt.NoModifier
if event.key() == QtCore.Qt.Key_Backspace and no_modifiers:
if event.key() == Qt.Key_Backspace and not event.modifiers():
if self.editor.textCursor().atBlockStart():
return
tab_len = self.editor.tab_length
Expand All @@ -30,10 +29,10 @@ def _on_key_pressed(self, event):
tab_len = self.editor.tab_length
# count the number of spaces deletable, stop at tab len
spaces = 0
cursor = QtGui.QTextCursor(self.editor.textCursor())
cursor = QTextCursor(self.editor.textCursor())
while spaces < tab_len or cursor.atBlockStart():
pos = cursor.position()
cursor.movePosition(QtGui.QTextCursor.Left, QtGui.QTextCursor.KeepAnchor)
cursor.movePosition(QTextCursor.Left, QTextCursor.KeepAnchor)
char = cursor.selectedText()
if char == " ":
spaces += 1
Expand Down