Skip to content

Commit afa48d4

Browse files
committed
prevent-file-completion-in-attr-matches
1 parent d801c16 commit afa48d4

2 files changed

Lines changed: 9 additions & 17 deletions

File tree

IPython/core/completer.py

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2209,15 +2209,6 @@ def file_matcher(self, context: CompletionContext) -> SimpleMatcherResult:
22092209
"""
22102210
# TODO: add a heuristic for suppressing (e.g. if it has OS-specific delimiter,
22112211
# starts with `/home/`, `C:\`, etc)
2212-
last_line = context.full_text.split("\n")[-1]
2213-
text_before_token = last_line[: last_line.rfind(context.token)]
2214-
2215-
# Suppress path completion when completing methods/attributes
2216-
if text_before_token.endswith((")", "]")):
2217-
return {
2218-
"completions": [],
2219-
"suppress": True,
2220-
}
22212212

22222213
text = context.token
22232214

@@ -2490,7 +2481,7 @@ def _jedi_matcher(self, context: CompletionContext) -> _JediMatcherResult:
24902481
return {
24912482
"completions": matches,
24922483
# static analysis should not suppress other matchers
2493-
"suppress": False,
2484+
"suppress": {_get_matcher_id(self.file_matcher)},
24942485
}
24952486

24962487
def _jedi_matches(
@@ -2750,12 +2741,16 @@ def python_matcher(self, context: CompletionContext) -> SimpleMatcherResult:
27502741
is None
27512742
)
27522743
matches = filter(no__name, matches)
2753-
return _convert_matcher_v1_result_to_v2(
2744+
matches = _convert_matcher_v1_result_to_v2(
27542745
matches, type="attribute", fragment=fragment
27552746
)
2747+
matches["suppress"] = {_get_matcher_id(self.file_matcher)}
2748+
return matches
27562749
except NameError:
27572750
# catches <undefined attributes>.<tab>
2758-
return SimpleMatcherResult(completions=[], suppress=False)
2751+
return SimpleMatcherResult(
2752+
completions=[], suppress={_get_matcher_id(self.file_matcher)}
2753+
)
27592754
else:
27602755
try:
27612756
matches = self.global_matches(context.token, context=context)
@@ -2766,7 +2761,7 @@ def python_matcher(self, context: CompletionContext) -> SimpleMatcherResult:
27662761
completions=[
27672762
SimpleCompletion(text=match, type="variable") for match in matches
27682763
],
2769-
suppress=False,
2764+
suppress={_get_matcher_id(self.file_matcher)},
27702765
)
27712766

27722767
@completion_matcher(api_version=1)

tests/test_completer.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2121,20 +2121,17 @@ def test_undefined_variables(use_jedi, evaluation, code, insert_text):
21212121
" return 1.1",
21222122
"my_test().",
21232123
]
2124-
),
2125-
'print("foo").',
2124+
)
21262125
],
21272126
)
21282127
def test_no_file_completions_in_attr_access(code):
21292128
"""Test that files are not suggested during attribute/method completion."""
21302129
with TemporaryWorkingDirectory():
2131-
# Create a hidden file
21322130
open(".hidden", "w", encoding="utf-8").close()
21332131
offset = len(code)
21342132
with provisionalcompleter():
21352133
completions = list(ip.Completer.completions(text=code, offset=offset))
21362134
matches = [c for c in completions if c.text.lstrip(".") == "hidden"]
2137-
# No file completions should appear
21382135
assert (
21392136
len(matches) == 0
21402137
), f"File '.hidden' should not appear in attribute completion"

0 commit comments

Comments
 (0)