Eclipse TM4E - Sharing features between code editors...
A good code editor
for a given language
(Java, JSON, XML, etc) must provide several features like:
completion
,hyperlink
,hover
,validation
- and
syntax highlighting
For several years now, each code editor has implemented those features for each language. Today the strategy for a code editor is to share those features with other code editors by supporting Language Server Protocol (LSP): completion, hyperlink, hover, and validation for a given language are managed with a language server. The code editor is the client which consumes the language server.
Atom, Eclipse IDE (with Eclipse LSP4E), Sublime Text, VSCode are code editor samples which support LSP.
But what about syntax highlighting
(as LSP doesn't support it), how to share
syntax highlighting
between code editor?
Syntax Highlighting - TextMate
The TextMate MacOS editor uses TextMate grammar to support syntax highlighting
in her editor. A TextMate grammar is a simple file which describes a language grammar with advanced RegExp.
A lot of code editors have the capability to consume a TextMate grammar to support syntax highlighting
like Atom, Sublime Text,and VSCode.
What is Eclipse TM4E?
Eclipse TM4E is the TextMate support for Eclipse IDE, it gives the capability to:
- Consume a TextMate grammar to support
syntax highlighting
in any Eclipse editor. - Consume a language configuration to support other features like
matching bracket
,auto close
,on enter support
.
TM4E - TextMate grammar
Once you have found a TextMate grammar file for your language, you can consume it inside Eclipse IDE with TM4E in two ways:
- user preferences
- plugin development
Imagine you want to have syntax highlighting
for Shell Script of your test.sh file:
#!/bin/csh echo compiling... cc -c foo.c cc -c bar.c cc -c qux.c cc -o myprog foo.o bar.o qux.o echo done.
You can download Shell-Unix-Bash.tmLanguage
User preferences
Here steps to follow to consume the shell TextMate grammar:
- Register content type of
*.sh
files:
- Register TextMate grammar of
*.sh
files:
Now you can open your sh file with Generic Text Editor and you should see:
Plugin development
- Defines a content type for your file:
<extension point="org.eclipse.core.contenttype.contentTypes"> <content-type base-type="org.eclipse.core.runtime.text" file-extensions="shell" id="com.youcompany.youapp.shell" name="Shell" priority="normal"> </content-type> </extension>
- Register your TextMate grammar and bind it with your content type:
<extension point="org.eclipse.tm4e.registry.grammars"> <grammar scopeName="source.shell" path="./syntaxes/Shell-Unix-Bash.tmLanguage" > </grammar> <scopeNameContentTypeBinding contentTypeId="com.youcompany.youapp.shell" scopeName="source.shell"> </scopeNameContentTypeBinding> </extension>
Now you can open your sh file with Generic Text Editor and you should see:
TM4E - Language configuration
VSCode Language configuration gives the capability to configure matching bracket character pair, etc for a language. TM4E is able to consume this language configuration to bind it with a content type. Today it supports only auto close and on enter support.
Here a sample of language configuration for JSON:
{ "comments": { "lineComment": "//", "blockComment": [ "/*", "*/" ] }, "brackets": [ ["{", "}"], ["[", "]"] ], "autoClosingPairs": [ { "open": "{", "close": "}", "notIn": ["string"] }, { "open": "[", "close": "]", "notIn": ["string"] }, { "open": "(", "close": ")", "notIn": ["string"] }, { "open": "'", "close": "'", "notIn": ["string"] }, { "open": "\"", "close": "\"", "notIn": ["string", "comment"] }, { "open": "`", "close": "`", "notIn": ["string", "comment"] } ] }
To bind the JSON content type with the language configuration, you can use the following extension point:
<extension point="org.eclipse.tm4e.languageconfiguration.languageConfigurations"> <languageConfiguration contentTypeId="content-type.of.json" path="language-configurations/json.language-configuration.json"> </languageConfiguration> </extension>
Here are two demos that show you the benefit with language configuration:
1. Without language configuration:
2. With language configuration:
Conclusion
Configuring syntax coloration for any language in Eclipse IDE is now possible with TM4E which consumes a simple TextMate grammar file. More and more projects are using the TM4E/LSP4E combo like Eclipse Corrosion and Eclipse BlueSky.
About the Author
