2

I'm programming for a product that includes snippets of Java (actually BeanShell) code embedded in larger XML files. These are executed on the fly at runtime. There can be more than one of these code tags at various levels throughout the document.

<larger-xml-file>
 <java>
  // java code that I want to syntax highlight
 </java>
 <more-xml...>
</larger-xml-file>

It would be great to allow basic syntax highlighting of the code within specific XML tags. I know that vi can do this with <script> tags inside of HTML, for example. It would really help me catch silly bugs like missing end quotes.

If it could allow completion or basic syntax checking, that would be even better.

Is there a way to easily configure this in Eclipse?

7
  • 1
    Thought about splitting xml and Java ? Although XML does not offer an include command you could use a preprocessor that combines the elements and creates a final XML. This has the advantage that you could run the separate Java code in Eclipse Commented Sep 20, 2018 at 15:41
  • That's one of the options we've considered. One of our best practices is to keep as much of the code as possible in compiled Java classes that get bundled with the app, and then just call those from the XML. Turns out deploying those requires restarting the application, so during development, it's often easier to just edit them directly in the XML, which can be hot-deployed. Commented Sep 20, 2018 at 15:48
  • I didn't mean to create jar files. Create a .java file, run it in Eclipse until it is working or at least has no syntax / compile errors. Then run your "preprocessor" that replaces the include "foo.java with the sourcecode and creates a new file. Hot deploy the created XML and see if it works. The preprocessor step chould be easily done with maven or any other build tool and you could even include the step where you copy the file for the app to hot deploy Commented Sep 20, 2018 at 15:54
  • 1
    Fair enough. I like it. I'll leave this open to see if there's a slick plugin-y way to do it, but that's definitely a great option. Commented Sep 20, 2018 at 15:54
  • 2
    There is the Eclipse project TM4E for syntax highlighting via TextMate grammars. You might have a look at the Eclipse aCute project which uses TM4E and where the HTML grammar references the JavaScript grammar. Commented Sep 21, 2018 at 7:20

1 Answer 1

4

This should be possible via the Eclipse project TM4E for syntax highlighting via TextMate grammars.

Eclipse Wild Web Developer which uses Eclipse TM4E shows how embedded/included/injected grammars work for JavaScript in HTML:

<extension point="org.eclipse.core.contenttype.contentTypes">
   <content-type
      base-type="org.eclipse.core.runtime.text"
      file-extensions="html"
      id="contentType.html"
      name="HTML"
      priority="low"/>
</extension>
<extension point="org.eclipse.ui.genericeditor.presentationReconcilers">
   <presentationReconciler
      class="org.eclipse.tm4e.ui.text.TMPresentationReconciler"
      contentType="contentType.html"/>
</extension>
<extension point="org.eclipse.ui.editors">
   <editor
      name="HTML Editor"
      icon="icons/html_editor_icon.png"
      class="org.eclipse.ui.internal.genericeditor.ExtensionBasedTextEditor"
      contributorClass="org.eclipse.ui.editors.text.TextEditorActionContributor"
      id="language-editor.html"
      default="true"
      extensions="html">
   <contentTypeBinding
      contentTypeId="contentType.html"/>
   </editor>
</extension>
Sign up to request clarification or add additional context in comments.

1 Comment

This looks promising. I'll give it a shot and then come back and accept the answer if it works!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.