This is a Visual Studio extension that adds highlighting for PostgreSQL syntax in C# string literals.
Works with Visual Studio 2019 and Visual Studio 2022. Project targetting Visual Studio 2019 is located on a separate branch.
Applies only to C# .cs
files.
Supports parametrized queries. Parameter placeholders prefixed with an @
(named) or $
(positional) are colorized in golden.
Supports all string literal types!
- regular quoted
" "
, - verbatim
@" "
, - interpolated
$" "
, - interpolated verbatim
$@" "
, - raw
""" """
- interpolated raw
$""" """
.
Correctly handles string escape sequences.
PostgreSQL in a String uses an ANTLR 4 lexer to tokenize PostgreSQL syntax.
- In Visual Studio, navigate to
Extensions > Manage Extensions > Online
. - Search for
PostgreSQL in a String
. - Click Download. The extension is then scheduled for install.
Alternatively, you can download the extension from Visual Studio Marketplace.
- In Visual Studio, navigate to
Extensions > Manage Extensions > Online
. - Search for
PostgreSQL in a String for VS2019
. - Click Download. The extension is then scheduled for install.
Alternatively, you can download the extension from Visual Studio Marketplace.
The extension does not automatically detect string literals with PostgreSQL syntax. The syntax highlighting must be explicitly enabled for a single string literal, a code region or whole project.
Note
Rule configuration inside XML documentation comments (starting with ///
or /**
) are ignored.
Highlighting can be enabled inline by prefixing a string literal with a comment containing strpsql
text or PostgreSqlInAString
(if you prefer to be more explicit). Note that only block comments can be used as the comment must be directly adjacent to the beginning of string literal.
string sql = /*strpsql*/@"SELECT COUNT(*) FROM order;";
When the string literal is inside an enabled region, its highlighting can be disabled inline by prefixing it with a comment containing strpsql-ignore
text or PostgreSqlInAString-ignore
for a longer version.
string text = /*strpsql-ignore*/"Just a regular string, no SQL here";
Enable or disable highlighting in code regions with rule configuration comments. A region configuration will override highlighting of every string literal after it, except for those prefixed with inline rule configuration, until the next region configuration or end of file.
Short version:
// strpsql-on
// strpsql-off
Longer version:
/* PostgreSqlInAString-enable */
/* PostgreSqlInAString-disable */
The rule parts can be mixed, e.g. /*strpsql-enable*/
will work too.
To have the highlighting enabled by default for an entire project, add the following section in the .csproj
file (or merge it with existing ProjectExtensions
element).
<ProjectExtensions>
<PostgreSqlInAString>
<Enabled>true</Enabled>
</PostgreSqlInAString>
</ProjectExtensions>
This may be useful when you have a separate data access project where most of the strings are database queries.
Adding/changing nodes in .csproj
might require reopening all already open files from that project for the configuration to take effect.
Rule configuration comments can contain explanations similar to what ESLint rule configuration comments allow.
To add an explanation to the configuration, add two dashes after the rule name (separated by some whitespace) and place your explanation after the dashes.
/* strpsql-off -- This is HTML */
string str = "<select>" +
" <option>Apple</option>" +
" <option>Orange</option>" +
"</select>";
/* strpsql-on -- That was HTML */
string sql = /* PostgreSqlInAString -- hey,
* this is an explanation
* that spans multiple lines.
*/@"TRUNCATE address, order_product, order, product RESTART IDENTITY;";
The default highlight colors were selected for VS dark theme but they can be customized in Tools > Options > Environment > Fonts and Colors > String SQL * [ - escape character]
The default colors except for parameter placeholder color are a mix of 50% default string color and 50% default SQL token color. This means that the highlighted text will have an orange tint, which is by design, to not overwhelm when combined with other C# syntax as it is still just a string literal.
- Highlighting of PL/pgSQL in a string in a string
- Autocompletion
- Light-mode-friendly color palette and auto-switching between palettes when mode is changed
- Basic error recognition (typos, unsupported keyword order, etc.) by using an ANTLR parser
- Support for more language syntaxes in string literals: T-SQL, HTML, etc.