Apache NetBeans
Apache NetBeans
Latest release

Apache NetBeans 25

Download

Netbeans Rcp Antlr Format And Indent

I do not have the formatting and indenting fully working yet, but I did figure out the integration points. It took me almost a full day to figure out how to just get that working so I figure it was worth at least starting a tutorial.

It is actually quite simple! And, once you have things registered correctly, the next step (to be documented later) will be to use your ANTLR Lexer to do the formatting.

This is part of the ANTLR integration tutorials.

IndentTask

1.

The indent task will make a callback to the reindent() method when the enter key is pressed while editing. The Context object contains everything that you will need, including the editor document object. It should just be a matter of taking the text after the cursor and before the next line and indenting things properly.

public class SqlIndentTask implements IndentTask {

    private Context context;

    SqlIndentTask(Context context) {
        this.context = context;
    }

    public void reindent() throws BadLocationException {
        System.out.println("we will indent this now");
    }

    public ExtraLock indentLock() {
        return null;
    }

}

2.

The IndentTask.Factory is the hook for Netbeans to use your IndentTask.

public class SqlIndentTaskFactory implements IndentTask.Factory {

    public IndentTask createTask(Context context) {
        return new SqlIndentTask(context);
    }

}

3.

Lastly, register the IndentTask with the layer.xml file.

<folder name="Editors">
    <folder name="text">
        <folder name="x-sqlr">
            <file name="com-sqlraider-editor-format-SqlIndentTaskFactory.instance"/>
        </folder>
    </folder>
</folder>

ReformatTask

1.

The reformat task will make a callback to the reformat() method when you invoke the built in Netbeans format command. In other words when you use the menu Source → Format action or press Alt+Shift+F. The Context object contains everything that you will need, including the editor document object. It should just be a matter of taking the text and running it through the ANTLR Lexer to format things properly.

public class SqlReformatTask implements ReformatTask {

    private Context context;

    public SqlReformatTask(Context context) {
        this.context = context;
    }

    @Override
    public void reformat()
            throws BadLocationException {
        System.out.println("we will format this now");
    }

    @Override
    public ExtraLock reformatLock() {
        return null;
    }

}

2.

The ReformatTask.Factory is the hook for Netbeans to use your ReformatTask.

public class SqlReformatTaskFactory implements ReformatTask.Factory {

    public ReformatTask createTask(Context context) {
        return new SqlReformatTask(context);
    }
}

3.

Lastly, register the ReformatTask with the layer.xml file.

<folder name="Editors">
    <folder name="text">
        <folder name="x-sqlr">
            <file name="com-sqlraider-editor-format-SqlReformatTaskFactory.instance"/>
        </folder>
    </folder>
</folder>