简体   繁体   English

DomainmodelGenerator.xtend中的XTEND循环索引控件(XTEXT代码生成项目)

[英]XTEND For-Loop indexcontrol in DomainmodelGenerator.xtend (XTEXT codegeneration project)

I worked through the Tutorials at eclipse.org/Xtext/documentation and get into expanding these samples. 我浏览了eclipse.org/Xtext/documentation中的教程,并开始扩展这些示例。 Working with the Domainmodel.xtext sample I generate a Java-Classfile for each entity as stated in the Tut. 我使用Domainmodel.xtext示例为Tut中所述的每个实体生成一个Java-Classfile。

The DSL specifies an arbitry number of features, aka class properties: DSL指定了任意数量的功能,也称为类属性:

  Entity:
  'entity' name = ID 
          ('extends' superType = [Entity | QualifiedName])?
   '{'
   (features += Feature)*
   '}'
    ;

In DomainmodelGenerator.xtend then I added code to generate a JAVA-classconstructor. 然后在DomainmodelGenerator.xtend中添加代码以生成JAVA类构造函数。 The XTEND-Forloop cycles through all arguements - looks like this: XTEND-Forloop遍历所有争论-看起来像这样:

def compile_Constructors(Entity e) '''
public «e.name.toFirstUpper»
       (
      «FOR f : e.features»
           «f.type.fullyQualifiedName» «f.name.toFirstUpper», 
      «ENDFOR»
        ) 
{}  
'''

Problem With this the last parameter there is still a comma emitted. 问题与此最后一个参数仍然发出逗号。 How can I get control in XTEND over the loopindex, to make the generator to emit legal JAVA code? 如何在XTEND中获得对loopindex的控制,以使生成器发出合法的JAVA代码?

How about: 怎么样:

def compile_Constructors(Entity e) '''
    public «e.name.toFirstUpper»
           (
           «e.features.map[type.fullyQualifiedName + ' ' + name.toFirstUpper].join(', ')»
           ) 
    {}
'''

The «FOR» loop has some options which are quite handy: «FOR»循环具有一些非常方便的选项:

  • BEFORE string
  • SEPARATOR string
  • AFTER string

These allows you to emit additional strings before, between and after items. 这些允许您在项目之前,之间和之后发出其他字符串。 If there are no items (empty list) none of them is emitted. 如果没有项目(空列表),则不会发射任何项目。

So in your case just use 所以就你而言

«FOR f : e.features SEPARATOR ', '»

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM