/
XMLExtensionsRegistry.java
556 lines (469 loc) · 18.3 KB
/
XMLExtensionsRegistry.java
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
/**
* Copyright (c) 2018, 2023 Angelo ZERR
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v20.html
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Angelo Zerr <angelo.zerr@gmail.com> - initial API and implementation
*/
package org.eclipse.lemminx.services.extensions;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.ServiceConfigurationError;
import java.util.ServiceLoader;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.eclipse.lemminx.services.IXMLDocumentProvider;
import org.eclipse.lemminx.services.IXMLNotificationService;
import org.eclipse.lemminx.services.IXMLValidationService;
import org.eclipse.lemminx.services.extensions.codeaction.ICodeActionParticipant;
import org.eclipse.lemminx.services.extensions.codelens.ICodeLensParticipant;
import org.eclipse.lemminx.services.extensions.commands.IXMLCommandService;
import org.eclipse.lemminx.services.extensions.completion.ICompletionParticipant;
import org.eclipse.lemminx.services.extensions.diagnostics.IDiagnosticsParticipant;
import org.eclipse.lemminx.services.extensions.format.IFormatterParticipant;
import org.eclipse.lemminx.services.extensions.hover.IHoverParticipant;
import org.eclipse.lemminx.services.extensions.rename.IRenameParticipant;
import org.eclipse.lemminx.services.extensions.save.ISaveContext;
import org.eclipse.lemminx.services.extensions.save.ISaveContext.SaveContextType;
import org.eclipse.lemminx.telemetry.TelemetryManager;
import org.eclipse.lemminx.uriresolver.URIResolverExtensionManager;
import org.eclipse.lsp4j.InitializeParams;
/**
* XML extensions registry.
*
*/
public class XMLExtensionsRegistry implements IComponentProvider {
private static final Logger LOGGER = Logger.getLogger(XMLExtensionsRegistry.class.getName());
private final URIResolverExtensionManager resolverExtensionManager;
private final Collection<IXMLExtension> extensions;
private final List<ICompletionParticipant> completionParticipants;
private final List<IHoverParticipant> hoverParticipants;
private final List<IDiagnosticsParticipant> diagnosticsParticipants;
private final List<ICodeActionParticipant> codeActionsParticipants;
private final List<IDocumentLinkParticipant> documentLinkParticipants;
private final List<IDocumentColorParticipant> documentColorParticipants;
private final List<IDefinitionParticipant> definitionParticipants;
private final List<ITypeDefinitionParticipant> typeDefinitionParticipants;
private final List<IReferenceParticipant> referenceParticipants;
private final List<ICodeLensParticipant> codeLensParticipants;
private final List<IHighlightingParticipant> highlightingParticipants;
private final List<IRenameParticipant> renameParticipants;
private final List<ILinkedEditingRangesParticipant> linkedEditingRangesParticipants;
private final List<IFormatterParticipant> formatterParticipants;
private final List<ISymbolsProviderParticipant> symbolsProviderParticipants;
private final List<IWorkspaceServiceParticipant> workspaceServiceParticipants;
private final List<IDocumentLifecycleParticipant> documentLifecycleParticipants;
private IXMLDocumentProvider documentProvider;
private IXMLValidationService validationService;
private IXMLCommandService commandService;
private InitializeParams params;
private ISaveContext initialSaveContext;
private boolean initialized;
private IXMLNotificationService notificationService;
private final Map<Class<?>, Object> components;
private TelemetryManager telemetryManager;
public XMLExtensionsRegistry() {
extensions = new ArrayList<>();
completionParticipants = new ArrayList<>();
hoverParticipants = new ArrayList<>();
diagnosticsParticipants = new ArrayList<>();
codeActionsParticipants = new ArrayList<>();
documentLinkParticipants = new ArrayList<>();
documentColorParticipants = new ArrayList<>();
definitionParticipants = new ArrayList<>();
typeDefinitionParticipants = new ArrayList<>();
referenceParticipants = new ArrayList<>();
codeLensParticipants = new ArrayList<>();
highlightingParticipants = new ArrayList<>();
renameParticipants = new ArrayList<>();
linkedEditingRangesParticipants = new ArrayList<>();
formatterParticipants = new ArrayList<>();
symbolsProviderParticipants = new ArrayList<>();
workspaceServiceParticipants = new ArrayList<>();
documentLifecycleParticipants = new ArrayList<>();
resolverExtensionManager = new URIResolverExtensionManager();
components = new HashMap<>();
telemetryManager = new TelemetryManager(null);
registerComponent(resolverExtensionManager);
}
public void registerComponent(Object component) {
this.components.put(component.getClass(), component);
}
@Override
public <T> T getComponent(Class clazz) {
return (T) components.get(clazz);
}
public void initializeParams(InitializeParams params) {
if (initialized) {
extensions.stream().forEach(extension -> {
try {
extension.start(params, this);
} catch (Exception e) {
LOGGER.log(Level.SEVERE, "Error while starting extension <" + extension.getClass().getName() + ">",
e);
}
});
} else {
this.params = params;
}
}
public void doSave(ISaveContext saveContext) {
if (initialized) {
extensions.stream().forEach(extension -> extension.doSave(saveContext));
} else if (this.initialSaveContext == null
|| (saveContext != null && saveContext.getType() == SaveContextType.SETTINGS)) {
// capture initial configuration iff:
// 1. the saveContext is for configuration, not document save
// 2. we haven't captured settings before
this.initialSaveContext = saveContext;
}
}
public Collection<IXMLExtension> getExtensions() {
initializeIfNeeded();
return extensions;
}
public Collection<ICompletionParticipant> getCompletionParticipants() {
initializeIfNeeded();
return completionParticipants;
}
public Collection<IHoverParticipant> getHoverParticipants() {
initializeIfNeeded();
return hoverParticipants;
}
public Collection<IDiagnosticsParticipant> getDiagnosticsParticipants() {
initializeIfNeeded();
return diagnosticsParticipants;
}
public List<ICodeActionParticipant> getCodeActionsParticipants() {
initializeIfNeeded();
return codeActionsParticipants;
}
public Collection<IDocumentLinkParticipant> getDocumentLinkParticipants() {
initializeIfNeeded();
return documentLinkParticipants;
}
public Collection<IDocumentColorParticipant> getDocumentColorParticipants() {
initializeIfNeeded();
return documentColorParticipants;
}
public Collection<IDefinitionParticipant> getDefinitionParticipants() {
initializeIfNeeded();
return definitionParticipants;
}
public Collection<ITypeDefinitionParticipant> getTypeDefinitionParticipants() {
initializeIfNeeded();
return typeDefinitionParticipants;
}
public Collection<IReferenceParticipant> getReferenceParticipants() {
initializeIfNeeded();
return referenceParticipants;
}
public Collection<ICodeLensParticipant> getCodeLensParticipants() {
initializeIfNeeded();
return codeLensParticipants;
}
public Collection<IHighlightingParticipant> getHighlightingParticipants() {
initializeIfNeeded();
return highlightingParticipants;
}
public Collection<IRenameParticipant> getRenameParticipants() {
initializeIfNeeded();
return renameParticipants;
}
public List<ILinkedEditingRangesParticipant> getLinkedEditingRangesParticipants() {
initializeIfNeeded();
return linkedEditingRangesParticipants;
}
public Collection<IFormatterParticipant> getFormatterParticipants() {
initializeIfNeeded();
return formatterParticipants;
}
public Collection<ISymbolsProviderParticipant> getSymbolsProviderParticipants() {
initializeIfNeeded();
return symbolsProviderParticipants;
}
/**
* Return the registered workspace service participants.
*
* @return the registered workspace service participants.
* @since 0.14.2
*/
public Collection<IWorkspaceServiceParticipant> getWorkspaceServiceParticipants() {
initializeIfNeeded();
return workspaceServiceParticipants;
}
/**
* Return the registered document lifecycle participants.
*
* @return the registered document lifecycle participants.
* @since 0.18.0
*/
public List<IDocumentLifecycleParticipant> getDocumentLifecycleParticipants() {
initializeIfNeeded();
return documentLifecycleParticipants;
}
public void initializeIfNeeded() {
if (initialized) {
return;
}
initialize();
}
private synchronized void initialize() {
if (initialized) {
return;
}
if (commandService != null) {
commandService.beginCommandsRegistration();
}
Iterator<IXMLExtension> extensions = ServiceLoader.load(IXMLExtension.class).iterator();
while (extensions.hasNext()) {
try {
registerExtension(extensions.next());
} catch (ServiceConfigurationError e) {
LOGGER.log(Level.SEVERE, "Error while instantiating extension", e);
}
}
initialized = true;
if (commandService != null) {
commandService.endCommandsRegistration();
}
}
void registerExtension(IXMLExtension extension) {
try {
extensions.add(extension);
extension.start(params, this);
if (initialSaveContext != null) {
extension.doSave(initialSaveContext);
}
} catch (Exception e) {
LOGGER.log(Level.SEVERE, "Error while initializing extension <" + extension.getClass().getName() + ">", e);
}
}
void unregisterExtension(IXMLExtension extension) {
try {
extensions.remove(extension);
extension.stop(this);
} catch (Exception e) {
LOGGER.log(Level.SEVERE, "Error while stopping extension <" + extension.getClass().getName() + ">", e);
}
}
/**
* Unregisters all registered extensions.
*/
public void dispose() {
// Copy the list of extensions to avoid ConcurrentModificationError
List<IXMLExtension> extensionReferences = new ArrayList<>();
extensions.forEach(extensionReferences::add);
extensionReferences.forEach(this::unregisterExtension);
}
public void registerCompletionParticipant(ICompletionParticipant completionParticipant) {
completionParticipants.add(completionParticipant);
}
public void unregisterCompletionParticipant(ICompletionParticipant completionParticipant) {
completionParticipants.remove(completionParticipant);
}
public void registerHoverParticipant(IHoverParticipant hoverParticipant) {
hoverParticipants.add(hoverParticipant);
}
public void unregisterHoverParticipant(IHoverParticipant hoverParticipant) {
hoverParticipants.remove(hoverParticipant);
}
public void registerDiagnosticsParticipant(IDiagnosticsParticipant diagnosticsParticipant) {
diagnosticsParticipants.add(diagnosticsParticipant);
}
public void unregisterDiagnosticsParticipant(IDiagnosticsParticipant diagnosticsParticipant) {
diagnosticsParticipants.remove(diagnosticsParticipant);
}
public void registerCodeActionParticipant(ICodeActionParticipant codeActionsParticipant) {
codeActionsParticipants.add(codeActionsParticipant);
}
public void unregisterCodeActionParticipant(ICodeActionParticipant codeActionsParticipant) {
codeActionsParticipants.remove(codeActionsParticipant);
}
public void registerDocumentLinkParticipant(IDocumentLinkParticipant documentLinkParticipant) {
documentLinkParticipants.add(documentLinkParticipant);
}
public void unregisterDocumentLinkParticipant(IDocumentLinkParticipant documentLinkParticipant) {
documentLinkParticipants.remove(documentLinkParticipant);
}
public void registerDocumentColorParticipant(IDocumentColorParticipant documentColorParticipant) {
documentColorParticipants.add(documentColorParticipant);
}
public void unregisterDocumentColorParticipant(IDocumentColorParticipant documentColorParticipant) {
documentColorParticipants.remove(documentColorParticipant);
}
public void registerDefinitionParticipant(IDefinitionParticipant definitionParticipant) {
definitionParticipants.add(definitionParticipant);
}
public void unregisterDefinitionParticipant(IDefinitionParticipant definitionParticipant) {
definitionParticipants.remove(definitionParticipant);
}
public void registerTypeDefinitionParticipant(ITypeDefinitionParticipant typeDefinitionParticipant) {
typeDefinitionParticipants.add(typeDefinitionParticipant);
}
public void unregisterTypeDefinitionParticipant(ITypeDefinitionParticipant typeDefinitionParticipant) {
typeDefinitionParticipants.remove(typeDefinitionParticipant);
}
public void registerReferenceParticipant(IReferenceParticipant referenceParticipant) {
referenceParticipants.add(referenceParticipant);
}
public void unregisterReferenceParticipant(IReferenceParticipant referenceParticipant) {
referenceParticipants.remove(referenceParticipant);
}
public void registerCodeLensParticipant(ICodeLensParticipant codeLensParticipant) {
codeLensParticipants.add(codeLensParticipant);
}
public void unregisterCodeLensParticipant(ICodeLensParticipant codeLensParticipant) {
codeLensParticipants.remove(codeLensParticipant);
}
public void registerHighlightingParticipant(IHighlightingParticipant highlightingParticipant) {
highlightingParticipants.add(highlightingParticipant);
}
public void unregisterHighlightingParticipant(IHighlightingParticipant highlightingParticipant) {
highlightingParticipants.remove(highlightingParticipant);
}
public void registerRenameParticipant(IRenameParticipant renameParticipant) {
renameParticipants.add(renameParticipant);
}
public void unregisterRenameParticipant(IRenameParticipant renameParticipant) {
renameParticipants.remove(renameParticipant);
}
public void registerLinkedEditingRangesParticipants(
ILinkedEditingRangesParticipant linkedEditingRangesParticipant) {
linkedEditingRangesParticipants.add(linkedEditingRangesParticipant);
}
public void unregisterLinkedEditingRangesParticipants(
ILinkedEditingRangesParticipant linkedEditingRangesParticipant) {
linkedEditingRangesParticipants.remove(linkedEditingRangesParticipant);
}
public void registerFormatterParticipant(IFormatterParticipant formatterParticipant) {
formatterParticipants.add(formatterParticipant);
}
public void unregisterFormatterParticipant(IFormatterParticipant formatterParticipant) {
formatterParticipants.remove(formatterParticipant);
}
public void registerSymbolsProviderParticipant(ISymbolsProviderParticipant symbolsProviderParticipant) {
symbolsProviderParticipants.add(symbolsProviderParticipant);
}
public void unregisterSymbolsProviderParticipant(ISymbolsProviderParticipant symbolsProviderParticipant) {
symbolsProviderParticipants.remove(symbolsProviderParticipant);
}
/**
* Register a new workspace service participant
*
* @param workspaceServiceParticipant the participant to register
* @since 0.14.2
*/
public void registerWorkspaceServiceParticipant(IWorkspaceServiceParticipant workspaceServiceParticipant) {
workspaceServiceParticipants.add(workspaceServiceParticipant);
}
/**
* Unregister a new workspace service participant.
*
* @param workspaceServiceParticipant the participant to unregister
* @since 0.14.2
*/
public void unregisterWorkspaceServiceParticipant(IWorkspaceServiceParticipant workspaceServiceParticipant) {
workspaceServiceParticipants.remove(workspaceServiceParticipant);
}
/**
* Register a new document lifecycle participant
*
* @param documentLifecycleParticipant the participant to register
* @since 0.18.0
*/
public void registerDocumentLifecycleParticipant(IDocumentLifecycleParticipant documentLifecycleParticipant) {
documentLifecycleParticipants.add(documentLifecycleParticipant);
}
/**
* Unregister a new document lifecycle participant.
*
* @param documentLifecycleParticipant the participant to unregister
* @since 0.18.0
*/
public void unregisterDocumentLifecycleParticipant(IDocumentLifecycleParticipant documentLifecycleParticipant) {
documentLifecycleParticipants.remove(documentLifecycleParticipant);
}
/**
* Returns the XML Document provider and null otherwise.
*
* @return the XML Document provider and null otherwise.
*/
public IXMLDocumentProvider getDocumentProvider() {
return documentProvider;
}
/**
* Set the XML Document provider
*
* @param documentProvider XML Document provider
*/
public void setDocumentProvider(IXMLDocumentProvider documentProvider) {
this.documentProvider = documentProvider;
}
public URIResolverExtensionManager getResolverExtensionManager() {
return resolverExtensionManager;
}
/**
* Returns the notification service
*
* @return the notification service
*/
public IXMLNotificationService getNotificationService() {
return notificationService;
}
/**
* Sets the notification service
*
* @param notificationService the new notification service
*/
public void setNotificationService(IXMLNotificationService notificationService) {
this.notificationService = notificationService;
}
/**
* Returns the XML document validation service
*
* @return the validation service
*/
public IXMLValidationService getValidationService() {
return validationService;
}
/**
* Sets the XML document validation service
*
* @param validationService
*/
public void setValidationService(IXMLValidationService validationService) {
this.validationService = validationService;
}
/**
* Returns the LS command service
*
* @return the command service
*/
public IXMLCommandService getCommandService() {
return commandService;
}
/**
* Sets the LS command service
*
* @param commandService
*/
public void setCommandService(IXMLCommandService commandService) {
this.commandService = commandService;
}
public TelemetryManager getTelemetryManager() {
return telemetryManager;
}
public void setTelemetryManager(TelemetryManager telemetryManager) {
this.telemetryManager = telemetryManager;
}
}