如何将TM4E(TextMate)接入Eclipse?求相关文档教程
基于Eclipse TM4E实现插件的正则语法高亮
1. 引入TM4E依赖
在插件的MANIFEST.MF中添加必要的TM4E核心及UI依赖:
Require-Bundle: org.eclipse.tm4e.core;bundle-version="0.1.0", org.eclipse.tm4e.ui;bundle-version="0.1.0", org.eclipse.tm4e.languageconfiguration;bundle-version="0.1.0"
版本号根据你使用的TM4E实际版本调整。
2. 编写TextMate语法定义文件
创建.tmLanguage格式的XML文件,用正则表达式定义语法规则。比如针对自定义配置文件,实现注释和变量的高亮:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>name</key> <string>MyConfig</string> <key>scopeName</key> <string>source.myconfig</string> <key>fileTypes</key> <array> <string>mycfg</string> </array> <key>patterns</key> <array> <!-- 注释高亮规则 --> <dict> <key>name</key> <string>comment.line.myconfig</string> <key>match</key> <string>#.*$</string> </dict> <!-- 变量高亮规则 --> <dict> <key>name</key> <string>variable.other.myconfig</string> <key>match</key> <string>\$\w+</string> </dict> </array> </dict> </plist>
其中match字段就是你要使用的正则表达式,name对应的是语法作用域,后续会关联颜色主题。
3. 注册语法到TM4E
在插件的plugin.xml中扩展org.eclipse.tm4e.core.grammar扩展点,将你的语法文件注册到TM4E:
<extension point="org.eclipse.tm4e.core.grammar"> <grammar grammarURI="platform:/plugin/com.your.plugin.id/grammars/MyConfig.tmLanguage" id="myconfig.grammar" name="MyConfig" scopeName="source.myconfig"> </grammar> </extension>
替换com.your.plugin.id为你实际的插件ID,grammarURI指向你存放.tmLanguage文件的路径。
4. 关联编辑器与语法高亮
如果是自定义编辑器,需要配置源码查看器的高亮规则。可以继承TextMateSourceViewerConfiguration,或者在自定义的ISourceViewerConfiguration中集成TM4E的高亮:
public class MyConfigSourceViewerConfiguration extends TextMateSourceViewerConfiguration { public MyConfigSourceViewerConfiguration(IPreferenceStore preferenceStore) { super(preferenceStore); } @Override protected ITextMateLanguageConfiguration getLanguageConfiguration() { return TextMateCorePlugin.getGrammarRegistry().getLanguageConfiguration("source.myconfig"); } }
然后在编辑器创建SourceViewer时,使用这个配置类即可。
如果是给已有编辑器(比如普通文本编辑器)添加高亮,可通过扩展org.eclipse.tm4e.ui.textmateLanguageAssociation关联文件类型和语法:
<extension point="org.eclipse.tm4e.ui.textmateLanguageAssociation"> <languageAssociation fileType="mycfg" scopeName="source.myconfig"> </languageAssociation> </extension>
5. 自定义颜色主题(可选)
如果默认主题的颜色不符合需求,可扩展org.eclipse.tm4e.ui.themes定义自己的颜色映射:
<extension point="org.eclipse.tm4e.ui.themes"> <theme id="com.your.plugin.id.mytheme" label="My Config Theme"> <scopeSetting scope="comment.line.myconfig" foregroundColor="0x808080"> </scopeSetting> <scopeSetting scope="variable.other.myconfig" foregroundColor="0x0000FF"> </scopeSetting> </theme> </extension>
内容的提问来源于stack exchange,提问作者Nikolai
相关产品推荐
相关产品推荐

