Skip to content

Commit 3893bfc

Browse files
committed
Editor: Fix file completions when requesting them to the right of a dot
Also add tests for that case.
1 parent 3fcb31c commit 3893bfc

File tree

2 files changed

+42
-9
lines changed

2 files changed

+42
-9
lines changed

spyder/plugins/editor/widgets/base.py

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -963,22 +963,45 @@ def insert_completion(self, completion, completion_position):
963963
if current_text == '.':
964964
is_auto_completion_character = True
965965
else:
966-
if current_text in self.auto_completion_characters:
966+
if (
967+
kind != CompletionItemKind.FILE and
968+
current_text in self.auto_completion_characters
969+
):
967970
is_auto_completion_character = True
968971

969972
# Adjustments for file completions
970973
if kind == CompletionItemKind.FILE:
971-
# This is necessary to inseert file completions when
972-
# requesting them next to a colon
973974
if current_text in ['"', "'"]:
975+
# This is necessary to insert file completions when
976+
# requesting them next to a colon
974977
current_text = ''
975978
start_position += 1
976-
977-
# And this insert completions for files or directories that
978-
# start with a dot
979-
if current_text in ['".', "'."]:
979+
elif current_text in ['".', "'."]:
980+
# This inserts completions for files or directories
981+
# that start with a dot
980982
current_text = '.'
981983
start_position += 1
984+
elif current_text == '.':
985+
# This is needed if users are asking for file
986+
# completions to the right of a dot
987+
cursor_1 = self.textCursor()
988+
found_start = False
989+
990+
# Select text backwards until we find where the file
991+
# name starts
992+
while not found_start:
993+
cursor_1.movePosition(
994+
QTextCursor.PreviousCharacter,
995+
QTextCursor.KeepAnchor,
996+
)
997+
998+
selection = str(cursor_1.selectedText())
999+
if text.startswith(selection):
1000+
found_start = True
1001+
1002+
current_text = str(cursor_1.selectedText())
1003+
start_position = cursor_1.selectionStart()
1004+
end_position = cursor_1.selectionEnd()
9821005

9831006
if not is_auto_completion_character:
9841007
# Check if the completion position is in the expected range

spyder/plugins/editor/widgets/tests/test_introspection.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1223,7 +1223,9 @@ def test_dot_completions(completions_codeeditor, qtbot):
12231223

12241224
@pytest.mark.slow
12251225
@pytest.mark.order(1)
1226-
@pytest.mark.parametrize("filename", ['000_test.txt', '.hidden', 'any_file'])
1226+
@pytest.mark.parametrize(
1227+
"filename", ['000_test.txt', '.hidden', 'any_file.txt', 'abc.py',
1228+
'part.0.parquet'])
12271229
def test_file_completions(filename, mock_completions_codeeditor, qtbot):
12281230
"""
12291231
Test that completions for files are handled as expected.
@@ -1235,10 +1237,18 @@ def test_file_completions(filename, mock_completions_codeeditor, qtbot):
12351237

12361238
# Set text to complete and move cursor to the position we want to ask for
12371239
# completions.
1238-
if filename == 'any_file':
1240+
if filename == 'any_file.txt':
12391241
# This checks if we're able to introduce file completions as expected
12401242
# for any file when requesting them inside a string.
12411243
qtbot.keyClicks(code_editor, "''")
1244+
elif filename == 'abc.py':
1245+
# This checks that we can insert file completions correctly after a
1246+
# dot
1247+
qtbot.keyClicks(code_editor, "'abc.'")
1248+
elif filename == 'part.0.parquet':
1249+
# This checks that we can insert file completions next to a dot when a
1250+
# filename has several dots.
1251+
qtbot.keyClicks(code_editor, "'part.0.'")
12421252
else:
12431253
qtbot.keyClicks(code_editor, f"'{filename[0]}'")
12441254
code_editor.moveCursor(QTextCursor.PreviousCharacter)

0 commit comments

Comments
 (0)