Skip to content

Commit 8b95b0f

Browse files
committed
Editor: Fix completions next to path separators
1 parent d37af0f commit 8b95b0f

File tree

2 files changed

+33
-19
lines changed

2 files changed

+33
-19
lines changed

spyder/plugins/editor/widgets/base.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -971,19 +971,22 @@ def insert_completion(self, completion, completion_position):
971971

972972
# Adjustments for file completions
973973
if kind == CompletionItemKind.FILE:
974-
if current_text in ['"', "'"]:
975-
# This is necessary to insert file completions when
976-
# requesting them next to a colon
977-
current_text = ''
978-
start_position += 1
979-
elif current_text in ['".', "'."]:
974+
special_chars = ['"', "'", '/']
975+
976+
if any(
977+
[current_text.endswith(c) for c in special_chars]
978+
):
979+
# This is necessary when completions are requested next
980+
# to special characters.
981+
start_position = end_position
982+
elif current_text.endswith('.') and len(current_text) > 1:
980983
# This inserts completions for files or directories
981984
# that start with a dot
982-
current_text = '.'
983-
start_position += 1
985+
start_position = end_position - 1
984986
elif current_text == '.':
985987
# This is needed if users are asking for file
986-
# completions to the right of a dot
988+
# completions to the right of a dot when some of its
989+
# name is part of the completed text
987990
cursor_1 = self.textCursor()
988991
found_start = False
989992

@@ -1012,10 +1015,7 @@ def insert_completion(self, completion, completion_position):
10121015
cursor.setPosition(start_position)
10131016

10141017
# Remove the word under the cursor
1015-
if current_text:
1016-
cursor.setPosition(
1017-
end_position, QTextCursor.KeepAnchor
1018-
)
1018+
cursor.setPosition(end_position, QTextCursor.KeepAnchor)
10191019
else:
10201020
# Check if we are in the correct position
10211021
if cursor.position() != completion_position:

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

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1225,7 +1225,7 @@ def test_dot_completions(completions_codeeditor, qtbot):
12251225
@pytest.mark.order(1)
12261226
@pytest.mark.parametrize(
12271227
"filename", ['000_test.txt', '.hidden', 'any_file.txt', 'abc.py',
1228-
'part.0.parquet'])
1228+
'part.0.parquet', '/home', '/usr/bin'])
12291229
def test_file_completions(filename, mock_completions_codeeditor, qtbot):
12301230
"""
12311231
Test that completions for files are handled as expected.
@@ -1249,21 +1249,35 @@ def test_file_completions(filename, mock_completions_codeeditor, qtbot):
12491249
# This checks that we can insert file completions next to a dot when a
12501250
# filename has several dots.
12511251
qtbot.keyClicks(code_editor, "'part.0.'")
1252+
elif filename == '/home':
1253+
# This checks that we can insert file completions next to a /
1254+
qtbot.keyClicks(code_editor, "'/'")
1255+
elif filename == '/usr/bin':
1256+
# This checks that we can insert file completions next to a / placed at
1257+
# second-level
1258+
qtbot.keyClicks(code_editor, "'/usr/'")
12521259
else:
12531260
qtbot.keyClicks(code_editor, f"'{filename[0]}'")
12541261
code_editor.moveCursor(QTextCursor.PreviousCharacter)
12551262
qtbot.wait(500)
12561263

1257-
# Complete '0' -> '000_testing.txt'
1264+
# Set text that will be completed
1265+
if filename == '/home':
1266+
completion_text = 'home'
1267+
elif filename == '/usr/bin':
1268+
completion_text = 'bin'
1269+
else:
1270+
completion_text = filename
1271+
12581272
mock_response.side_effect = lambda lang, method, params: {'params': [{
1259-
'label': f'{filename}',
1273+
'label': f'{completion_text}',
12601274
'kind': CompletionItemKind.FILE,
1261-
'sortText': (0, f'a{filename}'),
1262-
'insertText': f'{filename}',
1275+
'sortText': (0, f'a{completion_text}'),
1276+
'insertText': f'{completion_text}',
12631277
'data': {'doc_uri': path_as_uri(__file__)},
12641278
'detail': '',
12651279
'documentation': '',
1266-
'filterText': f'{filename}',
1280+
'filterText': f'{completion_text}',
12671281
'insertTextFormat': 1,
12681282
'provider': 'LSP',
12691283
'resolve': True

0 commit comments

Comments
 (0)