Skip to content

Changed dict key-matching regex to capture any valid dict key #920

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

Merged
merged 13 commits into from
Oct 6, 2021
Merged
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
minor formatting changes to pass black linter test
  • Loading branch information
arian-deimling committed Sep 29, 2021
commit a0f7fae37ef567c15a9dc7901336ffe623fa788c
28 changes: 14 additions & 14 deletions bpython/line.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ def current_word(cursor_offset: int, line: str) -> Optional[LinePart]:
_match_all_dict_keys = r"""[^\]]*"""

# https://docs.python.org/3/reference/lexical_analysis.html#string-and-bytes-literals
_match_single_quote_str_bytes = \
r""" # bytes repr() begins with `b` character; bytes and str begin with `'`
_match_single_quote_str_bytes = r"""
# bytes repr() begins with `b` character; bytes and str begin with `'`
b?'
# after an even number of `\`, next `\` and subsequent char are interpreted
# as an escape sequence; this handles `\'` in the string repr()
Expand All @@ -57,8 +57,8 @@ def current_word(cursor_offset: int, line: str) -> Optional[LinePart]:

# bytes and str repr() only uses double quotes if the string contains 1 or more
# `'` character and exactly 0 `"` characters
_match_double_quote_str_bytes = \
r""" # bytes repr() begins with `b` character
_match_double_quote_str_bytes = r"""
# bytes repr() begins with `b` character
b?"
# string continues until a `"` character is reached
[^"]*
Expand All @@ -69,11 +69,11 @@ def current_word(cursor_offset: int, line: str) -> Optional[LinePart]:
_match_dict_before_key = r"""[\w_][\w0-9._]*\["""

_current_dict_key_re = LazyReCompile(
f'{_match_dict_before_key}((?:'
f'{_match_single_quote_str_bytes}|'
f'{_match_double_quote_str_bytes}|'
f'{_match_all_dict_keys}|)*)',
re.VERBOSE
f"{_match_dict_before_key}((?:"
f"{_match_single_quote_str_bytes}|"
f"{_match_double_quote_str_bytes}|"
f"{_match_all_dict_keys}|)*)",
re.VERBOSE,
)


Expand All @@ -89,11 +89,11 @@ def current_dict_key(cursor_offset: int, line: str) -> Optional[LinePart]:
_capture_dict_name = r"""([\w_][\w0-9._]*)\["""

_current_dict_re = LazyReCompile(
f'{_capture_dict_name}((?:'
f'{_match_single_quote_str_bytes}|'
f'{_match_double_quote_str_bytes}|'
f'{_match_all_dict_keys}|)*)',
re.VERBOSE
f"{_capture_dict_name}((?:"
f"{_match_single_quote_str_bytes}|"
f"{_match_double_quote_str_bytes}|"
f"{_match_all_dict_keys}|)*)",
re.VERBOSE,
)


Expand Down
2 changes: 1 addition & 1 deletion bpython/test/test_line_properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ def test_simple(self):
self.assertAccess("asdf[<(1, 2)>|]")
# TODO self.assertAccess('d[d[<12|>')
self.assertAccess("d[<'a>|")
self.assertAccess("object.dict['a\'bcd'], object.dict[<'abc>|")
self.assertAccess("object.dict['a'bcd'], object.dict[<'abc>|")
self.assertAccess(r"object.dict[<'a\'\\\"\n\\'>|")
self.assertAccess("object.dict[<\"abc'>|")
self.assertAccess("object.dict[<(1, 'apple', 2.134>|]")
Expand Down