Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sublime Text: how to correct SQL Server syntax

I am new to Sublime Text (version 3) and I am experiencing difficulties to get a proper SQL syntax highlighting.

Whenever I am looking at SQL queries, ST considers "#" as comment. For example, here everything beyond # gets greyed out:

INSERT INTO #TEST (A,B,C,D)
VALUES ('a','b','c','d')

I would like to correct this, so I looked around for answers for weeks but none of them works...

I started by looking in C:\Program Files\Sublime Text 3\Packages to find the file SQL.sublime-package.

I found this text in the comments section:

- match: "#"
  scope: punctuation.definition.comment.sql
  push:
   - meta_scope: comment.line.number-sign.sql
    - match: \n
      pop: true

So I tried to edit it:

  • Deleting the text block results in a Sublime Text error:

Error loading syntax file "Packages/SQL/SQL.sublime-syntax": Unable to read Packages/SQL/SQL.sublime-syntax

  • Changing the # sign by something else (e.g. "--"): no effect

Do you have a solution to get a proper SQL syntax highlighting in Sublime Text 3?

Thanks a lot

like image 725
Tjamat Avatar asked Jan 26 '23 16:01

Tjamat


1 Answers

You're on the right track; what you tried should work for you. If you get an error indicating that Sublime can't load the syntax, the most likely cause is that you deleted too much or altered the file in some other subtle way. sublime-syntax files are YAML, and as such they're sensitive to things like indentation.

Note also that doing anything in the Packages folder directly is a Bad Idea; modifying a sublime-package file works in the short term, but they're upgraded by removing them and replacing them. So unless you're the one that made the file in the first place, modifying the contents directly is a recipe for your change to be unceremoniously removed at some future point when you least expect it.

Making the following changes worked for me. This uses the OverrideAudit package to make the change (disclaimer: I am the package author). That will let you easily make the change in a safe way and also let you know if the underlying package ever gets upgraded.

  1. Install OverrideAudit
  2. From the Command Palette, select OverrideAudit: Create Override
  3. Select the SQL package, then the SQL.sublime-syntax file
  4. Use the find panel to search for comments: to see the context that contains all of the match patterns that represent comments. This should be around line 128, depending on what version of Sublime you're using
  comments:
    - match: "--"
      scope: punctuation.definition.comment.sql
      push:
        - meta_scope: comment.line.double-dash.sql
        - match: \n
          pop: true
    - match: "#"
      scope: punctuation.definition.comment.sql
      push:
        - meta_scope: comment.line.number-sign.sql
        - match: \n
          pop: true
    - match: /\*
      scope: punctuation.definition.comment.sql
      push:
        - meta_scope: comment.block.c
        - match: \*/
          pop: true
  1. Delete just the match rule that matches the comment style you don't want; make sure that you don't modify any other lines before or after or change the indent. The result should look like this when you're done:
  comments:
    - match: "--"
      scope: punctuation.definition.comment.sql
      push:
        - meta_scope: comment.line.double-dash.sql
        - match: \n
          pop: true
    - match: /\*
      scope: punctuation.definition.comment.sql
      push:
        - meta_scope: comment.block.c
        - match: \*/
          pop: true
  1. Save the syntax file.

As soon as you save the file, the change should take effect immediately. If you check the Sublime console with View > Show Console you should see a line that says generating syntax summary, which indicates that Sublime has seen and reloaded the syntax. If there's an error during this process, it will be displayed here.

After performing these steps, your sample text renders like this (using the Adaptive theme and the Monokai color scheme:

Sample Image

If you've previously modified the actual sublime-package file this may not work. In such a case you may need to do a reinstall of Sublime to get the original file back; doing so won't remove your settings. Alternatively you can download the Windows portable version from the Sublime website (even if you're not using windows) and get the pristine package from there to replace the one you modified.

like image 187
OdatNurd Avatar answered Feb 03 '23 17:02

OdatNurd