Skip to content

Commit aa0f450

Browse files
committed
Implement auto dedentation.
You can set the number of empty lines after which auto dedentation will happen with the new config option `dedent_after` (set to 0 to disable auto dedentation). Also fix a stupid typo. This will close issue bpython#72.
1 parent 8d1eb74 commit aa0f450

File tree

4 files changed

+21
-6
lines changed

4 files changed

+21
-6
lines changed

bpython/cli.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464

6565
from bpython import __version__
6666
from bpython.pager import page
67-
from bpython.repl import Interpreter, Repl, next_indentantion
67+
from bpython.repl import Interpreter, Repl, next_indentation
6868

6969

7070
def log(x):
@@ -538,13 +538,12 @@ def get_line(self):
538538
which returns None if Enter is pressed (that means "Return",
539539
idiot)."""
540540

541-
indentation = next_indentantion(self.s)
542541
self.s = ''
543542
self.rl_history.reset()
544543
self.iy, self.ix = self.scr.getyx()
545544

546545
if not self.paste_mode:
547-
for _ in xrange(indentation):
546+
for _ in xrange(self.next_indentation()):
548547
self.p_key('\t')
549548

550549
self.cpos = 0

bpython/config.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ def loadini(struct, configfile):
3737
'arg_spec': True,
3838
'auto_display_list': True,
3939
'color_scheme': 'default',
40+
'dedent_after': 1,
4041
'flush_output': True,
4142
'highlight_show_source': True,
4243
'hist_file': '~/.pythonhist',
@@ -63,6 +64,7 @@ def loadini(struct, configfile):
6364
'yank_from_buffer': 'C-y'}})
6465
config.read(config_path)
6566

67+
struct.dedent_after = config.getint('general', 'dedent_after')
6668
struct.tab_length = config.getint('general', 'tab_length')
6769
struct.auto_display_list = config.getboolean('general',
6870
'auto_display_list')

bpython/gtk_.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ def do_key_press_event(self, event):
377377
if self.reset_indent:
378378
self.reset_indent = False
379379
else:
380-
indentation = repl.next_indentantion(line)
380+
indentation = self.next_indentation()
381381
if indentation:
382382
with self.editing:
383383
self.text_buffer.insert(self.get_cursor_iter(),

bpython/repl.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -568,6 +568,20 @@ def format_docstring(self, docstring, width, height):
568568
out[-1] = out[-1].rstrip()
569569
return out
570570

571+
def next_indentation(self):
572+
"""Return the indentation of the next line based on the current
573+
input buffer."""
574+
if self.buffer:
575+
indentation = next_indentation(self.buffer[-1])
576+
if indentation and OPTS.dedent_after > 0:
577+
line_is_empty = lambda line: not line.strip()
578+
empty_lines = takewhile(line_is_empty, reversed(self.buffer))
579+
if sum(1 for _ in empty_lines) >= OPTS.dedent_after:
580+
indentation -= 1
581+
else:
582+
indentation = 0
583+
return indentation
584+
571585
def getstdout(self):
572586
"""This method returns the 'spoofed' stdout buffer, for writing to a
573587
file or sending to a pastebin or whatever."""
@@ -695,7 +709,7 @@ def reevaluate(self):
695709
self.iy, self.ix = self.scr.getyx()
696710

697711
self.cpos = 0
698-
indent = next_indentantion(self.s)
712+
indent = next_indentation(self.s)
699713
self.s = ''
700714
self.scr.refresh()
701715

@@ -837,7 +851,7 @@ def clear_current_line(self):
837851
It prevents autoindentation from occuring after a traceback."""
838852

839853

840-
def next_indentantion(line):
854+
def next_indentation(line):
841855
"""Given a code line, return the indentation of the next line."""
842856
line = line.expandtabs(OPTS.tab_length)
843857
indentation = (len(line) - len(line.lstrip(' '))) // OPTS.tab_length

0 commit comments

Comments
 (0)