X Tutup
Skip to content

Commit c9c0cbf

Browse files
current_line is a property
1 parent ec1f436 commit c9c0cbf

File tree

2 files changed

+23
-20
lines changed

2 files changed

+23
-20
lines changed

bpython/cli.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,13 @@ def __init__(self, scr, interp, statusbar, config, idle=None):
356356
if config.cli_suggestion_width <= 0 or config.cli_suggestion_width > 1:
357357
config.cli_suggestion_width = 0.8
358358

359+
def _get_cursor_offset(self):
360+
return len(self.s) - self.cpos
361+
def _set_cursor_offset(self, offset):
362+
self.cpos = len(self.s) - offset
363+
cursor_offset = property(_get_cursor_offset, _set_cursor_offset, None,
364+
"The cursor offset from the beginning of the line")
365+
359366
def addstr(self, s):
360367
"""Add a string to the current input line and figure out
361368
where it should go, depending on the cursor position."""
@@ -487,9 +494,12 @@ def clrtobol(self):
487494
self.scr.redrawwin()
488495
self.scr.refresh()
489496

490-
def current_line(self):
491-
"""Return the current line."""
497+
def _get_current_line(self):
492498
return self.s
499+
def _set_current_line(self, line):
500+
self.s = line
501+
current_line = property(_get_current_line, _set_current_line, None,
502+
"The characters of the current line")
493503

494504
def cut_to_buffer(self):
495505
"""Clear from cursor to end of line, placing into cut buffer"""

bpython/repl.py

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,7 @@ def startup(self):
465465

466466
def current_string(self, concatenate=False):
467467
"""If the line ends in a string get it, otherwise return ''"""
468-
tokens = self.tokenize(self.current_line())
468+
tokens = self.tokenize(self.current_line)
469469
string_tokens = list(takewhile(token_is_any_of([Token.String,
470470
Token.Text]),
471471
reversed(tokens)))
@@ -515,7 +515,7 @@ def get_args(self):
515515
stack = [['', 0, '']]
516516
try:
517517
for (token, value) in PythonLexer().get_tokens(
518-
self.current_line()):
518+
self.current_line):
519519
if token is Token.Punctuation:
520520
if value in '([{':
521521
stack.append(['', 0, value])
@@ -576,7 +576,7 @@ def get_source_of_current_name(self):
576576
try:
577577
obj = self.current_func
578578
if obj is None:
579-
line = self.current_line()
579+
line = self.current_line
580580
if inspection.is_eval_safe_name(line):
581581
obj = self.get_object(line)
582582
source = inspect.getsource(obj)
@@ -603,7 +603,7 @@ def set_docstring(self):
603603
def magic_method_completions(self, cw):
604604
if (self.config.complete_magic_methods and self.buffer and
605605
self.buffer[0].startswith("class ") and
606-
self.current_line().lstrip().startswith("def ")):
606+
self.current_line.lstrip().startswith("def ")):
607607
return [name for name in self.config.magic_methods
608608
if name.startswith(cw)]
609609
else:
@@ -618,27 +618,22 @@ def complete(self, tab=False):
618618
self.set_docstring()
619619

620620
matches, completer = autocomplete.get_completer(
621-
len(self.current_line()) - self.cpos,
622-
self.current_line(),
621+
self.cursor_offset,
622+
self.current_line,
623623
self.interp.locals,
624624
self.argspec,
625625
self.config,
626626
self.magic_method_completions)
627627

628-
if matches is None: # no completion is relevant
628+
if (matches is None # no completion is relevant
629+
or len(matches) == 0): # some completion works, but no matches
629630
self.matches_iter.clear()
630631
return bool(self.argspec)
631632

632-
elif len(matches) == 0: # some completion works, but no matches
633-
self.matches_iter.update(len(self.current_line()) - self.cpos,
634-
self.current_line(),
635-
matches, completer)
636-
return bool(self.argspec)
633+
self.matches_iter.update(self.cursor_offset,
634+
self.current_line, matches, completer)
637635

638-
elif len(matches) == 1:
639-
self.matches_iter.update(len(self.current_line()) - self.cpos,
640-
self.current_line(),
641-
matches, completer)
636+
if len(matches) == 1:
642637
self.matches_iter.next()
643638
if tab: # if this complete is being run for a tab key press, tab() to do the swap
644639
self.tab()
@@ -650,8 +645,6 @@ def complete(self, tab=False):
650645
return True
651646

652647
else:
653-
self.matches_iter.update(len(self.current_line()) - self.cpos,
654-
self.current_line(), matches, completer)
655648
return True
656649

657650
def format_docstring(self, docstring, width, height):

0 commit comments

Comments
 (0)
X Tutup