@@ -135,7 +135,7 @@ defmodule ElixirLS.LanguageServer.Providers.Completion do
135
135
136
136
items =
137
137
ElixirSense . suggestions ( text , line + 1 , character + 1 )
138
- |> Enum . map ( & from_completion_item ( & 1 , context ) )
138
+ |> Enum . map ( & from_completion_item ( & 1 , context , options ) )
139
139
|> Enum . concat ( module_attr_snippets ( context ) )
140
140
|> Enum . concat ( keyword_completions ( context ) )
141
141
@@ -151,12 +151,16 @@ defmodule ElixirLS.LanguageServer.Providers.Completion do
151
151
152
152
## Helpers
153
153
154
- defp from_completion_item ( % { type: :attribute , name: name } , % {
155
- prefix: prefix ,
156
- def_before: nil ,
157
- capture_before?: false ,
158
- pipe_before?: false
159
- } ) do
154
+ defp from_completion_item (
155
+ % { type: :attribute , name: name } ,
156
+ % {
157
+ prefix: prefix ,
158
+ def_before: nil ,
159
+ capture_before?: false ,
160
+ pipe_before?: false
161
+ } ,
162
+ _options
163
+ ) do
160
164
name_only = String . trim_leading ( name , "@" )
161
165
insert_text = if String . starts_with? ( prefix , "@" ) , do: name_only , else: name
162
166
@@ -175,11 +179,15 @@ defmodule ElixirLS.LanguageServer.Providers.Completion do
175
179
end
176
180
end
177
181
178
- defp from_completion_item ( % { type: :variable , name: name } , % {
179
- def_before: nil ,
180
- pipe_before?: false ,
181
- capture_before?: false
182
- } ) do
182
+ defp from_completion_item (
183
+ % { type: :variable , name: name } ,
184
+ % {
185
+ def_before: nil ,
186
+ pipe_before?: false ,
187
+ capture_before?: false
188
+ } ,
189
+ _options
190
+ ) do
183
191
% __MODULE__ {
184
192
label: to_string ( name ) ,
185
193
kind: :variable ,
@@ -192,7 +200,8 @@ defmodule ElixirLS.LanguageServer.Providers.Completion do
192
200
193
201
defp from_completion_item (
194
202
% { type: :return , description: description , spec: spec , snippet: snippet } ,
195
- % { def_before: nil , capture_before?: false , pipe_before?: false }
203
+ % { def_before: nil , capture_before?: false , pipe_before?: false } ,
204
+ _options
196
205
) do
197
206
snippet = Regex . replace ( Regex . recompile! ( ~r/ "\$ \{ (.*)\} \$ "/ U ) , snippet , "${\\ 1}" )
198
207
@@ -212,7 +221,8 @@ defmodule ElixirLS.LanguageServer.Providers.Completion do
212
221
% {
213
222
def_before: nil ,
214
223
prefix: prefix
215
- }
224
+ } ,
225
+ _options
216
226
) do
217
227
capitalized? = String . first ( name ) == String . upcase ( String . first ( name ) )
218
228
@@ -252,7 +262,8 @@ defmodule ElixirLS.LanguageServer.Providers.Completion do
252
262
origin: origin ,
253
263
metadata: metadata
254
264
} ,
255
- context
265
+ context ,
266
+ options
256
267
) do
257
268
if ( context [ :def_before ] == :def && String . starts_with? ( spec , "@macrocallback" ) ) ||
258
269
( context [ :def_before ] == :defmacro && String . starts_with? ( spec , "@callback" ) ) do
@@ -267,15 +278,15 @@ defmodule ElixirLS.LanguageServer.Providers.Completion do
267
278
end
268
279
end
269
280
270
- full_snippet = " #{ def_str } #{ snippet ( name , args , arity ) } do \n \t $0 \n end"
281
+ insert_text = def_snippet ( def_str , name , args , arity , options )
271
282
label = "#{ def_str } #{ function_label ( name , args , arity ) } "
272
283
273
284
% __MODULE__ {
274
285
label: label ,
275
286
kind: :interface ,
276
287
detail: "#{ origin } callback" ,
277
288
documentation: summary ,
278
- insert_text: full_snippet ,
289
+ insert_text: insert_text ,
279
290
priority: 2 ,
280
291
filter_text: name ,
281
292
tags: metadata_to_tags ( metadata )
@@ -294,19 +305,20 @@ defmodule ElixirLS.LanguageServer.Providers.Completion do
294
305
origin: origin ,
295
306
metadata: metadata
296
307
} ,
297
- context
308
+ context ,
309
+ options
298
310
) do
299
311
def_str = if ( context [ :def_before ] == nil , do: "def " )
300
312
301
- full_snippet = " #{ def_str } #{ snippet ( name , args , arity ) } do \n \t $0 \n end"
313
+ insert_text = def_snippet ( def_str , name , args , arity , options )
302
314
label = "#{ def_str } #{ function_label ( name , args , arity ) } "
303
315
304
316
% __MODULE__ {
305
317
label: label ,
306
318
kind: :interface ,
307
319
detail: "#{ origin } protocol function" ,
308
320
documentation: summary ,
309
- insert_text: full_snippet ,
321
+ insert_text: insert_text ,
310
322
priority: 2 ,
311
323
filter_text: name ,
312
324
tags: metadata_to_tags ( metadata )
@@ -315,7 +327,8 @@ defmodule ElixirLS.LanguageServer.Providers.Completion do
315
327
316
328
defp from_completion_item (
317
329
% { type: :field , subtype: subtype , name: name , origin: origin , call?: call? } ,
318
- _context
330
+ _context ,
331
+ _options
319
332
) do
320
333
detail =
321
334
case { subtype , origin } do
@@ -334,7 +347,7 @@ defmodule ElixirLS.LanguageServer.Providers.Completion do
334
347
}
335
348
end
336
349
337
- defp from_completion_item ( % { type: :param_option } = suggestion , _context ) do
350
+ defp from_completion_item ( % { type: :param_option } = suggestion , _context , _options ) do
338
351
% { name: name , origin: _origin , doc: doc , type_spec: type_spec , expanded_spec: expanded_spec } =
339
352
suggestion
340
353
@@ -356,7 +369,11 @@ defmodule ElixirLS.LanguageServer.Providers.Completion do
356
369
}
357
370
end
358
371
359
- defp from_completion_item ( % { type: :type_spec , metadata: metadata } = suggestion , _context ) do
372
+ defp from_completion_item (
373
+ % { type: :type_spec , metadata: metadata } = suggestion ,
374
+ _context ,
375
+ _options
376
+ ) do
360
377
% { name: name , arity: arity , origin: _origin , doc: doc , signature: signature , spec: spec } =
361
378
suggestion
362
379
@@ -387,9 +404,10 @@ defmodule ElixirLS.LanguageServer.Providers.Completion do
387
404
388
405
defp from_completion_item (
389
406
% { name: name , origin: origin } = item ,
390
- % { def_before: nil } = context
407
+ % { def_before: nil } = context ,
408
+ options
391
409
) do
392
- completion = function_completion ( item , context )
410
+ completion = function_completion ( item , context , options )
393
411
394
412
completion =
395
413
if origin == "Kernel" || origin == "Kernel.SpecialForms" do
@@ -405,7 +423,7 @@ defmodule ElixirLS.LanguageServer.Providers.Completion do
405
423
end
406
424
end
407
425
408
- defp from_completion_item ( _suggestion , _context ) do
426
+ defp from_completion_item ( _suggestion , _context , _options ) do
409
427
nil
410
428
end
411
429
@@ -417,30 +435,43 @@ defmodule ElixirLS.LanguageServer.Providers.Completion do
417
435
end
418
436
end
419
437
420
- defp snippet ( name , args , arity , opts \\ [ ] ) do
421
- if Keyword . get ( opts , :capture_before? ) && arity <= 1 do
422
- Enum . join ( [ name , "/" , arity ] )
438
+ defp def_snippet ( def_str , name , args , arity , opts ) do
439
+ if Keyword . get ( opts , :snippets_supported , false ) do
440
+ " #{ def_str } #{ function_snippet ( name , args , arity ) } do \n \t $0 \n end"
423
441
else
424
- args_list =
425
- if args && args != "" do
426
- split_args ( args )
427
- else
428
- for i <- Enum . slice ( 0 .. arity , 1 .. - 1 ) , do: "arg#{ i } "
429
- end
442
+ "#{ def_str } #{ name } "
443
+ end
444
+ end
430
445
431
- args_list =
432
- if Keyword . get ( opts , :pipe_before? ) do
433
- Enum . slice ( args_list , 1 .. - 1 )
434
- else
435
- args_list
436
- end
446
+ defp function_snippet ( name , args , arity , opts \\ [ ] ) do
447
+ cond do
448
+ Keyword . get ( opts , :capture_before? ) && arity <= 1 ->
449
+ Enum . join ( [ name , "/" , arity ] )
437
450
438
- tabstops =
439
- args_list
440
- |> Enum . with_index ( )
441
- |> Enum . map ( fn { arg , i } -> "${#{ i + 1 } :#{ arg } }" end )
451
+ not Keyword . get ( opts , :snippets_supported , false ) ->
452
+ name
442
453
443
- Enum . join ( [ name , "(" , Enum . join ( tabstops , ", " ) , ")" ] )
454
+ true ->
455
+ args_list =
456
+ if args && args != "" do
457
+ split_args ( args )
458
+ else
459
+ for i <- Enum . slice ( 0 .. arity , 1 .. - 1 ) , do: "arg#{ i } "
460
+ end
461
+
462
+ args_list =
463
+ if Keyword . get ( opts , :pipe_before? ) do
464
+ Enum . slice ( args_list , 1 .. - 1 )
465
+ else
466
+ args_list
467
+ end
468
+
469
+ tabstops =
470
+ args_list
471
+ |> Enum . with_index ( )
472
+ |> Enum . map ( fn { arg , i } -> "${#{ i + 1 } :#{ arg } }" end )
473
+
474
+ Enum . join ( [ name , "(" , Enum . join ( tabstops , ", " ) , ")" ] )
444
475
end
445
476
end
446
477
@@ -534,7 +565,7 @@ defmodule ElixirLS.LanguageServer.Providers.Completion do
534
565
end )
535
566
end
536
567
537
- defp function_completion ( info , context ) do
568
+ defp function_completion ( info , context , options ) do
538
569
% {
539
570
type: type ,
540
571
args: args ,
@@ -555,7 +586,7 @@ defmodule ElixirLS.LanguageServer.Providers.Completion do
555
586
text_after_cursor: text_after_cursor
556
587
} = context
557
588
558
- { label , snippet } =
589
+ { label , insert_text } =
559
590
cond do
560
591
match? ( "sigil_" <> _ , name ) ->
561
592
"sigil_" <> sigil_name = name
@@ -568,16 +599,19 @@ defmodule ElixirLS.LanguageServer.Providers.Completion do
568
599
true ->
569
600
label = function_label ( name , args , arity )
570
601
571
- snippet =
572
- snippet (
602
+ insert_text =
603
+ function_snippet (
573
604
name ,
574
605
args ,
575
606
arity ,
576
- pipe_before?: pipe_before? ,
577
- capture_before?: capture_before?
607
+ Keyword . merge (
608
+ options ,
609
+ pipe_before?: pipe_before? ,
610
+ capture_before?: capture_before?
611
+ )
578
612
)
579
613
580
- { label , snippet }
614
+ { label , insert_text }
581
615
end
582
616
583
617
detail =
@@ -597,7 +631,7 @@ defmodule ElixirLS.LanguageServer.Providers.Completion do
597
631
kind: :function ,
598
632
detail: detail ,
599
633
documentation: summary ,
600
- insert_text: snippet ,
634
+ insert_text: insert_text ,
601
635
priority: 7 ,
602
636
tags: metadata_to_tags ( metadata )
603
637
}
@@ -670,7 +704,7 @@ defmodule ElixirLS.LanguageServer.Providers.Completion do
670
704
end
671
705
672
706
defp snippet? ( item ) do
673
- item . kind == :snippet || String . match? ( item . insert_text , ~r/ \$ \d / )
707
+ item . kind == :snippet || String . match? ( item . insert_text , ~r/ \$ {? \d / )
674
708
end
675
709
676
710
# As defined by CompletionItemTag in https://microsoft.github.io/language-server-protocol/specifications/specification-current/
0 commit comments