Skip to content

Commit eda83d9

Browse files
committed
Editor: Fix introducing file completions
- This correctly introduces completions for files that start with a dot and when completions are requested at the beginning of a string. - Expand tests to cover those cases.
1 parent e01d826 commit eda83d9

File tree

2 files changed

+40
-16
lines changed

2 files changed

+40
-16
lines changed

spyder/plugins/editor/widgets/base.py

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,16 @@
2525
# Local imports
2626
from spyder.config.gui import get_font
2727
from spyder.config.manager import CONF
28-
from spyder.py3compat import PY3, to_text_string
29-
from spyder.widgets.calltip import CallTipWidget, ToolTipWidget
30-
from spyder.widgets.mixins import BaseEditMixin
3128
from spyder.plugins.editor.api.decoration import TextDecoration, DRAW_ORDERS
3229
from spyder.plugins.editor.utils.decoration import TextDecorationsManager
3330
from spyder.plugins.editor.widgets.completion import CompletionWidget
31+
from spyder.plugins.completion.api import CompletionItemKind
3432
from spyder.plugins.outlineexplorer.api import is_cell_header, document_cells
33+
from spyder.py3compat import PY3, to_text_string
3534
from spyder.utils.palette import SpyderPalette
35+
from spyder.widgets.calltip import CallTipWidget, ToolTipWidget
36+
from spyder.widgets.mixins import BaseEditMixin
37+
3638

3739
class TextEditBaseWidget(QPlainTextEdit, BaseEditMixin):
3840
"""Text edit base widget"""
@@ -941,8 +943,10 @@ def insert_completion(self, completion, completion_position):
941943
text = str(completion['textEdit']['newText'])
942944
else:
943945
text = completion
946+
kind = None
944947
if isinstance(completion, dict):
945948
text = completion['insertText']
949+
kind = completion['kind']
946950
text = str(text)
947951

948952
# Get word to the left of the cursor.
@@ -962,6 +966,20 @@ def insert_completion(self, completion, completion_position):
962966
if current_text in self.auto_completion_characters:
963967
is_auto_completion_character = True
964968

969+
# Adjustments for file completions
970+
if kind == CompletionItemKind.FILE:
971+
# This is necessary to inseert file completions when
972+
# requesting them next to a colon
973+
if current_text in ['"', "'"]:
974+
current_text = ''
975+
start_position += 1
976+
977+
# And this insert completions for files or directories that
978+
# start with a dot
979+
if current_text in ['".', "'."]:
980+
current_text = '.'
981+
start_position += 1
982+
965983
if not is_auto_completion_character:
966984
# Check if the completion position is in the expected range
967985
if not (
@@ -971,7 +989,10 @@ def insert_completion(self, completion, completion_position):
971989
cursor.setPosition(start_position)
972990

973991
# Remove the word under the cursor
974-
cursor.setPosition(end_position, QTextCursor.KeepAnchor)
992+
if current_text:
993+
cursor.setPosition(
994+
end_position, QTextCursor.KeepAnchor
995+
)
975996
else:
976997
# Check if we are in the correct position
977998
if cursor.position() != completion_position:

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

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

12241224
@pytest.mark.slow
12251225
@pytest.mark.order(1)
1226-
def test_completions_for_files_that_start_with_numbers(
1227-
mock_completions_codeeditor, qtbot):
1226+
@pytest.mark.parametrize("filename", ['000_test.txt', '.hidden', 'any_file'])
1227+
def test_file_completions(filename, mock_completions_codeeditor, qtbot):
12281228
"""
1229-
Test that completions for files that start with numbers are handled as
1230-
expected.
1229+
Test that completions for files are handled as expected.
12311230
1232-
This is a regression test for issue spyder-ide/spyder#20156
1231+
This includes a regression test for issue spyder-ide/spyder#20156
12331232
"""
12341233
code_editor, mock_response = mock_completions_codeeditor
12351234
completion = code_editor.completion_widget
1236-
file_name = '000_testing.txt'
12371235

12381236
# Set text to complete and move cursor to the position we want to ask for
12391237
# completions.
1240-
qtbot.keyClicks(code_editor, "'0'")
1238+
if filename == 'any_file':
1239+
# This checks if we're able to introduce file completions as expected
1240+
# for any file when requesting them inside a string.
1241+
qtbot.keyClicks(code_editor, "''")
1242+
else:
1243+
qtbot.keyClicks(code_editor, f"'{filename[0]}'")
12411244
code_editor.moveCursor(QTextCursor.PreviousCharacter)
12421245
qtbot.wait(500)
12431246

12441247
# Complete '0' -> '000_testing.txt'
12451248
mock_response.side_effect = lambda lang, method, params: {'params': [{
1246-
'label': f'{file_name}',
1249+
'label': f'{filename}',
12471250
'kind': CompletionItemKind.FILE,
1248-
'sortText': (0, f'a{file_name}'),
1249-
'insertText': f'{file_name}',
1251+
'sortText': (0, f'a{filename}'),
1252+
'insertText': f'{filename}',
12501253
'data': {'doc_uri': path_as_uri(__file__)},
12511254
'detail': '',
12521255
'documentation': '',
1253-
'filterText': f'{file_name}',
1256+
'filterText': f'{filename}',
12541257
'insertTextFormat': 1,
12551258
'provider': 'LSP',
12561259
'resolve': True
@@ -1261,7 +1264,7 @@ def test_completions_for_files_that_start_with_numbers(
12611264
qtbot.keyPress(code_editor, Qt.Key_Tab, delay=300)
12621265

12631266
qtbot.wait(500)
1264-
assert code_editor.get_text_with_eol() == f"'{file_name}'"
1267+
assert code_editor.get_text_with_eol() == f"'{filename}'"
12651268

12661269

12671270
if __name__ == '__main__':

0 commit comments

Comments
 (0)