以编程方式将源代码添加到自定义DSL文件的问题

时间:2015-08-27 09:21:14

标签: dsl xtext

我目前有一个xtext语法,如下所示:

Features:
'feature' name = ID
'{'(
    ('action'           '{' action+=Actions (',' action+=Actions)*  '}')? &
    ('dependencies'     '{' dependencies    = Dependencies          '}')? &
    ('children'         '{' children        = Children              '}')?
)'}'

我想要做的是以编程方式向已经存在的源文件添加一个动作,因为我正在使用IUnitOfWork.Void类,我将其子类化以便于实现,它目前看起来像这样(它的有意义的部分) ):

final XtextEditor editor = (XtextEditor)sourcepart;
        final IXtextDocument document = editor.getDocument();
        document.modify(new IUnitOfWork.Void<XtextResource>(){
            public void process (XtextResource resource) throws Exception {
                IParseResult parseResult = resource.getParseResult();
                if(parseResult ==null)
                    return;
            CompositeNode rootNode=(CompositeNode) parseResult.getRootNode();
            LeafNode node = (LeafNode)NodeModelUtils.findLeafNodeAtOffset(rootNode, 0);
            EObject object =NodeModelUtils.findActualSemanticObjectFor(node);

通过这个我遍历模型的树并进入我想要添加动作的Features对象(这是通过我正在实现的自定义树视图中的弹出菜单完成的)

这是我的问题:无论何时我想添加一个动作,都会搞砸标签放在源文件中的方式,我的意思是代替:

action {
                    act1.set (foo),
                    act2.set (bar),
                    act3.set (baz),
                    act4.set (booze)  //where this is the new action that I add
                }

它会将其添加为

 action {
                            act1.set (foo),
                            act2.set (bar),
                            act3.set (baz)
}

action { 
                             act4.set(booze)
       }

根据我的语法规则,这是违法的,我不允许改变应该写的方式。 (我可以对规则的实施方式做一些小改动,但是真的要避免它,因为这意味着要重新实现依赖它们的其他东西的全新工作量)

我试过了:

  1. 通过Features.getAction()直接添加.add(* new action);
  2. 使用toArray()方法将列表中的项目复制到数组中,以避免引用,将我的操作添加到数组中,清除列表,然后逐个添加所有元素
  3. 创建一个全新的Features对象并将其中的所有内容设置为与当前编辑的对象相同,然后将该功能替换为新的
  4. 之后我就没有想法了。令人沮丧的是,第三种方法适用于我的语法中的不同类型的对象,并且没有错误。 我怎么能做这个工作?

0 个答案:

没有答案