Skip to content
This repository was archived by the owner on Jan 24, 2025. It is now read-only.

Commit 14369f8

Browse files
authored
Fix path completion when client doesn't support code snippets (python-lsp#497)
1 parent 2a7d5b7 commit 14369f8

File tree

2 files changed

+27
-10
lines changed

2 files changed

+27
-10
lines changed

pylsp/plugins/jedi_completion.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ def pylsp_completions(config, document, position):
8888
include_params=include_params if c.type in ["class", "function"] else False,
8989
resolve=resolve_eagerly,
9090
resolve_label_or_snippet=(i < max_to_resolve),
91+
snippet_support=snippet_support,
9192
)
9293
for i, c in enumerate(completions)
9394
]
@@ -102,6 +103,7 @@ def pylsp_completions(config, document, position):
102103
include_params=False,
103104
resolve=resolve_eagerly,
104105
resolve_label_or_snippet=(i < max_to_resolve),
106+
snippet_support=snippet_support,
105107
)
106108
completion_dict["kind"] = lsp.CompletionItemKind.TypeParameter
107109
completion_dict["label"] += " object"
@@ -116,6 +118,7 @@ def pylsp_completions(config, document, position):
116118
include_params=False,
117119
resolve=resolve_eagerly,
118120
resolve_label_or_snippet=(i < max_to_resolve),
121+
snippet_support=snippet_support,
119122
)
120123
completion_dict["kind"] = lsp.CompletionItemKind.TypeParameter
121124
completion_dict["label"] += " object"
@@ -226,6 +229,7 @@ def _format_completion(
226229
include_params=True,
227230
resolve=False,
228231
resolve_label_or_snippet=False,
232+
snippet_support=False,
229233
):
230234
completion = {
231235
"label": _label(d, resolve_label_or_snippet),
@@ -240,16 +244,20 @@ def _format_completion(
240244
# Adjustments for file completions
241245
if d.type == "path":
242246
path = os.path.normpath(d.name)
243-
path = path.replace("\\", "\\\\")
244-
path = path.replace("/", "\\/")
245247

246-
# If the completion ends with os.sep, it means it's a directory. So we add an escaped os.sep
247-
# at the end to ease additional file completions.
248+
# If the completion ends with os.sep, it means it's a directory. So we add os.sep at the end
249+
# to ease additional file completions.
248250
if d.name.endswith(os.sep):
249251
if os.name == "nt":
250-
path = path + "\\\\"
252+
path = path + "\\"
251253
else:
252-
path = path + "\\/"
254+
path = path + "/"
255+
256+
# Escape to prevent conflicts with the code snippets grammer
257+
# See also https://github.com/python-lsp/python-lsp-server/issues/373
258+
if snippet_support:
259+
path = path.replace("\\", "\\\\")
260+
path = path.replace("/", "\\/")
253261

254262
completion["insertText"] = path
255263

test/plugins/test_completion.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -583,9 +583,18 @@ def test_file_completions(workspace, tmpdir):
583583
# Check completions
584584
assert len(completions) == 2
585585
assert [c["kind"] == lsp.CompletionItemKind.File for c in completions]
586-
assert (
587-
completions[0]["insertText"] == ("bar" + "\\\\")
588-
if os.name == "nt"
589-
else ("bar" + "\\/")
586+
assert completions[0]["insertText"] == (
587+
("bar" + "\\") if os.name == "nt" else ("bar" + "/")
588+
)
589+
assert completions[1]["insertText"] == 'foo.txt"'
590+
591+
# When snippets are supported, ensure that path separators are escaped.
592+
support_snippet = {
593+
"textDocument": {"completion": {"completionItem": {"snippetSupport": True}}}
594+
}
595+
doc._config.capabilities.update(support_snippet)
596+
completions = pylsp_jedi_completions(doc._config, doc, com_position)
597+
assert completions[0]["insertText"] == (
598+
("bar" + "\\\\") if os.name == "nt" else ("bar" + "\\/")
590599
)
591600
assert completions[1]["insertText"] == 'foo.txt"'

0 commit comments

Comments
 (0)