Eclipse中UI装饰器顺序由何决定?如何自定义后缀装饰器排序?
解决Eclipse后缀装饰器的顺序问题
这个问题我之前也踩过坑——Eclipse的装饰器顺序根本不是靠plugin.xml里的扩展排列顺序来控制的,核心是要给每个装饰器显式设置**priority属性**来指定执行顺序。
具体操作步骤
- 打开你的
plugin.xml,找到两个org.eclipse.ui.decorators扩展的定义节点。 - 给每个
<decorator>元素添加priority属性:- 你希望先显示的装饰器(也就是输出
[READONLY]的那个)设置更高的数值(比如200) - 另一个装饰器设置较低的数值(比如100)
- 你希望先显示的装饰器(也就是输出
为什么要这么设置?因为Eclipse中装饰器的执行顺序是按priority从高到低来的:数值越大,装饰器越先被调用。先执行的装饰器会把它的后缀先追加到原标签上,之后执行的装饰器再追加自己的内容,这样就能得到你想要的com.mycorp.mypackage - [READONLY][mysource.xml]顺序。
配置示例
只读装饰器(高优先级,先执行)
<extension point="org.eclipse.ui.decorators"> <decorator id="com.mycorp.readOnlyDecorator" class="com.mycorp.decorators.ReadOnlyDecorator" label="Read Only Decorator" lightweight="true" priority="200"> <enablement> <!-- 这里填写你的装饰器启用条件,比如针对包资源的匹配规则 --> </enablement> </decorator> </extension>
源文件装饰器(低优先级,后执行)
<extension point="org.eclipse.ui.decorators"> <decorator id="com.mycorp.mySourceDecorator" class="com.mycorp.decorators.MySourceDecorator" label="My Source Decorator" lightweight="true" priority="100"> <enablement> <!-- 对应的启用条件 --> </enablement> </decorator> </extension>
额外说明
- 你之前调整扩展列表顺序没用,是因为Eclipse完全不依赖这个顺序来执行装饰器——只有当两个装饰器的
priority值相同时,才会 fallback 到扩展的定义顺序,但这种情况非常不可靠,一定要显式设置priority。 - 不管你用的是轻量级装饰器(
lightweight="true",实现ILightweightLabelDecorator)还是传统的ILabelDecorator,这个priority属性都适用。 - 注意:如果用户在Eclipse偏好设置(
Window > Preferences > General > Appearance > Label Decorations)里手动调整过装饰器顺序,会覆盖你在扩展里设置的priority。如果需要强制顺序,可以在装饰器代码中做额外处理,但大部分场景下,显式设置priority已经足够解决问题。
内容的提问来源于stack exchange,提问作者Adam Horvath
相关产品推荐
相关产品推荐

