-
Notifications
You must be signed in to change notification settings - Fork 105
/
Copy pathSemanticTokensFeature.cs
1034 lines (925 loc) · 45.9 KB
/
SemanticTokensFeature.cs
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
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using MediatR;
using Newtonsoft.Json;
using OmniSharp.Extensions.JsonRpc;
using OmniSharp.Extensions.JsonRpc.Generation;
using OmniSharp.Extensions.LanguageServer.Protocol.Client;
using OmniSharp.Extensions.LanguageServer.Protocol.Client.Capabilities;
using OmniSharp.Extensions.LanguageServer.Protocol.Document;
using OmniSharp.Extensions.LanguageServer.Protocol.Generation;
using OmniSharp.Extensions.LanguageServer.Protocol.Models;
using OmniSharp.Extensions.LanguageServer.Protocol.Progress;
using OmniSharp.Extensions.LanguageServer.Protocol.Serialization;
using OmniSharp.Extensions.LanguageServer.Protocol.Serialization.Converters;
using OmniSharp.Extensions.LanguageServer.Protocol.Server;
using OmniSharp.Extensions.LanguageServer.Protocol.Server.Capabilities;
// ReSharper disable once CheckNamespace
namespace OmniSharp.Extensions.LanguageServer.Protocol
{
namespace Models
{
/// <summary>
/// @since 3.16.0
/// </summary>
[Parallel]
[Method(TextDocumentNames.SemanticTokensFull, Direction.ClientToServer)]
[GenerateHandler("OmniSharp.Extensions.LanguageServer.Protocol.Document", Name = "SemanticTokensFull")]
[GenerateHandlerMethods]
[GenerateRequestMethods(typeof(ITextDocumentLanguageClient), typeof(ILanguageClient))]
[RegistrationOptions(typeof(SemanticTokensRegistrationOptions))]
[Capability(typeof(SemanticTokensCapability))]
public partial record SemanticTokensParams : IWorkDoneProgressParams, ITextDocumentIdentifierParams,
IPartialItemRequest<SemanticTokens?, SemanticTokensPartialResult>
{
/// <summary>
/// The text document.
/// </summary>
public TextDocumentIdentifier TextDocument { get; init; } = null!;
}
/// <summary>
/// @since 3.16.0
/// </summary>
[Parallel]
[Method(TextDocumentNames.SemanticTokensFullDelta, Direction.ClientToServer)]
[GenerateHandler("OmniSharp.Extensions.LanguageServer.Protocol.Document")]
[GenerateHandlerMethods]
[GenerateRequestMethods(typeof(ITextDocumentLanguageClient), typeof(ILanguageClient))]
[RegistrationOptions(typeof(SemanticTokensRegistrationOptions))]
[Capability(typeof(SemanticTokensCapability))]
public partial record SemanticTokensDeltaParams : IWorkDoneProgressParams, ITextDocumentIdentifierParams,
IPartialItemRequest<SemanticTokensFullOrDelta?, SemanticTokensFullOrDeltaPartialResult>,
IDoesNotParticipateInRegistration
{
/// <summary>
/// The text document.
/// </summary>
public TextDocumentIdentifier TextDocument { get; init; } = null!;
/// <summary>
/// The previous result id.
/// </summary>
public string PreviousResultId { get; init; } = null!;
}
/// <summary>
/// @since 3.16.0
/// </summary>
[Parallel]
[Method(TextDocumentNames.SemanticTokensRange, Direction.ClientToServer)]
[GenerateHandler("OmniSharp.Extensions.LanguageServer.Protocol.Document")]
[GenerateHandlerMethods]
[GenerateRequestMethods(typeof(ITextDocumentLanguageClient), typeof(ILanguageClient))]
[RegistrationOptions(typeof(SemanticTokensRegistrationOptions))]
[Capability(typeof(SemanticTokensCapability))]
public partial record SemanticTokensRangeParams : IWorkDoneProgressParams, ITextDocumentIdentifierParams,
IPartialItemRequest<SemanticTokens?, SemanticTokensPartialResult>, IDoesNotParticipateInRegistration
{
/// <summary>
/// The text document.
/// </summary>
public TextDocumentIdentifier TextDocument { get; init; } = null!;
/// <summary>
/// The range the semantic tokens are requested for.
/// </summary>
public Range Range { get; init; } = null!;
}
[Parallel]
[Method(WorkspaceNames.SemanticTokensRefresh, Direction.ServerToClient)]
[GenerateHandler("OmniSharp.Extensions.LanguageServer.Protocol.Workspace")]
[GenerateHandlerMethods]
[GenerateRequestMethods(typeof(IWorkspaceLanguageServer), typeof(ILanguageServer))]
[Capability(typeof(SemanticTokensWorkspaceCapability))]
public partial record SemanticTokensRefreshParams : IRequest;
public interface ISemanticTokenResult
{
/// <summary>
/// An optional result id. If provided and clients support delta updating
/// the client will include the result id in the next semantic token request.
/// A server can then instead of computing all semantic tokens again simply
/// send a delta.
/// </summary>
[Optional]
public string? ResultId { get; init; }
}
/// <summary>
/// @since 3.16.0
/// </summary>
public partial record SemanticTokens : ISemanticTokenResult
{
public SemanticTokens()
{
}
public SemanticTokens(SemanticTokensPartialResult partialResult)
{
Data = partialResult.Data;
}
/// <summary>
/// An optional result id. If provided and clients support delta updating
/// the client will include the result id in the next semantic token request.
/// A server can then instead of computing all semantic tokens again simply
/// send a delta.
/// </summary>
[Optional]
public string? ResultId { get; init; }
/// <summary>
/// The actual tokens. For a detailed description about how the data is
/// structured pls see
/// https://github.com/microsoft/vscode-extension-samples/blob/5ae1f7787122812dcc84e37427ca90af5ee09f14/semantic-tokens-sample/vscode.proposed.d.ts#L71
/// </summary>
/// <remarks>
/// <see cref="uint" /> in the LSP spec
/// </remarks>
public ImmutableArray<int> Data { get; init; }
[return: NotNullIfNotNull("result")]
public static SemanticTokens? From(SemanticTokensPartialResult? result)
{
return result switch
{
not null => new SemanticTokens(result),
_ => null
};
}
}
/// <summary>
/// @since 3.16.0
/// </summary>
public partial record SemanticTokensPartialResult
{
/// <summary>
/// The actual tokens. For a detailed description about how the data is
/// structured pls see
/// https://github.com/microsoft/vscode-extension-samples/blob/5ae1f7787122812dcc84e37427ca90af5ee09f14/semantic-tokens-sample/vscode.proposed.d.ts#L71
/// </summary>
public ImmutableArray<int> Data { get; init; }
public SemanticTokensPartialResult()
{
}
internal SemanticTokensPartialResult(SemanticTokens? result)
{
Data = result?.Data ?? ImmutableArray<int>.Empty;
}
public static SemanticTokensPartialResult From(SemanticTokens? result) => new SemanticTokensPartialResult(result);
}
/// <summary>
/// @since 3.16.0
/// </summary>
public record SemanticTokensDelta : ISemanticTokenResult
{
public SemanticTokensDelta()
{
}
public SemanticTokensDelta(SemanticTokensDeltaPartialResult partialResult)
{
Edits = partialResult.Edits;
}
/// <summary>
/// An optional result id. If provided and clients support delta updating
/// the client will include the result id in the next semantic token request.
/// A server can then instead of computing all semantic tokens again simply
/// send a delta.
/// </summary>
[Optional]
public string? ResultId { get; init; }
/// <summary>
/// For a detailed description how these edits are structured pls see
/// https://github.com/microsoft/vscode-extension-samples/blob/5ae1f7787122812dcc84e37427ca90af5ee09f14/semantic-tokens-sample/vscode.proposed.d.ts#L131
/// </summary>
public Container<SemanticTokensEdit> Edits { get; init; } = null!;
}
/// <summary>
/// @since 3.16.0
/// </summary>
public record SemanticTokensDeltaPartialResult
{
/// <summary>
/// The actual tokens. For a detailed description about how the data is
/// structured pls see
/// https://github.com/microsoft/vscode-extension-samples/blob/5ae1f7787122812dcc84e37427ca90af5ee09f14/semantic-tokens-sample/vscode.proposed.d.ts#L71
/// </summary>
public Container<SemanticTokensEdit> Edits { get; init; } = null!;
public SemanticTokensDeltaPartialResult()
{
}
internal SemanticTokensDeltaPartialResult(SemanticTokensDelta? result)
{
Edits = result?.Edits ?? new Container<SemanticTokensEdit>();
}
}
/// <summary>
/// @since 3.16.0
/// </summary>
public record SemanticTokensEdit
{
/// <summary>
/// The start index of the edit
/// </summary>
/// <remarks>
/// <see cref="uint" /> in the LSP spec
/// </remarks>
public int Start { get; init; }
/// <summary>
/// The number of items to delete
/// </summary>
/// <remarks>
/// <see cref="uint" /> in the LSP spec
/// </remarks>
public int DeleteCount { get; init; }
/// <summary>
/// The actual tokens. For a detailed description about how the data is
/// structured pls see
/// https://github.com/microsoft/vscode-extension-samples/blob/5ae1f7787122812dcc84e37427ca90af5ee09f14/semantic-tokens-sample/vscode.proposed.d.ts#L71
/// </summary>
/// <remarks>
/// <see cref="uint" /> in the LSP spec
/// </remarks>
[Optional]
public ImmutableArray<int>? Data { get; init; } = ImmutableArray<int>.Empty;
}
[JsonConverter(typeof(SemanticTokensFullOrDeltaConverter))]
public record SemanticTokensFullOrDelta
{
public SemanticTokensFullOrDelta(SemanticTokensDelta delta)
{
Delta = delta;
Full = null;
}
public SemanticTokensFullOrDelta(SemanticTokens full)
{
Delta = null;
Full = full;
}
public SemanticTokensFullOrDelta(SemanticTokensFullOrDeltaPartialResult partialResult)
{
Full = null;
Delta = null;
if (partialResult.IsDelta)
{
Delta = new SemanticTokensDelta(partialResult.Delta!)
{
Edits = partialResult.Delta!.Edits
};
}
if (partialResult.IsFull)
{
Full = new SemanticTokens(partialResult.Full!);
}
}
public bool IsFull => Full != null;
public SemanticTokens? Full { get; init; }
public bool IsDelta => Delta != null;
public SemanticTokensDelta? Delta { get; init; }
[return: NotNullIfNotNull("semanticTokensDelta")]
public static SemanticTokensFullOrDelta? From(SemanticTokensDelta? semanticTokensDelta)
{
return semanticTokensDelta switch
{
not null => new(semanticTokensDelta),
_ => null
};
}
[return: NotNullIfNotNull("semanticTokensDelta")]
public static implicit operator SemanticTokensFullOrDelta?(SemanticTokensDelta? semanticTokensDelta)
{
return semanticTokensDelta switch
{
not null => new(semanticTokensDelta),
_ => null
};
}
[return: NotNullIfNotNull("semanticTokens")]
public static SemanticTokensFullOrDelta? From(SemanticTokens? semanticTokens)
{
return semanticTokens switch
{
not null => new(semanticTokens),
_ => null
};
}
[return: NotNullIfNotNull("semanticTokens")]
public static implicit operator SemanticTokensFullOrDelta?(SemanticTokens? semanticTokens)
{
return semanticTokens switch
{
not null => new(semanticTokens),
_ => null
};
}
[return: NotNullIfNotNull("semanticTokens")]
public static SemanticTokensFullOrDelta? From(SemanticTokensFullOrDeltaPartialResult? semanticTokens)
{
return semanticTokens switch
{
not null => new(semanticTokens),
_ => null
};
}
[return: NotNullIfNotNull("semanticTokens")]
public static implicit operator SemanticTokensFullOrDelta?(SemanticTokensFullOrDeltaPartialResult? semanticTokens)
{
return semanticTokens switch
{
not null => new(semanticTokens),
_ => null
};
}
}
[JsonConverter(typeof(SemanticTokensFullOrDeltaPartialResultConverter))]
public record SemanticTokensFullOrDeltaPartialResult
{
public SemanticTokensFullOrDeltaPartialResult(
SemanticTokensPartialResult full
)
{
Full = full;
Delta = null;
}
public SemanticTokensFullOrDeltaPartialResult(
SemanticTokensDeltaPartialResult delta
)
{
Full = null;
Delta = delta;
}
public SemanticTokensFullOrDeltaPartialResult(SemanticTokensFullOrDelta delta)
{
if (delta.IsFull)
{
Full = new SemanticTokensPartialResult(delta.Full);
}
if (delta.IsDelta)
{
Delta = new SemanticTokensDeltaPartialResult(delta.Delta);
}
}
public bool IsDelta => Delta != null;
public SemanticTokensDeltaPartialResult? Delta { get; }
public bool IsFull => Full != null;
public SemanticTokensPartialResult? Full { get; }
public static implicit operator SemanticTokensFullOrDeltaPartialResult(SemanticTokensPartialResult semanticTokensPartialResult)
{
return new SemanticTokensFullOrDeltaPartialResult(semanticTokensPartialResult);
}
public static implicit operator SemanticTokensFullOrDeltaPartialResult(SemanticTokensDeltaPartialResult semanticTokensDeltaPartialResult)
{
return new SemanticTokensFullOrDeltaPartialResult(semanticTokensDeltaPartialResult);
}
public static implicit operator SemanticTokensFullOrDelta(SemanticTokensFullOrDeltaPartialResult semanticTokensDeltaPartialResult)
{
return new SemanticTokensFullOrDelta(semanticTokensDeltaPartialResult);
}
public static SemanticTokensFullOrDeltaPartialResult From(SemanticTokensFullOrDelta? result) => new SemanticTokensFullOrDeltaPartialResult(result);
}
/// <summary>
/// @since 3.16.0
/// </summary>
public record SemanticTokensLegend
{
private ImmutableDictionary<SemanticTokenModifier, int>? _tokenModifiersData;
private ImmutableDictionary<SemanticTokenType, int>? _tokenTypesData;
/// <summary>
/// The token types a server uses.
/// </summary>
public Container<SemanticTokenType> TokenTypes { get; init; } = new Container<SemanticTokenType>(SemanticTokenType.Defaults);
/// <summary>
/// The token modifiers a server uses.
/// </summary>
public Container<SemanticTokenModifier> TokenModifiers { get; init; } = new Container<SemanticTokenModifier>(SemanticTokenModifier.Defaults);
public int GetTokenTypeIdentity(string tokenType)
{
EnsureTokenTypes();
if (string.IsNullOrWhiteSpace(tokenType)) return 0;
return _tokenTypesData != null && _tokenTypesData.TryGetValue(tokenType, out var tokenTypeNumber) ? tokenTypeNumber : 0;
}
public int GetTokenTypeIdentity(SemanticTokenType? tokenType)
{
EnsureTokenTypes();
if (!tokenType.HasValue) return 0;
if (string.IsNullOrWhiteSpace(tokenType.Value)) return 0;
return _tokenTypesData != null && _tokenTypesData.TryGetValue(tokenType.Value, out var tokenTypeNumber) ? tokenTypeNumber : 0;
}
public int GetTokenModifiersIdentity(params string[]? tokenModifiers)
{
EnsureTokenModifiers();
if (tokenModifiers == null) return 0;
return tokenModifiers
.Where(z => !string.IsNullOrWhiteSpace(z))
.Aggregate(
0,
(acc, value) => _tokenModifiersData != null && _tokenModifiersData.TryGetValue(value, out var tokenModifer)
? acc + tokenModifer
: acc
);
}
public int GetTokenModifiersIdentity(IEnumerable<string>? tokenModifiers)
{
EnsureTokenModifiers();
if (tokenModifiers == null) return 0;
return tokenModifiers
.Where(z => !string.IsNullOrWhiteSpace(z))
.Aggregate(
0,
(acc, value) => _tokenModifiersData != null && _tokenModifiersData.TryGetValue(value, out var tokenModifer)
? acc + tokenModifer
: acc
);
}
public int GetTokenModifiersIdentity(params SemanticTokenModifier[]? tokenModifiers)
{
EnsureTokenModifiers();
if (tokenModifiers == null) return 0;
return tokenModifiers
.Where(z => !string.IsNullOrWhiteSpace(z))
.Aggregate(
0,
(acc, value) => _tokenModifiersData != null && _tokenModifiersData.TryGetValue(value, out var tokenModifer)
? acc + tokenModifer
: acc
);
}
public int GetTokenModifiersIdentity(IEnumerable<SemanticTokenModifier>? tokenModifiers)
{
EnsureTokenModifiers();
if (tokenModifiers == null) return 0;
return tokenModifiers
.Where(z => !string.IsNullOrWhiteSpace(z))
.Aggregate(
0,
(acc, value) => _tokenModifiersData != null && _tokenModifiersData.TryGetValue(value, out var tokenModifer)
? acc + tokenModifer
: acc
);
}
private void EnsureTokenTypes()
{
_tokenTypesData ??= TokenTypes
.Select(
(value, index) => (
value: new SemanticTokenType(value),
index
)
)
.Where(z => !string.IsNullOrWhiteSpace(z.value))
.ToImmutableDictionary(z => z.value, z => z.index);
}
private void EnsureTokenModifiers()
{
_tokenModifiersData ??= TokenModifiers
.Select(
(value, index) => (
value: new SemanticTokenModifier(value),
index
)
)
.Where(z => !string.IsNullOrWhiteSpace(z.value))
.ToImmutableDictionary(z => z.value, z => Convert.ToInt32(Math.Pow(2, z.index)));
}
}
/// <summary>
/// The protocol defines an additional token format capability to allow future extensions of the format.
/// The only format that is currently specified is `relative` expressing that the tokens are described using relative positions.
///
/// @since 3.16.0
/// </summary>
[StringEnum]
public readonly partial struct SemanticTokenFormat
{
public static SemanticTokenFormat Relative { get; } = new SemanticTokenFormat("relative");
}
/// <summary>
/// A set of predefined token modifiers. This set is not fixed
/// an clients can specify additional token types via the
/// corresponding client capabilities.
///
/// @since 3.16.0
/// </summary>
[StringEnum]
public readonly partial struct SemanticTokenModifier
{
public static SemanticTokenModifier Documentation { get; } = new SemanticTokenModifier("documentation");
public static SemanticTokenModifier Declaration { get; } = new SemanticTokenModifier("declaration");
public static SemanticTokenModifier Definition { get; } = new SemanticTokenModifier("definition");
public static SemanticTokenModifier Static { get; } = new SemanticTokenModifier("static");
public static SemanticTokenModifier Async { get; } = new SemanticTokenModifier("async");
public static SemanticTokenModifier Abstract { get; } = new SemanticTokenModifier("abstract");
public static SemanticTokenModifier Deprecated { get; } = new SemanticTokenModifier("deprecated");
public static SemanticTokenModifier Readonly { get; } = new SemanticTokenModifier("readonly");
public static SemanticTokenModifier Modification { get; } = new SemanticTokenModifier("modification");
public static SemanticTokenModifier DefaultLibrary { get; } = new SemanticTokenModifier("defaultLibrary");
}
/// <summary>
/// A set of predefined token types. This set is not fixed
/// an clients can specify additional token types via the
/// corresponding client capabilities.
///
/// @since 3.16.0
/// </summary>
[StringEnum]
public readonly partial struct SemanticTokenType
{
public static SemanticTokenType Comment { get; } = new SemanticTokenType("comment");
public static SemanticTokenType Keyword { get; } = new SemanticTokenType("keyword");
public static SemanticTokenType String { get; } = new SemanticTokenType("string");
public static SemanticTokenType Number { get; } = new SemanticTokenType("number");
public static SemanticTokenType Regexp { get; } = new SemanticTokenType("regexp");
public static SemanticTokenType Operator { get; } = new SemanticTokenType("operator");
public static SemanticTokenType Namespace { get; } = new SemanticTokenType("namespace");
public static SemanticTokenType Type { get; } = new SemanticTokenType("type");
public static SemanticTokenType Struct { get; } = new SemanticTokenType("struct");
public static SemanticTokenType Class { get; } = new SemanticTokenType("class");
public static SemanticTokenType Interface { get; } = new SemanticTokenType("interface");
public static SemanticTokenType Enum { get; } = new SemanticTokenType("enum");
public static SemanticTokenType TypeParameter { get; } = new SemanticTokenType("typeParameter");
public static SemanticTokenType Function { get; } = new SemanticTokenType("function");
public static SemanticTokenType Method { get; } = new SemanticTokenType("method");
public static SemanticTokenType Property { get; } = new SemanticTokenType("property");
public static SemanticTokenType Macro { get; } = new SemanticTokenType("macro");
public static SemanticTokenType Variable { get; } = new SemanticTokenType("variable");
public static SemanticTokenType Parameter { get; } = new SemanticTokenType("parameter");
public static SemanticTokenType Label { get; } = new SemanticTokenType("label");
public static SemanticTokenType Modifier { get; } = new SemanticTokenType("modifier");
public static SemanticTokenType Event { get; } = new SemanticTokenType("event");
public static SemanticTokenType EnumMember { get; } = new SemanticTokenType("enumMember");
/// <summary>
/// @since 3.17.0
/// </summary>
public static SemanticTokenType Decorator { get; } = new SemanticTokenType("decorator");
}
[RegistrationName(TextDocumentNames.SemanticTokensRegistration)]
[GenerateRegistrationOptions(nameof(ServerCapabilities.SemanticTokensProvider))]
[RegistrationOptionsConverter(typeof(SemanticTokensRegistrationOptionsConverter))]
public partial class SemanticTokensRegistrationOptions : ITextDocumentRegistrationOptions, IWorkDoneProgressOptions, IStaticRegistrationOptions
{
/// <summary>
/// The legend used by the server
/// </summary>
public SemanticTokensLegend Legend { get; set; } = null!;
/// <summary>
/// Server supports providing semantic tokens for a specific range
/// of a document.
/// </summary>
[Optional]
public BooleanOr<SemanticTokensCapabilityRequestRange>? Range { get; set; }
/// <summary>
/// Server supports providing semantic tokens for a full document.
/// </summary>
[Optional]
public BooleanOr<SemanticTokensCapabilityRequestFull>? Full { get; set; }
private class SemanticTokensRegistrationOptionsConverter : RegistrationOptionsConverterBase<SemanticTokensRegistrationOptions, StaticOptions>
{
private readonly IHandlersManager _handlersManager;
public SemanticTokensRegistrationOptionsConverter(IHandlersManager handlersManager)
{
_handlersManager = handlersManager;
}
public override StaticOptions Convert(SemanticTokensRegistrationOptions source)
{
var result = new StaticOptions
{
WorkDoneProgress = source.WorkDoneProgress,
Legend = source.Legend,
Full = source.Full,
Range = source.Range
};
if (result.Full != null && result.Full?.Value?.Delta != true)
{
var edits = _handlersManager.Descriptors.Any(z => z.HandlerType == typeof(ISemanticTokensDeltaHandler));
if (edits)
{
result.Full = new BooleanOr<SemanticTokensCapabilityRequestFull>(
new SemanticTokensCapabilityRequestFull
{
Delta = true
}
);
}
}
return result;
}
}
}
}
namespace Client.Capabilities
{
/// <summary>
/// Capabilities specific to the `textDocument/semanticTokens`
///
/// @since 3.16.0
/// </summary>
[CapabilityKey(nameof(ClientCapabilities.TextDocument), nameof(TextDocumentClientCapabilities.SemanticTokens))]
public partial class SemanticTokensCapability : DynamicCapability
{
/// <summary>
/// Which requests the client supports and might send to the server.
/// </summary>
public SemanticTokensCapabilityRequests Requests { get; set; } = null!;
/// <summary>
/// The token types that the client supports.
/// </summary>
public Container<SemanticTokenType> TokenTypes { get; set; } = null!;
/// <summary>
/// The token modifiers that the client supports.
/// </summary>
public Container<SemanticTokenModifier> TokenModifiers { get; set; } = null!;
/// <summary>
/// The formats the clients supports.
/// </summary>
public Container<SemanticTokenFormat> Formats { get; set; } = null!;
/// <summary>
/// Whether the client supports tokens that can overlap each other.
/// </summary>
[Optional]
public bool OverlappingTokenSupport { get; set; }
/// <summary>
/// Whether the client supports tokens that can span multiple lines.
/// </summary>
[Optional]
public bool MultilineTokenSupport { get; set; }
/// <summary>
/// Whether the client allows the server to actively cancel a
/// semantic token request, e.g. supports returning
/// ErrorCodes.ServerCancelled. If a server does the client
/// needs to retrigger the request.
///
/// @since 3.17.0
/// </summary>
[Optional]
public bool ServerCancelSupport { get; set; }
/// <summary>
/// Whether the client uses semantic tokens to augment existing
/// syntax tokens. If set to `true` client side created syntax
/// tokens and semantic tokens are both used for colorization. If
/// set to `false` the client only uses the returned semantic tokens
/// for colorization.
///
/// If the value is `undefined` then the client behavior is not
/// specified.
///
/// @since 3.17.0
/// </summary>
[Optional]
public bool AugmentsSyntaxTokens { get; set; }
}
public partial class SemanticTokensCapabilityRequests
{
/// <summary>
/// The client will send the `textDocument/semanticTokens/range` request if
/// the server provides a corresponding handler.
/// </summary>
[Optional]
public Supports<SemanticTokensCapabilityRequestRange?> Range { get; set; }
/// <summary>
/// The client will send the `textDocument/semanticTokens/full` request if
/// the server provides a corresponding handler.
/// </summary>
[Optional]
public Supports<SemanticTokensCapabilityRequestFull?> Full { get; set; }
}
/// <summary>
/// The client will send the `textDocument/semanticTokens/range` request if
/// the server provides a corresponding handler.
/// </summary>
public partial class SemanticTokensCapabilityRequestRange
{
}
/// <summary>
/// The client will send the `textDocument/semanticTokens/full` request if
/// the server provides a corresponding handler.
/// </summary>
public partial class SemanticTokensCapabilityRequestFull
{
/// <summary>
/// The client will send the `textDocument/semanticTokens/full/delta` request if
/// the server provides a corresponding handler.
/// </summary>
[Optional]
public bool Delta { get; set; }
}
/// <summary>
/// Capabilities specific to the semantic token requests scoped to the
/// workspace.
///
/// @since 3.16.0.
/// </summary>
[CapabilityKey(nameof(ClientCapabilities.Workspace), nameof(WorkspaceClientCapabilities.SemanticTokens))]
public class SemanticTokensWorkspaceCapability : ICapability
{
/// <summary>
/// Whether the client implementation supports a refresh request sent from
/// the server to the client.
///
/// Note that this event is global and will force the client to refresh all
/// semantic tokens currently shown. It should be used with absolute care
/// and is useful for situation where a server for example detect a project
/// wide change that requires such a calculation.
/// </summary>
[Optional]
public bool RefreshSupport { get; set; }
}
}
namespace Document
{
public abstract class SemanticTokensHandlerBase : AbstractHandlers.Base<SemanticTokensRegistrationOptions, SemanticTokensCapability>,
ISemanticTokensFullHandler,
ISemanticTokensDeltaHandler,
ISemanticTokensRangeHandler
{
public virtual async Task<SemanticTokens?> Handle(SemanticTokensParams request, CancellationToken cancellationToken)
{
var document = await GetSemanticTokensDocument(request, cancellationToken).ConfigureAwait(false);
var builder = document.Create();
await Tokenize(builder, request, cancellationToken).ConfigureAwait(false);
return builder.Commit().GetSemanticTokens();
}
public virtual async Task<SemanticTokensFullOrDelta?> Handle(SemanticTokensDeltaParams request, CancellationToken cancellationToken)
{
var document = await GetSemanticTokensDocument(request, cancellationToken).ConfigureAwait(false);
var builder = document.Edit(request);
await Tokenize(builder, request, cancellationToken).ConfigureAwait(false);
return builder.Commit().GetSemanticTokensEdits();
}
public virtual async Task<SemanticTokens?> Handle(SemanticTokensRangeParams request, CancellationToken cancellationToken)
{
var document = await GetSemanticTokensDocument(request, cancellationToken).ConfigureAwait(false);
var builder = document.Create();
await Tokenize(builder, request, cancellationToken).ConfigureAwait(false);
return builder.Commit().GetSemanticTokens(request.Range);
}
protected abstract Task Tokenize(SemanticTokensBuilder builder, ITextDocumentIdentifierParams identifier, CancellationToken cancellationToken);
protected abstract Task<SemanticTokensDocument> GetSemanticTokensDocument(
ITextDocumentIdentifierParams @params, CancellationToken cancellationToken
);
}
public static partial class SemanticTokensExtensions
{
private static SemanticTokensRegistrationOptions RegistrationOptionsFactory(
SemanticTokensCapability capability, ClientCapabilities clientCapabilities
)
{
var registrationOptions = new SemanticTokensRegistrationOptions
{
Full = new SemanticTokensCapabilityRequestFull()
};
registrationOptions.Range ??= new SemanticTokensCapabilityRequestRange();
if (registrationOptions is { Full: { IsValue: true, Value: { } } })
{
registrationOptions.Full.Value.Delta = true;
}
// Ensure the legend is created properly.
registrationOptions.Legend = new SemanticTokensLegend
{
TokenModifiers = SemanticTokenModifier.Defaults.Join(capability.TokenModifiers, z => z, z => z, (a, _) => a).ToArray(),
TokenTypes = SemanticTokenType.Defaults.Join(capability.TokenTypes, z => z, z => z, (a, _) => a).ToArray(),
};
return registrationOptions;
}
public static ILanguageServerRegistry OnSemanticTokens(
this ILanguageServerRegistry registry,
Func<SemanticTokensBuilder, ITextDocumentIdentifierParams, SemanticTokensCapability, CancellationToken, Task> tokenize,
Func<ITextDocumentIdentifierParams, SemanticTokensCapability, CancellationToken, Task<SemanticTokensDocument>> getSemanticTokensDocument,
RegistrationOptionsDelegate<SemanticTokensRegistrationOptions, SemanticTokensCapability>? registrationOptionsFactory
)
{
registrationOptionsFactory ??= RegistrationOptionsFactory;
return registry.AddHandlers(
new DelegatingHandlerBase(
HandlerAdapter<SemanticTokensCapability, ITextDocumentIdentifierParams>.Adapt(tokenize),
HandlerAdapter<SemanticTokensCapability>.Adapt(getSemanticTokensDocument),
RegistrationAdapter<SemanticTokensCapability>.Adapt(registrationOptionsFactory)
)
);
}
public static ILanguageServerRegistry OnSemanticTokens(
this ILanguageServerRegistry registry,
Func<SemanticTokensBuilder, ITextDocumentIdentifierParams, CancellationToken, Task> tokenize,
Func<ITextDocumentIdentifierParams, CancellationToken, Task<SemanticTokensDocument>> getSemanticTokensDocument,
RegistrationOptionsDelegate<SemanticTokensRegistrationOptions, SemanticTokensCapability>? registrationOptionsFactory
)
{
registrationOptionsFactory ??= RegistrationOptionsFactory;
return registry.AddHandlers(
new DelegatingHandlerBase(
HandlerAdapter<SemanticTokensCapability, ITextDocumentIdentifierParams>.Adapt(tokenize),
HandlerAdapter<SemanticTokensCapability>.Adapt(getSemanticTokensDocument),
RegistrationAdapter<SemanticTokensCapability>.Adapt(registrationOptionsFactory)
)
);
}
public static ILanguageServerRegistry OnSemanticTokens(
this ILanguageServerRegistry registry,
Func<SemanticTokensBuilder, ITextDocumentIdentifierParams, Task> tokenize,
Func<ITextDocumentIdentifierParams, Task<SemanticTokensDocument>> getSemanticTokensDocument,
RegistrationOptionsDelegate<SemanticTokensRegistrationOptions, SemanticTokensCapability>? registrationOptionsFactory
)
{
registrationOptionsFactory ??= RegistrationOptionsFactory;
return registry.AddHandlers(
new DelegatingHandlerBase(
HandlerAdapter<SemanticTokensCapability, ITextDocumentIdentifierParams>.Adapt(tokenize),
HandlerAdapter<SemanticTokensCapability>.Adapt(getSemanticTokensDocument),
RegistrationAdapter<SemanticTokensCapability>.Adapt(registrationOptionsFactory)
)
);
}
private class DelegatingHandlerBase : SemanticTokensHandlerBase
{
private readonly Func<SemanticTokensBuilder, ITextDocumentIdentifierParams, SemanticTokensCapability, CancellationToken, Task> _tokenize;
private readonly Func<ITextDocumentIdentifierParams, SemanticTokensCapability, CancellationToken, Task<SemanticTokensDocument>>
_getSemanticTokensDocument;
private readonly RegistrationOptionsDelegate<SemanticTokensRegistrationOptions, SemanticTokensCapability> _registrationOptionsFactory;
public DelegatingHandlerBase(
Func<SemanticTokensBuilder, ITextDocumentIdentifierParams, SemanticTokensCapability, CancellationToken, Task> tokenize,
Func<ITextDocumentIdentifierParams, SemanticTokensCapability, CancellationToken, Task<SemanticTokensDocument>> getSemanticTokensDocument,
RegistrationOptionsDelegate<SemanticTokensRegistrationOptions, SemanticTokensCapability> registrationOptionsFactory
)
{
_tokenize = tokenize;
_getSemanticTokensDocument = getSemanticTokensDocument;
_registrationOptionsFactory = registrationOptionsFactory;
}
protected override Task Tokenize(SemanticTokensBuilder builder, ITextDocumentIdentifierParams identifier, CancellationToken cancellationToken)
{
return _tokenize(builder, identifier, Capability, cancellationToken);
}
protected override Task<SemanticTokensDocument> GetSemanticTokensDocument(
ITextDocumentIdentifierParams @params, CancellationToken cancellationToken
)
{
return _getSemanticTokensDocument(@params, Capability, cancellationToken);
}
protected internal override SemanticTokensRegistrationOptions CreateRegistrationOptions(
SemanticTokensCapability capability, ClientCapabilities clientCapabilities
)
{
return _registrationOptionsFactory(capability, clientCapabilities);
}
}
public static IRequestProgressObservable<SemanticTokensPartialResult, SemanticTokens?> RequestSemanticTokens(
this ITextDocumentLanguageClient mediator,
SemanticTokensParams @params, CancellationToken cancellationToken = default
)
{
return mediator.ProgressManager.MonitorUntil(
@params,
(result, partial) => new SemanticTokens
{
Data = partial.Data,
ResultId = result?.ResultId
},
tokens => new SemanticTokensPartialResult(tokens),
cancellationToken
);
}
public static IRequestProgressObservable<SemanticTokensFullOrDeltaPartialResult, SemanticTokensFullOrDelta?> RequestSemanticTokensDelta(
this ITextDocumentLanguageClient mediator, SemanticTokensDeltaParams @params, CancellationToken cancellationToken = default
)
{
return mediator.ProgressManager.MonitorUntil(
@params, (result, partial) =>
{
if (partial?.IsDelta == true)
{
return new SemanticTokensFullOrDelta(
new SemanticTokensDelta
{
Edits = partial.Delta!.Edits,
ResultId = result?.Delta?.ResultId ?? result?.Full?.ResultId
}
);
}
if (partial?.IsFull == true)