2

Notepad++ works with Scintilla lexer to recognize switches in language within a .php file somehow. It seems to default to HTML and recognizes <?php ... ?>, and <script type="text/javascript">...</script> as delimiters for embedded PHP and javascript languages and thus applies the correct syntax highlighting and code completion.

Question: is it possible to get it to do the same for SQL, perhaps with the heredoc delimiters such as <<<sql ... sql?

I searched the web and notepad++ forums without success. UDLs work on the content of a file based on its extension, which doesn't help because I'm specifically looking for code embedded in .php files. I also tried digging in the files in Notepad's ProgramData folder but couldn't find anything defining the language switching delimiters.

The Notepad plugins for SQL all want to format the entire content of the file as sql, so that doesn't help either, I just want it to work with the embedded sections only. In fact, I'd rather find a solution that works without plugins.

Edit: sample code as requested

<p>Some html code here</p>
<?php
$value1 = 1234;
$sql = <<<sql
select * from table1
where column1 = $value1
sql;
$rows = mysqli->query($sql);
?>
<script type="text/javascript">
var results = <?php echo queryToJSresults($rows);?>;
</script>

Everything is syntax highlighted correctly in Notepad++. Want I want is to syntax highlight the bit between <<<sql and sql using the language SQL. Currently it is sees it (rightly so) as a string literal, same as a quoted string.

2
  • Notepad++ plugin SQLinForm doesn't work by the way, as the current version insists on applying its style to the entire script despite selecting "format selected SQL". Also it has its own parser and doesn't apply the NPP style I use for SQL files. Dec 13, 2019 at 6:26
  • Maybe you can add an small example file showing the different embedded language sections? Dec 14, 2019 at 12:38

4 Answers 4

2
+25

What you to do is such a Dificult thing to do. Can be done with the user-defined-language (UDL) tool but I think will no get the exact result that you expect.

Let's supose you want to open a .php file extension and get de following line:

$result = mysql_query('SELECT * WHERE 1=1');

This line has a valid SQL sentence, but this sentence is a string, and UDL recognizes it as a string, prior to mark SELECT or WHERE

You could create a brand-new UDL, exactly the same as native Notepad++ for PHP does, but discard the highlighting of strings. And after that, adding reserved SQL Keys to highlight (SELECT, FROM, IN and so on).

PRO: GET SQL Highlighted

CON: LOST strings highlight in the whole file

For me, the cost is major than the gain

Add special marks as you propose

<<< sql ... sql?

This will make the file to not compile, so I'd discard this option.

2
  • The "special marks" are a standard part of PHP and already in my code. In PHP they are know as "heredoc" string delimiters. You solution is not too bad, but would require that one could build a UDL for HTML+PHP+JavaScript+SQL with keywords for all, and one color scheme for all. Thanks your suggestion I will give it some consideration before I mark it as an answer. Dec 14, 2019 at 15:23
  • Reading stackoverflow.com/questions/8575865/… made me realize I would lose so much functionality, that I agree with you we can discard the option Dec 14, 2019 at 16:00
2

In past I was thinking about something similar, but I gave up, as Notepad++ has no API or configuration for different language in another language format.

See search results for <?php in the source code: https://github.com/notepad-plus-plus/notepad-plus-plus/search?l=C%2B%2B&q=%22%3C%3Fphp%22

After a couple of another searches I found out: - PHP has no proper lexer, it highlights only basic things like strings (i.e. something enclosed in single/double quotes, heredoc, nowdoc, ...), variables (i.e. something with $ sign), keywords, ... you get the point. See https://github.com/notepad-plus-plus/notepad-plus-plus/blob/f02d166081fd5fcf7af14be1174910b2ec4f5656/PowerEditor/src/langs.model.xml for heredoc definition. - SQL/MySQL has a proper lexer

Based on this quick research you can not rewrite/generate the php rules to fully support SQL or any other lexer in it by only editing the rules files.

The only possibility is dig into the Notepad++ code directly (or an extension). Then find a ways how to:

  1. find a string part in PHP
  2. how to pass the string part to the SQL lexer and use it as a highligter for that part

I hope I helped you. If you will discover an easy way I will be very glad and you can even submit a PR for it to parse SQL in PHP heredoc/nowdoc.

1
  • 1
    Thanks for your feedback @Mvorrisek. I have since joined the scintilla forum and suggested it as a feature. First prize is to get them to do the change. I also downloaded the code to scintilla and notepad++. From my limited knowledge of C++ and reading their code it seems the language-change needs to be triggered from scintilla. I also had a look at writing a plugin for NPP but haven't got too far. It is good to know others would find the feature useful. Dec 16, 2019 at 7:38
0

Notepad++ only recognizes embedded languages in certain circumstances (like javascript inside of php or javascript inside html), and those circumstances are hard-coded. There is no way to change that behavior or add additional embedded languages.

Personally, I use MySQL Workbench to construct and test my queries, and then move them into my server-side code once they are ready.

MySQL Workbench does a great job at syntax highlighting, auto-complete, etc.

And since your are connected directly to the database, it will auto-complete column names, table names, etc.

4
  • How are you so sure there is "no way to change that"? That is the purpose of my quest, one which I wouldn't give up so easily. Someone could even find a way of hacking the DLL. I have in fact done exactly as you suggest in the past, but I find that tedious, and often the SQL code itself has PHP embedded in it which defeats the exercise. Having the code properly colored allows one to spot errors quickly, without having to break the workflow and use some other tool. I'm going to down-vote this as a non-answer in the category "can't be done, do something else" Dec 13, 2019 at 6:06
  • Let me rephrase that - there is no way to change that using the current capabilities of Notepad++. It is hard-coded into the software. I would not consider "hacking the DLL" a reasonable solution for this problem. However, if you are really stuck on making this happen, Notepad++ is open source. You can grab a copy of the source code from here - github.com/notepad-plus-plus/notepad-plus-plus - make the changes you want, and recompile it. Heck, if you get it working properly, you can even submit your changes for inclusion in the next release.
    – Tullio
    Dec 13, 2019 at 17:28
  • For what it's worth, I believe the source code file that controls this is "notepad-plus-plus-master\scintilla\lexers\LexHTML.cxx". You can see on line 569 it adds Javascript, VBScript, and other language keywords to the mix.
    – Tullio
    Dec 13, 2019 at 17:49
  • Well yes, one could recode Notepad++ and/or scintilla if that is required. I'll give the bounty to whomever comes up with the solution code. Dec 14, 2019 at 15:07
0

My solution until NotePad++ and/or Scintilla developers come up with an enhancement is to copy the sql-code into a new tab and select SQL as the language.

For those who would like to do the same, here is a macro to do that. Paste this into your shortcuts.xml file found in the folder where you set NPP+ to store your configuration files (%appdata%\notepad++ on windows by default). Select the sql-code then press the shortcut key Ctrl~ (you can change that to suit your needs).

<Macro name="SQL view" Ctrl="yes" Alt="no" Shift="no" Key="192">
    <Action type="0" message="2178" wParam="0" lParam="0" sParam=""/>
    <Action type="0" message="2025" wParam="0" lParam="0" sParam=""/>
    <Action type="0" message="2422" wParam="0" lParam="0" sParam=""/>
    <Action type="0" message="2325" wParam="0" lParam="0" sParam=""/>
    <Action type="2" message="0" wParam="41001" lParam="0" sParam=""/>
    <Action type="0" message="2179" wParam="0" lParam="0" sParam=""/>
    <Action type="2" message="0" wParam="46020" lParam="0" sParam=""/>
</Macro>

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.