@@ -17,28 +17,23 @@ class HandlerCollection : IHandlerCollection
1717 . GetTypeInfo ( )
1818 . GetMethod ( nameof ( GetRegistration ) , BindingFlags . NonPublic | BindingFlags . Static ) ;
1919 private readonly ISupportedCapabilities _supportedCapabilities ;
20- private readonly HashSet < HandlerDescriptor > _textDocumentIdentifiers = new HashSet < HandlerDescriptor > ( ) ;
20+ private readonly TextDocumentIdentifiers _textDocumentIdentifiers ;
2121 internal readonly HashSet < HandlerDescriptor > _handlers = new HashSet < HandlerDescriptor > ( ) ;
2222 private IServiceProvider _serviceProvider ;
2323
2424
25- public HandlerCollection ( ISupportedCapabilities supportedCapabilities )
25+ public HandlerCollection ( ISupportedCapabilities supportedCapabilities ,
26+ TextDocumentIdentifiers textDocumentIdentifiers )
2627 {
2728 _supportedCapabilities = supportedCapabilities ;
29+ _textDocumentIdentifiers = textDocumentIdentifiers ;
2830 }
2931
3032 public void SetServiceProvider ( IServiceProvider serviceProvider )
3133 {
3234 _serviceProvider = serviceProvider ;
3335 }
3436
35- public IEnumerable < ITextDocumentIdentifier > TextDocumentIdentifiers ( )
36- {
37- return _textDocumentIdentifiers
38- . Select ( descriptor => descriptor . Handler )
39- . OfType < ITextDocumentIdentifier > ( ) ;
40- }
41-
4237 public IEnumerator < ILspHandlerDescriptor > GetEnumerator ( )
4338 {
4439 return _handlers . GetEnumerator ( ) ;
@@ -53,74 +48,85 @@ public LspHandlerDescriptorDisposable Add(string method, IJsonRpcHandler handler
5348 {
5449 var descriptor = GetDescriptor ( method , handler . GetType ( ) , handler ) ;
5550 _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 ) ;
5757 }
5858
5959 public LspHandlerDescriptorDisposable Add ( string method , Func < IServiceProvider , IJsonRpcHandler > handlerFunc )
6060 {
6161 var handler = handlerFunc ( _serviceProvider ) ;
6262 var descriptor = GetDescriptor ( method , handler . GetType ( ) , handler ) ;
6363 _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 ) ;
6570 }
6671
6772 public LspHandlerDescriptorDisposable Add ( string method , Type handlerType )
6873 {
6974 var descriptor = GetDescriptor ( method , handlerType , _serviceProvider ) ;
7075 _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 ) ;
7282 }
7383
7484 public LspHandlerDescriptorDisposable Add ( params Type [ ] handlerTypes )
7585 {
7686 var descriptors = new HashSet < HandlerDescriptor > ( ) ;
87+ var cd = new CompositeDisposable ( ) ;
7788 foreach ( var handlerType in handlerTypes )
7889 {
7990 foreach ( var ( method , implementedInterface ) in handlerType . GetTypeInfo ( )
8091 . ImplementedInterfaces
8192 . Select ( x => ( method : LspHelper . GetMethodName ( x ) , implementedInterface : x ) )
8293 . Where ( x => ! string . IsNullOrWhiteSpace ( x . method ) ) )
8394 {
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+ }
94102 }
95103 }
96104
97- return new LspHandlerDescriptorDisposable ( descriptors ) ;
105+ return new LspHandlerDescriptorDisposable ( descriptors , cd ) ;
98106 }
99107
100108 public LspHandlerDescriptorDisposable Add ( params IJsonRpcHandler [ ] handlers )
101109 {
102110 var descriptors = new HashSet < HandlerDescriptor > ( ) ;
111+ var cd = new CompositeDisposable ( ) ;
103112 foreach ( var handler in handlers )
104113 {
105114 foreach ( var ( method , implementedInterface ) in handler . GetType ( ) . GetTypeInfo ( )
106115 . ImplementedInterfaces
107116 . Select ( x => ( method : LspHelper . GetMethodName ( x ) , implementedInterface : x ) )
108117 . Where ( x => ! string . IsNullOrWhiteSpace ( x . method ) ) )
109118 {
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+ }
120126 }
121127 }
122128
123- return new LspHandlerDescriptorDisposable ( descriptors ) ;
129+ return new LspHandlerDescriptorDisposable ( descriptors , cd ) ;
124130 }
125131
126132 private HandlerDescriptor GetDescriptor ( string method , Type handlerType , IServiceProvider serviceProvider )
@@ -153,8 +159,7 @@ private HandlerDescriptor GetDescriptor(string method, Type handlerType, IJsonRp
153159
154160 if ( _supportedCapabilities . AllowsDynamicRegistration ( capabilityType ) )
155161 {
156- registration = new Registration ( )
157- {
162+ registration = new Registration ( ) {
158163 Id = Guid . NewGuid ( ) . ToString ( ) ,
159164 Method = method ,
160165 RegisterOptions = registrationOptions
@@ -190,10 +195,8 @@ private HandlerDescriptor GetDescriptor(string method, Type handlerType, IJsonRp
190195 registrationType ,
191196 registration ,
192197 capabilityType ,
193- ( ) =>
194- {
198+ ( ) => {
195199 _handlers . RemoveWhere ( d => d . Handler == handler ) ;
196- _textDocumentIdentifiers . RemoveWhere ( d => d . Handler == handler ) ;
197200 } ) ;
198201
199202 return descriptor ;
0 commit comments