Skip to content

Commit ba57887

Browse files
committed
Editor: Fix introducing completions after a dot
Also, expand test to check that an entry selected in the completion widget when it's shown after a dot is introduced as expected.
1 parent 5f31691 commit ba57887

File tree

2 files changed

+36
-7
lines changed

2 files changed

+36
-7
lines changed

spyder/plugins/editor/widgets/base.py

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -952,13 +952,30 @@ def insert_completion(self, completion, completion_position):
952952
current_text, start_position = result
953953
end_position = start_position + len(current_text)
954954

955-
# Check if the completion position is in the expected range
956-
if not (start_position <= completion_position <= end_position):
957-
return
958-
cursor.setPosition(start_position)
959-
960-
# Remove the word under the cursor
961-
cursor.setPosition(end_position, QTextCursor.KeepAnchor)
955+
# Remove text under cursor only if it's not an autocompletion
956+
# character
957+
is_auto_completion_character = False
958+
if self.objectName() == 'console':
959+
if current_text == '.':
960+
is_auto_completion_character = True
961+
else:
962+
if current_text in self.auto_completion_characters:
963+
is_auto_completion_character = True
964+
965+
if not is_auto_completion_character:
966+
# Check if the completion position is in the expected range
967+
if not (
968+
start_position <= completion_position <= end_position
969+
):
970+
return
971+
cursor.setPosition(start_position)
972+
973+
# Remove the word under the cursor
974+
cursor.setPosition(end_position, QTextCursor.KeepAnchor)
975+
else:
976+
# Check if we are in the correct position
977+
if cursor.position() != completion_position:
978+
return
962979
else:
963980
# Check if we are in the correct position
964981
if cursor.position() != completion_position:

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1193,6 +1193,7 @@ def test_dot_completions(completions_codeeditor, qtbot):
11931193
"""
11941194
code_editor, _ = completions_codeeditor
11951195
completion = code_editor.completion_widget
1196+
code_editor.toggle_code_snippets(False)
11961197

11971198
# Import module and check completions are shown for it after writing a dot
11981199
# after it
@@ -1208,6 +1209,17 @@ def test_dot_completions(completions_codeeditor, qtbot):
12081209
qtbot.wait(500)
12091210
assert completion.isVisible()
12101211

1212+
# Select a raondom entry in the completion widget
1213+
entry_index = random.randint(0, 30)
1214+
inserted_entry = completion.completion_list[entry_index]['insertText']
1215+
for _ in range(entry_index):
1216+
qtbot.keyPress(completion, Qt.Key_Down, delay=50)
1217+
1218+
# Insert completion and check that the inserted text is the expected one
1219+
qtbot.keyPress(completion, Qt.Key_Enter)
1220+
qtbot.wait(500)
1221+
assert code_editor.toPlainText() == f'import math\nmath.{inserted_entry}'
1222+
12111223

12121224
@pytest.mark.slow
12131225
@pytest.mark.order(1)

0 commit comments

Comments
 (0)