Search in sources :

Example 1 with IXtextGeneratorLanguage

use of org.eclipse.xtext.xtext.generator.IXtextGeneratorLanguage in project xtext-core by eclipse.

the class XtextGenerator method generateExecutableExtensionFactory.

protected void generateExecutableExtensionFactory(final IXtextGeneratorLanguage language) {
    IXtextGeneratorFileSystemAccess _srcGen = this.projectConfig.getEclipsePlugin().getSrcGen();
    boolean _tripleNotEquals = (_srcGen != null);
    if (_tripleNotEquals) {
        this.templates.createEclipsePluginExecutableExtensionFactory(language, IterableExtensions.<XtextGeneratorLanguage>head(this.languageConfigs)).writeTo(this.projectConfig.getEclipsePlugin().getSrcGen());
    }
}
Also used : XtextGeneratorLanguage(org.eclipse.xtext.xtext.generator.XtextGeneratorLanguage) IXtextGeneratorLanguage(org.eclipse.xtext.xtext.generator.IXtextGeneratorLanguage) IXtextGeneratorFileSystemAccess(org.eclipse.xtext.xtext.generator.model.IXtextGeneratorFileSystemAccess)

Example 2 with IXtextGeneratorLanguage

use of org.eclipse.xtext.xtext.generator.IXtextGeneratorLanguage in project xtext-core by eclipse.

the class XtextGeneratorTemplates method createEclipsePluginActivator.

public JavaFileAccess createEclipsePluginActivator(final IXtextProjectConfig projectConfig, final List<? extends IXtextGeneratorLanguage> langConfigs) {
    final TypeReference activator = this.naming.getEclipsePluginActivator();
    final GeneratedJavaFileAccess file = this.fileAccessFactory.createGeneratedJavaFile(activator);
    StringConcatenationClient _client = new StringConcatenationClient() {

        @Override
        protected void appendTo(StringConcatenationClient.TargetStringConcatenation _builder) {
            _builder.append("/**");
            _builder.newLine();
            _builder.append(" ");
            _builder.append("* This class was generated. Customizations should only happen in a newly");
            _builder.newLine();
            _builder.append(" ");
            _builder.append("* introduced subclass. ");
            _builder.newLine();
            _builder.append(" ");
            _builder.append("*/");
            _builder.newLine();
        }
    };
    file.setTypeComment(_client);
    StringConcatenationClient _client_1 = new StringConcatenationClient() {

        @Override
        protected void appendTo(StringConcatenationClient.TargetStringConcatenation _builder) {
            _builder.append("public class ");
            String _simpleName = activator.getSimpleName();
            _builder.append(_simpleName);
            _builder.append(" extends ");
            TypeReference _typeRef = TypeReference.typeRef("org.eclipse.ui.plugin.AbstractUIPlugin");
            _builder.append(_typeRef);
            _builder.append(" {");
            _builder.newLineIfNotEmpty();
            _builder.newLine();
            _builder.append("\t");
            _builder.append("public static final String PLUGIN_ID = \"");
            String _name = projectConfig.getEclipsePlugin().getName();
            _builder.append(_name, "\t");
            _builder.append("\";");
            _builder.newLineIfNotEmpty();
            {
                for (final IXtextGeneratorLanguage lang : langConfigs) {
                    _builder.append("\t");
                    _builder.append("public static final String ");
                    String _replaceAll = lang.getGrammar().getName().toUpperCase().replaceAll("\\.", "_");
                    _builder.append(_replaceAll, "\t");
                    _builder.append(" = \"");
                    String _name_1 = lang.getGrammar().getName();
                    _builder.append(_name_1, "\t");
                    _builder.append("\";");
                    _builder.newLineIfNotEmpty();
                }
            }
            _builder.append("\t");
            _builder.newLine();
            _builder.append("\t");
            _builder.append("private static final ");
            _builder.append(Logger.class, "\t");
            _builder.append(" logger = ");
            _builder.append(Logger.class, "\t");
            _builder.append(".getLogger(");
            String _simpleName_1 = activator.getSimpleName();
            _builder.append(_simpleName_1, "\t");
            _builder.append(".class);");
            _builder.newLineIfNotEmpty();
            _builder.append("\t");
            _builder.newLine();
            _builder.append("\t");
            _builder.append("private static ");
            String _simpleName_2 = activator.getSimpleName();
            _builder.append(_simpleName_2, "\t");
            _builder.append(" INSTANCE;");
            _builder.newLineIfNotEmpty();
            _builder.append("\t");
            _builder.newLine();
            _builder.append("\t");
            _builder.append("private ");
            _builder.append(Map.class, "\t");
            _builder.append("<String, ");
            _builder.append(Injector.class, "\t");
            _builder.append("> injectors = ");
            _builder.append(Collections.class, "\t");
            _builder.append(".synchronizedMap(");
            _builder.append(Maps.class, "\t");
            _builder.append(".<String, ");
            _builder.append(Injector.class, "\t");
            _builder.append("> newHashMapWithExpectedSize(1));");
            _builder.newLineIfNotEmpty();
            _builder.append("\t");
            _builder.newLine();
            _builder.append("\t");
            _builder.append("@Override");
            _builder.newLine();
            _builder.append("\t");
            _builder.append("public void start(");
            TypeReference _typeRef_1 = TypeReference.typeRef("org.osgi.framework.BundleContext");
            _builder.append(_typeRef_1, "\t");
            _builder.append(" context) throws Exception {");
            _builder.newLineIfNotEmpty();
            _builder.append("\t\t");
            _builder.append("super.start(context);");
            _builder.newLine();
            _builder.append("\t\t");
            _builder.append("INSTANCE = this;");
            _builder.newLine();
            _builder.append("\t");
            _builder.append("}");
            _builder.newLine();
            _builder.append("\t");
            _builder.newLine();
            _builder.append("\t");
            _builder.append("@Override");
            _builder.newLine();
            _builder.append("\t");
            _builder.append("public void stop(");
            TypeReference _typeRef_2 = TypeReference.typeRef("org.osgi.framework.BundleContext");
            _builder.append(_typeRef_2, "\t");
            _builder.append(" context) throws Exception {");
            _builder.newLineIfNotEmpty();
            _builder.append("\t\t");
            _builder.append("injectors.clear();");
            _builder.newLine();
            _builder.append("\t\t");
            _builder.append("INSTANCE = null;");
            _builder.newLine();
            _builder.append("\t\t");
            _builder.append("super.stop(context);");
            _builder.newLine();
            _builder.append("\t");
            _builder.append("}");
            _builder.newLine();
            _builder.append("\t");
            _builder.newLine();
            _builder.append("\t");
            _builder.append("public static ");
            String _simpleName_3 = activator.getSimpleName();
            _builder.append(_simpleName_3, "\t");
            _builder.append(" getInstance() {");
            _builder.newLineIfNotEmpty();
            _builder.append("\t\t");
            _builder.append("return INSTANCE;");
            _builder.newLine();
            _builder.append("\t");
            _builder.append("}");
            _builder.newLine();
            _builder.append("\t");
            _builder.newLine();
            _builder.append("\t");
            _builder.append("public ");
            _builder.append(Injector.class, "\t");
            _builder.append(" getInjector(String language) {");
            _builder.newLineIfNotEmpty();
            _builder.append("\t\t");
            _builder.append("synchronized (injectors) {");
            _builder.newLine();
            _builder.append("\t\t\t");
            _builder.append(Injector.class, "\t\t\t");
            _builder.append(" injector = injectors.get(language);");
            _builder.newLineIfNotEmpty();
            _builder.append("\t\t\t");
            _builder.append("if (injector == null) {");
            _builder.newLine();
            _builder.append("\t\t\t\t");
            _builder.append("injectors.put(language, injector = createInjector(language));");
            _builder.newLine();
            _builder.append("\t\t\t");
            _builder.append("}");
            _builder.newLine();
            _builder.append("\t\t\t");
            _builder.append("return injector;");
            _builder.newLine();
            _builder.append("\t\t");
            _builder.append("}");
            _builder.newLine();
            _builder.append("\t");
            _builder.append("}");
            _builder.newLine();
            _builder.append("\t");
            _builder.newLine();
            _builder.append("\t");
            _builder.append("protected ");
            _builder.append(Injector.class, "\t");
            _builder.append(" createInjector(String language) {");
            _builder.newLineIfNotEmpty();
            _builder.append("\t\t");
            _builder.append("try {");
            _builder.newLine();
            _builder.append("\t\t\t");
            _builder.append(com.google.inject.Module.class, "\t\t\t");
            _builder.append(" runtimeModule = getRuntimeModule(language);");
            _builder.newLineIfNotEmpty();
            _builder.append("\t\t\t");
            _builder.append(com.google.inject.Module.class, "\t\t\t");
            _builder.append(" sharedStateModule = getSharedStateModule();");
            _builder.newLineIfNotEmpty();
            _builder.append("\t\t\t");
            _builder.append(com.google.inject.Module.class, "\t\t\t");
            _builder.append(" uiModule = getUiModule(language);");
            _builder.newLineIfNotEmpty();
            _builder.append("\t\t\t");
            _builder.append(com.google.inject.Module.class, "\t\t\t");
            _builder.append(" mergedModule = ");
            _builder.append(Modules2.class, "\t\t\t");
            _builder.append(".mixin(runtimeModule, sharedStateModule, uiModule);");
            _builder.newLineIfNotEmpty();
            _builder.append("\t\t\t");
            _builder.append("return ");
            _builder.append(Guice.class, "\t\t\t");
            _builder.append(".createInjector(mergedModule);");
            _builder.newLineIfNotEmpty();
            _builder.append("\t\t");
            _builder.append("} catch (Exception e) {");
            _builder.newLine();
            _builder.append("\t\t\t");
            _builder.append("logger.error(\"Failed to create injector for \" + language);");
            _builder.newLine();
            _builder.append("\t\t\t");
            _builder.append("logger.error(e.getMessage(), e);");
            _builder.newLine();
            _builder.append("\t\t\t");
            _builder.append("throw new RuntimeException(\"Failed to create injector for \" + language, e);");
            _builder.newLine();
            _builder.append("\t\t");
            _builder.append("}");
            _builder.newLine();
            _builder.append("\t");
            _builder.append("}");
            _builder.newLine();
            _builder.append("\t");
            _builder.newLine();
            _builder.append("\t");
            _builder.append("protected ");
            _builder.append(com.google.inject.Module.class, "\t");
            _builder.append(" getRuntimeModule(String grammar) {");
            _builder.newLineIfNotEmpty();
            {
                for (final IXtextGeneratorLanguage lang_1 : langConfigs) {
                    _builder.append("\t\t");
                    _builder.append("if (");
                    String _replaceAll_1 = lang_1.getGrammar().getName().toUpperCase().replaceAll("\\.", "_");
                    _builder.append(_replaceAll_1, "\t\t");
                    _builder.append(".equals(grammar)) {");
                    _builder.newLineIfNotEmpty();
                    _builder.append("\t\t");
                    _builder.append("\t");
                    _builder.append("return new ");
                    TypeReference _runtimeModule = XtextGeneratorTemplates.this.naming.getRuntimeModule(lang_1.getGrammar());
                    _builder.append(_runtimeModule, "\t\t\t");
                    _builder.append("();");
                    _builder.newLineIfNotEmpty();
                    _builder.append("\t\t");
                    _builder.append("}");
                    _builder.newLine();
                }
            }
            _builder.append("\t\t");
            _builder.append("throw new IllegalArgumentException(grammar);");
            _builder.newLine();
            _builder.append("\t");
            _builder.append("}");
            _builder.newLine();
            _builder.append("\t");
            _builder.newLine();
            _builder.append("\t");
            _builder.append("protected ");
            _builder.append(com.google.inject.Module.class, "\t");
            _builder.append(" getUiModule(String grammar) {");
            _builder.newLineIfNotEmpty();
            {
                for (final IXtextGeneratorLanguage lang_2 : langConfigs) {
                    _builder.append("\t\t");
                    _builder.append("if (");
                    String _replaceAll_2 = lang_2.getGrammar().getName().toUpperCase().replaceAll("\\.", "_");
                    _builder.append(_replaceAll_2, "\t\t");
                    _builder.append(".equals(grammar)) {");
                    _builder.newLineIfNotEmpty();
                    _builder.append("\t\t");
                    _builder.append("\t");
                    _builder.append("return new ");
                    TypeReference _eclipsePluginModule = XtextGeneratorTemplates.this.naming.getEclipsePluginModule(lang_2.getGrammar());
                    _builder.append(_eclipsePluginModule, "\t\t\t");
                    _builder.append("(this);");
                    _builder.newLineIfNotEmpty();
                    _builder.append("\t\t");
                    _builder.append("}");
                    _builder.newLine();
                }
            }
            _builder.append("\t\t");
            _builder.append("throw new IllegalArgumentException(grammar);");
            _builder.newLine();
            _builder.append("\t");
            _builder.append("}");
            _builder.newLine();
            _builder.append("\t");
            _builder.newLine();
            _builder.append("\t");
            _builder.append("protected ");
            _builder.append(com.google.inject.Module.class, "\t");
            _builder.append(" getSharedStateModule() {");
            _builder.newLineIfNotEmpty();
            _builder.append("\t\t");
            _builder.append("return new ");
            TypeReference _typeRef_3 = TypeReference.typeRef("org.eclipse.xtext.ui.shared.SharedStateModule");
            _builder.append(_typeRef_3, "\t\t");
            _builder.append("();");
            _builder.newLineIfNotEmpty();
            _builder.append("\t");
            _builder.append("}");
            _builder.newLine();
            _builder.append("\t");
            _builder.newLine();
            _builder.append("\t");
            _builder.newLine();
            _builder.append("}");
            _builder.newLine();
        }
    };
    file.setContent(_client_1);
    return file;
}
Also used : StringConcatenationClient(org.eclipse.xtend2.lib.StringConcatenationClient) IXtextGeneratorLanguage(org.eclipse.xtext.xtext.generator.IXtextGeneratorLanguage) GeneratedJavaFileAccess(org.eclipse.xtext.xtext.generator.model.GeneratedJavaFileAccess) TypeReference(org.eclipse.xtext.xtext.generator.model.TypeReference)

Aggregations

IXtextGeneratorLanguage (org.eclipse.xtext.xtext.generator.IXtextGeneratorLanguage)2 StringConcatenationClient (org.eclipse.xtend2.lib.StringConcatenationClient)1 XtextGeneratorLanguage (org.eclipse.xtext.xtext.generator.XtextGeneratorLanguage)1 GeneratedJavaFileAccess (org.eclipse.xtext.xtext.generator.model.GeneratedJavaFileAccess)1 IXtextGeneratorFileSystemAccess (org.eclipse.xtext.xtext.generator.model.IXtextGeneratorFileSystemAccess)1 TypeReference (org.eclipse.xtext.xtext.generator.model.TypeReference)1