引用元素的XText格式规则

时间:2014-10-24 23:21:46

标签: formatting xtext

我试图通过一个简单的例子来描述问题。可以说,我有一个语法,看起来像这样:

Model:
    rules+=Rule*;

Rule:
    View | Element;

Element:
    'element' name=ID members+=Member members+=Member* 'end';

View:
    'view' name=ID 'for' forElement=[Element] 
    forMembers+=[Member] forMembers+=[Member]* 'end';

Member:
    'member' name=ID;

这种语言的一个简单例子是:

// Element is where the Members are instantiated
element Foo
    member bar
    member foobar
    member barfoo
    member something
end

// Views only reference the members by simple name
view FooView for Foo
    bar 
    foobar 
    something
end

我有forMembers的{​​{1}}属性的自定义范围提供程序:

View rule

到目前为止一切都很好,我的问题来到这里:

我还有一个格式化程序,我无法为引用的class MyDslScopeProvider extends AbstractDeclarativeScopeProvider { def scope_View_forMembers(View view, EReference reference) { Scopes.scopeFor(view.forElement.members); } } 添加规则:

Member

如您所见,我已为class MyDslFormatter extends AbstractDeclarativeFormatter { @Inject extension MyDslGrammarAccess override protected void configureFormatting(FormattingConfig c) { // Rules for element: line breaks, indentation increment and decrement c.setLinewrap(1).after(elementAccess.nameAssignment_1) c.setLinewrap(1).after(elementRule) c.setIndentationIncrement.after(elementAccess.nameAssignment_1) c.setIndentationDecrement.before(elementAccess.endKeyword_4) // Rules for view: line breaks, indentation increment and decrement c.setLinewrap(1).after(viewAccess.forElementElementCrossReference_3_0) c.setLinewrap(1).after(viewRule) c.setIndentationIncrement.after(viewAccess.forElementElementCrossReference_3_0) c.setIndentationDecrement.before(viewAccess.endKeyword_6) //formatting for Member rule, only works, where Member rules are instantiated. c.setLinewrap(1).after(memberRule) } } Element规则以及View规则设置了格式。但是,Member规则的格式仅适用于实例化的(例如:Member声明)。当我运行这个格式化程序( Ctrl + Shift + F )时,我得到以下格式:

Element

但是我想让所有引用的成员都在一个新的行中。 如何实现这一目标?非常感谢任何帮助!

1 个答案:

答案 0 :(得分:1)

在View规则中引用了成员。你必须在这些成员之后设置linewrap:

    c.setLinewrap(1).after(viewAccess.forMembersAssignment_4)
    c.setLinewrap(1).after(viewAccess.forMembersAssignment_5)