Notch

Notch is a plugin for eclipse that extends the IDE with groovy scripts.

See notch-scripts repository for some examples

Eclipse Templates with the Power of Groovy

notch extends the eclipse code template feature with the ability to run groovy code after the template is run. Take a look at these examples

Method Generation

Template a standard private java field. Once the template is finished the getter and setter methods are automatically added to the end of your class. Notice the template variables are available for reference in the groovy closure allowing for programmatic manipulation of the values the user entered

    java.template("private \${Type} \${var};", {
        java.addMethod("public void set" + var.substring(0,1).toUpperCase() + var.substring(1) + "(" + Type + " " + var +") { this." + var + "=" + var + "; }")
        java.addMethod("public " + Type + " get" + var.substring(0,1).toUpperCase() + var.substring(1) + "() { return this." + var + ";}")
    });

Field Generation

This template adds a standard slf4j logger statement. If the logger field isn't available, the field is automatically generated at the top of your class. If the field is already there then just the template is invoked.

    java.addImport("org.slf4j.Logger");
    java.addImport("org.slf4j.LoggerFactory");
    java.addFieldFirst("private static final Logger LOGGER = LoggerFactory.getLogger(" + java.className +");");
    java.template("LOGGER.info(\"\${msg}\");\${cursor}");

Nest Templates

This template is just for the field type and then automatically infers the variable name (similar to eclipse proposals). Just enter the type name and the variable name will get automatically generated. template calls can be nested.

    java.template("\${Type} \${cursor}", {
        java.template(Type.toLowerCase() + "= new " + Type + "(\${arg1});");
    })

Auto Expansion

Auto expand previous text

Consider this junit example. User has entered

    String a;
    String b;

They add

    String a;
    String b;
    a > b

Then do CTRL+ENTER (or other hotkey) at the end of the line. The code gets automatically expanded to (with imports)

    String a;
    String b;
    assertThat(a, greaterThan(b));

All with the following script (shortened to just > and = for brevity)

    def line = text.getPrecedingLineText();

    String [] macros = line.split(" ");
    if (macros.length == 3) {
        String operator;
        java.addStaticImport("org.junit.Assert.assertThat");
        text.removePrecedingLineText();
        if (macros[1] == "=") {
            java.addStaticImport("org.hamcrest.CoreMatchers.equalTo");
            java.template("assertThat(" + macros[0] + ",equalTo(" + macros[2] + "));");
        } else if (macros[1] == ">") {
            java.addStaticImport("org.hamcrest.Matchers.greaterThan");
            java.template("assertThat(" + macros[0] + ",greaterThan(" + macros[2] + "));\${cursor}");
        }
    }

Test Generation and Quick Switch

This example generates a test class automatically for the currently selected java class and opens it. If a test already exists, then it is simply opened. Running the script from the test class will shift focus back to the class under test.

    String packageName = java.packageName;
    String content =  "package " + packageName + ";"
    String className = java.name + "Test";
    if (java.name.contains("Test")) {
        javaproject.addFile(java.relativeName.replace("test","src").replace("Test.java", ".java"),content);
        return;
    }

    content = content + "\n" + "public class " + className + "{\n}";
    javaproject.addFile(java.relativeName.replace("src","test").replace(".java", "Test.java"),content);

    java.addImport("org.junit.Test");

Hotkeys

All commands/templates can be registered to hotkeys allowing for quick access.

API

Text Binding

    /**
     * Adds text and then positions the cursor at the end of the text
     * 
     * @param text to insert
     */
    void addText(String text);

    /**
     * @return the text preceding the current cursor position
     */
    String getPrecedingLineText();

    /**
     * Removes and returns the preceding line of text. Cursor will be positioned at the indentation of the line
     * 
     * @return the preceding line text that was removed
     */
    String removePrecedingLineText();

Java Binding

The following api can be used with the 'java' binding. e.g. java.addImport("some.Example")

    /**
     * Adds an import to a java class
     * @param import the fully qualified class name to add as an import
     * @return the eclipse import declaration that was created
     */
    void addImport(String contents);

    /**
     * Adds a constructor to a java class. The constructor is automatically added
     * at the top of the class after any fields and before any methods are defined
     * 
     * @param constructor the full java code that defines the constructor
     * @return the eclipse method that was created
     */
    void addConstructor(String contents);


    /**
     * Adds a method to a java class. The method is automatically added
     * at the bottom of the class
     * 
     * @param constructor the full java code that defines the method
     * @return the eclipse method that was created
     */
    void addMethod(String contents);



    /**
     * Adds a method to a java class. The method is automatically added
     * at the top of the class
     * 
     * @param constructor the full java code that defines the method
     * @return the eclipse method that was created
     */
    void addFieldFirst(String contents);


    /**
     * Adds a method to a java class. The method is automatically added
     * at the bottom of the class
     * 
     * @param constructor the full java code that defines the method
     * @return the eclipse method that was created
     */
    void addFieldLast(String contents);


    /**
     * Creates a template and runs the closure at completion.
     * 
     * @param template the eclipse template
     * @param closure the closure to run after the template is finished
     */
    void template(String template, Closure closure);
    void template(String template);
  }

Getting Started

Install from the Neon update site OR Install from the Mars update site

Clone the notch-scripts repository for some examples

Configure the 'Notch' preference page to point at the scripts

Add your hotkeys (all commands are listed as their filename) and you are ready to go.