[...] are not recognized in mssql mode


#1

If you have a table test with column from (which is a reserved word) and have a simple query (in 4 variants) like:
select [from] from test; --column identifier select 'from' from test; --string literal select "from" from test; --column identifier
select `from` from test; --syntax error. backticks not supported in mssql

In the demo https://codemirror.net/mode/sql/index.html?mime=text/x-mssql it looks like this:

But in MSSQL you can have […] around column identifiers so it should look black like line/query 3.
In SQL Management Studio it’s correctly displayed:


#2

I solved it by making a hook function in sql.js:

  // [identifier] for MSSQL/Access support
  function hookIdentifierBracket(stream) {
    // ref: https://docs.microsoft.com/en-us/sql/relational-databases/databases/database-identifiers
    var ch;
    while ((ch = stream.next()) != null) {
      if (ch == "]" && !stream.eat("]")) return "variable-2";
    }
    stream.backUp(stream.current().length - 1);
    return stream.eatWhile(/\w/) ? "variable-2" : null;
  }

And by hooking it in my custom MSSQL MIME type:

  CodeMirror.defineMIME("text/x-ELO-mssql", {
    name: "sql",
    client: set(""),
    keywords: set("abs ascii avg cast ceiling char charindex coalesce concat convert count current_timestamp current_user datalength dateadd datediff datename datepart day format floor getdate getutcdate isdate isnull isnumeric lag lead left len lower ltrim max min month nchar nullif patindex rand replace replicate right round rtrim sessionproperty session_user sign space str stuff substring sum system_user upper user user_name year"),
    builtin: set(sqlKeywords + "right left inner outer full asc case"),
    atoms: set("null"), //true false don't exist!
    operatorChars: /^[*+\-%<>!=]/,
    dateSQL: set("date datetimeoffset datetime2 smalldatetime datetime time"),
    support: set("ODBCdotTable"),
    hooks: {
      "\"":   hookIdentifierDoublequote,
      "[":   hookIdentifierBracket
    }      
  });