【问题标题】:Xtend/Xpand find and replace (rule)Xtend/Xpand 查找和替换(规则)
【发布时间】:2013-02-06 17:48:36
【问题描述】:

是否可以设置一个规则,允许 Xpand 方法根据输入输出指定的文本字符串。例如:

«FOR a:e.attributes»
 Type = «a.eClass.name»
 Value = «a.name.toFirstUpper»

«ENDFOR»

上面的代码可能会输出:

Type = StringAttribute
Value = String1

Type = IntegerAttribute
Value = 123

我将如何制作这个输出:

Type = String
Value = String1

Type = int
Value = 123

我知道这可以通过 if 语句来完成,但我希望它或多或少是自动的。每次我需要在同一个文件中输出这些细节时都必须指定这样的规则是一种浪费。有人可以告诉我我可以使用什么样的代码来实现这一目标吗?谢谢。

【问题讨论】:

    标签: java loops xtext xtend xpand


    【解决方案1】:

    我建议你创建一个可重用的Xtend helper

    toSimpleName(String inp):
        switch (inp) {
            case "StringAttribute"  : "String"
            case "IntegerAttribute" : "int"
            // ...more cases here...
            default                 : inp
        }
    ;
    

    然后像这样从您的 Xpand 模板中调用它:

    «FOR a:e.attributes»
     Type = «a.eClass.name.toSimpleName()»
     Value = «a.name.toFirstUpper»
    
    «ENDFOR»
    

    【讨论】:

    • 非常感谢。没有我想象的那么难。虽然我不得不稍微更改代码以使其在 Xtend 中有效:def toJavaType(String inp){ switch (inp) { case "NumberAttribute" : "String" case "StringAttribute" : "int" default : "" } }
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-19
    • 2015-03-25
    • 2011-06-16
    • 1970-01-01
    • 2017-01-28
    • 2015-01-11
    相关资源
    最近更新 更多