cursive-ide / cursive

Cursive: The IDE for beautiful Clojure code
580 stars 7 forks source link

Support SQL language injection in clj source files #196

Open JonyEpsilon opened 10 years ago

JonyEpsilon commented 10 years ago

IntelliJ's SQL language injection is something I find really useful. It doesn't seem to work in clj files!

cursive-ide commented 10 years ago

The problem is knowing where to apply it - is there a particular library you'd like it to work with?

JonyEpsilon commented 10 years ago

In other languages, I think (but don't know - as I've never tried to write an IntelliJ plugin) that the IDE auto-detects which strings to apply it to. Otherwise you can manually enable it for a single editor session using Alt+Enter -> Inject language. Or you can give the IDE with a hint (usually a magic comment, or an annotation) so that it's always active. Usually "it just works"!

To answer the question you asked, I'm currently using it would like to use it with clojure.java.jdbc.

cursive-ide commented 10 years ago

I see - I'll take a look and see what's required. It should really work for regexps, too.

georgewfraser commented 10 years ago

This would be fantastic. +1

gered commented 10 years ago

+1 for this, would be very helpful.

cursive-ide commented 10 years ago

I've investigated this, sadly like most magic there's a ton of hard work behind the scenes. In particular, IntelliJ does need to know where to inject the different languages. It's easy enough for RegExps since they're identified by their reader form, but for SQL we'll need to identify strings that should have it injected. I'll need to think about how to set up the code patterns, since I'm not sure IntelliJ's built in code matching will be very nice to use from Clojure. Cases like "the first field in the vector which is the third parameter to clojure.java.jdbc/query should be SQL" are relatively simple, it's just a matter of identifying the cases. It gets a little trickier when cases are more like "if whatever function is of arity 3 and the first argument looks like an integer, then the third argument should be SQL" - hopefully there aren't many of those.

If you'd like to get an idea of the existing injection points, check out Settings→Language Injections.

gered commented 10 years ago

Indeed :) I did take a quick peek in there a while back to see how it was being done for other languages, and had the same thought -- would seem to be somewhat of a pain to identify all the (or at least the major) cases for Clojure.

Perhaps I need to take a second look at yesql, heh.

bilekt3 commented 10 years ago

@cursiveclojure for SQL - I think and for mots of us will be helpful if only strings starting with SQL keywords (there are few that can be on start of string like SELECT, DELETE, UPDATE, INSERT etc.) will be throated as SQL string with autocomplete functionality. This behavior is used in PHP and works pretty well.

cursive-ide commented 10 years ago

Yeah, that might be an option too. I'll see how the matching works, and fall back to the SQL keyword route if it's too difficult.

As a first cut, which libraries would everyone like SQL support for, in case I go down the specific injection point route? Anything other than clojure.java.jdbc? Most of the other libraries I've seen are DSLs which won't need this kind of support.

CmdrDats commented 9 years ago

A starting point that I might suggest is to just enable custom defined language injection (preferences -> Editor -> Language Injections) to start with and let users define their own injection patterns. Over time, you could bundle the most useful injections, but that's less important that just at least having the feature.

In my specific use case, I have the jdbc stuff abstracted (so no hard DELETE, UPDATE or INSERT) - but I use a (prepare-query "SELECT ...") function for the querying, so I'd want just that to match and inject on that basis.

alexanderkiel commented 9 years ago

In my opinion everybody who needs to use SQL should have a look at yesql. With Yesql, you put the SQL strings in an extra file were IntelliJ can use all its SQL magic.

It's an anti pattern anyway to use strings for things were using data would be better. Sadly there will be never a DSL which suits all SQL dialects.

CmdrDats commented 9 years ago

@alexanderkiel use of yesql absolutely obviates the need for this, and yesql is something I've considered when I built our system, but in our case it makes a lot more sense to have the SQL right next to the functions that call them (they are highly localised).

Each to their own opinion - all I'm saying is that I would really appreciate the injection feature in Cursive.

cursive-ide commented 9 years ago

Yeah, I would like to add this. Unfortunately adding a pattern language for the language injection is not trivial. I'll try to do this soon.

bilekt3 commented 8 years ago

Just saw IntelliJ IDEA Tips & Tricks and played about hour with inject languages and this is things which I really miss in Cursive.

DanEEStar commented 4 years ago

@cursive-ide Just wanted to ask if this feature is still on the roadmap? Just today I thought it would be really useful, also just the SQL keyword route.