Is it possible to configure Eclipse to highlight Java syntax within XML?

馋奶兔 提交于 2021-02-08 04:41:41

问题


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?


回答1:


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:

  • HTML grammar includes the JavaScript grammar
  • Via extension points both grammars have to be registered: HTML and JavaScript
  • Register the file extension .html for the Generic Text Editor:
<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>


来源:https://stackoverflow.com/questions/52428583/is-it-possible-to-configure-eclipse-to-highlight-java-syntax-within-xml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!