Sublime Text: how to correct SQL Server syntax
Asked Answered
L

2

6

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

Linnlinnaeus answered 23/10, 2019 at 12:21 Comment(4)
Sublime is smarter than SQL Server, what can one say? A simple Google search finds: github.com/tosher/TSQLEasy. This is just random googling, I've never seen it before.Cinereous
Yes I have also been googling that stuff but it seems like their syntax is also not perfect (TSQL syntax gets wrong for "@" characters). So I think my best chance is to create my own / correct the SQL.sublime-package default ST file...Linnlinnaeus
Is it mandatory to use SublimeText in your case ?Dourine
@xXx: yes, thats the best tool I need for my everyday tasksLinnlinnaeus
P
9

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.

Paolo answered 23/10, 2019 at 13:45 Comment(1)
Hi @OdatNurd, works like a charm. Thanks for the great tool and perfect procedure! :)Linnlinnaeus
I
0

Best solutions:

not remove: - match: "#" (for tmp table) ...

but only insert space: - match: "# " (for comments MySQL,...)

Intubate answered 13/2, 2020 at 9:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.