From 5dd44c947c6fe89d2f3f902d4ae3375ef1fa3b33 2018-10-20 15:53:25 From: Nguyen Duy Hai Date: 2018-10-20 15:53:25 Subject: [PATCH] Fix indentation for nested block --- diff --git a/IPython/core/inputtransformer2.py b/IPython/core/inputtransformer2.py index e2dd2d0..b6bbcb1 100644 --- a/IPython/core/inputtransformer2.py +++ b/IPython/core/inputtransformer2.py @@ -655,7 +655,15 @@ class TransformerManager: if len(tokens_by_line) == 1 and not tokens_by_line[-1]: return 'incomplete', 0 - if tokens_by_line[-1][-1].string == ':': + new_block = False + for token in reversed(tokens_by_line[-1]): + if token.type == tokenize.DEDENT: + continue + elif token.string == ':': + new_block = True + break + + if new_block: # The last line starts a block (e.g. 'if foo:') ix = 0 while tokens_by_line[-1][ix].type in {tokenize.INDENT, tokenize.DEDENT}: diff --git a/IPython/core/tests/test_inputtransformer2.py b/IPython/core/tests/test_inputtransformer2.py index d6c2fa3..ea06452 100644 --- a/IPython/core/tests/test_inputtransformer2.py +++ b/IPython/core/tests/test_inputtransformer2.py @@ -207,6 +207,7 @@ def test_check_complete(): cc = ipt2.TransformerManager().check_complete nt.assert_equal(cc("a = 1"), ('complete', None)) nt.assert_equal(cc("for a in range(5):"), ('incomplete', 4)) + nt.assert_equal(cc("for a in range(5):\n if a > 0:"), ('incomplete', 8)) nt.assert_equal(cc("raise = 2"), ('invalid', None)) nt.assert_equal(cc("a = [1,\n2,"), ('incomplete', 0)) nt.assert_equal(cc(")"), ('incomplete', 0))