-
Notifications
You must be signed in to change notification settings - Fork 326
/
protocolConverter.ts
1628 lines (1430 loc) · 76 KB
/
protocolConverter.ts
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
/* --------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
* ------------------------------------------------------------------------------------------ */
/// <reference path="../../typings/vscode.proposed.codeActionAI.d.ts" />
import * as code from 'vscode';
import * as ls from 'vscode-languageserver-protocol';
import * as Is from './utils/is';
import * as async from './utils/async';
import ProtocolCompletionItem from './protocolCompletionItem';
import ProtocolCodeLens from './protocolCodeLens';
import ProtocolDocumentLink from './protocolDocumentLink';
import ProtocolCodeAction from './protocolCodeAction';
import { ProtocolDiagnostic, DiagnosticCode } from './protocolDiagnostic';
import ProtocolCallHierarchyItem from './protocolCallHierarchyItem';
import ProtocolTypeHierarchyItem from './protocolTypeHierarchyItem';
import WorkspaceSymbol from './protocolWorkspaceSymbol';
import ProtocolInlayHint from './protocolInlayHint';
import { NotebookCellTextDocumentFilter, TextDocumentFilter } from 'vscode-languageserver-protocol';
interface InsertReplaceRange {
inserting: code.Range;
replacing: code.Range;
}
export interface Converter {
asUri(value: string): code.Uri;
asDocumentSelector(value: ls.DocumentSelector): code.DocumentSelector;
asPosition(value: undefined | null): undefined;
asPosition(value: ls.Position): code.Position;
asPosition(value: ls.Position | undefined | null): code.Position | undefined;
asRange(value: undefined | null): undefined;
asRange(value: ls.Range): code.Range;
asRange(value: ls.Range | undefined | null): code.Range | undefined;
asRanges(items: ReadonlyArray<ls.Range>, token?: code.CancellationToken): Promise<code.Range[]>;
asDiagnostic(diagnostic: ls.Diagnostic): code.Diagnostic;
asDiagnostics(diagnostics: ls.Diagnostic[], token?: code.CancellationToken): Promise<code.Diagnostic[]>;
asDiagnosticSeverity(value: number | undefined | null): code.DiagnosticSeverity;
asDiagnosticTag(tag: ls.DiagnosticTag): code.DiagnosticTag | undefined;
asHover(hover: undefined | null): undefined;
asHover(hover: ls.Hover): code.Hover;
asHover(hover: ls.Hover | undefined | null): code.Hover | undefined;
asCompletionResult(value: undefined | null, allCommitCharacters?: string[], token?: code.CancellationToken): Promise<undefined>;
asCompletionResult(value: ls.CompletionList, allCommitCharacters?: string[], token?: code.CancellationToken): Promise<code.CompletionList>;
asCompletionResult(value: ls.CompletionItem[], allCommitCharacters?: string[], token?: code.CancellationToken): Promise<code.CompletionItem[]>;
asCompletionResult(value: ls.CompletionItem[] | ls.CompletionList | undefined | null, allCommitCharacters?: string[], token?: code.CancellationToken): Promise<code.CompletionItem[] | code.CompletionList | undefined>;
asCompletionItem(item: ls.CompletionItem, defaultCommitCharacters?: string[]): ProtocolCompletionItem;
asTextEdit(edit: undefined | null): undefined;
asTextEdit(edit: ls.TextEdit): code.TextEdit;
asTextEdit(edit: ls.TextEdit | undefined | null): code.TextEdit | undefined;
asTextEdits(items: undefined | null, token?: code.CancellationToken): Promise<undefined>;
asTextEdits(items: ls.TextEdit[], token?: code.CancellationToken): Promise<code.TextEdit[]>;
asTextEdits(items: ls.TextEdit[] | undefined | null, token?: code.CancellationToken): Promise<code.TextEdit[] | undefined>;
asSignatureHelp(item: undefined | null, token?: code.CancellationToken): Promise<undefined>;
asSignatureHelp(item: ls.SignatureHelp, token?: code.CancellationToken): Promise<code.SignatureHelp>;
asSignatureHelp(item: ls.SignatureHelp | undefined | null, token?: code.CancellationToken): Promise<code.SignatureHelp | undefined>;
asSignatureInformation(item: ls.SignatureInformation, token?: code.CancellationToken): Promise<code.SignatureInformation>;
asSignatureInformations(items: ls.SignatureInformation[], token?: code.CancellationToken): Promise<code.SignatureInformation[]>;
asParameterInformation(item: ls.ParameterInformation): code.ParameterInformation;
asParameterInformations(item: ls.ParameterInformation[], token?: code.CancellationToken): Promise<code.ParameterInformation[]>;
asLocation(item: ls.Location): code.Location;
asLocation(item: undefined | null): undefined;
asLocation(item: ls.Location | undefined | null): code.Location | undefined;
asDeclarationResult(item: undefined | null, token?: code.CancellationToken): Promise<undefined>;
asDeclarationResult(item: ls.Declaration, token?: code.CancellationToken): Promise<code.Location | code.Location[]>;
asDeclarationResult(item: ls.DeclarationLink[], token?: code.CancellationToken): Promise<code.LocationLink[]>;
asDeclarationResult(item: ls.Declaration | ls.DeclarationLink[] | undefined | null, token?: code.CancellationToken): Promise<code.Declaration | undefined>;
asDefinitionResult(item: undefined | null, token?: code.CancellationToken): Promise<undefined>;
asDefinitionResult(item: ls.Definition, token?: code.CancellationToken): Promise<code.Definition>;
asDefinitionResult(item: ls.DefinitionLink[], token?: code.CancellationToken): Promise<code.DefinitionLink[]>;
asDefinitionResult(item: ls.Definition | ls.DefinitionLink[] | undefined | null, token?: code.CancellationToken): Promise<code.Definition | code.DefinitionLink[] | undefined>;
asReferences(values: undefined | null, token?: code.CancellationToken): Promise<undefined>;
asReferences(values: ls.Location[], token?: code.CancellationToken): Promise<code.Location[]>;
asReferences(values: ls.Location[] | undefined | null, token?: code.CancellationToken): Promise<code.Location[] | undefined>;
asDocumentHighlightKind(item: number): code.DocumentHighlightKind;
asDocumentHighlight(item: ls.DocumentHighlight): code.DocumentHighlight;
asDocumentHighlights(values: undefined | null, token?: code.CancellationToken): Promise<undefined>;
asDocumentHighlights(values: ls.DocumentHighlight[], token?: code.CancellationToken): Promise<code.DocumentHighlight[]>;
asDocumentHighlights(values: ls.DocumentHighlight[] | undefined | null, token?: code.CancellationToken): Promise<code.DocumentHighlight[] | undefined>;
asSymbolKind(item: ls.SymbolKind): code.SymbolKind;
asSymbolTag(item: ls.SymbolTag): code.SymbolTag | undefined;
asSymbolTags(items: undefined | null): undefined;
asSymbolTags(items: ReadonlyArray<ls.SymbolTag>): code.SymbolTag[];
asSymbolTags(items: ReadonlyArray<ls.SymbolTag> | undefined | null): code.SymbolTag[] | undefined;
asSymbolInformation(item: ls.SymbolInformation | ls.WorkspaceSymbol): code.SymbolInformation;
asSymbolInformations(values: undefined | null, token?: code.CancellationToken): Promise<undefined>;
asSymbolInformations(values: ls.SymbolInformation[] | ls.WorkspaceSymbol[], token?: code.CancellationToken): Promise<code.SymbolInformation[]>;
asSymbolInformations(values: ls.SymbolInformation[] | ls.WorkspaceSymbol[] | undefined | null, token?: code.CancellationToken): Promise<code.SymbolInformation[] | undefined>;
asDocumentSymbol(value: ls.DocumentSymbol, token?: code.CancellationToken): code.DocumentSymbol;
asDocumentSymbols(value: undefined | null, token?: code.CancellationToken): Promise<undefined>;
asDocumentSymbols(value: ls.DocumentSymbol[], token?: code.CancellationToken): Promise<code.DocumentSymbol[]>;
asDocumentSymbols(value: ls.DocumentSymbol[] | undefined | null, token?: code.CancellationToken): Promise<code.DocumentSymbol[] | undefined>;
asCommand(item: ls.Command): code.Command;
asCommands(items: undefined | null, token?: code.CancellationToken): Promise<undefined>;
asCommands(items: ls.Command[], token?: code.CancellationToken): Promise<code.Command[]>;
asCommands(items: ls.Command[] | undefined | null, token?: code.CancellationToken): Promise<code.Command[] | undefined>;
asCodeAction(item: undefined | null, token?: code.CancellationToken): Promise<undefined>;
asCodeAction(item: ls.CodeAction, token?: code.CancellationToken): Promise<code.CodeAction>;
asCodeAction(item: ls.CodeAction | undefined | null, token?: code.CancellationToken): Promise<code.CodeAction | undefined>;
asCodeActionKind(item: null | undefined): undefined;
asCodeActionKind(item: ls.CodeActionKind): code.CodeActionKind;
asCodeActionKind(item: ls.CodeActionKind | null | undefined): code.CodeActionKind | undefined;
asCodeActionKinds(item: null | undefined): undefined;
asCodeActionKinds(items: ls.CodeActionKind[]): code.CodeActionKind[];
asCodeActionKinds(item: ls.CodeActionKind[] | null | undefined): code.CodeActionKind[] | undefined;
asCodeActionDocumentations(items: null | undefined): undefined;
asCodeActionDocumentations(items: ls.CodeActionKindDocumentation[]): code.CodeActionProviderMetadata['documentation'];
asCodeActionDocumentations(items: ls.CodeActionKindDocumentation[] | null | undefined): code.CodeActionProviderMetadata['documentation'] | undefined;
asCodeActionResult(items: (ls.Command | ls.CodeAction)[], token?: code.CancellationToken): Promise<(code.Command | code.CodeAction)[]>;
asCodeLens(item: ls.CodeLens): code.CodeLens;
asCodeLens(item: undefined | null): undefined;
asCodeLens(item: ls.CodeLens | undefined | null): code.CodeLens | undefined;
asCodeLenses(items: undefined | null, token?: code.CancellationToken): Promise<undefined>;
asCodeLenses(items: ls.CodeLens[], token?: code.CancellationToken): Promise<code.CodeLens[]>;
asCodeLenses(items: ls.CodeLens[] | undefined | null, token?: code.CancellationToken): Promise<code.CodeLens[] | undefined>;
asWorkspaceEdit(item: undefined | null, token?: code.CancellationToken): Promise<undefined>;
asWorkspaceEdit(item: ls.WorkspaceEdit, token?: code.CancellationToken): Promise<code.WorkspaceEdit>;
asWorkspaceEdit(item: ls.WorkspaceEdit | undefined | null, token?: code.CancellationToken): Promise<code.WorkspaceEdit | undefined>;
asDocumentLink(item: ls.DocumentLink): code.DocumentLink;
asDocumentLinks(items: undefined | null, token?: code.CancellationToken): Promise<undefined>;
asDocumentLinks(items: ls.DocumentLink[], token?: code.CancellationToken): Promise<code.DocumentLink[]>;
asDocumentLinks(items: ls.DocumentLink[] | undefined | null, token?: code.CancellationToken): Promise<code.DocumentLink[] | undefined>;
asColor(color: ls.Color): code.Color;
asColorInformation(ci: ls.ColorInformation): code.ColorInformation;
asColorInformations(colorPresentations: undefined | null, token?: code.CancellationToken): Promise<undefined>;
asColorInformations(colorPresentations: ls.ColorInformation[], token?: code.CancellationToken): Promise<code.ColorInformation[]>;
asColorInformations(colorInformation: ls.ColorInformation[] | undefined | null, token?: code.CancellationToken): Promise<code.ColorInformation[]>;
asColorPresentation(cp: ls.ColorPresentation): code.ColorPresentation;
asColorPresentations(colorPresentations: undefined | null, token?: code.CancellationToken): Promise<undefined>;
asColorPresentations(colorPresentations: ls.ColorPresentation[], token?: code.CancellationToken): Promise<code.ColorPresentation[]>;
asColorPresentations(colorPresentations: ls.ColorPresentation[] | undefined | null, token?: code.CancellationToken): Promise<code.ColorPresentation[] | undefined>;
asFoldingRangeKind(kind: string | undefined): code.FoldingRangeKind | undefined;
asFoldingRange(r: ls.FoldingRange): code.FoldingRange;
asFoldingRanges(foldingRanges: undefined | null, token?: code.CancellationToken): Promise<undefined>;
asFoldingRanges(foldingRanges: ls.FoldingRange[], token?: code.CancellationToken): Promise<code.FoldingRange[]>;
asFoldingRanges(foldingRanges: ls.FoldingRange[] | undefined | null, token?: code.CancellationToken): Promise<code.FoldingRange[] | undefined>;
asSelectionRange(selectionRange: ls.SelectionRange): code.SelectionRange;
asSelectionRanges(selectionRanges: undefined | null, token?: code.CancellationToken): Promise<undefined>;
asSelectionRanges(selectionRanges: ls.SelectionRange[], token?: code.CancellationToken): Promise<code.SelectionRange[]>;
asSelectionRanges(selectionRanges: ls.SelectionRange[] | undefined | null, token?: code.CancellationToken): Promise<code.SelectionRange[] | undefined>;
asInlineValue(value: ls.InlineValue): code.InlineValue;
asInlineValues(values: undefined | null, token?: code.CancellationToken): Promise<undefined>;
asInlineValues(values: ls.InlineValue[], token?: code.CancellationToken): Promise<code.InlineValue[]>;
asInlineValues(values: ls.InlineValue[] | undefined | null, token?: code.CancellationToken): Promise<code.InlineValue[] | undefined>;
asInlayHint(value: ls.InlayHint, token?: code.CancellationToken): Promise<code.InlayHint>;
asInlayHints(values: undefined | null, token?: code.CancellationToken): Promise<undefined>;
asInlayHints(values: ls.InlayHint[], token?: code.CancellationToken): Promise<code.InlayHint[]>;
asInlayHints(values: ls.InlayHint[] | undefined | null, token?: code.CancellationToken): Promise<code.InlayHint[] | undefined>;
asSemanticTokensLegend(value: ls.SemanticTokensLegend): code.SemanticTokensLegend;
asSemanticTokens(value: undefined | null, token?: code.CancellationToken): Promise<undefined>;
asSemanticTokens(value: ls.SemanticTokens, token?: code.CancellationToken): Promise<code.SemanticTokens>;
asSemanticTokens(value: ls.SemanticTokens | undefined | null, token?: code.CancellationToken): Promise<code.SemanticTokens | undefined>;
asSemanticTokensEdit(value: ls.SemanticTokensEdit): code.SemanticTokensEdit;
asSemanticTokensEdits(value: undefined | null, token?: code.CancellationToken): Promise<undefined>;
asSemanticTokensEdits(value: ls.SemanticTokensDelta, token?: code.CancellationToken): Promise<code.SemanticTokensEdits>;
asSemanticTokensEdits(value: ls.SemanticTokensDelta | undefined | null, token?: code.CancellationToken): Promise<code.SemanticTokensEdits | undefined>;
asCallHierarchyItem(item: null): undefined;
asCallHierarchyItem(item: ls.CallHierarchyItem): code.CallHierarchyItem;
asCallHierarchyItem(item: ls.CallHierarchyItem | null): code.CallHierarchyItem | undefined;
asCallHierarchyItems(items: null, token?: code.CancellationToken): Promise<undefined>;
asCallHierarchyItems(items: ls.CallHierarchyItem[], token?: code.CancellationToken): Promise<code.CallHierarchyItem[]>;
asCallHierarchyItems(items: ls.CallHierarchyItem[] | null, token?: code.CancellationToken): Promise<code.CallHierarchyItem[] | undefined>;
asCallHierarchyIncomingCall(item: ls.CallHierarchyIncomingCall, token?: code.CancellationToken): Promise<code.CallHierarchyIncomingCall>;
asCallHierarchyIncomingCalls(items: null, token?: code.CancellationToken): Promise<undefined>;
asCallHierarchyIncomingCalls(items: ReadonlyArray<ls.CallHierarchyIncomingCall>, token?: code.CancellationToken): Promise<code.CallHierarchyIncomingCall[]>;
asCallHierarchyIncomingCalls(items: ReadonlyArray<ls.CallHierarchyIncomingCall> | null, token?: code.CancellationToken): Promise<code.CallHierarchyIncomingCall[] | undefined>;
asCallHierarchyOutgoingCall(item: ls.CallHierarchyOutgoingCall, token?: code.CancellationToken): Promise<code.CallHierarchyOutgoingCall>;
asCallHierarchyOutgoingCalls(items: null, token?: code.CancellationToken): Promise<undefined>;
asCallHierarchyOutgoingCalls(items: ReadonlyArray<ls.CallHierarchyOutgoingCall>, token?: code.CancellationToken): Promise<code.CallHierarchyOutgoingCall[]>;
asCallHierarchyOutgoingCalls(items: ReadonlyArray<ls.CallHierarchyOutgoingCall> | null, token?: code.CancellationToken): Promise<code.CallHierarchyOutgoingCall[] | undefined>;
asLinkedEditingRanges(value: null | undefined, token?: code.CancellationToken): Promise<undefined>;
asLinkedEditingRanges(value: ls.LinkedEditingRanges, token?: code.CancellationToken): Promise<code.LinkedEditingRanges>;
asLinkedEditingRanges(value: ls.LinkedEditingRanges | null | undefined, token?: code.CancellationToken): Promise<code.LinkedEditingRanges | undefined>;
asTypeHierarchyItem(item: null): undefined;
asTypeHierarchyItem(item: ls.TypeHierarchyItem): code.TypeHierarchyItem;
asTypeHierarchyItem(item: ls.TypeHierarchyItem | null): code.TypeHierarchyItem | undefined;
asTypeHierarchyItems(items: null, token?: code.CancellationToken): Promise<undefined>;
asTypeHierarchyItems(items: ls.TypeHierarchyItem[], token?: code.CancellationToken): Promise<code.TypeHierarchyItem[]>;
asTypeHierarchyItems(items: ls.TypeHierarchyItem[] | null, token?: code.CancellationToken): Promise<code.TypeHierarchyItem[] | undefined>;
asGlobPattern(pattern: undefined | null): undefined;
asGlobPattern(pattern: ls.GlobPattern): code.GlobPattern | undefined;
asInlineCompletionResult(value: undefined | null, token?: code.CancellationToken): Promise<undefined>;
asInlineCompletionResult(value: ls.InlineCompletionList, token?: code.CancellationToken): Promise<code.InlineCompletionList>;
asInlineCompletionResult(value: ls.InlineCompletionItem[], token?: code.CancellationToken): Promise<code.InlineCompletionItem[]>;
asInlineCompletionResult(value: ls.InlineCompletionItem[] | ls.InlineCompletionList | undefined | null, token?: code.CancellationToken): Promise<code.InlineCompletionItem[] | code.InlineCompletionList | undefined>;
asInlineCompletionItem(item: ls.InlineCompletionItem): code.InlineCompletionItem;
}
export interface URIConverter {
(value: string): code.Uri;
}
interface CodeFenceBlock {
language: string;
value: string;
}
namespace CodeBlock {
export function is(value: any): value is CodeFenceBlock {
const candidate: CodeFenceBlock = value;
return candidate && Is.string(candidate.language) && Is.string(candidate.value);
}
}
export function createConverter(
uriConverter: URIConverter | undefined,
trustMarkdown: boolean | { readonly enabledCommands: readonly string[] },
supportHtml: boolean,
supportThemeIcons: boolean): Converter {
const nullConverter = (value: string) => code.Uri.parse(value);
const _uriConverter: URIConverter = uriConverter || nullConverter;
function asUri(value: string): code.Uri {
return _uriConverter(value);
}
function asDocumentSelector(selector: ls.DocumentSelector): code.DocumentSelector {
const result: code.DocumentFilter | string | Array<code.DocumentFilter | string> = [];
for (const filter of selector) {
if (typeof filter === 'string') {
result.push(filter);
} else if (NotebookCellTextDocumentFilter.is(filter)) {
// We first need to check for the notebook cell filter since a TextDocumentFilter would
// match both (e.g. the notebook is optional).
if (typeof filter.notebook === 'string') {
result.push({notebookType: filter.notebook, language: filter.language});
} else {
const notebookType = filter.notebook.notebookType ?? '*';
result.push({ notebookType: notebookType, scheme: filter.notebook.scheme, pattern: asGlobPattern(filter.notebook.pattern), language: filter.language });
}
} else if (TextDocumentFilter.is(filter)) {
result.push({ language: filter.language, scheme: filter.scheme, pattern: asGlobPattern(filter.pattern) });
}
}
return result;
}
async function asDiagnostics(diagnostics: ReadonlyArray<ls.Diagnostic>, token?: code.CancellationToken): Promise<code.Diagnostic[]> {
return async.map(diagnostics, asDiagnostic, token);
}
function asDiagnosticsSync(diagnostics: ls.Diagnostic[]): code.Diagnostic[] {
const result: code.Diagnostic[] = new Array(diagnostics.length);
for (let i = 0; i < diagnostics.length; i++) {
result[i] = asDiagnostic(diagnostics[i]);
}
return result;
}
function asDiagnostic(diagnostic: ls.Diagnostic): code.Diagnostic {
const result = new ProtocolDiagnostic(asRange(diagnostic.range), diagnostic.message, asDiagnosticSeverity(diagnostic.severity), diagnostic.data);
if (diagnostic.code !== undefined) {
if (typeof diagnostic.code === 'string' || typeof diagnostic.code === 'number') {
if (ls.CodeDescription.is(diagnostic.codeDescription)) {
result.code = {
value: diagnostic.code,
target: asUri(diagnostic.codeDescription.href)
};
} else {
result.code = diagnostic.code;
}
} else if (DiagnosticCode.is(diagnostic.code)) {
// This is for backwards compatibility of a proposed API.
// We should remove this at some point.
result.hasDiagnosticCode = true;
const diagnosticCode = diagnostic.code as DiagnosticCode;
result.code = {
value: diagnosticCode.value,
target: asUri(diagnosticCode.target)
};
}
}
if (diagnostic.source) { result.source = diagnostic.source; }
if (diagnostic.relatedInformation) { result.relatedInformation = asRelatedInformation(diagnostic.relatedInformation); }
if (Array.isArray(diagnostic.tags)) { result.tags = asDiagnosticTags(diagnostic.tags); }
return result;
}
function asRelatedInformation(relatedInformation: ls.DiagnosticRelatedInformation[]): code.DiagnosticRelatedInformation[] {
const result: code.DiagnosticRelatedInformation[] = new Array(relatedInformation.length);
for (let i = 0; i < relatedInformation.length; i++) {
const info = relatedInformation[i];
result[i] = new code.DiagnosticRelatedInformation(asLocation(info.location), info.message);
}
return result;
}
function asDiagnosticTags(tags: undefined | null): undefined;
function asDiagnosticTags(tags: ls.DiagnosticTag[]): code.DiagnosticTag[];
function asDiagnosticTags(tags: ls.DiagnosticTag[] | undefined | null): code.DiagnosticTag[] | undefined;
function asDiagnosticTags(tags: ls.DiagnosticTag[] | undefined | null): code.DiagnosticTag[] | undefined {
if (!tags) {
return undefined;
}
const result: code.DiagnosticTag[] = [];
for (const tag of tags) {
const converted = asDiagnosticTag(tag);
if (converted !== undefined) {
result.push(converted);
}
}
return result.length > 0 ? result : undefined;
}
function asDiagnosticTag(tag: ls.DiagnosticTag): code.DiagnosticTag | undefined {
switch (tag) {
case ls.DiagnosticTag.Unnecessary:
return code.DiagnosticTag.Unnecessary;
case ls.DiagnosticTag.Deprecated:
return code.DiagnosticTag.Deprecated;
default:
return undefined;
}
}
function asPosition(value: undefined | null): undefined;
function asPosition(value: ls.Position): code.Position;
function asPosition(value: ls.Position | undefined | null): code.Position | undefined;
function asPosition(value: ls.Position | undefined | null): code.Position | undefined {
return value ? new code.Position(value.line, value.character) : undefined;
}
function asRange(value: undefined | null): undefined;
function asRange(value: ls.Range): code.Range;
function asRange(value: ls.Range | undefined | null): code.Range | undefined;
function asRange(value: ls.Range | undefined | null): code.Range | undefined {
return value ? new code.Range(value.start.line, value.start.character, value.end.line, value.end.character) : undefined;
}
async function asRanges(items: ReadonlyArray<ls.Range>, token?: code.CancellationToken): Promise<code.Range[]> {
return async.map(items, (range: ls.Range) => {
return new code.Range(range.start.line, range.start.character, range.end.line, range.end.character);
}, token);
}
function asDiagnosticSeverity(value: number | undefined | null): code.DiagnosticSeverity {
if (value === undefined || value === null) {
return code.DiagnosticSeverity.Error;
}
switch (value) {
case ls.DiagnosticSeverity.Error:
return code.DiagnosticSeverity.Error;
case ls.DiagnosticSeverity.Warning:
return code.DiagnosticSeverity.Warning;
case ls.DiagnosticSeverity.Information:
return code.DiagnosticSeverity.Information;
case ls.DiagnosticSeverity.Hint:
return code.DiagnosticSeverity.Hint;
}
return code.DiagnosticSeverity.Error;
}
function asHoverContent(value: ls.MarkedString | ls.MarkedString[] | ls.MarkupContent): code.MarkdownString | code.MarkdownString[] {
if (Is.string(value)) {
return asMarkdownString(value);
} else if (CodeBlock.is(value)) {
const result = asMarkdownString();
return result.appendCodeblock(value.value, value.language);
} else if (Array.isArray(value)) {
const result: code.MarkdownString[] = [];
for (const element of value) {
const item = asMarkdownString();
if (CodeBlock.is(element)) {
item.appendCodeblock(element.value, element.language);
} else {
item.appendMarkdown(element);
}
result.push(item);
}
return result;
} else {
return asMarkdownString(value);
}
}
function asDocumentation(value: string | ls.MarkupContent): string | code.MarkdownString {
if (Is.string(value)) {
return value;
} else {
switch (value.kind) {
case ls.MarkupKind.Markdown:
return asMarkdownString(value.value);
case ls.MarkupKind.PlainText:
return value.value;
default:
return `Unsupported Markup content received. Kind is: ${value.kind}`;
}
}
}
function asMarkdownString(value?: string | ls.MarkupContent): code.MarkdownString {
let result: code.MarkdownString;
if (value === undefined || typeof value === 'string') {
result = new code.MarkdownString(value);
} else {
switch (value.kind) {
case ls.MarkupKind.Markdown:
result = new code.MarkdownString(value.value);
break;
case ls.MarkupKind.PlainText:
result = new code.MarkdownString();
result.appendText(value.value);
break;
default:
result = new code.MarkdownString();
result.appendText(`Unsupported Markup content received. Kind is: ${value.kind}`);
break;
}
}
result.isTrusted = trustMarkdown;
result.supportHtml = supportHtml;
result.supportThemeIcons = supportThemeIcons;
return result;
}
function asHover(hover: ls.Hover): code.Hover;
function asHover(hover: undefined | null): undefined;
function asHover(hover: ls.Hover | undefined | null): code.Hover | undefined;
function asHover(hover: ls.Hover | undefined | null): code.Hover | undefined {
if (!hover) {
return undefined;
}
return new code.Hover(asHoverContent(hover.contents), asRange(hover.range));
}
function asCompletionResult(value: ls.CompletionList, allCommitCharacters: string[] | undefined, token?: code.CancellationToken): Promise<code.CompletionList>;
function asCompletionResult(value: ls.CompletionItem[], allCommitCharacters: string[] | undefined, token?: code.CancellationToken): Promise<code.CompletionItem[]>;
function asCompletionResult(value: undefined | null, allCommitCharacters: string[] | undefined, token?: code.CancellationToken): Promise<undefined>;
function asCompletionResult(value: ls.CompletionItem[] | ls.CompletionList | undefined | null, allCommitCharacters: string[] | undefined, token?: code.CancellationToken): Promise<code.CompletionItem[] | code.CompletionList | undefined>;
async function asCompletionResult(value: ls.CompletionItem[] | ls.CompletionList | undefined | null, allCommitCharacters: string[] | undefined, token?: code.CancellationToken): Promise<code.CompletionItem[]| code.CompletionList | undefined> {
if (!value) {
return undefined;
}
if (Array.isArray(value)) {
return async.map(value, (item) => asCompletionItem(item, allCommitCharacters), token);
}
const list = <ls.CompletionList>value;
const { defaultRange, commitCharacters } = getCompletionItemDefaults(list, allCommitCharacters);
const converted = await async.map(list.items, (item) => {
return asCompletionItem(item, commitCharacters, list.applyKind?.commitCharacters, defaultRange, list.itemDefaults?.insertTextMode, list.itemDefaults?.insertTextFormat, list.itemDefaults?.data, list.applyKind?.data);
}, token);
return new code.CompletionList(converted, list.isIncomplete);
}
function getCompletionItemDefaults(list: ls.CompletionList, allCommitCharacters?: string[]): { defaultRange: code.Range | InsertReplaceRange | undefined; commitCharacters: string[] | undefined } {
const rangeDefaults = list.itemDefaults?.editRange;
const commitCharacters = list.itemDefaults?.commitCharacters ?? allCommitCharacters;
return ls.Range.is(rangeDefaults)
? { defaultRange: asRange(rangeDefaults), commitCharacters }
: rangeDefaults !== undefined
? { defaultRange: { inserting: asRange(rangeDefaults.insert), replacing: asRange(rangeDefaults.replace) }, commitCharacters}
: { defaultRange: undefined, commitCharacters };
}
function asCompletionItemKind(value: ls.CompletionItemKind): [code.CompletionItemKind, ls.CompletionItemKind | undefined] {
// Protocol item kind is 1 based, codes item kind is zero based.
if (ls.CompletionItemKind.Text <= value && value <= ls.CompletionItemKind.TypeParameter) {
return [value - 1, undefined];
}
return [code.CompletionItemKind.Text, value];
}
function asCompletionItemTag(tag: ls.CompletionItemTag): code.CompletionItemTag | undefined {
switch (tag) {
case ls.CompletionItemTag.Deprecated:
return code.CompletionItemTag.Deprecated;
}
return undefined;
}
function asCompletionItemTags(tags: ls.CompletionItemTag[] | undefined | null): code.CompletionItemTag[] {
if (tags === undefined || tags === null) {
return [];
}
const result: code.CompletionItemTag[] = [];
for (const tag of tags) {
const converted = asCompletionItemTag(tag);
if (converted !== undefined) {
result.push(converted);
}
}
return result;
}
function asCompletionItem(
item: ls.CompletionItem,
defaultCommitCharacters?: string[],
commitCharactersApplyKind?: ls.ApplyKind,
defaultRange?: code.Range | InsertReplaceRange,
defaultInsertTextMode?: ls.InsertTextMode,
defaultInsertTextFormat?: ls.InsertTextFormat,
defaultData?: ls.LSPAny,
dataApplyKind?: ls.ApplyKind,
): ProtocolCompletionItem {
const tags: code.CompletionItemTag[] = asCompletionItemTags(item.tags);
const label = asCompletionItemLabel(item);
const result = new ProtocolCompletionItem(label);
if (item.detail) { result.detail = item.detail; }
if (item.documentation) {
result.documentation = asDocumentation(item.documentation);
result.documentationFormat = Is.string(item.documentation) ? '$string' : item.documentation.kind;
}
if (item.filterText) { result.filterText = item.filterText; }
const insertText = asCompletionInsertText(item, defaultRange, defaultInsertTextFormat);
if (insertText) {
result.insertText = insertText.text;
result.range = insertText.range;
result.fromEdit = insertText.fromEdit;
}
if (Is.number(item.kind)) {
const [itemKind, original] = asCompletionItemKind(item.kind);
result.kind = itemKind;
if (original) {
result.originalItemKind = original;
}
}
if (item.sortText) { result.sortText = item.sortText; }
if (item.additionalTextEdits) { result.additionalTextEdits = asTextEditsSync(item.additionalTextEdits); }
const commitCharacters = applyCommitCharacters(item, defaultCommitCharacters, commitCharactersApplyKind);
if (commitCharacters) { result.commitCharacters = commitCharacters.slice(); }
if (item.command) { result.command = asCommand(item.command); }
if (item.deprecated === true || item.deprecated === false) {
result.deprecated = item.deprecated;
if (item.deprecated === true) {
tags.push(code.CompletionItemTag.Deprecated);
}
}
if (item.preselect === true || item.preselect === false) { result.preselect = item.preselect; }
const data = applyData(item, defaultData, dataApplyKind);
if (data !== undefined) { result.data = data; }
if (tags.length > 0) {
result.tags = tags;
}
const insertTextMode = item.insertTextMode ?? defaultInsertTextMode;
if (insertTextMode !== undefined) {
result.insertTextMode = insertTextMode;
if (insertTextMode === ls.InsertTextMode.asIs) {
result.keepWhitespace = true;
}
}
return result;
}
function applyCommitCharacters(item: ls.CompletionItem, defaultCommitCharacters: string[] | undefined, applyKind: ls.ApplyKind | undefined): string[] | undefined {
if (applyKind === ls.ApplyKind.Merge) {
if (!defaultCommitCharacters && !item.commitCharacters) {
return undefined;
}
const set = new Set<string>();
if (defaultCommitCharacters) {
for (const char of defaultCommitCharacters) {
set.add(char);
}
}
if (Is.stringArray(item.commitCharacters)) {
for (const char of item.commitCharacters) {
set.add(char);
}
}
return Array.from(set);
}
return item.commitCharacters !== undefined
? Is.stringArray(item.commitCharacters) ? item.commitCharacters : undefined
: defaultCommitCharacters;
}
function applyData(item: ls.CompletionItem, defaultData: any, applyKind: ls.ApplyKind | undefined): string[] | undefined {
if (applyKind === ls.ApplyKind.Merge) {
const data = {
...defaultData,
};
if (item.data) {
Object.entries(item.data).forEach(([key, value]) => {
if (value !== undefined && value !== null) {
data[key] = value;
}
});
}
return data;
}
return item.data ?? defaultData;
}
function asCompletionItemLabel(item: ls.CompletionItem): code.CompletionItemLabel | string {
if (ls.CompletionItemLabelDetails.is(item.labelDetails)) {
return {
label: item.label,
detail: item.labelDetails.detail,
description: item.labelDetails.description
};
} else {
return item.label;
}
}
function asCompletionInsertText(item: ls.CompletionItem, defaultRange?: code.Range | InsertReplaceRange, defaultInsertTextFormat?: ls.InsertTextFormat): { text: string | code.SnippetString; range?: code.Range | InsertReplaceRange; fromEdit: boolean } | undefined {
const insertTextFormat = item.insertTextFormat ?? defaultInsertTextFormat;
if (item.textEdit !== undefined || defaultRange !== undefined) {
const [range, newText] = item.textEdit !== undefined
? getCompletionRangeAndText(item.textEdit)
: [defaultRange, item.textEditText ?? item.label];
if (insertTextFormat === ls.InsertTextFormat.Snippet) {
return { text: new code.SnippetString(newText), range: range, fromEdit: true };
} else {
return { text: newText, range: range, fromEdit: true };
}
} else if (item.insertText) {
if (insertTextFormat === ls.InsertTextFormat.Snippet) {
return { text: new code.SnippetString(item.insertText), fromEdit: false };
} else {
return { text: item.insertText, fromEdit: false };
}
} else {
return undefined;
}
}
function getCompletionRangeAndText(value: ls.TextEdit | ls.InsertReplaceEdit): [code.Range | InsertReplaceRange, string] {
if (ls.InsertReplaceEdit.is(value)) {
return [{ inserting: asRange(value.insert), replacing: asRange(value.replace) }, value.newText];
} else {
return [asRange(value.range), value.newText];
}
}
function asTextEdit(edit: undefined | null): undefined;
function asTextEdit(edit: ls.TextEdit): code.TextEdit;
function asTextEdit(edit: ls.TextEdit | undefined | null): code.TextEdit | undefined {
if (!edit) {
return undefined;
}
return new code.TextEdit(asRange(edit.range), edit.newText);
}
function asTextEdits(items: undefined | null, token?: code.CancellationToken): Promise<undefined>;
function asTextEdits(items: ls.TextEdit[], token?: code.CancellationToken): Promise<code.TextEdit[]>;
function asTextEdits(items: ls.TextEdit[] | undefined | null, token?: code.CancellationToken): Promise<code.TextEdit[] | undefined>;
async function asTextEdits(items: ls.TextEdit[] | undefined | null, token?: code.CancellationToken): Promise<code.TextEdit[] | undefined> {
if (!items) {
return undefined;
}
return async.map(items, asTextEdit, token);
}
function asTextEditsSync(items: undefined | null): undefined;
function asTextEditsSync(items: ls.TextEdit[]): code.TextEdit[];
function asTextEditsSync(items: ls.TextEdit[] | undefined | null): code.TextEdit[] | undefined;
function asTextEditsSync(items: ls.TextEdit[] | undefined | null): code.TextEdit[] | undefined {
if (!items) {
return undefined;
}
const result: code.TextEdit[] = new Array(items.length);
for (let i = 0; i < items.length; i++) {
result[i] = asTextEdit(items[i]);
}
return result;
}
function asSignatureHelp(item: undefined | null, token?: code.CancellationToken): Promise<undefined>;
function asSignatureHelp(item: ls.SignatureHelp, token?: code.CancellationToken): Promise<code.SignatureHelp>;
function asSignatureHelp(item: ls.SignatureHelp | undefined | null, token?: code.CancellationToken): Promise<code.SignatureHelp | undefined>;
async function asSignatureHelp(item: ls.SignatureHelp | undefined | null, token?: code.CancellationToken): Promise<code.SignatureHelp | undefined> {
if (!item) {
return undefined;
}
const result = new code.SignatureHelp();
if (Is.number(item.activeSignature)) {
result.activeSignature = item.activeSignature;
} else {
// activeSignature was optional in the past
result.activeSignature = 0;
}
if (Is.number(item.activeParameter)) {
result.activeParameter = item.activeParameter;
} else if (item.activeParameter === null) {
result.activeParameter = -1;
} else {
// activeParameter was optional in the past
result.activeParameter = 0;
}
if (item.signatures) { result.signatures = await asSignatureInformations(item.signatures, token); }
return result;
}
async function asSignatureInformations(items: ls.SignatureInformation[], token?: code.CancellationToken): Promise<code.SignatureInformation[]> {
return async.mapAsync(items, asSignatureInformation, token);
}
async function asSignatureInformation(item: ls.SignatureInformation, token?: code.CancellationToken): Promise<code.SignatureInformation> {
const result = new code.SignatureInformation(item.label);
if (item.documentation !== undefined) { result.documentation = asDocumentation(item.documentation); }
if (item.parameters !== undefined) { result.parameters = await asParameterInformations(item.parameters, token); }
if (item.activeParameter !== undefined) { result.activeParameter = item.activeParameter ?? -1; }
{ return result; }
}
function asParameterInformations(items: ls.ParameterInformation[], token?: code.CancellationToken): Promise<code.ParameterInformation[]> {
return async.map(items, asParameterInformation, token);
}
function asParameterInformation(item: ls.ParameterInformation): code.ParameterInformation {
const result = new code.ParameterInformation(item.label);
if (item.documentation) { result.documentation = asDocumentation(item.documentation); }
return result;
}
function asLocation(item: undefined | null): undefined;
function asLocation(item: ls.Location): code.Location;
function asLocation(item: ls.Location | undefined | null): code.Location | undefined;
function asLocation(item: ls.Location | undefined | null): code.Location | undefined {
return item ? new code.Location(_uriConverter(item.uri), asRange(item.range)) : undefined;
}
function asDeclarationResult(item: undefined | null, token?: code.CancellationToken): Promise<undefined>;
function asDeclarationResult(item: ls.Declaration, token?: code.CancellationToken): Promise<code.Location | code.Location[]>;
function asDeclarationResult(item: ls.DeclarationLink[], token?: code.CancellationToken): Promise<code.LocationLink[]>;
async function asDeclarationResult(item: ls.Declaration | ls.DeclarationLink[] | undefined | null, token?: code.CancellationToken): Promise<code.Declaration | undefined> {
if (!item) {
return undefined;
}
return asLocationResult(item, token);
}
function asDefinitionResult(item: undefined | null, token?: code.CancellationToken): Promise<undefined>;
function asDefinitionResult(item: ls.Definition, token?: code.CancellationToken): Promise<code.Definition>;
function asDefinitionResult(item: ls.DefinitionLink[], token?: code.CancellationToken): Promise<code.DefinitionLink[]>;
async function asDefinitionResult(item: ls.Definition | ls.DefinitionLink[] | undefined | null, token?: code.CancellationToken): Promise<code.Definition | code.DefinitionLink[] | undefined> {
if (!item) {
return undefined;
}
return asLocationResult(item, token);
}
function asLocationLink(item: undefined | null): undefined;
function asLocationLink(item: ls.LocationLink): code.LocationLink;
function asLocationLink(item: ls.LocationLink | undefined | null): code.LocationLink | undefined {
if (!item) {
return undefined;
}
const result = {
targetUri: _uriConverter(item.targetUri),
targetRange: asRange(item.targetRange), // See issue: https://github.com/Microsoft/vscode/issues/58649
originSelectionRange: asRange(item.originSelectionRange),
targetSelectionRange: asRange(item.targetSelectionRange)
};
if (!result.targetSelectionRange) {
throw new Error(`targetSelectionRange must not be undefined or null`);
}
return result;
}
async function asLocationResult(item: ls.Location | ls.Location[] | ls.LocationLink[] | undefined | null, token?: code.CancellationToken): Promise<code.Location | code.Location[] | code.LocationLink[] | undefined> {
if (!item) {
return undefined;
}
if (Is.array(item)) {
if (item.length === 0) {
return [];
} else if (ls.LocationLink.is(item[0])) {
const links = item as ls.LocationLink[];
return async.map(links, asLocationLink, token);
} else {
const locations = item as ls.Location[];
return async.map(locations, (asLocation as (item: ls.Location) => code.Location), token);
}
} else if (ls.LocationLink.is(item)) {
return [asLocationLink(item)];
} else {
return asLocation(item);
}
}
function asReferences(values: undefined | null, token?: code.CancellationToken): Promise<undefined>;
function asReferences(values: ls.Location[], token?: code.CancellationToken): Promise<code.Location[]>;
function asReferences(values: ls.Location[] | undefined | null, token?: code.CancellationToken): Promise<code.Location[] | undefined>;
async function asReferences(values: ls.Location[] | undefined | null, token?: code.CancellationToken): Promise<code.Location[] | undefined> {
if (!values) {
return undefined;
}
return async.map(values, (asLocation as (item: ls.Location) => code.Location), token);
}
function asDocumentHighlights(values: undefined | null, token?: code.CancellationToken): Promise<undefined>;
function asDocumentHighlights(values: ls.DocumentHighlight[], token?: code.CancellationToken): Promise<code.DocumentHighlight[]>;
function asDocumentHighlights(values: ls.DocumentHighlight[] | undefined | null, token?: code.CancellationToken): Promise<code.DocumentHighlight[] | undefined>;
async function asDocumentHighlights(values: ls.DocumentHighlight[] | undefined | null, token?: code.CancellationToken): Promise<code.DocumentHighlight[] | undefined> {
if (!values) {
return undefined;
}
return async.map(values, asDocumentHighlight, token);
}
function asDocumentHighlight(item: ls.DocumentHighlight): code.DocumentHighlight {
const result = new code.DocumentHighlight(asRange(item.range));
if (Is.number(item.kind)) { result.kind = asDocumentHighlightKind(item.kind); }
return result;
}
function asDocumentHighlightKind(item: number): code.DocumentHighlightKind {
switch (item) {
case ls.DocumentHighlightKind.Text:
return code.DocumentHighlightKind.Text;
case ls.DocumentHighlightKind.Read:
return code.DocumentHighlightKind.Read;
case ls.DocumentHighlightKind.Write:
return code.DocumentHighlightKind.Write;
}
return code.DocumentHighlightKind.Text;
}
function asSymbolInformations(values: undefined | null, token?: code.CancellationToken): Promise<undefined>;
function asSymbolInformations(values: ls.SymbolInformation[], token?: code.CancellationToken): Promise<code.SymbolInformation[]>;
function asSymbolInformations(values: ls.SymbolInformation[] | undefined | null, token?: code.CancellationToken): Promise<code.SymbolInformation[] | undefined>;
async function asSymbolInformations(values: ls.SymbolInformation[] | undefined | null, token?: code.CancellationToken): Promise<code.SymbolInformation[] | undefined> {
if (!values) {
return undefined;
}
return async.map(values, asSymbolInformation, token);
}
function asSymbolKind(item: ls.SymbolKind): code.SymbolKind {
if (item <= ls.SymbolKind.TypeParameter) {
// Symbol kind is one based in the protocol and zero based in code.
return item - 1;
}
return code.SymbolKind.Property;
}
function asSymbolTag(value: ls.SymbolTag): code.SymbolTag | undefined {
switch (value) {
case ls.SymbolTag.Deprecated:
return code.SymbolTag.Deprecated;
default:
return undefined;
}
}
function asSymbolTags(items: undefined | null): undefined;
function asSymbolTags(items: ReadonlyArray<ls.SymbolTag>): code.SymbolTag[];
function asSymbolTags(items: ReadonlyArray<ls.SymbolTag> | undefined | null): code.SymbolTag[] | undefined;
function asSymbolTags(items: ReadonlyArray<ls.SymbolTag> | undefined | null): code.SymbolTag[] | undefined {
if (items === undefined || items === null) {
return undefined;
}
const result: code.SymbolTag[] = [];
for (const item of items) {
const converted = asSymbolTag(item);
if (converted !== undefined) {
result.push(converted);
}
}
return result.length === 0 ? undefined : result;
}
function asSymbolInformation(item: ls.SymbolInformation | ls.WorkspaceSymbol): code.SymbolInformation {
const data: ls.LSPAny | undefined = (item as ls.WorkspaceSymbol).data;
const location: Omit<ls.Location, 'range'> & { range?: ls.Range } = item.location;
const result: code.SymbolInformation = location.range === undefined || data !== undefined
? new WorkspaceSymbol(
item.name, asSymbolKind(item.kind), item.containerName ?? '',
location.range === undefined ? _uriConverter(location.uri) : new code.Location(_uriConverter(item.location.uri), asRange(location.range)), data)
: new code.SymbolInformation(
item.name, asSymbolKind(item.kind), item.containerName ?? '',
new code.Location(_uriConverter(item.location.uri), asRange(location.range)));
fillTags(result, item);
return result;
}
function asDocumentSymbols(values: undefined | null, token?: code.CancellationToken): Promise<undefined>;
function asDocumentSymbols(values: ls.DocumentSymbol[], token?: code.CancellationToken): Promise<code.DocumentSymbol[]>;
async function asDocumentSymbols(values: ls.DocumentSymbol[] | undefined | null, token?: code.CancellationToken): Promise<code.DocumentSymbol[] | undefined> {
if (values === undefined || values === null) {
return undefined;
}
return async.map(values, asDocumentSymbol, token);
}
function asDocumentSymbol(value: ls.DocumentSymbol): code.DocumentSymbol {
const result = new code.DocumentSymbol(
value.name,
value.detail || '',
asSymbolKind(value.kind),
asRange(value.range),
asRange(value.selectionRange)
);
fillTags(result, value);
if (value.children !== undefined && value.children.length > 0) {
const children: code.DocumentSymbol[] = [];
for (const child of value.children) {
children.push(asDocumentSymbol(child));
}
result.children = children;
}
return result;
}
function fillTags(result: { tags?: ReadonlyArray<code.SymbolTag> }, value: { tags?: ls.SymbolTag[]; deprecated?: boolean }): void {
result.tags = asSymbolTags(value.tags);
if (value.deprecated) {
if (!result.tags) {
result.tags = [code.SymbolTag.Deprecated];
} else {
if (!result.tags.includes(code.SymbolTag.Deprecated)) {
result.tags = result.tags.concat(code.SymbolTag.Deprecated);
}
}
}
}
function asCommand(item: ls.Command): code.Command {
const result: code.Command = { title: item.title, command: item.command };
if (item.tooltip) { result.tooltip = item.tooltip; }
if (item.arguments) { result.arguments = item.arguments; }
return result;
}