tm4e - angelozerr/eclipse-articles GitHub Wiki

Sharing features between Code Editor...

A good Code Editor for a given language (Java, JSON, XML, etc) must provide several features like:

  • completion, hyperlink, hover, validation
  • and syntax highlighting

Since several years, each Code Editor have implemented those features for each language. Today the strategy for a Code Editor is to share those features with other Code Editor by supporting Language Server Protocol (LSP) : completion, hyperlink, hover, 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 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 Editor have the capability to consume a TextMate grammar to support syntax highlighting like Atom, Sublime Text, VSCode

What is TM4E?

TM4E is the TexMate 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 find a TextMate grammar file for your language, you can consume it inside Eclipse IDE with TM4E with 2 means:

  • user preferences.
  • plugin development.

Imagine you wish 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 User TextMate Grammar

  • register TextMate grammar of *.sh files:

Register User TextMate Grammar

Now you can open your sh file with Generic Text Editor and you should see:

Generic Editor

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:

Generic Editor

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 2 demos which show you the benefit with language configuration:

  • without language configuration:

  • with language configuration:

⚠️ **GitHub.com Fallback** ⚠️