8000 bpo-21261: IDLE: Add autocomplete when completing dictionary keys by louisom · Pull Request #1511 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

bpo-21261: IDLE: Add autocomplete when completing dictionary keys #1511

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Update will only complete on string key
  • Loading branch information
louisom committed May 10, 2017
commit 3c4a811a7897576a0d332522604abd3c5339b99b
14 changes: 6 additions & 8 deletions Lib/idlelib/autocomplete.py
8000
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

# These constants represent the two different types of completions.
# They must be defined here so autocomple_w can import them.
COMPLETE_ATTRIBUTES, COMPLETE_FILES, COMPLETE_DICTIONARY_KEY = range(1, 3+1)
COMPLETE_ATTRIBUTES, COMPLETE_FILES, COMPLETE_DICTIONARY_STRING_KEY = range(1, 3+1)

from idlelib import autocomplete_w
from idlelib.config import idleConf
Expand Down Expand Up @@ -119,15 +119,13 @@ def open_completions(self, evalfuncs, complete, userWantsWin, mode=None):
hp = HyperParser(self.editwin, "insert")
curline = self.text.get("insert linestart", "insert")
i = j = len(curline)
if hp.is_in_subscript() and (not mode or mode==COMPLETE_DICTIONARY_KEY):
if hp.is_in_subscript_string_key() and (not mode or mode==COMPLETE_DICTIONARY_STRING_KEY):
self._remove_autocomplete_window()
mode = COMPLETE_DICTIONARY_KEY
while i and (curline[i-1] not in '[\'"'):
mode = COMPLETE_DICTIONARY_STRING_KEY
while i and (curline[i-1] not in '\'"'):
i -= 1
comp_start = curline[i:j]
if i > 1:
if curline[i - 1] == '[':
i += 1
hp.set_index('insert-%dc' % (len(curline) - (i - 2)))
comp_what = hp.get_expression()
else:
Expand Down Expand Up @@ -230,14 +228,14 @@ def fetch_completions(self, what, mode):
except OSError:
return [], []

elif mode == COMPLETE_DICTIONARY_KEY:
elif mode == COMPLETE_DICTIONARY_STRING_KEY:
try:
entity = self.get_entity(what)

# Check the entity is dict
if not isinstance(entity, dict):
return [], []
bigl = [str(s) for s in entity]
bigl = [s for s in entity if isinstance(s, str)]
bigl.sort()
except:
return [], []
Expand Down
14 changes: 5 additions & 9 deletions Lib/idlelib/hyperparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,15 +113,11 @@ def is_in_code(self):
self.rawtext[self.bracketing[self.indexbracket][0]]
not in ('#', '"', "'"))

def is_in_subscript(self):
"""Is the index given to the HyperParser in subscript?"""
if not self.isopener[self.indexbracket]:
return False
for index, bracket in enumerate(self.bracketing[: self.indexbracket + 1]):
if (self.isopener[index] is True and
self.rawtext[bracket[0]] == '['):
return True
return False
def is_in_subscript_string_key(self):
"""Is the index given to the HyperParser in subscript with string key?"""
return (self.isopener[self.indexbracket] and
self.rawtext[self.bracketing[self.indexbracket - 1][0]] == '[' and
self.rawtext[self.bracketing[self.indexbracket][0]] in ('"', "'"))

def get_surrounding_brackets(self, openers='([{', mustclose=False):
"""Return bracket indexes or None.
Expand Down
22 changes: 11 additions & 11 deletions Lib/idlelib/idle_test/test_hyperparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,29 +117,29 @@ def test_is_in_code(self):
p = get('4.14')
self.assertFalse(p.is_in_code())

def test_is_in_subscript(self):
def test_is_in_subscript_string_key(self):
get = self.get_parser

p = get('5.0')
self.assertFalse(p.is_in_subscript())
self.assertFalse(p.is_in_subscript_string_key())
p = get('7.0')
self.assertFalse(p.is_in_subscript())
self.assertFalse(p.is_in_subscript_string_key())
p = get('8.0')
self.assertFalse(p.is_in_subscript())
self.assertFalse(p.is_in_subscript_string_key())
p = get('9.6')
self.assertFalse(p.is_in_subscript())
self.assertFalse(p.is_in_subscript_string_key())
p = get('13.2')
self.assertTrue(p.is_in_subscript())
self.assertFalse(p.is_in_subscript_string_key())
p = get('13.3')
self.assertTrue(p.is_in_subscript())
self.assertTrue(p.is_in_subscript_string_key())
p = get('13.15')
self.assertFalse(p.is_in_subscript())
self.assertFalse(p.is_in_subscript_string_key())
p = get('14.2')
self.assertTrue(p.is_in_subscript())
self.assertFalse(p.is_in_subscript_string_key())
p = get('14.3')
self.assertTrue(p.is_in_subscript())
self.assertTrue(p.is_in_subscript_string_key())
p = get('15.2')
self.assertTrue(p.is_in_subscript())
self.assertFalse(p.is_in_subscript_string_key())

def test_get_surrounding_bracket(self):
get = self.get_parser
Expand Down
0