Friday Dec 28, 2012

Perl Code Folding

I took some first steps in working on Perl code folding for the Perl/NetBeans project that Sudeep Hazra is working on.

You can see the first signs of code folding above, though not completely correct yet.

The main class I changed was this one, as follows, giving you a lot of default functionality out of the box:

import org.netbeans.api.lexer.Language;
import org.netbeans.modules.csl.spi.DefaultLanguageConfig;
import org.netbeans.modules.csl.spi.LanguageRegistration;
import org.netbeans.perl.lexer.PerlTokenId;

@LanguageRegistration(mimeType = "text/x-perl")
public class PerlLanguageProvider extends DefaultLanguageConfig {

    public Language getLexerLanguage() {
        return PerlTokenId.getLanguage();

    public String getDisplayName() {
        return "Perl";
//    @Override
//    public Language findLanguage(String mimeType) {
//        if ("text/x-perl".equals(mimeType)){
//            return new PerlLanguageHierarchy().language();
//        }
//        return null;
//    }
//    @Override
//    public LanguageEmbedding findLanguageEmbedding(Token token, LanguagePath lp, InputAttributes ia) {
//         return null;
//    }

Next, unrelated but helpful, the brace matcher can be made to work quite simply by doing the following, which means that two classes can be deleted:

import org.netbeans.spi.editor.bracesmatching.BracesMatcher;
import org.netbeans.spi.editor.bracesmatching.BracesMatcherFactory;
import org.netbeans.spi.editor.bracesmatching.MatcherContext;

@MimeRegistration(mimeType="text/x-perl", service=BracesMatcherFactory.class)
public class PerlBracesMatcherFactory implements BracesMatcherFactory {
    public BracesMatcher createMatcher(MatcherContext context) {
        return BracesMatcherSupport.defaultMatcher(context, -1, -1);

The Editors/text/x-perl section of the layer needs to be rewritten to the following:

 <folder name="Editors">
    <folder name="text">
        <folder name="x-perl">
            <attr name="displayName" bundlevalue="org.netbeans.perl.file.Bundle#Editors/text/x-perl"/>
            <folder name="FoldManager">
                <file name="org-netbeans-editor-CustomFoldManager$Factory.instance"/>
            <file name="org-netbeans-perl-parser-PerlParserFactory.instance"/>
            <file name="org-netbeans-perl-parser-SyntaxErrorsHighlightingTaskFactory.instance"/>
            <folder name="FontsColors">
                <folder name="NetBeans">
                    <folder name="Defaults">
                        <file name="org-netbeans-perl-file-FontAndColors.xml" url="FontAndColors.xml">
                            <attr name="SystemFileSystem.localizingBundle" bundlevalue="org.netbeans.perl.file.Bundle"/>

Surprised by any of the above information? Ha. That means you haven't followed the related tutorials:

Do yourself a favor and follow the above two tutorials before even thinking about creating your own editor on top of the NetBeans IDE APIs. 

Next, I'm working on the FoldManager and its factory, which I so far have based on the following:

More soon. I'm aiming to have quite some fun documentation relating to code folding, which is one of the NetBeans IDE APIs about which sporadically (since, after all, how many people in the world are creating editors?) questions arise, soon!


Geertjan Wielenga (@geertjanw) is a Principal Product Manager in the Oracle Developer Tools group living & working in Amsterdam. He is a Java technology enthusiast, evangelist, trainer, speaker, and writer. He blogs here daily.

The focus of this blog is mostly on NetBeans (a development tool primarily for Java programmers), with an occasional reference to NetBeans, and sometimes diverging to topics relating to NetBeans. And then there are days when NetBeans is mentioned, just for a change.


  • NetBeans IDE
« December 2012 »