HTML Embedding for FreeMarker

When you click on @_sites, as you can see below, the IDE lets you open the file registered under that namespace, in this case, "/macros/sites.ftl":

But I'm thinking that any FreeMarker support would be better if it were embedded in the HTML Editor.

That means, the following needs to happen:

  1. Add this statement to the constructor of the FreeMarker data object, so that its MIME type is set to "text/html":
    getLookup().lookup(DataEditorSupport.class).setMIMEType("text/html");
    
    
  2. Create a language provider that will embed the FreeMarker MIME type within the HTML editor, if the current token ID is "TEXT", which is what unrecognized tokens in HTML, such as those provided by the FreeMarker support, are mapped to:

    @ServiceProvider(service = LanguageProvider.class)
    public class HTMLEmbeddingLanguageProvider extends LanguageProvider {
    
        private Language embeddedLanguage;
    
        @Override
        public Language<?> findLanguage(String mimeType) {
            return HTMLTokenId.language();
        }
    
        @Override
        public LanguageEmbedding<?> findLanguageEmbedding(Token<?> token, 
            LanguagePath languagePath, InputAttributes inputAttributes) {
            initLanguage();
            if (languagePath.mimePath().equals("text/html")) {
                if (token.id().name().equals("TEXT")) {
                    return LanguageEmbedding.create(embeddedLanguage, 0, 0, false);
                }
            }
            return null;
        }
    
        private void initLanguage() {
            embeddedLanguage = MimeLookup.getLookup("text/x-ftl").lookup(Language.class);
            if (embeddedLanguage == null) {
                throw new NullPointerException("Can't find language for embedding");
            }
        }
        
    }

  3. Change all the hyperlinkproviders. First, in the annotation at the top of the hyperlinkproviders, change from your MIME type (e.g., text/x-ftl) to text/html. Then, get your embedded language like this, from the HTML token sequence:
    
    TokenHierarchy<?> th = TokenHierarchy.get(doc);
    TokenSequence htmlTs = th.tokenSequence(Language.find("text/html"));
    if (htmlTs == null || !htmlTs.moveNext() && !htmlTs.movePrevious()) {
        return null;
    }
    TokenSequence<SJTokenId> ts = htmlTs.embedded();
    if (ts == null) {
        return null;
    }
    ts.move(offset);

The above gets you here, which is not perfect, but on the way to where it probably should be, i.e., HTML support within a FreeMarker editor:

Click to enlarge the image above to get a fuller perspective.

Comments:

This goes above and beyond my expectations of Netbeans freemarker support -- wonderful!

Do you think it'll be possible for end-users to use Netbeans with highlighting to edit freemarker templates for non-HTML files, like JSP, CSS, Java, or Resource Properties?

Posted by rgm on May 20, 2012 at 11:48 AM PDT #

Post a Comment:
  • HTML Syntax: NOT allowed
About

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.

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
12
13
14
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today