Search in sources :

Example 1 with XtendFileAccess

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

the class FileAccessFactory method createXtendFile.

public XtendFileAccess createXtendFile(final TypeReference typeRef, final StringConcatenationClient content) {
    final XtendFileAccess result = this.createXtendFile(typeRef);
    result.setContent(content);
    return result;
}
Also used : XtendFileAccess(org.eclipse.xtext.xtext.generator.model.XtendFileAccess)

Example 2 with XtendFileAccess

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

the class Formatter2Fragment2 method doGenerateStubFile.

protected void doGenerateStubFile() {
    boolean _isGenerateStub = this.isGenerateStub();
    boolean _not = (!_isGenerateStub);
    if (_not) {
        return;
    }
    boolean _isGenerateXtendStub = this.isGenerateXtendStub();
    if (_isGenerateXtendStub) {
        final XtendFileAccess xtendFile = this.fileAccessFactory.createXtendFile(this.getFormatter2Stub(this.getGrammar()));
        xtendFile.setResourceSet(this.getLanguage().getResourceSet());
        final LinkedHashMultimap<EClass, EReference> type2ref = LinkedHashMultimap.<EClass, EReference>create();
        this.getLocallyAssignedContainmentReferences(this.getLanguage().getGrammar(), type2ref);
        final LinkedHashMultimap<EClass, EReference> inheritedTypes = LinkedHashMultimap.<EClass, EReference>create();
        this.getInheritedContainmentReferences(this.getLanguage().getGrammar(), inheritedTypes, CollectionLiterals.<Grammar>newHashSet());
        final Set<EClass> types = type2ref.keySet();
        StringConcatenationClient _client = new StringConcatenationClient() {

            @Override
            protected void appendTo(StringConcatenationClient.TargetStringConcatenation _builder) {
                _builder.append("class ");
                String _simpleName = Formatter2Fragment2.this.getFormatter2Stub(Formatter2Fragment2.this.getGrammar()).getSimpleName();
                _builder.append(_simpleName);
                _builder.append(" extends ");
                TypeReference _stubSuperClass = Formatter2Fragment2.this.getStubSuperClass();
                _builder.append(_stubSuperClass);
                _builder.append(" {");
                _builder.newLineIfNotEmpty();
                _builder.append("\t");
                _builder.newLine();
                _builder.append("\t");
                _builder.append("@");
                _builder.append(Inject.class, "\t");
                _builder.append(" extension ");
                TypeReference _grammarAccess = Formatter2Fragment2.this._grammarAccessExtensions.getGrammarAccess(Formatter2Fragment2.this.getGrammar());
                _builder.append(_grammarAccess, "\t");
                _builder.newLineIfNotEmpty();
                {
                    Iterable<EClass> _take = IterableExtensions.<EClass>take(types, 2);
                    for (final EClass type : _take) {
                        _builder.newLine();
                        _builder.append("\t");
                        StringConcatenationClient _generateFormatMethod = Formatter2Fragment2.this.generateFormatMethod(type, type2ref.get(type), inheritedTypes.containsKey(type));
                        _builder.append(_generateFormatMethod, "\t");
                        _builder.newLineIfNotEmpty();
                    }
                }
                _builder.append("\t");
                _builder.newLine();
                _builder.append("\t");
                _builder.append("// TODO: implement for ");
                final Function1<EClass, String> _function = (EClass it) -> {
                    return it.getName();
                };
                String _join = IterableExtensions.join(IterableExtensions.<EClass, String>map(IterableExtensions.<EClass>drop(types, 2), _function), ", ");
                _builder.append(_join, "\t");
                _builder.newLineIfNotEmpty();
                _builder.append("}");
                _builder.newLine();
            }
        };
        xtendFile.setContent(_client);
        xtendFile.writeTo(this.getProjectConfig().getRuntime().getSrc());
    } else {
        String _name = this.getClass().getName();
        String _plus = (_name + " has been configured to generate a Java stub, but that\'s not yet supported. See https://bugs.eclipse.org/bugs/show_bug.cgi?id=481563");
        Formatter2Fragment2.LOG.error(_plus);
    }
}
Also used : XtendFileAccess(org.eclipse.xtext.xtext.generator.model.XtendFileAccess) EClass(org.eclipse.emf.ecore.EClass) StringConcatenationClient(org.eclipse.xtend2.lib.StringConcatenationClient) TypeReference(org.eclipse.xtext.xtext.generator.model.TypeReference) EReference(org.eclipse.emf.ecore.EReference)

Example 3 with XtendFileAccess

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

the class SimpleProjectWizardFragment2 method generateNewProjectWizardInitialContents.

public void generateNewProjectWizardInitialContents() {
    final TypeReference initialContentsClass = TypeReference.typeRef(this.getProjectWizardInitialContentsClassName());
    final XtendFileAccess file = this.fileAccessFactory.createXtendFile(initialContentsClass);
    StringConcatenationClient _client = new StringConcatenationClient() {

        @Override
        protected void appendTo(StringConcatenationClient.TargetStringConcatenation _builder) {
            _builder.append("import com.google.inject.Inject");
            _builder.newLine();
            _builder.append("import org.eclipse.xtext.generator.IFileSystemAccess2");
            _builder.newLine();
            _builder.append("import org.eclipse.xtext.resource.FileExtensionProvider");
            _builder.newLine();
            _builder.newLine();
            _builder.append("class ");
            String _simpleName = initialContentsClass.getSimpleName();
            _builder.append(_simpleName);
            _builder.append(" {");
            _builder.newLineIfNotEmpty();
            _builder.append("\t");
            _builder.append("@Inject");
            _builder.newLine();
            _builder.append("\t");
            _builder.append("FileExtensionProvider fileExtensionProvider");
            _builder.newLine();
            _builder.newLine();
            _builder.append("\t");
            _builder.append("def generateInitialContents(IFileSystemAccess2 fsa) {");
            _builder.newLine();
            _builder.append("\t\t");
            _builder.append("fsa.generateFile(");
            _builder.newLine();
            _builder.append("\t\t\t");
            _builder.append("\"src/model/Model.\" + fileExtensionProvider.primaryFileExtension,");
            _builder.newLine();
            _builder.append("\t\t\t");
            _builder.append("\'\'");
            _builder.append("\'");
            _builder.newLine();
            _builder.append("\t\t\t");
            _builder.append("/*");
            _builder.newLine();
            _builder.append("\t\t\t ");
            _builder.append("* This is an example model");
            _builder.newLine();
            _builder.append("\t\t\t ");
            _builder.append("*/");
            _builder.newLine();
            _builder.append("\t\t\t");
            _builder.append("Hello Xtext!");
            _builder.newLine();
            _builder.append("\t\t\t");
            _builder.append("\'\'");
            _builder.append("\'");
            _builder.newLine();
            _builder.append("\t\t\t");
            _builder.append(")");
            _builder.newLine();
            _builder.append("\t");
            _builder.append("}");
            _builder.newLine();
            _builder.append("}");
            _builder.newLine();
        }
    };
    file.setContent(_client);
    file.writeTo(this.getProjectConfig().getEclipsePlugin().getSrc());
}
Also used : XtendFileAccess(org.eclipse.xtext.xtext.generator.model.XtendFileAccess) StringConcatenationClient(org.eclipse.xtend2.lib.StringConcatenationClient) TypeReference(org.eclipse.xtext.xtext.generator.model.TypeReference)

Example 4 with XtendFileAccess

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

the class TemplateProjectWizardFragment method generateProjectTemplateProvider.

public void generateProjectTemplateProvider() {
    final TypeReference initialContentsClass = TypeReference.typeRef(this.getProjectTemplateProviderClassName());
    final String quotes = "\'\'\'";
    final String openVar = "�";
    final String closeVar = "�";
    final XtendFileAccess file = this.fileAccessFactory.createXtendFile(initialContentsClass);
    StringConcatenationClient _client = new StringConcatenationClient() {

        @Override
        protected void appendTo(StringConcatenationClient.TargetStringConcatenation _builder) {
            _builder.append("import org.eclipse.core.runtime.Status");
            _builder.newLine();
            {
                if (TemplateProjectWizardFragment.this.pluginProject) {
                    _builder.append("import org.eclipse.jdt.core.JavaCore");
                    _builder.newLine();
                }
            }
            _builder.append("import org.eclipse.xtext.ui.XtextProjectHelper");
            _builder.newLine();
            {
                if (TemplateProjectWizardFragment.this.pluginProject) {
                    _builder.append("import org.eclipse.xtext.ui.util.PluginProjectFactory");
                    _builder.newLine();
                } else {
                    _builder.append("import org.eclipse.xtext.ui.util.ProjectFactory");
                    _builder.newLine();
                }
            }
            _builder.append("import org.eclipse.xtext.ui.wizard.template.IProjectGenerator");
            _builder.newLine();
            _builder.append("import org.eclipse.xtext.ui.wizard.template.IProjectTemplateProvider");
            _builder.newLine();
            _builder.append("import org.eclipse.xtext.ui.wizard.template.ProjectTemplate");
            _builder.newLine();
            _builder.newLine();
            _builder.append("import static org.eclipse.core.runtime.IStatus.*");
            _builder.newLine();
            _builder.newLine();
            _builder.append("/**");
            _builder.newLine();
            _builder.append(" ");
            _builder.append("* Create a list with all project templates to be shown in the template new project wizard.");
            _builder.newLine();
            _builder.append(" ");
            _builder.append("* ");
            _builder.newLine();
            _builder.append(" ");
            _builder.append("* Each template is able to generate one or more projects. Each project can be configured such that any number of files are included.");
            _builder.newLine();
            _builder.append(" ");
            _builder.append("*/");
            _builder.newLine();
            _builder.append("class ");
            String _simpleName = initialContentsClass.getSimpleName();
            _builder.append(_simpleName);
            _builder.append(" implements IProjectTemplateProvider {");
            _builder.newLineIfNotEmpty();
            _builder.append("\t");
            _builder.append("override getProjectTemplates() {");
            _builder.newLine();
            _builder.append("\t\t");
            _builder.append("#[new HelloWorldProject]");
            _builder.newLine();
            _builder.append("\t");
            _builder.append("}");
            _builder.newLine();
            _builder.append("}");
            _builder.newLine();
            _builder.newLine();
            _builder.append("@ProjectTemplate(label=\"Hello World\", icon=\"project_template.png\", description=\"<p><b>Hello World</b></p>");
            _builder.newLine();
            _builder.append("<p>This is a parameterized hello world for ");
            String _name = TemplateProjectWizardFragment.this.getGrammar().getName();
            _builder.append(_name);
            _builder.append(". You can set a parameter to modify the content in the generated file");
            _builder.newLineIfNotEmpty();
            _builder.append("and a parameter to set the package the file is created in.</p>\")");
            _builder.newLine();
            _builder.append("final class HelloWorldProject {");
            _builder.newLine();
            _builder.append("\t");
            _builder.append("val advanced = check(\"Advanced\", false)");
            _builder.newLine();
            _builder.append("\t");
            _builder.append("val advancedGroup = group(\"Properties\")");
            _builder.newLine();
            _builder.append("\t");
            _builder.append("val name = combo(\"Name\", #[\"Xtext\", \"World\", \"Foo\", \"Bar\"], \"The name to say \'Hello\' to\", advancedGroup)");
            _builder.newLine();
            _builder.append("\t");
            _builder.append("val path = text(\"Package\", \"mydsl\", \"The package path to place the files in\", advancedGroup)");
            _builder.newLine();
            _builder.newLine();
            _builder.append("\t");
            _builder.append("override protected updateVariables() {");
            _builder.newLine();
            _builder.append("\t\t");
            _builder.append("name.enabled = advanced.value");
            _builder.newLine();
            _builder.append("\t\t");
            _builder.append("path.enabled = advanced.value");
            _builder.newLine();
            _builder.append("\t\t");
            _builder.append("if (!advanced.value) {");
            _builder.newLine();
            _builder.append("\t\t\t");
            _builder.append("name.value = \"Xtext\"");
            _builder.newLine();
            _builder.append("\t\t\t");
            _builder.append("path.value = \"");
            String _get = TemplateProjectWizardFragment.this.getLanguage().getFileExtensions().get(0);
            _builder.append(_get, "\t\t\t");
            _builder.append("\"");
            _builder.newLineIfNotEmpty();
            _builder.append("\t\t");
            _builder.append("}");
            _builder.newLine();
            _builder.append("\t");
            _builder.append("}");
            _builder.newLine();
            _builder.newLine();
            _builder.append("\t");
            _builder.append("override protected validate() {");
            _builder.newLine();
            _builder.append("\t\t");
            _builder.append("if (path.value.matches(\'[a-z][a-z0-9_]*(/[a-z][a-z0-9_]*)*\'))");
            _builder.newLine();
            _builder.append("\t\t\t");
            _builder.append("null");
            _builder.newLine();
            _builder.append("\t\t");
            _builder.append("else");
            _builder.newLine();
            _builder.append("\t\t\t");
            _builder.append("new Status(ERROR, \"Wizard\", \"\'\" + path + \"\' is not a valid package name\")");
            _builder.newLine();
            _builder.append("\t");
            _builder.append("}");
            _builder.newLine();
            _builder.newLine();
            {
                if (TemplateProjectWizardFragment.this.pluginProject) {
                    _builder.append("\t");
                    _builder.append("override generateProjects(IProjectGenerator generator) {");
                    _builder.newLine();
                    _builder.append("\t");
                    _builder.append("\t");
                    _builder.append("generator.generate(new PluginProjectFactory => [");
                    _builder.newLine();
                    _builder.append("\t");
                    _builder.append("\t\t");
                    _builder.append("projectName = projectInfo.projectName");
                    _builder.newLine();
                    _builder.append("\t");
                    _builder.append("\t\t");
                    _builder.append("location = projectInfo.locationPath");
                    _builder.newLine();
                    _builder.append("\t");
                    _builder.append("\t\t");
                    _builder.append("projectNatures += #[JavaCore.NATURE_ID, \"org.eclipse.pde.PluginNature\", XtextProjectHelper.NATURE_ID]");
                    _builder.newLine();
                    _builder.append("\t");
                    _builder.append("\t\t");
                    _builder.append("builderIds += JavaCore.BUILDER_ID");
                    _builder.newLine();
                    _builder.append("\t");
                    _builder.append("\t\t");
                    _builder.append("folders += \"src\"");
                    _builder.newLine();
                    _builder.append("\t");
                    _builder.append("\t\t");
                    _builder.append("addFile(");
                    _builder.append(quotes, "\t\t\t");
                    _builder.append("src/");
                    _builder.append(openVar, "\t\t\t");
                    _builder.append("path");
                    _builder.append(closeVar, "\t\t\t");
                    _builder.append("/Model.");
                    String _get_1 = TemplateProjectWizardFragment.this.getLanguage().getFileExtensions().get(0);
                    _builder.append(_get_1, "\t\t\t");
                    _builder.append(quotes, "\t\t\t");
                    _builder.append(", ");
                    _builder.append(quotes, "\t\t\t");
                    _builder.newLineIfNotEmpty();
                    _builder.append("\t");
                    _builder.append("\t\t\t");
                    _builder.append("/*");
                    _builder.newLine();
                    _builder.append("\t");
                    _builder.append("\t\t\t ");
                    _builder.append("* This is an example model");
                    _builder.newLine();
                    _builder.append("\t");
                    _builder.append("\t\t\t ");
                    _builder.append("*/");
                    _builder.newLine();
                    _builder.append("\t");
                    _builder.append("\t\t\t");
                    _builder.append("Hello ");
                    _builder.append(openVar, "\t\t\t\t");
                    _builder.append("name");
                    _builder.append(closeVar, "\t\t\t\t");
                    _builder.append("!");
                    _builder.newLineIfNotEmpty();
                    _builder.append("\t");
                    _builder.append("\t\t");
                    _builder.append(quotes, "\t\t\t");
                    _builder.append(")");
                    _builder.newLineIfNotEmpty();
                    _builder.append("\t");
                    _builder.append("\t");
                    _builder.append("])");
                    _builder.newLine();
                    _builder.append("\t");
                    _builder.append("}");
                    _builder.newLine();
                } else {
                    _builder.append("\t");
                    _builder.append("override generateProjects(IProjectGenerator generator) {");
                    _builder.newLine();
                    _builder.append("\t");
                    _builder.append("\t");
                    _builder.append("generator.generate(new ProjectFactory => [");
                    _builder.newLine();
                    _builder.append("\t");
                    _builder.append("\t\t");
                    _builder.append("projectName = projectInfo.projectName");
                    _builder.newLine();
                    _builder.append("\t");
                    _builder.append("\t\t");
                    _builder.append("location = projectInfo.locationPath");
                    _builder.newLine();
                    _builder.append("\t");
                    _builder.append("\t\t");
                    _builder.append("projectNatures += XtextProjectHelper.NATURE_ID");
                    _builder.newLine();
                    _builder.append("\t");
                    _builder.append("\t\t");
                    _builder.append("addFile(");
                    _builder.append(quotes, "\t\t\t");
                    _builder.append("src/");
                    _builder.append(openVar, "\t\t\t");
                    _builder.append("path");
                    _builder.append(closeVar, "\t\t\t");
                    _builder.append("/Model.");
                    String _get_2 = TemplateProjectWizardFragment.this.getLanguage().getFileExtensions().get(0);
                    _builder.append(_get_2, "\t\t\t");
                    _builder.append(quotes, "\t\t\t");
                    _builder.append(", ");
                    _builder.append(quotes, "\t\t\t");
                    _builder.newLineIfNotEmpty();
                    _builder.append("\t");
                    _builder.append("\t\t\t");
                    _builder.append("/*");
                    _builder.newLine();
                    _builder.append("\t");
                    _builder.append("\t\t\t ");
                    _builder.append("* This is an example model");
                    _builder.newLine();
                    _builder.append("\t");
                    _builder.append("\t\t\t ");
                    _builder.append("*/");
                    _builder.newLine();
                    _builder.append("\t");
                    _builder.append("\t\t\t");
                    _builder.append("Hello ");
                    _builder.append(openVar, "\t\t\t\t");
                    _builder.append("name");
                    _builder.append(closeVar, "\t\t\t\t");
                    _builder.append("!");
                    _builder.newLineIfNotEmpty();
                    _builder.append("\t");
                    _builder.append("\t\t");
                    _builder.append(quotes, "\t\t\t");
                    _builder.append(")");
                    _builder.newLineIfNotEmpty();
                    _builder.append("\t");
                    _builder.append("\t");
                    _builder.append("])");
                    _builder.newLine();
                    _builder.append("\t");
                    _builder.append("}");
                    _builder.newLine();
                }
            }
            _builder.append("}");
            _builder.newLine();
        }
    };
    file.setContent(_client);
    file.writeTo(this.getProjectConfig().getEclipsePlugin().getSrc());
}
Also used : XtendFileAccess(org.eclipse.xtext.xtext.generator.model.XtendFileAccess) StringConcatenationClient(org.eclipse.xtend2.lib.StringConcatenationClient) TypeReference(org.eclipse.xtext.xtext.generator.model.TypeReference)

Aggregations

XtendFileAccess (org.eclipse.xtext.xtext.generator.model.XtendFileAccess)4 StringConcatenationClient (org.eclipse.xtend2.lib.StringConcatenationClient)3 TypeReference (org.eclipse.xtext.xtext.generator.model.TypeReference)3 EClass (org.eclipse.emf.ecore.EClass)1 EReference (org.eclipse.emf.ecore.EReference)1