问题
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