7
7
import parso
8
8
9
9
from pylsp import _utils , hookimpl , lsp
10
- from pylsp .plugins ._resolvers import LABEL_RESOLVER
10
+ from pylsp .plugins ._resolvers import LABEL_RESOLVER , SNIPPET_RESOLVER
11
11
12
12
log = logging .getLogger (__name__ )
13
13
@@ -59,6 +59,7 @@ def pylsp_completions(config, document, position):
59
59
modules_to_cache_labels_for = settings .get ('cache_labels_for' , None )
60
60
if modules_to_cache_labels_for is not None :
61
61
LABEL_RESOLVER .cached_modules = modules_to_cache_labels_for
62
+ SNIPPET_RESOLVER .cached_modules = modules_to_cache_labels_for
62
63
63
64
include_params = snippet_support and should_include_params and use_snippets (document , position )
64
65
include_class_objects = snippet_support and should_include_class_objects and use_snippets (document , position )
@@ -68,7 +69,7 @@ def pylsp_completions(config, document, position):
68
69
c ,
69
70
include_params ,
70
71
resolve = resolve_eagerly ,
71
- resolve_label = (i < max_labels_resolve )
72
+ resolve_label_or_snippet = (i < max_labels_resolve )
72
73
)
73
74
for i , c in enumerate (completions )
74
75
]
@@ -81,7 +82,7 @@ def pylsp_completions(config, document, position):
81
82
c ,
82
83
False ,
83
84
resolve = resolve_eagerly ,
84
- resolve_label = (i < max_labels_resolve )
85
+ resolve_label_or_snippet = (i < max_labels_resolve )
85
86
)
86
87
completion_dict ['kind' ] = lsp .CompletionItemKind .TypeParameter
87
88
completion_dict ['label' ] += ' object'
@@ -173,9 +174,9 @@ def _resolve_completion(completion, d):
173
174
return completion
174
175
175
176
176
- def _format_completion (d , include_params = True , resolve = False , resolve_label = False ):
177
+ def _format_completion (d , include_params = True , resolve = False , resolve_label_or_snippet = False ):
177
178
completion = {
178
- 'label' : _label (d , resolve_label ),
179
+ 'label' : _label (d , resolve_label_or_snippet ),
179
180
'kind' : _TYPE_MAP .get (d .type ),
180
181
'sortText' : _sort_text (d ),
181
182
'insertText' : d .name
@@ -191,29 +192,8 @@ def _format_completion(d, include_params=True, resolve=False, resolve_label=Fals
191
192
completion ['insertText' ] = path
192
193
193
194
if include_params and not is_exception_class (d .name ):
194
- sig = d .get_signatures ()
195
- if not sig :
196
- return completion
197
-
198
- positional_args = [param for param in sig [0 ].params
199
- if '=' not in param .description and
200
- param .name not in {'/' , '*' }]
201
-
202
- if len (positional_args ) > 1 :
203
- # For completions with params, we can generate a snippet instead
204
- completion ['insertTextFormat' ] = lsp .InsertTextFormat .Snippet
205
- snippet = d .name + '('
206
- for i , param in enumerate (positional_args ):
207
- snippet += '${%s:%s}' % (i + 1 , param .name )
208
- if i < len (positional_args ) - 1 :
209
- snippet += ', '
210
- snippet += ')$0'
211
- completion ['insertText' ] = snippet
212
- elif len (positional_args ) == 1 :
213
- completion ['insertTextFormat' ] = lsp .InsertTextFormat .Snippet
214
- completion ['insertText' ] = d .name + '($0)'
215
- else :
216
- completion ['insertText' ] = d .name + '()'
195
+ snippet = _snippet (d , resolve_label_or_snippet )
196
+ completion .update (snippet )
217
197
218
198
return completion
219
199
@@ -227,6 +207,13 @@ def _label(definition, resolve=False):
227
207
return definition .name
228
208
229
209
210
+ def _snippet (definition , resolve = False ):
211
+ if not resolve :
212
+ return {}
213
+ snippet = SNIPPET_RESOLVER .get_or_create (definition )
214
+ return snippet
215
+
216
+
230
217
def _detail (definition ):
231
218
try :
232
219
return definition .parent ().full_name or ''
0 commit comments