X Tutup
Skip to content

Commit fbe0046

Browse files
committed
Fix redrawing of lines for long lines in paren matching.
1 parent ea9966a commit fbe0046

File tree

1 file changed

+29
-5
lines changed

1 file changed

+29
-5
lines changed

bpython/cli.py

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,23 @@ def log(x):
7979
orig_stdout = sys.__stdout__
8080
stdscr = None
8181

82+
83+
def calculate_screen_lines(tokens, width, cursor=0):
84+
"""Given a stream of tokens and a screen width plus an optional
85+
initial cursor position, return the amount of needed lines on the
86+
screen."""
87+
lines = 1
88+
pos = cursor
89+
for (token, value) in tokens:
90+
if token is Token.Text and value == '\n':
91+
lines += 1
92+
else:
93+
pos += len(value)
94+
lines += pos // width
95+
pos %= width
96+
return lines
97+
98+
8299
def parsekeywordpairs(signature):
83100
tokens = PythonLexer().get_tokens(signature)
84101
stack = []
@@ -1672,17 +1689,23 @@ def reprint_line(lineno, s, to_replace=[]):
16721689
x = self.ix + len(s) - self.cpos
16731690
if not self.cpos:
16741691
x -= 1
1692+
max_x = self.scr.getmaxyx()[1]
16751693
if self.highlighted_paren:
16761694
# Clear previous highlighted paren
16771695
reprint_line(*self.highlighted_paren)
16781696
self.highlighted_paren = None
16791697
stack = list()
16801698
source = '\n'.join(self.buffer) + '\n%s' % (s, )
1681-
i = line = 0
1682-
pos = 3
1699+
all_tokens = list(PythonLexer().get_tokens(source))
1700+
screen_lines = calculate_screen_lines(all_tokens, max_x, 3) - 1
1701+
i = line = real_line = 0
1702+
real_pos = pos = 3
16831703
parens = dict(zip('{([', '})]'))
1684-
for (token, value) in PythonLexer().get_tokens(source):
1704+
for (token, value) in all_tokens:
16851705
pos += len(value)
1706+
if real_pos + len(value) > max_x:
1707+
real_line += (real_pos + len(value)) // max_x
1708+
real_pos %= max_x
16861709
under_cursor = (line == len(self.buffer) and pos == x)
16871710
if token is Token.Punctuation:
16881711
if value in parens:
@@ -1691,7 +1714,7 @@ def reprint_line(lineno, s, to_replace=[]):
16911714
# Push marker on the stack
16921715
stack.append((Parenthesis, value))
16931716
else:
1694-
stack.append((line, i, value))
1717+
stack.append((real_line, i, value))
16951718
elif value in parens.itervalues():
16961719
saved_stack = list(stack)
16971720
try:
@@ -1722,7 +1745,7 @@ def reprint_line(lineno, s, to_replace=[]):
17221745
# Parenthesis.UnderCursor token.
17231746
tokens[i] = (Parenthesis, value)
17241747
(line, i, opening) = opening
1725-
screen_line = y - len(self.buffer) + line
1748+
screen_line = y - screen_lines + line + 1
17261749
if line == len(self.buffer):
17271750
self.highlighted_paren = (screen_line, s)
17281751
tokens[i] = (Parenthesis, opening)
@@ -1741,6 +1764,7 @@ def reprint_line(lineno, s, to_replace=[]):
17411764
break
17421765
elif token is Token.Text and value == '\n':
17431766
line += 1
1767+
real_line += 1
17441768
i = -1
17451769
pos = 3
17461770
i += 1

0 commit comments

Comments
 (0)
X Tutup