Bug 27747: Add CodeMirror custom syntax highlighting for column placeholders

This patch adds some additional configuration to CodeMirror so that
column placeholders have a distinct color in the CodeMirror SQL editor.

To test, apply the patch and create or edit an SQL report which contains
one or more column placeholders, e.g. [[itemnumber|Item number]],
[[borrowernumber|Borrower number]], etc.

Confirm that when editing the SQL, these placeholders should appear as
red text. Save your report and view it. The syntax highlighting should
be updated in this view too.

Signed-off-by: Lucas Gass <lucas@bywatersolutions.com>

Signed-off-by: Lucas Gass <lucas@bywatersolutions.com>

Signed-off-by: Nick Clemens <nick@bywatersolutions.com>

Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
This commit is contained in:
Owen Leonard 2021-02-22 12:36:08 +00:00 committed by Jonathan Druart
parent 601a969915
commit 3c8be8bf7a

View file

@ -47,6 +47,9 @@
.cm-sqlParams {
color: #11917B;
}
.cm-columnPlaceholder {
color: #BF2D5D;
}
#mana_search_errortext { font-family: monospace; font-weight: bold; }
</style>
[% Asset.css("css/reports.css") | $raw %]
@ -1420,22 +1423,32 @@
}
/* overlay a syntax-highlighting definition on top of the existing sql one */
CodeMirror.defineMode("sqlParams", function(config, parserConfig) {
var sqlParamsOverlay = {
CodeMirror.defineMode("sqlPlaceholders", function(config, parserConfig) {
var sqlPlaceholdersOverlay = {
token: function(stream, state) {
var ch;
if (stream.match("<<")) {
while ((ch = stream.next()) != null)
if (ch == ">" && stream.next() == ">") {
stream.eat(">");
return "sqlParams";
var ch;
if (stream.match("<<")) {
while ((ch = stream.next()) != null)
if (ch == ">" && stream.next() == ">") {
stream.eat(">");
return "sqlParams";
}
}
if (stream.match("[[")) {
while ((ch = stream.next()) != null)
if (ch == "]" && stream.next() == "]") break;
stream.eat("]");
return "columnPlaceholder";
}
else if (stream.next() != null) {
return null;
}
}
while (stream.next() != null && !stream.match("<<", false)) {}
return null;
}
};
return CodeMirror.overlayMode(CodeMirror.getMode(config, parserConfig.backdrop || "text/x-sql"), sqlParamsOverlay);
return CodeMirror.overlayMode(CodeMirror.getMode(config, parserConfig.backdrop || "text/x-sql"), sqlPlaceholdersOverlay);
});
var MSG_CONFIRM_DELETE = _("Are you sure you want to delete this report? This cannot be undone.");
@ -1454,7 +1467,7 @@
var editor = CodeMirror.fromTextArea(sql, {
lineNumbers: true,
mode: "sqlParams", /* text/x-sql plus custom sqlParams configuration */
mode: "sqlPlaceholders", /* text/x-sql plus custom sqlPlaceholders configuration */
lineWrapping: true,
smartIndent: false
});
@ -1476,7 +1489,7 @@
[% IF ( showsql ) %]
var editor = CodeMirror.fromTextArea(sql, {
lineNumbers: false,
mode: "sqlParams", /* text/x-sql plus custom sqlParams configuration */
mode: "sqlPlaceholders", /* text/x-sql plus custom sqlPlaceholders configuration */
lineWrapping: true,
readOnly: true
});