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

Skip to content

Commit d801c16

Browse files
committed
suppress-file-completions
1 parent 79f313a commit d801c16

2 files changed

Lines changed: 37 additions & 0 deletions

File tree

IPython/core/completer.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2209,6 +2209,15 @@ 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+
}
22122221

22132222
text = context.token
22142223

tests/test_completer.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2112,6 +2112,34 @@ def test_undefined_variables(use_jedi, evaluation, code, insert_text):
21122112
assert len(match) == 1, message_on_fail
21132113

21142114

2115+
@pytest.mark.parametrize(
2116+
"code",
2117+
[
2118+
"\n".join(
2119+
[
2120+
"def my_test() -> float:",
2121+
" return 1.1",
2122+
"my_test().",
2123+
]
2124+
),
2125+
'print("foo").',
2126+
],
2127+
)
2128+
def test_no_file_completions_in_attr_access(code):
2129+
"""Test that files are not suggested during attribute/method completion."""
2130+
with TemporaryWorkingDirectory():
2131+
# Create a hidden file
2132+
open(".hidden", "w", encoding="utf-8").close()
2133+
offset = len(code)
2134+
with provisionalcompleter():
2135+
completions = list(ip.Completer.completions(text=code, offset=offset))
2136+
matches = [c for c in completions if c.text.lstrip(".") == "hidden"]
2137+
# No file completions should appear
2138+
assert (
2139+
len(matches) == 0
2140+
), f"File '.hidden' should not appear in attribute completion"
2141+
2142+
21152143
@pytest.mark.parametrize(
21162144
"line,expected",
21172145
[

0 commit comments

Comments
 (0)