-
-
Notifications
You must be signed in to change notification settings - Fork 48
/
complete.d
1263 lines (1152 loc) · 35.1 KB
/
complete.d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
module served.commands.complete;
import served.commands.format : formatCode, formatSnippet;
import served.extension;
import served.types;
import served.utils.ddoc;
import served.utils.fibermanager;
import workspaced.api;
import workspaced.com.dfmt : DfmtComponent;
import workspaced.com.dcd;
import workspaced.com.dcdext;
import workspaced.com.snippets;
import workspaced.coms;
import std.algorithm : among, any, canFind, chunkBy, endsWith, filter, findSplit,
map, min, reverse, sort, startsWith, uniq;
import std.array : appender, array;
import std.conv : text, to;
import std.format : format;
import std.experimental.logger;
import std.json : JSONType, JSONValue;
import std.string : indexOf, join, lastIndexOf, lineSplitter, strip, stripLeft,
stripRight, toLower;
import std.utf : decodeFront;
import dparse.lexer : Token;
import painlessjson : fromJSON, toJSON;
import fs = std.file;
import io = std.stdio;
static immutable sortPrefixDoc = "0_";
static immutable sortPrefixSnippets = "2_5_";
// dcd additionally sorts inside with sortFromDCDType return value (appends to this)
static immutable sortPrefixDCD = "2_";
CompletionItemKind convertFromDCDType(string type)
{
if (type.length != 1)
return CompletionItemKind.text;
switch (type[0])
{
case 'c': // class name
return CompletionItemKind.class_;
case 'i': // interface name
return CompletionItemKind.interface_;
case 's': // struct name
case 'u': // union name
return CompletionItemKind.struct_;
case 'a': // array
case 'A': // associative array
case 'v': // variable name
return CompletionItemKind.variable;
case 'm': // member variable
return CompletionItemKind.field;
case 'e': // enum member
return CompletionItemKind.enumMember;
case 'k': // keyword
return CompletionItemKind.keyword;
case 'f': // function
return CompletionItemKind.function_;
case 'g': // enum name
return CompletionItemKind.enum_;
case 'P': // package name
case 'M': // module name
return CompletionItemKind.module_;
case 'l': // alias name
return CompletionItemKind.reference;
case 't': // template name
case 'T': // mixin template name
return CompletionItemKind.property;
case 'h': // template type parameter
case 'p': // template variadic parameter
return CompletionItemKind.typeParameter;
default:
return CompletionItemKind.text;
}
}
string sortFromDCDType(string type)
{
if (type.length != 1)
return "9_";
switch (type[0])
{
case 'v': // variable name
return "2_";
case 'm': // member variable
return "3_";
case 'f': // function
return "4_";
case 'k': // keyword
case 'e': // enum member
return "5_";
case 'c': // class name
case 'i': // interface name
case 's': // struct name
case 'u': // union name
case 'a': // array
case 'A': // associative array
case 'g': // enum name
case 'P': // package name
case 'M': // module name
case 'l': // alias name
case 't': // template name
case 'T': // mixin template name
case 'h': // template type parameter
case 'p': // template variadic parameter
return "6_";
default:
return "9_";
}
}
SymbolKind convertFromDCDSearchType(string type)
{
if (type.length != 1)
return cast(SymbolKind) 0;
switch (type[0])
{
case 'c':
return SymbolKind.class_;
case 'i':
return SymbolKind.interface_;
case 's':
case 'u':
return SymbolKind.package_;
case 'a':
case 'A':
case 'v':
return SymbolKind.variable;
case 'm':
case 'e':
return SymbolKind.field;
case 'f':
case 'l':
return SymbolKind.function_;
case 'g':
return SymbolKind.enum_;
case 'P':
case 'M':
return SymbolKind.namespace;
case 't':
case 'T':
return SymbolKind.property;
case 'k':
default:
return cast(SymbolKind) 0;
}
}
SymbolKind convertFromDscannerType(string type, string name = null)
{
if (type.length != 1)
return cast(SymbolKind) 0;
switch (type[0])
{
case 'c':
return SymbolKind.class_;
case 's':
return SymbolKind.struct_;
case 'i':
return SymbolKind.interface_;
case 'T':
return SymbolKind.property;
case 'f':
case 'U':
case 'Q':
case 'W':
case 'P':
if (name == "this")
return SymbolKind.constructor;
else
return SymbolKind.function_;
case 'C':
case 'S':
return SymbolKind.constructor;
case 'g':
return SymbolKind.enum_;
case 'u':
return SymbolKind.struct_;
case 'D':
case 'V':
case 'e':
return SymbolKind.constant;
case 'v':
return SymbolKind.variable;
case 'a':
return SymbolKind.field;
default:
return cast(SymbolKind) 0;
}
}
SymbolKindEx convertExtendedFromDscannerType(string type)
{
if (type.length != 1)
return cast(SymbolKindEx) 0;
switch (type[0])
{
case 'U':
return SymbolKindEx.test;
case 'D':
return SymbolKindEx.debugSpec;
case 'V':
return SymbolKindEx.versionSpec;
case 'C':
return SymbolKindEx.staticCtor;
case 'S':
return SymbolKindEx.sharedStaticCtor;
case 'Q':
return SymbolKindEx.staticDtor;
case 'W':
return SymbolKindEx.sharedStaticDtor;
case 'P':
return SymbolKindEx.postblit;
default:
return cast(SymbolKindEx) 0;
}
}
C[] substr(C, T)(C[] s, T start, T end)
{
if (!s.length)
return s;
if (start < 0)
start = 0;
if (start >= s.length)
start = s.length - 1;
if (end > s.length)
end = s.length;
if (end < start)
return s[start .. start];
return s[start .. end];
}
/// Extracts all function parameters for a given declaration string.
/// Params:
/// sig = the function signature such as `string[] example(string sig, bool exact = false)`
/// exact = set to true to make the returned values include the closing paren at the end (if exists)
const(char)[][] extractFunctionParameters(scope const(char)[] sig, bool exact = false)
{
if (!sig.length)
return [];
auto params = appender!(const(char)[][]);
ptrdiff_t i = sig.length - 1;
if (sig[i] == ')' && !exact)
i--;
ptrdiff_t paramEnd = i + 1;
void skipStr()
{
i--;
if (sig[i + 1] == '\'')
for (; i >= 0; i--)
if (sig[i] == '\'')
return;
bool escapeNext = false;
while (i >= 0)
{
if (sig[i] == '\\')
escapeNext = false;
if (escapeNext)
break;
if (sig[i] == '"')
escapeNext = true;
i--;
}
}
void skip(char open, char close)
{
i--;
int depth = 1;
while (i >= 0 && depth > 0)
{
if (sig[i] == '"' || sig[i] == '\'')
skipStr();
else
{
if (sig[i] == close)
depth++;
else if (sig[i] == open)
depth--;
i--;
}
}
}
while (i >= 0)
{
switch (sig[i])
{
case ',':
params.put(sig.substr(i + 1, paramEnd).strip);
paramEnd = i;
i--;
break;
case ';':
case '(':
auto param = sig.substr(i + 1, paramEnd).strip;
if (param.length)
params.put(param);
auto ret = params.data;
reverse(ret);
return ret;
case ')':
skip('(', ')');
break;
case '}':
skip('{', '}');
break;
case ']':
skip('[', ']');
break;
case '"':
case '\'':
skipStr();
break;
default:
i--;
break;
}
}
auto ret = params.data;
reverse(ret);
return ret;
}
unittest
{
void assertEqual(A, B)(A a, B b)
{
import std.conv : to;
assert(a == b, a.to!string ~ " is not equal to " ~ b.to!string);
}
assertEqual(extractFunctionParameters("void foo()"), cast(string[])[]);
assertEqual(extractFunctionParameters(`auto bar(int foo, Button, my.Callback cb)`),
["int foo", "Button", "my.Callback cb"]);
assertEqual(extractFunctionParameters(`SomeType!(int, "int_") foo(T, Args...)(T a, T b, string[string] map, Other!"(" stuff1, SomeType!(double, ")double") myType, Other!"(" stuff, Other!")")`),
[
"T a", "T b", "string[string] map", `Other!"(" stuff1`,
`SomeType!(double, ")double") myType`, `Other!"(" stuff`, `Other!")"`
]);
assertEqual(extractFunctionParameters(`SomeType!(int,"int_")foo(T,Args...)(T a,T b,string[string] map,Other!"(" stuff1,SomeType!(double,")double")myType,Other!"(" stuff,Other!")")`),
[
"T a", "T b", "string[string] map", `Other!"(" stuff1`,
`SomeType!(double,")double")myType`, `Other!"(" stuff`, `Other!")"`
]);
assertEqual(extractFunctionParameters(`some_garbage(code); before(this); funcCall(4`,
true), [`4`]);
assertEqual(extractFunctionParameters(`some_garbage(code); before(this); funcCall(4, f(4)`,
true), [`4`, `f(4)`]);
assertEqual(extractFunctionParameters(`some_garbage(code); before(this); funcCall(4, ["a"], JSONValue(["b": JSONValue("c")]), recursive(func, call!s()), "texts )\"(too"`,
true), [
`4`, `["a"]`, `JSONValue(["b": JSONValue("c")])`,
`recursive(func, call!s())`, `"texts )\"(too"`
]);
}
/// Provide snippets in auto-completion
__gshared bool doCompleteSnippets = false;
// === Protocol Methods starting here ===
@protocolMethod("textDocument/completion")
CompletionList provideComplete(TextDocumentPositionParams params)
{
Document document = documents[params.textDocument.uri];
auto instance = activeInstance = backend.getBestInstance(document.uri.uriToFile);
trace("Completing from instance ", instance ? instance.cwd : "null");
if (document.uri.toLower.endsWith("dscanner.ini"))
{
trace("Providing dscanner.ini completion");
auto possibleFields = backend.get!DscannerComponent.listAllIniFields;
scope line = document.lineAtScope(params.position).strip;
auto defaultList = CompletionList(false, possibleFields.map!((a) {
CompletionItem ret = CompletionItem(a.name, CompletionItemKind.field.opt);
ret.documentation = MarkupContent(a.documentation).opt;
ret.insertText = (a.name ~ '=').opt;
return ret;
}).array);
if (!line.length)
return defaultList;
if (line[0] == '[')
return CompletionList(false, [
CompletionItem("analysis.config.StaticAnalysisConfig",
CompletionItemKind.keyword.opt),
CompletionItem("analysis.config.ModuleFilters", CompletionItemKind.keyword.opt, Optional!string.init,
MarkupContent("In this optional section a comma-separated list of inclusion and exclusion"
~ " selectors can be specified for every check on which selective filtering"
~ " should be applied. These given selectors match on the module name and"
~ " partial matches (std. or .foo.) are possible. Moreover, every selectors"
~ " must begin with either + (inclusion) or - (exclusion). Exclusion selectors"
~ " take precedence over all inclusion operators.").opt)
]);
auto eqIndex = line.indexOf('=');
auto quotIndex = line.lastIndexOf('"');
if (quotIndex != -1 && params.position.character >= quotIndex)
return CompletionList.init;
if (params.position.character < eqIndex)
return defaultList;
else
return CompletionList(false, [
CompletionItem(`"disabled"`, CompletionItemKind.value.opt,
"Check is disabled".opt),
CompletionItem(`"enabled"`, CompletionItemKind.value.opt,
"Check is enabled".opt),
CompletionItem(`"skip-unittest"`, CompletionItemKind.value.opt,
"Check is enabled but not operated in the unittests".opt)
]);
}
else
{
if (!instance)
{
trace("Providing no completion because no instance");
return CompletionList.init;
}
if (document.getLanguageId == "d")
return provideDSourceComplete(params, instance, document);
else if (document.getLanguageId == "diet")
return provideDietSourceComplete(params, instance, document);
else if (document.getLanguageId == "dml")
return provideDMLSourceComplete(params, instance, document);
else
{
tracef("Providing no completion for unknown language ID %s.", document.getLanguageId);
return CompletionList.init;
}
}
}
CompletionList provideDMLSourceComplete(TextDocumentPositionParams params,
WorkspaceD.Instance instance, ref Document document)
{
import workspaced.com.dlangui : DlanguiComponent, CompletionType;
CompletionList ret;
auto items = backend.get!DlanguiComponent.complete(document.rawText,
cast(int) document.positionToBytes(params.position)).getYield();
ret.items.length = items.length;
foreach (i, item; items)
{
CompletionItem translated;
translated.sortText = ((item.type == CompletionType.Class ? "1." : "0.") ~ item.value).opt;
translated.label = item.value;
if (item.documentation.length)
translated.documentation = MarkupContent(item.documentation).opt;
if (item.enumName.length)
translated.detail = item.enumName.opt;
switch (item.type)
{
case CompletionType.Class:
translated.insertTextFormat = InsertTextFormat.snippet;
translated.insertText = item.value ~ ` {$0}`;
break;
case CompletionType.Color:
translated.insertTextFormat = InsertTextFormat.snippet;
translated.insertText = item.value ~ `: ${0:#000000}`;
break;
case CompletionType.String:
translated.insertTextFormat = InsertTextFormat.snippet;
translated.insertText = item.value ~ `: "$0"`;
break;
case CompletionType.EnumDefinition:
translated.insertTextFormat = InsertTextFormat.plainText;
translated.insertText = item.enumName ~ "." ~ item.value;
break;
case CompletionType.Rectangle:
case CompletionType.Number:
translated.insertTextFormat = InsertTextFormat.snippet;
translated.insertText = item.value ~ `: ${0:0}`;
break;
case CompletionType.Keyword:
// don't set, inherit from label
break;
default:
translated.insertTextFormat = InsertTextFormat.plainText;
translated.insertText = item.value ~ ": ";
break;
}
switch (item.type)
{
case CompletionType.Class:
translated.kind = CompletionItemKind.class_;
break;
case CompletionType.String:
translated.kind = CompletionItemKind.value;
break;
case CompletionType.Number:
translated.kind = CompletionItemKind.value;
break;
case CompletionType.Color:
translated.kind = CompletionItemKind.color;
break;
case CompletionType.EnumDefinition:
translated.kind = CompletionItemKind.enum_;
break;
case CompletionType.EnumValue:
translated.kind = CompletionItemKind.enumMember;
break;
case CompletionType.Rectangle:
translated.kind = CompletionItemKind.typeParameter;
break;
case CompletionType.Boolean:
translated.kind = CompletionItemKind.constant;
break;
case CompletionType.Keyword:
translated.kind = CompletionItemKind.keyword;
break;
default:
case CompletionType.Undefined:
break;
}
ret.items[i] = translated;
}
return ret;
}
CompletionList provideDietSourceComplete(TextDocumentPositionParams params,
WorkspaceD.Instance instance, ref Document document)
{
import served.utils.diet;
import dc = dietc.complete;
auto completion = updateDietFile(document.uri.uriToFile, document.rawText.idup);
auto dcdext = instance.has!DCDExtComponent ? instance.get!DCDExtComponent : null;
size_t offset = document.positionToBytes(params.position);
auto raw = completion.completeAt(offset);
CompletionItem[] ret;
if (raw is dc.Completion.completeD)
{
auto d = workspace(params.textDocument.uri).config.d;
string code;
contextExtractD(completion, offset, code, offset, d.dietContextCompletion);
if (offset <= code.length && instance.has!DCDComponent)
{
info("DCD Completing Diet for ", code, " at ", offset);
auto dcd = instance.get!DCDComponent.listCompletion(code, cast(int) offset).getYield;
if (dcd.type == DCDCompletions.Type.identifiers)
{
ret = dcd.identifiers.convertDCDIdentifiers(d.argumentSnippets, dcdext);
}
}
}
else
ret = raw.map!((a) {
CompletionItem ret;
ret.label = a.text;
ret.kind = a.type.mapToCompletionItemKind.opt;
if (a.definition.length)
{
ret.detail = a.definition.opt;
if (capabilities.textDocument.completion.completionItem.labelDetailsSupport)
ret.labelDetails = CompletionItemLabelDetails(ret.detail);
}
if (a.documentation.length)
ret.documentation = MarkupContent(a.documentation).opt;
if (a.preselected)
ret.preselect = true.opt;
return ret;
}).array;
return CompletionList(false, ret);
}
CompletionList provideDSourceComplete(TextDocumentPositionParams params,
WorkspaceD.Instance instance, ref Document document)
{
auto lineRange = document.lineByteRangeAt(params.position.line);
auto byteOff = cast(int) document.positionToBytes(params.position);
auto dcdext = instance.has!DCDExtComponent ? instance.get!DCDExtComponent : null;
string line = document.rawText[lineRange[0] .. lineRange[1]].idup;
string prefix = line[0 .. min($, params.position.character)].strip;
CompletionItem[] completion;
Token commentToken;
if (document.rawText.isInComment(byteOff, backend, &commentToken))
{
import dparse.lexer : tok;
if (commentToken.type == tok!"__EOF__")
return CompletionList.init;
if (commentToken.text.startsWith("///", "/**", "/++"))
{
trace("Providing comment completion");
int prefixLen = prefix[0] == '/' ? 3 : 1;
auto remaining = prefix[prefixLen .. $].stripLeft;
foreach (compl; import("ddocs.txt").lineSplitter)
{
if (compl.startsWith(remaining))
{
auto item = CompletionItem(compl, CompletionItemKind.snippet.opt);
item.insertText = compl ~ ": ";
completion ~= item;
}
}
// make the comment one "line" so provide doc complete shows complete
// after a /** */ comment block if you are on the first line.
lineRange[1] = commentToken.index + commentToken.text.length;
provideDocComplete(params, instance, document, completion, line, lineRange);
return CompletionList(false, completion);
}
}
bool completeDCD = instance.has!DCDComponent;
bool completeDoc = instance.has!DscannerComponent;
bool completeSnippets = doCompleteSnippets && instance.has!SnippetsComponent;
tracef("Performing regular D comment completion (DCD=%s, Documentation=%s, Snippets=%s)",
completeDCD, completeDoc, completeSnippets);
const config = workspace(params.textDocument.uri).config;
DCDCompletions result = DCDCompletions.empty;
joinAll({
if (completeDCD)
result = instance.get!DCDComponent.listCompletion(document.rawText, byteOff).getYield;
}, {
if (completeDoc)
provideDocComplete(params, instance, document, completion, line, lineRange);
}, {
if (completeSnippets)
provideSnippetComplete(params, instance, document, config, completion, byteOff);
});
if (completeDCD && result != DCDCompletions.init)
{
if (result.type == DCDCompletions.Type.identifiers)
{
auto d = config.d;
completion ~= convertDCDIdentifiers(result.identifiers, d.argumentSnippets, dcdext);
}
else if (result.type != DCDCompletions.Type.calltips)
{
trace("Unexpected result from DCD: ", result);
}
}
return CompletionList(false, completion);
}
private void provideDocComplete(TextDocumentPositionParams params, WorkspaceD.Instance instance,
ref Document document, ref CompletionItem[] completion, string line, size_t[2] lineRange)
{
string lineStripped = line.strip;
if (lineStripped.among!("", "/", "/*", "/+", "//", "///", "/**", "/++"))
{
auto defs = instance.get!DscannerComponent.listDefinitions(uriToFile(
params.textDocument.uri), document.rawText[lineRange[1] .. $]).getYield;
ptrdiff_t di = -1;
FuncFinder: foreach (i, def; defs)
{
if (def.line >= 0 && def.line <= 5)
{
di = i;
break FuncFinder;
}
}
if (di == -1)
return;
auto def = defs[di];
auto sig = "signature" in def.attributes;
if (!sig)
{
CompletionItem doc = CompletionItem("///");
doc.kind = CompletionItemKind.snippet;
doc.insertTextFormat = InsertTextFormat.snippet;
auto eol = document.eolAt(params.position.line).toString;
doc.insertText = "/// ";
CompletionItem doc2 = doc;
CompletionItem doc3 = doc;
doc2.label = "/**";
doc2.insertText = "/** " ~ eol ~ " * $0" ~ eol ~ " */";
doc3.label = "/++";
doc3.insertText = "/++ " ~ eol ~ " * $0" ~ eol ~ " +/";
completion.addDocComplete(doc, lineStripped);
completion.addDocComplete(doc2, lineStripped);
completion.addDocComplete(doc3, lineStripped);
return;
}
auto funcArgs = extractFunctionParameters(*sig);
string[] docs = ["$0"];
int argNo = 1;
foreach (arg; funcArgs)
{
auto space = arg.stripRight.lastIndexOf(' ');
if (space == -1)
continue;
auto identifier = arg[space + 1 .. $];
if (!identifier.isValidDIdentifier)
continue;
if (argNo == 1)
docs ~= "Params:";
docs ~= text(" ", identifier, " = $", argNo.to!string);
argNo++;
}
auto retAttr = "return" in def.attributes;
if (retAttr && *retAttr != "void")
{
docs ~= "Returns: $" ~ argNo.to!string;
argNo++;
}
auto depr = "deprecation" in def.attributes;
if (depr)
{
docs ~= "Deprecated: $" ~ argNo.to!string ~ *depr;
argNo++;
}
CompletionItem doc = CompletionItem("///");
doc.kind = CompletionItemKind.snippet;
doc.insertTextFormat = InsertTextFormat.snippet;
auto eol = document.eolAt(params.position.line).toString;
doc.insertText = docs.map!(a => "/// " ~ a).join(eol);
CompletionItem doc2 = doc;
CompletionItem doc3 = doc;
doc2.label = "/**";
doc2.insertText = "/** " ~ eol ~ docs.map!(a => " * " ~ a ~ eol).join() ~ " */";
doc3.label = "/++";
doc3.insertText = "/++ " ~ eol ~ docs.map!(a => " + " ~ a ~ eol).join() ~ " +/";
doc.sortText = opt(sortPrefixDoc ~ "0");
doc2.sortText = opt(sortPrefixDoc ~ "1");
doc3.sortText = opt(sortPrefixDoc ~ "2");
completion.addDocComplete(doc, lineStripped);
completion.addDocComplete(doc2, lineStripped);
completion.addDocComplete(doc3, lineStripped);
}
}
private void provideSnippetComplete(TextDocumentPositionParams params, WorkspaceD.Instance instance,
ref Document document, ref const UserConfiguration config,
ref CompletionItem[] completion, int byteOff)
{
if (byteOff > 0 && document.rawText[byteOff - 1 .. $].startsWith("."))
return; // no snippets after '.' character
auto snippets = instance.get!SnippetsComponent;
auto ret = snippets.getSnippetsYield(document.uri.uriToFile, document.rawText, byteOff);
trace("got ", ret.snippets.length, " snippets fitting in this context: ",
ret.snippets.map!"a.shortcut");
auto eol = document.eolAt(0);
foreach (Snippet snippet; ret.snippets)
{
auto item = snippet.snippetToCompletionItem;
item.data["level"] = JSONValue(ret.info.level.to!string);
if (!snippet.unformatted)
item.data["format"] = toJSON(generateDfmtArgs(config, eol));
item.data["params"] = toJSON(params);
completion ~= item;
}
}
private void addDocComplete(ref CompletionItem[] completion, CompletionItem doc, string prefix)
{
if (!doc.label.startsWith(prefix))
return;
if (prefix.length > 0)
doc.insertText = doc.insertText[prefix.length .. $];
completion ~= doc;
}
private bool isInComment(scope const(char)[] code, size_t at, WorkspaceD backend, Token* outToken = null)
{
if (!backend)
return false;
import dparse.lexer : DLexer, LexerConfig, StringBehavior, tok;
// TODO: does this kind of token parsing belong in serve-d?
LexerConfig config;
config.fileName = "stdin";
config.stringBehavior = StringBehavior.source;
auto lexer = DLexer(code, config, &backend.stringCache);
while (!lexer.empty)
{
if (lexer.front.index > at)
return false;
switch (lexer.front.type)
{
case tok!"comment":
auto t = lexer.front;
auto commentEnd = t.index + t.text.length;
if (t.text.startsWith("//"))
commentEnd++;
if (t.index <= at && at < commentEnd)
{
if (outToken !is null)
*outToken = t;
return true;
}
lexer.popFront();
break;
case tok!"__EOF__":
if (outToken !is null)
*outToken = lexer.front;
return true;
default:
lexer.popFront();
break;
}
}
return false;
}
@protocolMethod("completionItem/resolve")
CompletionItem resolveCompletionItem(CompletionItem item)
{
auto data = item.data;
if (item.insertTextFormat.get == InsertTextFormat.snippet
&& item.kind.get == CompletionItemKind.snippet && data.type == JSONType.object)
{
const resolved = "resolved" in data.object;
if (resolved.type != JSONType.true_)
{
TextDocumentPositionParams params = data.object["params"]
.fromJSON!TextDocumentPositionParams;
Document document = documents[params.textDocument.uri];
auto f = document.uri.uriToFile;
auto instance = backend.getBestInstance(f);
if (instance.has!SnippetsComponent)
{
auto snippets = instance.get!SnippetsComponent;
auto snippet = snippetFromCompletionItem(item);
snippet = snippets.resolveSnippet(f, document.rawText,
cast(int) document.positionToBytes(params.position), snippet).getYield;
item = snippetToCompletionItem(snippet);
}
}
if (const format = "format" in data.object)
{
auto args = (*format).fromJSON!(string[]);
if (item.insertTextFormat.get == InsertTextFormat.snippet)
{
SnippetLevel level = SnippetLevel.global;
if (const levelStr = "level" in data.object)
level = levelStr.str.to!SnippetLevel;
item.insertText = formatSnippet(item.insertText.get, args, level).opt;
}
else
{
item.insertText = formatCode(item.insertText.get, args).opt;
}
}
// TODO: format code
return item;
}
else
{
return item;
}
}
CompletionItem snippetToCompletionItem(Snippet snippet)
{
CompletionItem item;
item.label = snippet.shortcut;
item.sortText = opt(sortPrefixSnippets ~ snippet.shortcut);
item.detail = snippet.title.opt;
item.kind = CompletionItemKind.snippet.opt;
item.documentation = MarkupContent(MarkupKind.markdown,
snippet.documentation ~ "\n\n```d\n" ~ snippet.snippet ~ "\n```\n");
item.filterText = snippet.shortcut.opt;
if (capabilities.textDocument.completion.completionItem.snippetSupport)
{
item.insertText = snippet.snippet.opt;
item.insertTextFormat = InsertTextFormat.snippet.opt;
}
else
item.insertText = snippet.plain.opt;
item.data = JSONValue([
"resolved": JSONValue(snippet.resolved),
"id": JSONValue(snippet.id),
"providerId": JSONValue(snippet.providerId),
"data": snippet.data
]);
return item;
}
Snippet snippetFromCompletionItem(CompletionItem item)
{
Snippet snippet;
snippet.shortcut = item.label;
snippet.title = item.detail.get;
snippet.documentation = item.documentation.get.value;
auto end = snippet.documentation.lastIndexOf("\n\n```d\n");
if (end != -1)
snippet.documentation = snippet.documentation[0 .. end];
if (capabilities.textDocument.completion.completionItem.snippetSupport)
snippet.snippet = item.insertText.get;
else
snippet.plain = item.insertText.get;
snippet.resolved = item.data["resolved"].boolean;
snippet.id = item.data["id"].str;
snippet.providerId = item.data["providerId"].str;
snippet.data = item.data["data"];
return snippet;
}
unittest
{
auto backend = new WorkspaceD();
assert(isInComment(`hello /** world`, 10, backend));
assert(!isInComment(`hello /** world`, 3, backend));
assert(isInComment(`hello /* world */ bar`, 8, backend));
assert(isInComment(`hello /* world */ bar`, 16, backend));
assert(!isInComment(`hello /* world */ bar`, 17, backend));
assert(!isInComment("int x;\n// line comment\n", 6, backend));
assert(isInComment("int x;\n// line comment\n", 7, backend));
assert(isInComment("int x;\n// line comment\n", 9, backend));
assert(isInComment("int x;\n// line comment\n", 21, backend));
assert(isInComment("int x;\n// line comment\n", 22, backend));
assert(!isInComment("int x;\n// line comment\n", 23, backend));
}
auto convertDCDIdentifiers(DCDIdentifier[] identifiers, bool argumentSnippets, DCDExtComponent dcdext)
{
CompletionItem[] completion;
foreach (identifier; identifiers)
{
CompletionItem item;
string detailDetail, detailDescription;
item.label = identifier.identifier;
item.kind = identifier.type.convertFromDCDType;
if (identifier.documentation.length)
item.documentation = MarkupContent(identifier.documentation.ddocToMarked);
if (identifier.definition.length == 0)
{
if (identifier.type.length == 1)
{
switch (identifier.type[0])
{
case 'c':
detailDescription = "Class";
break;
case 'i':
detailDescription = "Interface";
break;
case 's':
detailDescription = "Struct";
break;
case 'u':
detailDescription = "Union";
break;
case 'a':
detailDescription = "Array";
break;
case 'A':
detailDescription = "AA";
break;
case 'v':
detailDescription = "Variable";
break;
case 'm':
detailDescription = "Member";
break;
case 'e':
// lowercare to differentiate member from enum name
detailDescription = "enum";
break;
case 'k':
detailDescription = "Keyword";