PAYATHON 2026

Eclipse Generic Code Editor 使用错误的 TextMate grammar

数据小陈

结论:在 Eclipse 2026-06 中,Generic Code Editor 选择 TextMate grammar 时取决于文件所属的 Content Type,不能只靠新加的 Mumps grammar 来决定。由于 .m 同时被 Mumps 和 Objective-C 注册,且 Objective-C 的 grammar 是写保护的,只添加 Mumps grammar 和 .m 的 Content Type 通常不能稳定覆盖它。若不能移除或禁用 Objective-C grammar,Eclipse UI 中没有可靠的“强制 .m 使用 Mumps grammar”开关;更稳妥的做法是避开扩展名冲突,或在独立 Eclipse 安装/配置中禁用 Objective-C grammar 提供者。

原因

  • Generic Code Editor 的语法高亮由 TextMate grammar 决定。
  • grammar 与 Content Type 关联,Content Type 再与 .m 扩展名关联。
  • 同一个扩展名关联多个 Content Type 时,最终选哪个 grammar 取决于 Eclipse/TM4E 的匹配与优先级;写保护的 Objective-C grammar 仍可参与匹配。
  • 在 preference page 里看到 Mumps grammar “可用”并不等于它会优先用于 .m。

可尝试的步骤

  1. 先确认冲突点:打开 Preferences > General > Content Types,在树中找到 Mumps 和 Objective-C 相关类型,检查 .m 是否同时出现在两者下。再到 TextMate Grammar 页面确认 Mumps grammar 已绑定到 Mumps Content Type。
  2. 尝试移除 Objective-C Content Type 下的 .m 关联。如果 Remove 按钮灰色或提示写保护,说明这条路径被插件锁定,不要继续靠重复添加 Mumps 关联解决。
  3. 在 Preferences > General > Editors > File Associations 中为 *.m 添加 Generic Code Editor 并设为默认。这只能保证用哪个编辑器打开,不能保证 TextMate grammar 选对。
  4. 如果项目允许,给 Mumps 文件使用不冲突的扩展名,例如 .mumps,再将该扩展名关联到 Mumps Content Type 和 Mumps grammar。这是最可控的规避方式。
  5. 如果必须保留 .m,考虑卸载/禁用提供 Objective-C grammar 的插件或使用不含该 grammar 的独立 Eclipse 安装/工作空间。可在 Help > About Eclipse IDE > Installation Details > Installed Software 中查找提供者;具体插件名以你的安装为准。修改后重启 Eclipse,必要时用 eclipse -clean 启动一次排除缓存影响。

如果允许自建插件,可尝试用更高优先级的 Content Type 参与匹配,但能否覆盖写保护 grammar 取决于 TM4E 的匹配策略,需要实测:

<extension point='org.eclipse.core.contenttype.contentTypes'>
  <content-type id='com.example.mumps.contentType'
                name='Mumps Source'
                base-type='org.eclipse.core.runtime.text'
                priority='high'>
    <file-extensions>m</file-extensions>
  </content-type>
</extension>

注意事项

  • 不要直接改 Eclipse 安装目录里的插件文件来删除写保护 grammar,升级或校验后容易失效。
  • 多个 Content Type 共用 .m 时,单纯在 TextMate Grammar preference page 里添加 grammar 不会自动改变优先级。
  • 如果必须多人协作,建议统一使用非冲突扩展名或统一 Eclipse 配置,避免每个人本地的 grammar 选择不一致。

备注:内容仅供参考。