Skip to content

Commit 25fe9b0

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

File tree

2 files changed

+29
-9
lines changed

2 files changed

+29
-9
lines changed

spyder/plugins/editor/widgets/base.py

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -963,22 +963,37 @@ 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+
cursor_1.movePosition(
989+
QTextCursor.PreviousCharacter,
990+
QTextCursor.MoveAnchor,
991+
2
992+
)
993+
cursor_1.select(QTextCursor.WordUnderCursor)
994+
current_text = str(cursor_1.selectedText())
995+
start_position = cursor_1.selectionStart()
996+
end_position = start_position + len(current_text) + 1
982997

983998
if not is_auto_completion_character:
984999
# Check if the completion position is in the expected range

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1223,7 +1223,8 @@ 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'])
12271228
def test_file_completions(filename, mock_completions_codeeditor, qtbot):
12281229
"""
12291230
Test that completions for files are handled as expected.
@@ -1235,10 +1236,14 @@ def test_file_completions(filename, mock_completions_codeeditor, qtbot):
12351236

12361237
# Set text to complete and move cursor to the position we want to ask for
12371238
# completions.
1238-
if filename == 'any_file':
1239+
if filename == 'any_file.txt':
12391240
# This checks if we're able to introduce file completions as expected
12401241
# for any file when requesting them inside a string.
12411242
qtbot.keyClicks(code_editor, "''")
1243+
elif filename == 'abc.py':
1244+
# This checks that we can insert file completions correctly after a
1245+
# dot
1246+
qtbot.keyClicks(code_editor, "'abc.'")
12421247
else:
12431248
qtbot.keyClicks(code_editor, f"'{filename[0]}'")
12441249
code_editor.moveCursor(QTextCursor.PreviousCharacter)

0 commit comments

Comments
 (0)