@@ -17,28 +17,23 @@ class HandlerCollection : IHandlerCollection
17
17
. GetTypeInfo ( )
18
18
. GetMethod ( nameof ( GetRegistration ) , BindingFlags . NonPublic | BindingFlags . Static ) ;
19
19
private readonly ISupportedCapabilities _supportedCapabilities ;
20
- private readonly HashSet < HandlerDescriptor > _textDocumentIdentifiers = new HashSet < HandlerDescriptor > ( ) ;
20
+ private readonly TextDocumentIdentifiers _textDocumentIdentifiers ;
21
21
internal readonly HashSet < HandlerDescriptor > _handlers = new HashSet < HandlerDescriptor > ( ) ;
22
22
private IServiceProvider _serviceProvider ;
23
23
24
24
25
- public HandlerCollection ( ISupportedCapabilities supportedCapabilities )
25
+ public HandlerCollection ( ISupportedCapabilities supportedCapabilities ,
26
+ TextDocumentIdentifiers textDocumentIdentifiers )
26
27
{
27
28
_supportedCapabilities = supportedCapabilities ;
29
+ _textDocumentIdentifiers = textDocumentIdentifiers ;
28
30
}
29
31
30
32
public void SetServiceProvider ( IServiceProvider serviceProvider )
31
33
{
32
34
_serviceProvider = serviceProvider ;
33
35
}
34
36
35
- public IEnumerable < ITextDocumentIdentifier > TextDocumentIdentifiers ( )
36
- {
37
- return _textDocumentIdentifiers
38
- . Select ( descriptor => descriptor . Handler )
39
- . OfType < ITextDocumentIdentifier > ( ) ;
40
- }
41
-
42
37
public IEnumerator < ILspHandlerDescriptor > GetEnumerator ( )
43
38
{
44
39
return _handlers . GetEnumerator ( ) ;
@@ -53,74 +48,85 @@ public LspHandlerDescriptorDisposable Add(string method, IJsonRpcHandler handler
53
48
{
54
49
var descriptor = GetDescriptor ( method , handler . GetType ( ) , handler ) ;
55
50
_handlers . Add ( descriptor ) ;
56
- return new LspHandlerDescriptorDisposable ( descriptor ) ;
51
+ var cd = new CompositeDisposable ( ) ;
52
+ if ( descriptor . Handler is ITextDocumentIdentifier textDocumentIdentifier )
53
+ {
54
+ cd . Add ( _textDocumentIdentifiers . Add ( textDocumentIdentifier ) ) ;
55
+ }
56
+ return new LspHandlerDescriptorDisposable ( new [ ] { descriptor } , cd ) ;
57
57
}
58
58
59
59
public LspHandlerDescriptorDisposable Add ( string method , Func < IServiceProvider , IJsonRpcHandler > handlerFunc )
60
60
{
61
61
var handler = handlerFunc ( _serviceProvider ) ;
62
62
var descriptor = GetDescriptor ( method , handler . GetType ( ) , handler ) ;
63
63
_handlers . Add ( descriptor ) ;
64
- return new LspHandlerDescriptorDisposable ( descriptor ) ;
64
+ var cd = new CompositeDisposable ( ) ;
65
+ if ( descriptor . Handler is ITextDocumentIdentifier textDocumentIdentifier )
66
+ {
67
+ _textDocumentIdentifiers . Add ( textDocumentIdentifier ) ;
68
+ }
69
+ return new LspHandlerDescriptorDisposable ( new [ ] { descriptor } , cd ) ;
65
70
}
66
71
67
72
public LspHandlerDescriptorDisposable Add ( string method , Type handlerType )
68
73
{
69
74
var descriptor = GetDescriptor ( method , handlerType , _serviceProvider ) ;
70
75
_handlers . Add ( descriptor ) ;
71
- return new LspHandlerDescriptorDisposable ( descriptor ) ;
76
+ var cd = new CompositeDisposable ( ) ;
77
+ if ( descriptor . Handler is ITextDocumentIdentifier textDocumentIdentifier )
78
+ {
79
+ cd . Add ( _textDocumentIdentifiers . Add ( textDocumentIdentifier ) ) ;
80
+ }
81
+ return new LspHandlerDescriptorDisposable ( new [ ] { descriptor } , cd ) ;
72
82
}
73
83
74
84
public LspHandlerDescriptorDisposable Add ( params Type [ ] handlerTypes )
75
85
{
76
86
var descriptors = new HashSet < HandlerDescriptor > ( ) ;
87
+ var cd = new CompositeDisposable ( ) ;
77
88
foreach ( var handlerType in handlerTypes )
78
89
{
79
90
foreach ( var ( method , implementedInterface ) in handlerType . GetTypeInfo ( )
80
91
. ImplementedInterfaces
81
92
. Select ( x => ( method : LspHelper . GetMethodName ( x ) , implementedInterface : x ) )
82
93
. Where ( x => ! string . IsNullOrWhiteSpace ( x . method ) ) )
83
94
{
84
- descriptors . Add ( GetDescriptor ( method , implementedInterface , _serviceProvider ) ) ;
85
- }
86
- }
87
-
88
- foreach ( var descriptor in descriptors )
89
- {
90
- _handlers . Add ( descriptor ) ;
91
- if ( typeof ( ITextDocumentIdentifier ) . IsAssignableFrom ( descriptor . ImplementationType ) )
92
- {
93
- _textDocumentIdentifiers . Add ( descriptor ) ;
95
+ var descriptor = GetDescriptor ( method , implementedInterface , _serviceProvider ) ;
96
+ descriptors . Add ( descriptor ) ;
97
+ _handlers . Add ( descriptor ) ;
98
+ if ( descriptor . Handler is ITextDocumentIdentifier textDocumentIdentifier )
99
+ {
100
+ cd . Add ( _textDocumentIdentifiers . Add ( textDocumentIdentifier ) ) ;
101
+ }
94
102
}
95
103
}
96
104
97
- return new LspHandlerDescriptorDisposable ( descriptors ) ;
105
+ return new LspHandlerDescriptorDisposable ( descriptors , cd ) ;
98
106
}
99
107
100
108
public LspHandlerDescriptorDisposable Add ( params IJsonRpcHandler [ ] handlers )
101
109
{
102
110
var descriptors = new HashSet < HandlerDescriptor > ( ) ;
111
+ var cd = new CompositeDisposable ( ) ;
103
112
foreach ( var handler in handlers )
104
113
{
105
114
foreach ( var ( method , implementedInterface ) in handler . GetType ( ) . GetTypeInfo ( )
106
115
. ImplementedInterfaces
107
116
. Select ( x => ( method : LspHelper . GetMethodName ( x ) , implementedInterface : x ) )
108
117
. Where ( x => ! string . IsNullOrWhiteSpace ( x . method ) ) )
109
118
{
110
- descriptors . Add ( GetDescriptor ( method , implementedInterface , handler ) ) ;
111
- }
112
- }
113
-
114
- foreach ( var descriptor in descriptors )
115
- {
116
- _handlers . Add ( descriptor ) ;
117
- if ( descriptor . Handler is ITextDocumentIdentifier )
118
- {
119
- _textDocumentIdentifiers . Add ( descriptor ) ;
119
+ var descriptor = GetDescriptor ( method , implementedInterface , handler ) ;
120
+ descriptors . Add ( descriptor ) ;
121
+ _handlers . Add ( descriptor ) ;
122
+ if ( descriptor . Handler is ITextDocumentIdentifier textDocumentIdentifier )
123
+ {
124
+ cd . Add ( _textDocumentIdentifiers . Add ( textDocumentIdentifier ) ) ;
125
+ }
120
126
}
121
127
}
122
128
123
- return new LspHandlerDescriptorDisposable ( descriptors ) ;
129
+ return new LspHandlerDescriptorDisposable ( descriptors , cd ) ;
124
130
}
125
131
126
132
private HandlerDescriptor GetDescriptor ( string method , Type handlerType , IServiceProvider serviceProvider )
@@ -153,8 +159,7 @@ private HandlerDescriptor GetDescriptor(string method, Type handlerType, IJsonRp
153
159
154
160
if ( _supportedCapabilities . AllowsDynamicRegistration ( capabilityType ) )
155
161
{
156
- registration = new Registration ( )
157
- {
162
+ registration = new Registration ( ) {
158
163
Id = Guid . NewGuid ( ) . ToString ( ) ,
159
164
Method = method ,
160
165
RegisterOptions = registrationOptions
@@ -190,10 +195,8 @@ private HandlerDescriptor GetDescriptor(string method, Type handlerType, IJsonRp
190
195
registrationType ,
191
196
registration ,
192
197
capabilityType ,
193
- ( ) =>
194
- {
198
+ ( ) => {
195
199
_handlers . RemoveWhere ( d => d . Handler == handler ) ;
196
- _textDocumentIdentifiers . RemoveWhere ( d => d . Handler == handler ) ;
197
200
} ) ;
198
201
199
202
return descriptor ;
0 commit comments