Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit eec4268

Browse files
committed
Do not duplicate '=' when tab completing a kwarg with the cursor on '='
1 parent fb1ba5e commit eec4268

2 files changed

Lines changed: 21 additions & 2 deletions

File tree

IPython/terminal/ptutils.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ def _elide(string, *, min_elide=30):
4343
return '{}.{}\N{HORIZONTAL ELLIPSIS}{}.{}'.format(parts[0], parts[1][0], parts[-2][-1], parts[-1])
4444

4545

46+
def _adjust_completion_text_based_on_context(text, body, offset):
47+
if text.endswith('=') and len(body) > offset and body[offset] is '=':
48+
return text[:-1]
49+
else:
50+
return text
4651

4752

4853
class IPythonPTCompleter(Completer):
@@ -113,10 +118,11 @@ def _get_completions(body, offset, cursor_position, ipyc):
113118
# display_meta=meta_text)
114119
display_text = c.text
115120

121+
adjusted_text = _adjust_completion_text_based_on_context(c.text, body, offset)
116122
if c.type == 'function':
117123
display_text = display_text + '()'
118124

119-
yield Completion(c.text, start_position=c.start - offset, display=_elide(display_text), display_meta=c.type)
125+
yield Completion(adjusted_text, start_position=c.start - offset, display=_elide(display_text), display_meta=c.type)
120126

121127
class IPythonPTLexer(Lexer):
122128
"""

IPython/terminal/tests/test_interactivshell.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from IPython.testing import tools as tt
1111
from IPython.utils.capture import capture_output
1212

13-
from IPython.terminal.ptutils import _elide
13+
from IPython.terminal.ptutils import _elide, _adjust_completion_text_based_on_context
1414
import nose.tools as nt
1515

1616
class TestElide(unittest.TestCase):
@@ -20,6 +20,19 @@ def test_elide(self):
2020
_elide('concatenate((a1, a2, ..), . axis') # do not raise
2121
nt.assert_equal(_elide('aaaa.bbbb.ccccc.dddddd.eeeee.fffff.gggggg.hhhhhh'), 'aaaa.b…g.hhhhhh')
2222

23+
24+
class TestContextAwareCompletion(unittest.TestCase):
25+
26+
def test_adjust_completion_text_based_on_context(self):
27+
# Adjusted case
28+
nt.assert_equal(_adjust_completion_text_based_on_context('arg1=', 'func1(a=)', 7), 'arg1')
29+
30+
# Untouched cases
31+
nt.assert_equal(_adjust_completion_text_based_on_context('arg1=', 'func1(a)', 7), 'arg1=')
32+
nt.assert_equal(_adjust_completion_text_based_on_context('arg1=', 'func1(a', 7), 'arg1=')
33+
nt.assert_equal(_adjust_completion_text_based_on_context('%magic', 'func1(a=)', 7), '%magic')
34+
nt.assert_equal(_adjust_completion_text_based_on_context('func2', 'func1(a=)', 7), 'func2')
35+
2336
# Decorator for interaction loop tests -----------------------------------------
2437

2538
class mock_input_helper(object):

0 commit comments

Comments
 (0)