diff --git a/IPython/frontend/cocoa/cocoa_frontend.py b/IPython/frontend/cocoa/cocoa_frontend.py index 9f9aa55..63c7947 100644 --- a/IPython/frontend/cocoa/cocoa_frontend.py +++ b/IPython/frontend/cocoa/cocoa_frontend.py @@ -288,9 +288,13 @@ class IPythonCocoaController(NSObject, FrontEndBase): def current_indent_string(self): """returns string for indent or None if no indent""" - if(len(self.current_block()) > 0): - lines = self.current_block().split('\n') - currentIndent = len(lines[-1]) - len(lines[-1]) + return self._indent_for_block(self.currentBlock()) + + + def _indent_for_block(self, block): + lines = block.split('\n') + if(len(lines) > 1): + currentIndent = len(lines[-1]) - len(lines[-1].lstrip()) if(currentIndent == 0): currentIndent = self.tabSpaces diff --git a/IPython/frontend/cocoa/tests/test_cocoa_frontend.py b/IPython/frontend/cocoa/tests/test_cocoa_frontend.py index eb4ae43..d85728d 100644 --- a/IPython/frontend/cocoa/tests/test_cocoa_frontend.py +++ b/IPython/frontend/cocoa/tests/test_cocoa_frontend.py @@ -70,3 +70,22 @@ class TestIPythonCocoaControler(DeferredTestCase): self.controller.execute(code).addCallback(testCompletes) + + def testCurrentIndent(self): + """test that current_indent_string returns current indent or None. + Uses _indent_for_block for direct unit testing. + """ + + self.controller.tabUsesSpaces = True + self.assert_(self.controller._indent_for_block("""a=3""") == None) + self.assert_(self.controller._indent_for_block("") == None) + block = """def test():\n a=3""" + self.assert_(self.controller._indent_for_block(block) == \ + ' ' * self.controller.tabSpaces) + + block = """if(True):\n%sif(False):\n%spass""" % \ + (' '*self.controller.tabSpaces, + 2*' '*self.controller.tabSpaces) + self.assert_(self.controller._indent_for_block(block) == \ + 2*(' '*self.controller.tabSpaces)) +