Commits

Henning Schröder  committed 9d7f9b6

semi smart auto indent/dedent in editor

  • Participants
  • Parent commits dbf6e7e

Comments (0)

Files changed (1)

File embpyqt/python/embeddedpyqt/miniconsole.py

         super(EditorWidget, self).__init__(parent)
         self.highlighter = PythonHighlighter(self)
 
+        
+    def keyPressEvent(self, event):
+        key = event.key()
+        if key == Qt.Key_Tab:
+            self.textCursor().insertText(" " * 4)
+            event.accept()
+        elif key in (Qt.Key_Return, Qt.Key_Enter):
+            cursor = self.textCursor()
+            line = unicode(cursor.block().text())
+            indent = (len(line) - len(line.lstrip(" "))) / 4
+            cursor.insertText("\n")
+            if line.endswith(":"):
+                indent += 1
+            elif line.strip().startswith(("return", "pass")):
+                indent = max(indent - 1, 0)
+            cursor.insertText(" " * (indent*4))
+            event.accept()
+        else:
+            return super(EditorWidget, self).keyPressEvent(event)
+
+
 
 class OutputWidget(QPlainTextEdit):