19
19
import org .eclipse .core .resources .IFile ;
20
20
import org .eclipse .core .resources .IProject ;
21
21
import org .eclipse .core .runtime .CoreException ;
22
+ import org .eclipse .core .runtime .IConfigurationElement ;
23
+ import org .eclipse .core .runtime .IExtensionDelta ;
24
+ import org .eclipse .core .runtime .IExtensionRegistry ;
25
+ import org .eclipse .core .runtime .IRegistryChangeEvent ;
26
+ import org .eclipse .core .runtime .IRegistryChangeListener ;
22
27
import org .eclipse .core .runtime .Platform ;
23
28
import org .eclipse .core .runtime .content .IContentType ;
24
29
import org .eclipse .core .runtime .content .IContentTypeManager .ContentTypeChangeEvent ;
25
30
import org .eclipse .core .runtime .content .IContentTypeManager .IContentTypeChangeListener ;
26
31
27
32
import ts .client .ScriptKindName ;
33
+ import ts .eclipse .ide .core .TypeScriptCorePlugin ;
28
34
import ts .eclipse .ide .core .preferences .TypeScriptCorePreferenceConstants ;
29
35
import ts .eclipse .ide .core .resources .IIDETypeScriptProject ;
30
36
import ts .eclipse .ide .core .resources .ITypeScriptElementChangedListener ;
37
+ import ts .eclipse .ide .core .resources .ITypeScriptResourceParticipant ;
31
38
import ts .eclipse .ide .core .resources .UseSalsa ;
32
39
import ts .eclipse .ide .core .resources .buildpath .ITypeScriptBuildPath ;
33
40
import ts .eclipse .ide .core .utils .PreferencesHelper ;
34
41
import ts .eclipse .ide .internal .core .Trace ;
35
42
import ts .resources .ITypeScriptResourcesManagerDelegate ;
36
43
import ts .utils .FileUtils ;
37
44
38
- public class IDEResourcesManager implements ITypeScriptResourcesManagerDelegate {
45
+ public class IDEResourcesManager implements ITypeScriptResourcesManagerDelegate , IRegistryChangeListener {
39
46
40
47
private static IDEResourcesManager instance = new IDEResourcesManager ();
41
-
42
48
private final List <ITypeScriptElementChangedListener > listeners ;
43
49
44
50
private boolean useJsAsJsx ;
45
51
52
+ private static final String EXTENSION_TYPESCRIPT_RESOURCE_PARTICIPANTS = "typeScriptResourceParticipants" ;
53
+ private static final String CLASS_ATTR = "class" ;
54
+ private boolean registryListenerIntialized ;
55
+ private boolean extensionResourceParticipantsLoaded ;
56
+ private List <ITypeScriptResourceParticipant > resourceParticipants ;
57
+
46
58
/**
47
59
* Contents Types IDS
48
60
*/
@@ -52,6 +64,8 @@ public class IDEResourcesManager implements ITypeScriptResourcesManagerDelegate
52
64
private static final String JSX_CONTENT_TYPE_ID = "ts.eclipse.ide.core.jsxSource" ;
53
65
54
66
private IDEResourcesManager () {
67
+ this .registryListenerIntialized = false ;
68
+ this .resourceParticipants = new ArrayList <>();
55
69
this .listeners = new ArrayList <ITypeScriptElementChangedListener >();
56
70
updateUseJsAsJsx (Platform .getContentTypeManager ().getContentType (JSX_CONTENT_TYPE_ID ));
57
71
@@ -300,7 +314,17 @@ public boolean canConsumeTsserver(IProject project, Object fileObject) {
300
314
if (isJsFile (fileObject )) {
301
315
return hasSalsaNature (project );
302
316
}
303
- return (isTsOrTsxOrJsxFile (fileObject ));
317
+ if (isTsOrTsxOrJsxFile (fileObject )) {
318
+ return true ;
319
+ }
320
+ // Use extension point
321
+ loadExtensionResourceParticipants ();
322
+ for (ITypeScriptResourceParticipant participant : resourceParticipants ) {
323
+ if (participant .canConsumeTsserver (project , fileObject )) {
324
+ return true ;
325
+ }
326
+ }
327
+ return false ;
304
328
}
305
329
306
330
@ Override
@@ -354,4 +378,93 @@ public void removeTypeScriptElementChangedListener(ITypeScriptElementChangedList
354
378
}
355
379
}
356
380
381
+ private synchronized void loadExtensionResourceParticipants () {
382
+ if (extensionResourceParticipantsLoaded )
383
+ return ;
384
+ // Immediately set the flag, as to ensure that this method is never
385
+ // called twice
386
+ extensionResourceParticipantsLoaded = true ;
387
+
388
+ Trace .trace (Trace .EXTENSION_POINT , "->- Loading .typeScriptResourceParticipants extension point ->-" );
389
+
390
+ IExtensionRegistry registry = Platform .getExtensionRegistry ();
391
+ IConfigurationElement [] cf = registry .getConfigurationElementsFor (TypeScriptCorePlugin .PLUGIN_ID ,
392
+ EXTENSION_TYPESCRIPT_RESOURCE_PARTICIPANTS );
393
+
394
+ addExtensionResourceParticipants (cf );
395
+ addRegistryListenerIfNeeded ();
396
+
397
+ Trace .trace (Trace .EXTENSION_POINT , "-<- Done loading .typeScriptResourceParticipants extension point -<-" );
398
+ }
399
+
400
+ @ Override
401
+ public void registryChanged (final IRegistryChangeEvent event ) {
402
+ IExtensionDelta [] deltas = event .getExtensionDeltas (TypeScriptCorePlugin .PLUGIN_ID ,
403
+ EXTENSION_TYPESCRIPT_RESOURCE_PARTICIPANTS );
404
+ if (deltas != null ) {
405
+ synchronized (this ) {
406
+ for (IExtensionDelta delta : deltas ) {
407
+ IConfigurationElement [] cf = delta .getExtension ().getConfigurationElements ();
408
+ if (delta .getKind () == IExtensionDelta .ADDED ) {
409
+ addExtensionResourceParticipants (cf );
410
+ } else {
411
+ removeExtensionResourceParticipants (cf );
412
+ }
413
+ }
414
+ }
415
+ }
416
+ }
417
+
418
+ private void addExtensionResourceParticipants (IConfigurationElement [] cf ) {
419
+ for (IConfigurationElement ce : cf ) {
420
+ try {
421
+ String className = ce .getAttribute (CLASS_ATTR );
422
+ ITypeScriptResourceParticipant participant = (ITypeScriptResourceParticipant ) ce
423
+ .createExecutableExtension (CLASS_ATTR );
424
+ synchronized (resourceParticipants ) {
425
+ resourceParticipants .add (participant );
426
+ }
427
+ Trace .trace (Trace .EXTENSION_POINT , " Loaded typeScriptResourceParticipants: " + className );
428
+ } catch (Throwable t ) {
429
+ Trace .trace (Trace .SEVERE , " Error while loading typeScriptResourceParticipants" , t );
430
+ }
431
+ }
432
+ }
433
+
434
+ private void removeExtensionResourceParticipants (IConfigurationElement [] cf ) {
435
+ for (IConfigurationElement ce : cf ) {
436
+ try {
437
+ String className = ce .getAttribute (CLASS_ATTR );
438
+ synchronized (resourceParticipants ) {
439
+ for (ITypeScriptResourceParticipant participant : resourceParticipants ) {
440
+ if (className .equals (participant .getClass ().getName ())) {
441
+ resourceParticipants .remove (participant );
442
+ Trace .trace (Trace .EXTENSION_POINT , "Unloaded typeScriptResourceParticipants: " + className );
443
+
444
+ break ;
445
+ }
446
+ }
447
+ }
448
+ } catch (Throwable t ) {
449
+ Trace .trace (Trace .SEVERE , "Error while unloading typeScriptResourceParticipants" , t );
450
+ }
451
+ }
452
+ }
453
+
454
+ private void addRegistryListenerIfNeeded () {
455
+ if (registryListenerIntialized )
456
+ return ;
457
+
458
+ IExtensionRegistry registry = Platform .getExtensionRegistry ();
459
+ registry .addRegistryChangeListener (this , TypeScriptCorePlugin .PLUGIN_ID );
460
+ registryListenerIntialized = true ;
461
+ }
462
+
463
+ public void initialize () {
464
+
465
+ }
466
+
467
+ public void destroy () {
468
+ Platform .getExtensionRegistry ().removeRegistryChangeListener (this );
469
+ }
357
470
}
0 commit comments