Anonymous avatar Anonymous committed 42ec0eb

Deindentation using backspace at the first non-space char of line

Comments (0)

Files changed (5)

docs/workingon.txt

+* Pressing back space should deindent; shortcuts for indenting and deindenting
 
+* Indenter.deindent
+^ Rename Indenter.indent_line to Indenter.correct_indentation
 
 ? Adding EditorManager class
 ? Adding selection list class
         create_dialog = Frame(toplevel)
         parent_label = Label(create_dialog, text=parent_name)
         parent_entry = Entry(create_dialog)
-        resource_label = Label(create_dialog, text=('New' + resource_name))
+        resource_label = Label(create_dialog, text=('New ' + resource_name))
         resource_entry = Entry(create_dialog)
         
         def do_create_resource():
         self.text.bind('<<BackwardSearch>>',
                        lambda event: self._search_event(False))
         self.text.bind('<Any-KeyPress>', self._search_handler)
+        def backspace(event):
+            line_starting = self.text.get('insert linestart', 'insert')
+            current_char = self.text.get(INSERT)
+            if line_starting.isspace() and (not current_char.isspace() 
+                                            or current_char == '' or current_char == '\n'):
+                self.indenter.deindent(self.get_insert())
+                return 'break'
+        self.text.bind('<BackSpace>', backspace, '+')
+
 
     def get_text(self):
         return self.text.get('1.0', END)[0 : -1]
         return new_indent
 
     def indent_line(self, index):
+        '''Correct the indentation of the line containing the given index'''
         start = self._get_line_start(index)
         self._set_line_indents(start, self._get_correct_indentation(start))
 
+    def deindent(self, index):
+        '''Deindent the line containing the given index'''
+        start = self._get_line_start(index)
+        indents = self._count_line_indents(start)
+        new_indents = max(0, indents - 4)
+        self._set_line_indents(start, new_indents)

ropetest/indentertest.py

         self.indenter.indent_line(self.editor.get_end())
         self.assertEquals('def f():\n    return (2,\n            3)\na = 10', self.editor.get_text())
 
+    def test_deindenting_empty_lines(self):
+        self.editor.set_text('\n')
+        self.indenter.deindent(self.editor.get_end())
+        self.assertEquals('\n', self.editor.get_text())
+
+    def test_deindenting_four_spaces(self):
+        self.editor.set_text('    print "hello"\n')
+        self.indenter.deindent(self.editor.get_start())
+        self.assertEquals('print "hello"\n', self.editor.get_text())
+
+    def test_deindenting(self):
+        self.editor.set_text('def f()\n    print "hello"\n    def g():\n')
+        self.indenter.deindent(self.editor.get_relative(self.editor.get_end(), -2))
+        self.assertEquals('def f()\n    print "hello"\ndef g():\n', self.editor.get_text())
+
 
 if __name__ == '__main__':
     unittest.main()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.