2

I'm trying to improve on the MySQL syntax highlighting, for my particular code base.

I have a lot of this sort of thing

FROM   TBL_ENTITIES E
JOIN   TBL_RELATED_ENTITIES RE  ON  RE.entityFK  =  E.entityPK

My table nams are always uppercase and always start with TBL_, and my aliases are always uppercase and 1-3 characters long.

In this example, I'd like to create a syntax group mysqlQualifier for RE and E.

In the ON clause (and further use when they're a qualifier before a column name) it seems quite trivial

syn match mysqlQualifier "\<[A-Z]\+\."

That correctly picks up on "RE." and "R." (Though I'd prefer it not to highlight the period if possible!)

But the alias clause is proving harder. I'm sure it'll be very simple to someone who's edited a syntax file before, but this is all new to me.

This is what I've tried, based on my understanding of Custom syntax highlighting: highlight word following keyword :

syn match mysqlTable             "TBL_[A-Z_]\+" nextgroup=mysqlQualifier skipwhite
syn match mysqlQualifier         "\<[A-Z]\+\>" contained

But it doesn't seem to work, and matches far too much (e.g. COALESCE when COALESCE hasn't immediately followed a table name.

I thought maybe I could simply match one, and then match the other, so as to "overwrite" part of it:

syn match mysqlQualifier         "TBL_[A-Z_]\+.\s\+[A-Z]\+"
syn match mysqlTable             "TBL_[A-Z_]\+"

But the second match seems to undo the first one; TBL_ENTITIES correctly recognised as a table, but E is not recognised as a qualifier, so I guess syntax can't be "overwritten" in the way that I hoped it might.

EDIT: I've now improved on the above...

syn match mysqlTable2            "TBL_[A-Z_]\+"

syn match mysqlQualifier         "TBL_[A-Z_]\+.\s\+[A-Z]\+" contains=mysqlTable
syn match mysqlTable             "TBL_[A-Z_]\+" contained
syn match mysqlQualifier         "[A-Z]\+\."

This is almost perfect, with one exception - I still have the problem with the period being hightlighted after the qualifier, e.g. in RE.entityFK.

Any help appreciated, thank you!

3

The easiest way is to use \ze to mark end of the match:

syn match mysqlTable2            "TBL_[A-Z_]\+"

syn match mysqlQualifier         "TBL_[A-Z_]\+.\s\+[A-Z]\+" contains=mysqlTable
syn match mysqlTable             "TBL_[A-Z_]\+" contained
syn match mysqlQualifier         "[A-Z]\+\ze\."

enter image description here

| improve this answer | |
  • Perfect, thank you! Although the rest of what I've put together works... I think it's probably kludgy - is there a better way to achieve what I wanted? Thank you :-) – Codemonkey May 15 at 10:45
  • 1
    Well, I would stop at "good enough" solution until rough edges would bother you enough to fix it. – Maxim Kim May 15 at 10:55
  • Wise words Maxim! – Codemonkey May 15 at 12:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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