Skip to main content
Jump to: navigation, search


< Efxclipse
Revision as of 04:41, 29 February 2016 by (Talk | contribs) (Editor Registration)

e(fx)clipse provides a SmartCode-Editing Framework who can be embedded in fairly any application (not only OSGi)

Integration into OSGi


Before adding the source you need to configure your application to support the smart code editor. To make that happen you need to add the following bundle to a feature or create a new one:

  • org.eclipse.fx.code.editor
  • org.eclipse.fx.code.editor.configuration
  • org.eclipse.fx.code.editor.configuration.text
  • org.eclipse.fx.code.editor.configuration.text.e4
  • org.eclipse.fx.code.editor.configuration.text.fx
  • org.eclipse.fx.code.editor.e4
  • org.eclipse.fx.code.editor.fx
  • org.eclipse.fx.code.editor.fx.e4

Basic Control with Syntax Highlighting


To make it easier to define syntax highlighting for any language the smart-code framework uses unlike the Eclipse IDE a declarative language named ldef.

The first step when integrating a syntax highlighting editor into your application is to create a file ending with .ldef (eg java.ldef, ...).

package my.plugin
js {
  partitioning {
    partition __dftl_partition_content_type
    partition __js_single_line_comment
    partition __js_multi_line_comment
    partition __js_string
    partition __js_regex
    rule {
      single_line __js_single_line_comment  "//"  => ''
      multi_line __js_multi_line_comment    "/*"  => "*/"
      single_line __js_string               "'"   => "'" escaped by "\\"
      single_line __js_string               '"'   => '"' escaped by "\\"
      single_line __js_regex                '/'   => '/' escaped by "\\"
  lexical_highlighting {
    rule __dftl_partition_content_type whitespace javawhitespace {
      default js_default
      js_operator {
        character [ 
          ';', '.', '=', '/', '\\', '+', '-', '*', 
          '<', '>', ':', '?', '!', ',', '|', '&', '^', '%', '~' 
      js_bracket {
        character [ '(', ')', '{', '}', '[', ']' ]
      js_keyword {
        keywords [
          "break", "case", "catch", "continue",
          "debugger","default",	"delete", "do",
          "else", "finally", "for", "function",
          "if", "in", "instanceof", "new",
          "return", "switch", "this", "throw",
          "try", "typeof", "var", "void",
          "while", "with" 
      js_constant {
        keywords [ "true", "false", "undefined" ]
      js_number {
        pattern "\\d" containing "[\\d|\\.]"
    rule __js_single_line_comment {
      default js_doc_default
    rule __js_multi_line_comment {
      default js_doc_default
    rule __js_string {
      default js_string
    rule __js_regex {
      default js_string
    token_def {
      js_default "-source-editor-code";
      js_operator "-source-editor-operator";
      js_bracket "-source-editor-bracket";
      js_keyword "-source-editor-keyword" bold;
      js_doc_default "-source-editor-doc";
      js_string "-source-editor-string";
      js_constant "-source-editor-keyword" bold;
      js_number "-source-editor-number";

As the smart code editor framework is built on top of the EclipseText-Framework you notice that the DSL aligns heavily to the concepts you find there:

  • Documents are first split in partitions (eg source-code, documentation, strings, ...)
  • Single Partitions are then split in tokens like constants, reserved words, ...
  • Tokens are then associated with styling information (color, font-weight,...)

Once you created an ldef-File for your favorite language the Eclipse-Tooling will generate a total of 3 files in the src-gen folder:

  • $language$.json: Configuration data loaded at runtime and used to configure Eclipse Text
  • $language$.css: Styleing information when using JavaFX as the renderer
  • $language$-swt-style.json: Styleing information when using SWT as the renderer (we are not looking into this feature in this document)


To integrate a smart editor into your application you need to add dependencies to the following bundles your MANIFEST.MF:

  • org.eclipse.fx.code.editor
  • org.eclipse.fx.code.editor.configuration
  • org.eclipse.fx.code.editor.configuration.text

Or if you prefer the following packages:

  • org.eclipse.fx.code.editor
  • org.eclipse.fx.code.editor.configuration
  • org.eclipse.fx.code.editor.configuration.text

Editor Registration

From the DSL we generated various artifacts are generated and we need to register them in the framework:

  • $language$.json file is registered through a org.eclipse.fx.code.editor.configuration.text.ConfigurationModelProvider OSGi-Service-Component
  • $language$.css file is registered through a OSGi-Service-Component

Samples for JavaScript could look like this:


public class JavaScriptConfigurationProvider implements ConfigurationModelProvider {
  public boolean applies(Input<?> input) {
    return ((URIProvider)input).getURI().endsWith(".js");
  public LanguageDef getModel(Input<?> input) {
    try(InputStream in = getClass().getResourceAsStream("js.json");
        Reader r = new InputStreamReader(in)) {
      return EditorGModel.create().createObject(r);
    } catch (IOException e) {
      throw new RuntimeException(e);


public class JavaScriptStylesheet implements Stylesheet {
  public boolean appliesToTheme(Theme t) {
    return true;
  public URL getURL(Theme t) {
    try {
      return new URL("platform:/plugin/my.plugin/my/plugin/js.css");
    } catch (MalformedURLException e) {
      throw new RuntimeException(e);

Syntax Highlighting

Open an editor

Using the editor opener service

The smart code framework by default publishes an who can be retrieved via injection and used to open an editor based on an URI like this

class MyComponent {
  EditorOpener opener;
  private void openFile(File f) {
    opener.openEditor( t.toURI().toURL().toExternalForm() );

The default implementation of the opener function will search the window/perspective for a MElementContainer tagged with editorContainer, so if you want to use that service you need to update your application model and tag eg one of your MPartStacks with it.

Doing it yourself

If you don't want to use the EditorOpener-Service but do things yourself all you need to do is to create an MPart instance like this:

EModelService modelService = // ...;
MPart part = modelService.createModelElement(MPart.class);
part.getPersistedState().put(org.eclipse.fx.code.editor.Constants.DOCUMENT_URL, uri);
// ...

Adding support for Autocomplete Features

Adding support for Error-Markers

Back to the top