SQL keywords defined in MIME not highlighted

Hi,

I use CodeMirror in a website where you have a SQL editor where you can type and run SQL queries in, and dynamically switch from SQL syntax (MSSQL/MySQL/Oracle/Access) by pressing a button. I have created extra MIME types for each SQL syntax like this in […]/mode/sql/sql.js :

CodeMirror.defineMIME(“text/x-ELO-mssql”, {
name: “sql”,
client: set(“”),
keywords: set(sqlKeywords + “ABS ASCII AVG … YEAR”),
builtin: set(“”),
atoms: set(“false true null”),
operatorChars: /[1]/,
dateSQL: set(“date datetimeoffset datetime2 smalldatetime datetime time”),
support: set(“ODBCdotTable”),
hooks: { }
});

If I type “select count(price) from plant” the SQL keywords are drawn in a purple color. These words get a cm-keyword CSS style applied. This works for all standard sqlKeywords defined in sql.js. :

var sqlKeywords = "alter and as asc between by count create delete desc distinct drop from group having in insert into is join like not on or order select set table union update values where limit ";

But the extra SQL keywords defined in the MIME for example avg doesn’t get this style applied (color stays the same), I also checked this with Firebug.

In your SQL mode demo at CodeMirror: SQL Mode for CodeMirror it does work for both standard keywords and extra defined SQL keywords. But your method of switching mode is different. You reload the complete page and set the mode when instantiating the editor. I use the following method:

    var curmode=editor.getOption("mode");  //get current database type
    if (curmode.indexOf(dbType)==-1) {  //is database type changed?
      var mode="text/x-ELO-"+dbType;
      console.log("Changing mode from "+curmode+" to "+mode);
      editor.setOption("mode",mode);
    }

How do I get the same behaviour like standard SQL keywords for the extra own defined SQL keywords in the MIME?


  1. *+-%<>!= ↩︎

Oh swell, you had to define the extra sql keywords lowercase!! Then it worked. Maybe it’s better to make the keywords comparison less restrictive.

You should only define the keywords in lowercase when defining a dialect for the SQL mode. It’ll convert identifiers to lowercase before comparing them against the set of keywords you provide.

If there’s no other way to relax this constraint, then please document this. For example in the comment section “How Properties of Mime Types are used by SQL Mode” at the bottom in sql.js.