Dotnet logo

.NET Tools

Essential productivity kit for .NET and game developers

How-To's

SQL inside C# strings, fragment editor, run query in console – Language injection updates in Rider 2018.3

The latest Rider 2018.3 EAP build extends Rider’s language injections functionality quite a bit, with SQL language support in string literals. This gives us highlighting, code completion and code analysis, and a Run in console action for SQL inside of a C# string!

For all existing and newly added language injections, we can edit fragments in a separate editor! And last but not least, we can now configure automatic language injections, so that Rider automatically recognizes language fragments inside strings.

More than enough for a long blog post! Grab a coffee or tea, sit back, and let’s dive in!

What are language injections again?

Sometimes we have to embed a piece of code in a string literal. For example, pieces of HTML inside a JavaScript string, or a quick JSON fragment inside a C# string. Rider supports marking a string literal as containing another programming language, so that we get highlighting, code completion, code analysis and more inside of a string!

Inject JSON into string literal

That’s the main premise. In case you want to read more about language injections in Rider before, go read up on them. Now let’s look at what’s new in Rider 2018.3!

SQL language injections – make working with Entity Framework and Dapper more enjoyable

For those developers using Entity Framework’s SqlQuery to work with raw SQL queries, or those using a data access framework like Dapper which is string-based, we have great news!

After setting up a database connection in Rider, we can mark any string as being a SQL fragment, and get highlighting, code completion and code analysis based on the schema of the database we are connected to!

Code completion inside a string for a connected SQL database

That’s right: code completion based on our connected SQL database, inside a C# string literal!

Note that this also works with Data Definition Language (DDL) database connections: if you have a folder containing .sql files that define tables, views, …, Rider will provide code completion for those as well.

Data definition language database code completion

Having support for injected SQL fragments should help us in writing raw SQL queries in our code, which is especially useful when using the various micro-ORM’s (such as Dapper) out there.

The Run query in console context action

For strings that contain injected SQL, Rider provides highlighting, code completion and code analysis. Not only that: database-related context actions (Alt+Enter) become available as well, such as running a query from C# string in the database console:

Run query from C# editor in database console

No more need to open up a database console and copy/paste a query in there!

Tip: check our blog series to learn more about working with databases in Rider.

Edit language injection fragments in a separate editor

Previously, when working with language injections, we would have to manually escape special characters that were not allowed inside the “host” language. For example when injecting JSON into a string, we’d still have to escape " to \".

Rider 2018.3 adds the ability to edit an injected language fragment in a separate editor, and ensures that the contents we are editing are properly escaped in the original document:

Edit language fragment in separate editor

This works for SQL as well, including context actions (Alt+Enter):

Edit SQL statement in fragment editor

Automatic language injections

Marking a string as an injected language only applies to our local Rider instance. To share language injections with team members, we can add a // language={language-name} comment on the line before the string literal. However, that does require some discipline.

Rider 2018.3 comes with automatic language injections based on patterns, so that strings similar to SELECT ... FROM ... are recognized as a SQL language injection while typing:

Automatic recognizition of SQL

We can add our own patterns in Rider’s settings, under Editor | Language Injections:

Edit automatic language injection patterns

Have a look at the existing ones for inspiration.

Additional injected languages

So far, we have covered support for SQL language injections. For Rider 2018.3, we have been hard at work to support injecting additional languages.

By merging language injection functionality from IntelliJ IDEA and ReSharper, we now support languages like CSS, HTML, JSON, regular expressions and JavaScript, as well as SQL, XML, MsBuild, YAML, and many more.

Current limitations/external annotations

When using certain types and methods, Rider knows that certain parameters accept string value containing a certain language. For example, when typing Regex.Matches(value, "{caret here}"), the editor knows the second string parameter accepts a regular expression and provides highlighting and code completion for it.

This works because we maintain a large collection of external annotations, where we keep information about the .NET base class library (BCL) and various open-source projects. The Regex.Matches example can be seen here.

Tip: check our blog series about how code annotations can help make Rider smarter on your code base!

Currently, Rider (and ReSharper) do not have external annotations that would, for example, annotate methods in Entity Framework and Dapper. The automatic language injection patterns described earlier should help for these cases.

Download Rider 2018.3 EAP and give it a try! We would love to hear your feedback on these updates!

image description