Multi-language syntax-highlighting
-
I’m sure this is a pretty obscure context …
We have some XML files that contain embedded JavaScript. The files are part of a SmartCard authentication system. The main file consists of 740 lines of XML with 300+ lines of JavaScript plopped down in the middle of it. The XML portion of the file is properly highlighted. The JavaScript is contained in a CDATA section. Obviously it’s just text as far as the syntax highlighter is concerned.It would be really nice if there was a way to have NPP use the JavaScript highlighter on the JavaScript lines. Here’s a redacted version of how the JavaScript is embedded in the XML file:
<Setting Name="identitytransformer1" Type="htf:map" xmlns="http://higgins.eclipse.org/sts/Configuration"> <Setting Name="Provider" Type="xsd:string">com.some_combany.sso.idp.sts.ext.identitytransformer.x509.scripted.X509ScriptedMapperFactory</Setting> <Setting Name="X509CertificateToUserNameScript" Type="htf:jscriptexec"> <![CDATA[ // This section is JavaScript var USE_PIV = false ; var USE_CAC = true ; // ...etc... ]]> </Setting> </Setting>
-
In HTML, it is able to apply javascript rendering to embedded javascript, because it’s in the universally-understood
<script>
tag. In generic XML, there is no single tag that always means “this is a script, so use javascript”, so there’s no unambiguous decision indicator that the lexer could use in XML: not all CDATA in XML is guaranteed to be javascript (and, in fact, is quite likely to not be), so it cannot just say “all CDATA”; not every XML-based file will use aSetting
tag with theType="htf:jscriptexec"
attribute to indicate javascript. I don’t see how you could expect Notepad++ or any syntax highlighter to guess for you that it’s javascript, without highlighting non JS as JS for someone else, or not highlighting their JS which is indicated differently than in your circumstance.(Also, next time, please post in Help Wanted or General Discussion; the FAQ area where you tried to post is only used for presenting answers, not for asking questions.)
-
@PeterJones said in Multi-language syntax-highlighting:
not every XML-based file will use a Setting tag with the Type=“htf:jscriptexec” attribute to indicate javascript
True, but to be fair, the
jscript
part of theType
attribute does give a sensible starting point for language-specific formatting rules. Whether or not the feature is easy to implement this way is another story…Given that @Blastocystis focused more on the CDATA section of the sample code, I’m guessing that (s)he was hoping for a more generic secondary language feature - one that gives users flexibility with the secondary language used. So, instead of applying a single language (the one selected in the current Language menu) to an entire document, have a Secondary Language menu that is designed to work on subsections of the document (to be marked in a specific manner). I could see this being equally complex to implement, though.
Either way, perhaps it would make more sense to use/add a feature like Bookmarks to control where secondary language formatting is applied. In other words, give users the ability to directly choose which lines they want to format in another language by clicking on the margin.