-
Notifications
You must be signed in to change notification settings - Fork 316
/
XtextGeneratorTemplates.xtend
590 lines (511 loc) · 20.2 KB
/
XtextGeneratorTemplates.xtend
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
/*******************************************************************************
* Copyright (c) 2015, 20201 itemis AG (http://www.itemis.eu) and others.
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*******************************************************************************/
package org.eclipse.xtext.xtext.generator
import com.google.common.collect.Maps
import com.google.inject.Binder
import com.google.inject.Guice
import com.google.inject.Inject
import com.google.inject.Injector
import com.google.inject.Module
import com.google.inject.Provider
import com.google.inject.Singleton
import com.google.inject.name.Names
import java.util.Collections
import java.util.List
import java.util.Map
import java.util.Properties
import org.apache.log4j.Logger
import org.eclipse.emf.ecore.EPackage
import org.eclipse.emf.ecore.resource.Resource
import org.eclipse.xtend.lib.annotations.FinalFieldsConstructor
import org.eclipse.xtend2.lib.StringConcatenationClient
import org.eclipse.xtext.Constants
import org.eclipse.xtext.ISetup
import org.eclipse.xtext.XtextPackage
import org.eclipse.xtext.resource.impl.BinaryGrammarResourceFactoryImpl
import org.eclipse.xtext.service.SingletonBinding
import org.eclipse.xtext.util.Modules2
import org.eclipse.xtext.xtext.generator.model.FileAccessFactory
import org.eclipse.xtext.xtext.generator.model.GeneratedJavaFileAccess
import org.eclipse.xtext.xtext.generator.model.GuiceModuleAccess
import org.eclipse.xtext.xtext.generator.model.JavaFileAccess
import org.eclipse.xtext.xtext.generator.model.TypeReference
import org.eclipse.xtext.xtext.generator.model.annotations.SuppressWarningsAnnotation
import org.eclipse.xtext.xtext.generator.model.project.IXtextProjectConfig
import static extension org.eclipse.xtext.xtext.generator.model.TypeReference.*
import org.eclipse.xtext.xtext.generator.model.TextFileAccess
/**
* Templates for generating the common language infrastructure.
*/
@Singleton
class XtextGeneratorTemplates {
@Inject CodeConfig codeConfig
@Inject FileAccessFactory fileAccessFactory
@Inject extension XtextGeneratorNaming naming
def JavaFileAccess createRuntimeSetup(IXtextGeneratorLanguage langConfig) {
val it = langConfig.grammar
if (codeConfig.preferXtendStubs) {
return fileAccessFactory.createXtendFile(runtimeSetup,'''
/**
* Initialization support for running Xtext languages without Equinox extension registry.
*/
class «runtimeSetup.simpleName» extends «runtimeGenSetup» {
def static void doSetup() {
new «runtimeSetup.simpleName»().createInjectorAndDoEMFRegistration()
}
}
''')
} else {
return fileAccessFactory.createJavaFile(runtimeSetup,'''
/**
* Initialization support for running Xtext languages without Equinox extension registry.
*/
public class «runtimeSetup.simpleName» extends «runtimeGenSetup» {
public static void doSetup() {
new «runtimeSetup.simpleName»().createInjectorAndDoEMFRegistration();
}
}
''')
}
}
@Deprecated
private static def addBackwardsCompabibleImportsTo(IXtextGeneratorLanguage langConfig, GeneratedJavaFileAccess file) {
for (type : langConfig.runtimeGenSetup.imports) {
file.importType(type)
}
}
def JavaFileAccess createRuntimeGenSetup(IXtextGeneratorLanguage langConfig) {
val it = langConfig.grammar
val file = fileAccessFactory.createGeneratedJavaFile(runtimeGenSetup)
// The following imports are added for backwards-compatibility
langConfig.addBackwardsCompabibleImportsTo(file);
file.annotations += new SuppressWarningsAnnotation
file.content = '''
public class «runtimeGenSetup.simpleName» implements «ISetup» {
@Override
public «Injector» createInjectorAndDoEMFRegistration() {
«FOR usedGrammar : langConfig.grammar.usedGrammars»
«usedGrammar.runtimeSetup».doSetup();
«ENDFOR»
«IF langConfig.grammar.usedGrammars.isEmpty»
// register default ePackages
if (!«Resource.typeRef».Factory.Registry.INSTANCE.getExtensionToFactoryMap().containsKey("ecore"))
«Resource.typeRef».Factory.Registry.INSTANCE.getExtensionToFactoryMap().put(
"ecore", new «'org.eclipse.emf.ecore.xmi.impl.EcoreResourceFactoryImpl'.typeRef»());
if (!«Resource.typeRef».Factory.Registry.INSTANCE.getExtensionToFactoryMap().containsKey("xmi"))
«Resource.typeRef».Factory.Registry.INSTANCE.getExtensionToFactoryMap().put(
"xmi", new «'org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl'.typeRef»());
if (!«Resource.typeRef».Factory.Registry.INSTANCE.getExtensionToFactoryMap().containsKey("xtextbin"))
«Resource.typeRef».Factory.Registry.INSTANCE.getExtensionToFactoryMap().put(
"xtextbin", new «BinaryGrammarResourceFactoryImpl»());
if (!«EPackage.typeRef».Registry.INSTANCE.containsKey(«XtextPackage».eNS_URI))
«EPackage.typeRef».Registry.INSTANCE.put(«XtextPackage».eNS_URI, «XtextPackage».eINSTANCE);
«ENDIF»
«Injector» injector = createInjector();
register(injector);
return injector;
}
public «Injector» createInjector() {
return «Guice».createInjector(new «runtimeModule»());
}
public void register(«Injector» injector) {
«FOR reg : langConfig.runtimeGenSetup.registrations»
«reg»
«ENDFOR»
}
}
'''
return file
}
private def getBindMethodName(GuiceModuleAccess.Binding it) {
'''«IF !value.provider && value.statements.isEmpty
»bind«
ELSEIF value.statements.isEmpty
»provide«
ELSE
»configure«
ENDIF
»«key.name?.replace('.', '$') ?: key.type.simpleMethodName
»«IF value.expression !== null && !value.provider»ToInstance«ENDIF»'''
}
private def String getSimpleMethodName(TypeReference type) {
type.simpleNames.join('$') + type.typeArguments.join('$', '$', '', [simpleMethodName])
}
private def StringConcatenationClient createBindingMethod(GuiceModuleAccess.Binding it) '''
«IF !value.provider && value.statements.isEmpty»
// contributed by «contributedBy»
«IF key.singleton»@«SingletonBinding»«IF key.eagerSingleton»(eager=true)«ENDIF»«ENDIF»
public «IF value.expression === null»Class<? extends «key.type»>«ELSE»«key.type»«ENDIF» «bindMethodName»() {
return «IF value.expression !== null»«value.expression»«ELSEIF value.type !== null»«value.type».class«ELSE»null«ENDIF»;
}
«ELSEIF value.statements.isEmpty»
// contributed by «contributedBy»
«IF key.singleton»@«SingletonBinding»«IF key.eagerSingleton»(eager=true)«ENDIF»«ENDIF»
public «IF value.expression===null»Class<? extends «Provider»<? extends «key.type»>>«ELSE»«Provider»<? extends «key.type»>«ENDIF» «bindMethodName»() {
return «IF value.expression!==null»«value.expression»«ELSE»«value.type».class«ENDIF»;
}
«ELSE»
// contributed by «contributedBy»
public void «bindMethodName»(«Binder» binder) {
«FOR statement : value.statements»
«statement»
«ENDFOR»
}
«ENDIF»
'''
def JavaFileAccess createRuntimeModule(IXtextGeneratorLanguage langConfig) {
val it = langConfig.grammar
if (codeConfig.preferXtendStubs) {
return fileAccessFactory.createXtendFile(runtimeModule,'''
/**
* Use this class to register components to be used at runtime / without the Equinox extension registry.
*/
class «runtimeModule.simpleName» extends «runtimeGenModule» {
}
''')
} else {
return fileAccessFactory.createJavaFile(runtimeModule,'''
/**
* Use this class to register components to be used at runtime / without the Equinox extension registry.
*/
public class «runtimeModule.simpleName» extends «runtimeGenModule» {
}
''')
}
}
def JavaFileAccess createRuntimeGenModule(IXtextGeneratorLanguage langConfig) {
val it = langConfig.grammar
val superClass = langConfig.runtimeGenModule.superClass ?: runtimeDefaultModule
val file = fileAccessFactory.createGeneratedJavaFile(runtimeGenModule)
file.importNestedTypeThreshold = JavaFileAccess.DONT_IMPORT_NESTED_TYPES
file.typeComment = '''
/**
* Manual modifications go to {@link «runtimeModule.simpleName»}.
*/
'''
file.annotations += new SuppressWarningsAnnotation
file.content = '''
public abstract class «runtimeGenModule.simpleName» extends «superClass» {
protected «Properties» properties = null;
@Override
public void configure(«Binder» binder) {
properties = tryBindProperties(binder, "«langConfig.grammar.name.replaceAll("\\.","/")».properties");
super.configure(binder);
}
public void configureLanguageName(«Binder» binder) {
binder.bind(String.class).annotatedWith(«Names».named(«Constants».LANGUAGE_NAME)).toInstance("«langConfig.grammar.name»");
}
public void configureFileExtensions(«Binder» binder) {
if (properties == null || properties.getProperty(Constants.FILE_EXTENSIONS) == null)
binder.bind(String.class).annotatedWith(«Names».named(«Constants».FILE_EXTENSIONS)).toInstance("«langConfig.fileExtensions.join(',')»");
}
«FOR binding : langConfig.runtimeGenModule.bindings»
«binding.createBindingMethod»
«ENDFOR»
}
'''
file.markedAsGenerated = true
return file
}
def JavaFileAccess createIdeModule(IXtextGeneratorLanguage langConfig) {
val it = langConfig.grammar
if (codeConfig.preferXtendStubs) {
return fileAccessFactory.createXtendFile(genericIdeModule,'''
/**
* Use this class to register ide components.
*/
class «genericIdeModule.simpleName» extends «genericIdeGenModule» {
}
''')
} else {
return fileAccessFactory.createJavaFile(genericIdeModule,'''
/**
* Use this class to register ide components.
*/
public class «genericIdeModule.simpleName» extends «genericIdeGenModule» {
}
''')
}
}
def JavaFileAccess createIdeGenModule(IXtextGeneratorLanguage langConfig) {
val it = langConfig.grammar
val superClass = langConfig.ideGenModule.superClass ?: genericIdeDefaultModule
val file = fileAccessFactory.createGeneratedJavaFile(genericIdeGenModule)
file.importNestedTypeThreshold = JavaFileAccess.DONT_IMPORT_NESTED_TYPES
file.typeComment = '''
/**
* Manual modifications go to {@link «genericIdeModule.simpleName»}.
*/
'''
file.annotations += new SuppressWarningsAnnotation
file.content = '''
public abstract class «genericIdeGenModule.simpleName» extends «superClass» {
«FOR binding : langConfig.ideGenModule.bindings»
«binding.createBindingMethod»
«ENDFOR»
}
'''
file.markedAsGenerated = true
return file
}
def JavaFileAccess createIdeSetup(IXtextGeneratorLanguage langConfig) {
val it = langConfig.grammar
if (codeConfig.preferXtendStubs) {
return fileAccessFactory.createXtendFile(genericIdeSetup,'''
/**
* Initialization support for running Xtext languages as language servers.
*/
class «genericIdeSetup.simpleName» extends «runtimeSetup» {
override createInjector() {
«Guice».createInjector(«Modules2».mixin(new «runtimeModule», new «genericIdeModule»))
}
}
''')
} else {
return fileAccessFactory.createJavaFile(genericIdeSetup,'''
/**
* Initialization support for running Xtext languages as language servers.
*/
public class «genericIdeSetup.simpleName» extends «runtimeSetup» {
@Override
public «Injector» createInjector() {
return «Guice».createInjector(«Modules2».mixin(new «runtimeModule»(), new «genericIdeModule»()));
}
}
''')
}
}
def JavaFileAccess createEclipsePluginModule(IXtextGeneratorLanguage langConfig) {
val it = langConfig.grammar
if (codeConfig.preferXtendStubs) {
return fileAccessFactory.createXtendFile(eclipsePluginModule,'''
/**
* Use this class to register components to be used within the Eclipse IDE.
*/
@«FinalFieldsConstructor»
class «eclipsePluginModule.simpleName» extends «eclipsePluginGenModule» {
}
''')
} else {
return fileAccessFactory.createJavaFile(eclipsePluginModule,'''
/**
* Use this class to register components to be used within the Eclipse IDE.
*/
public class «eclipsePluginModule.simpleName» extends «eclipsePluginGenModule» {
public «eclipsePluginModule.simpleName»(«'org.eclipse.ui.plugin.AbstractUIPlugin'.typeRef» plugin) {
super(plugin);
}
}
''')
}
}
def JavaFileAccess createEclipsePluginGenModule(IXtextGeneratorLanguage langConfig) {
val it = langConfig.grammar
val superClass = langConfig.eclipsePluginGenModule.superClass ?: eclipsePluginDefaultModule
val file = fileAccessFactory.createGeneratedJavaFile(eclipsePluginGenModule)
file.importNestedTypeThreshold = JavaFileAccess.DONT_IMPORT_NESTED_TYPES
file.typeComment = '''
/**
* Manual modifications go to {@link «eclipsePluginModule.simpleName»}.
*/
'''
file.annotations += new SuppressWarningsAnnotation
file.content = '''
public abstract class «eclipsePluginGenModule.simpleName» extends «superClass» {
public «eclipsePluginGenModule.simpleName»(«'org.eclipse.ui.plugin.AbstractUIPlugin'.typeRef» plugin) {
super(plugin);
}
«FOR binding : langConfig.eclipsePluginGenModule.bindings»
«binding.createBindingMethod»
«ENDFOR»
}
'''
file.markedAsGenerated = true
return file
}
def JavaFileAccess createWebModule(IXtextGeneratorLanguage langConfig) {
val it = langConfig.grammar
if (codeConfig.preferXtendStubs) {
return fileAccessFactory.createXtendFile(webModule,'''
/**
* Use this class to register additional components to be used within the web application.
*/
class «webModule.simpleName» extends «webGenModule» {
}
''')
} else {
return fileAccessFactory.createJavaFile(webModule,'''
/**
* Use this class to register additional components to be used within the web application.
*/
public class «webModule.simpleName» extends «webGenModule» {
}
''')
}
}
def JavaFileAccess createWebGenModule(IXtextGeneratorLanguage langConfig) {
val it = langConfig.grammar
val superClass = langConfig.webGenModule.superClass ?: webDefaultModule
val file = fileAccessFactory.createGeneratedJavaFile(webGenModule)
file.importNestedTypeThreshold = JavaFileAccess.DONT_IMPORT_NESTED_TYPES
file.typeComment = '''
/**
* Manual modifications go to {@link «webModule.simpleName»}.
*/
'''
file.annotations += new SuppressWarningsAnnotation
file.content = '''
public abstract class «webGenModule.simpleName» extends «superClass» {
«FOR binding : langConfig.webGenModule.bindings»
«binding.createBindingMethod»
«ENDFOR»
}
'''
file.markedAsGenerated = true
return file
}
def JavaFileAccess createWebSetup(IXtextGeneratorLanguage langConfig) {
val it = langConfig.grammar
if (codeConfig.preferXtendStubs) {
return fileAccessFactory.createXtendFile(webSetup, '''
/**
* Initialization support for running Xtext languages in web applications.
*/
class «webSetup.simpleName» extends «runtimeSetup» {
override «Injector» createInjector() {
return «Guice».createInjector(«Modules2».mixin(new «runtimeModule», new «genericIdeModule», new «webModule»))
}
}
''')
} else {
return fileAccessFactory.createJavaFile(webSetup, '''
/**
* Initialization support for running Xtext languages in web applications.
*/
public class «webSetup.simpleName» extends «runtimeSetup» {
@Override
public «Injector» createInjector() {
return «Guice».createInjector(«Modules2».mixin(new «runtimeModule»(), new «genericIdeModule»(), new «webModule»()));
}
}
''')
}
}
def JavaFileAccess createEclipsePluginExecutableExtensionFactory(IXtextGeneratorLanguage langConfig, IXtextGeneratorLanguage activatorLanguage) {
val grammar = langConfig.grammar
val file = fileAccessFactory.createGeneratedJavaFile(grammar.eclipsePluginExecutableExtensionFactory)
file.typeComment = '''
/**
* This class was generated. Customizations should only happen in a newly
* introduced subclass.
*/
'''
file.content = '''
public class «grammar.eclipsePluginExecutableExtensionFactory.simpleName» extends «'org.eclipse.xtext.ui.guice.AbstractGuiceAwareExecutableExtensionFactory'.typeRef» {
@Override
protected «'org.osgi.framework.Bundle'.typeRef» getBundle() {
return «'org.osgi.framework.FrameworkUtil'.typeRef».getBundle(«eclipsePluginActivator».class);
}
@Override
protected «Injector» getInjector() {
«eclipsePluginActivator» activator = «eclipsePluginActivator».getInstance();
return activator != null ? activator.getInjector(«eclipsePluginActivator».«langConfig.grammar.name.toUpperCase.replaceAll('\\.', '_')») : null;
}
}
'''
return file
}
def JavaFileAccess createEclipsePluginActivator(IXtextProjectConfig projectConfig, List<? extends IXtextGeneratorLanguage> langConfigs) {
val activator = eclipsePluginActivator
val file = fileAccessFactory.createGeneratedJavaFile(activator)
file.typeComment = '''
/**
* This class was generated. Customizations should only happen in a newly
* introduced subclass.
*/
'''
file.content = '''
public class «activator.simpleName» extends «'org.eclipse.ui.plugin.AbstractUIPlugin'.typeRef» {
public static final String PLUGIN_ID = "«projectConfig.eclipsePlugin.name»";
«FOR lang : langConfigs»
public static final String «lang.grammar.name.toUpperCase.replaceAll('\\.', '_')» = "«lang.grammar.name»";
«ENDFOR»
private static final «Logger» logger = «Logger».getLogger(«activator.simpleName».class);
private static «activator.simpleName» INSTANCE;
private «Map»<String, «Injector»> injectors = «Collections».synchronizedMap(«Maps».<String, «Injector»> newHashMapWithExpectedSize(1));
@Override
public void start(«'org.osgi.framework.BundleContext'.typeRef» context) throws Exception {
super.start(context);
INSTANCE = this;
}
@Override
public void stop(«'org.osgi.framework.BundleContext'.typeRef» context) throws Exception {
injectors.clear();
INSTANCE = null;
super.stop(context);
}
public static «activator.simpleName» getInstance() {
return INSTANCE;
}
public «Injector» getInjector(String language) {
synchronized (injectors) {
«Injector» injector = injectors.get(language);
if (injector == null) {
injectors.put(language, injector = createInjector(language));
}
return injector;
}
}
protected «Injector» createInjector(String language) {
try {
«Module» runtimeModule = getRuntimeModule(language);
«Module» sharedStateModule = getSharedStateModule();
«Module» uiModule = getUiModule(language);
«Module» mergedModule = «Modules2».mixin(runtimeModule, sharedStateModule, uiModule);
return «Guice».createInjector(mergedModule);
} catch (Exception e) {
logger.error("Failed to create injector for " + language);
logger.error(e.getMessage(), e);
throw new RuntimeException("Failed to create injector for " + language, e);
}
}
protected «Module» getRuntimeModule(String grammar) {
«FOR lang : langConfigs»
if («lang.grammar.name.toUpperCase.replaceAll('\\.', '_')».equals(grammar)) {
return new «getRuntimeModule(lang.grammar)»();
}
«ENDFOR»
throw new IllegalArgumentException(grammar);
}
protected «Module» getUiModule(String grammar) {
«FOR lang : langConfigs»
if («lang.grammar.name.toUpperCase.replaceAll('\\.', '_')».equals(grammar)) {
return new «getEclipsePluginModule(lang.grammar)»(this);
}
«ENDFOR»
throw new IllegalArgumentException(grammar);
}
protected «Module» getSharedStateModule() {
return new «'org.eclipse.xtext.ui.shared.SharedStateModule'.typeRef»();
}
}
'''
return file
}
def TextFileAccess createMetaInfServices(List<? extends IXtextGeneratorLanguage> languageConfigs) {
val file = new TextFileAccess()
file.path = "META-INF/services/org.eclipse.xtext.ISetup"
file.content = '''
«FOR lang : languageConfigs»
«naming.getGenericIdeSetup(lang.grammar)»
«ENDFOR»
'''
file
}
}