2006-09-11

NetBeans Code Folding

Tired of lose the fight against NetBeans MDR stuff, I decided to check out Code Folding. Pretty easy, indeed. I haven't done a full code fold example, but I was able to use NB 'editor-fold' pseudo-tag. Considering that you have the syntax support (see NBP Tutorials), the steps are:

  1. On your SettingsInitializer, you need to add this line:
    settingsMap.put(SettingsNames.CODE_FOLDING_ENABLE, Boolean.TRUE);
    This is not obvious, but it is the most important step. Without it, you will never activate folding (this was hard to find out - like any other "non-layer.xml" stuff).

  2. On your EditorKit, you need to implement the SyntaxUpdateTokens class and put it as a document property. Like this:
    public class MyEditorKit extends NbEditorKit {
    [ ... ]

    protected void initDocument(BaseDocument doc) {
    doc.putProperty(SyntaxUpdateTokens.class, new SUT());
    }

    public class SUT extends SyntaxUpdateTokens {

    private List list;

    public void syntaxUpdateStart() {
    list = new ArrayList();
    }

    public List syntaxUpdateEnd() {
    return list;
    }

    public void syntaxUpdateToken(TokenID tid, TokenContextPath tcp,
    int offset, int len) {
    if (tokenID == MyTokenContext.LINE_COMMENT) {
    list.add(new TokenInfo(tid, tcp, offset, len));
    }
    }
    }
    }
    This will tell CustomFold which tokens it must use to find the pseudo-tag.

  3. You need to create a three-line class to add the sidebar to the editor:
    public class MyFoldingSideBarFactory implements SideBarFactory {
    public JComponent createSideBar(JTextComponent jTextComponent) {
    return new CodeFoldingSideBar(jTextComponent);
    }
    }
    This is a weird step. I don't know why there's no ready-to-go class to do this job.

  4. Now, glue everything in your layer.xml, under "Editor/MIME" folder:
    <folder name="SideBar">
    <file name="mypkg-MyFoldingSideBarFactory.instance"/>
    <attr name="org-netbeans-editor-GlyphGutter.instance
    /mypkg-MyFoldingSideBarFactory.instance"
    boolvalue="true"/>
    </folder>
    <folder name="FoldManager">
    <file name="org-netbeans-editor-CustomFoldManager$Factory.instance"/>
    </folder>
    FoldManager can be chained, just like the SideBar. Making it easy to add a "real" code fold based on tokens.

2006-09-09

First post - current misson brief

This is the first post. I don't have any ready tip yet, but I'll post my "current mission":

I'm trying to understand how to add a new language to NetBeans. I want everything Java Editor has: Code Completion, Code Folding, etc. In theory, it's simple: I just need to add editor support and create a metamodel to feed other subsystems.

The first part is well explained on NetBeans Platform web site (actually, it's incredibly simple to add syntax highlight), but there aren't good docs about how the "Java Language Model" works on NB.

And now my problem: I've created a metadata repository using NetBeans' MDR stuff, but I simply do not know how to populate the repository using source files (and how to create source files from the repository).