You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[aot repositories] use text block when converting spring data jdbc query strings #1726
When converting generated queries for Spring Data JDBC repositories, the query can contain a lot of strings in quotes (and therefore escaped quote characters). A better way of writing those queries is to put them into a text block, which then avoid having to escape the quotes.
@catturtle123 please go ahead since I haven't started this at all. Thanks very much! I guess only 2 additional things came to my mind:
I'd have this logic for all repositories not just jdbc (don't see a reason why not)
I'd check what the java version on the project. if 15 or above then text blocks otherwise the current single line string. The IJavaProject should have info about the java version.
Originally, since the issue was raised in the context of JDBC, I was thinking of keeping the change minimal and scoped just to JDBC repositories.
But I agree there’s no strong reason to limit it, and applying this to all repository types makes sense and keeps the behavior consistent.
Regarding the Java version, I agree with this approach and will follow it when deciding whether to use text blocks.
I’ll rely on the Java version information from IJavaProject and reflect this logic in the implementation and accompanying tests when preparing the PR.
Thanks a lot @catturtle123 for jumping on this and working on a PR. That is awesome.
So, looks like the first step here is done and @BoykoAlex merged the PR #1762. Now, I think we need to go one step further... :-)
Now the query param is using a text block, but the result is missing important things:
the indentation of the text block is not correct
the content of the text block is always a single line (which makes using a text block more or less useless)
Instead of turning this into a single line text block, the text block should be split into multiple lines. The line break should be done at keyword boundaries, so that instead of generating:
@Query("SELECT u FROM users u WHERE u.username = :username")
the code lens action should generate:
@Query("""
SELECT u FROM users u
WHERE u.username = :username
""")
Ideally the decision whether to turn this into a single line query or a multiline text block query should also get a preference setting, so that users can decide which style they prefer.
@catturtle123 Splitting the query properly on multiple lines needs a bit of investigation work. You'd need to find the proper parser for the query and split it somehow around parser tree nodes, i.e. "select" clause one line, "where" clause second line and so on. Probably for each query type you'd have a "formatter" class which would share common interface. Give formatter a string query, the formatter uinder the hood parser the query and splits it over multiple lines a line per clause... Anyway, this would my rough idea for it.
Regarding the design, I'm planning to handle this via a global preference in the user settings so users can toggle their preferred query style.
As suggested by Alex, I'll introduce a common formatting interface and create separate implementation classes for each specific query type to keep the logic modular and extensible. For JPQL and SQL specifically, I’ve encapsulated the formatting logic using the existing JpqlLexer inside a dedicated JpqlQueryFormatter class.
Also, do you have a preference for the default style? Should it stay as the standard single-line string to maintain current behavior, or would you prefer the new multiline text block to be the default?
Does this design and the use of the lexer sound like the right direction to you?
the single line string to be the default in the preferences
the indentation of the text block to be correct (as mentioned above)
I don't know whether you need a parser/lexer to split the string or if a simple keyword matching would be enough, but that is up to you. @BoykoAlex has a much deeper insight into this, so I would follow his advice.
As a side note: I would prefer to keep all this in a branch for now and not yet merge things into main to avoid a situation where we would have to revert changes before shipping a release due to this not being fully done.
I think you can have a branch in your fork and create a regular draft PR from that, and then update the PR with new commits on your branch for further improvements. Then, if we decide to merge the PR, we can squash the PR into one commit and merge. Would that work for you?
@catturtle123 I saw the daft PR for the formatting. Wow! That was fast!
There is another idea i had in mind for the formatting... I'd try select this annotation with the query and use VScode command to format this code manually in the editor. If the results are okay perhaps we don't need to add all this support on the LS side to format the query and instead just use a "format" command that would perform the formatting on the client (add the command to the list on the server side). Therefore I'd check how the format works in VSCode java editor for the query. Then check the same in Eclipse...
I'm not convinced that IDE formatter would have better results but I'd give it atry before proceeding with the LS side solution.
I would prefer the format action as well, it makes things a lot easier. At the moment, you insert 8 spaces, but the code around the query might have a different style (tabs) and/or a different indentation. So using the format action from the surrounding IDE would solve those issues automatically for you. Otherwise, you would need to take those things into account yourself. I think you could take the indentation of the annotated method into account for this, for example.
you check a preference and decide which way to go, but haven't seen the other code that a new preference requires (default settings, data transfer, vscode definition, etc.). If you want to know more details about this, you can find other commits in the history that added preferences where you could take a look at the exact changes for this.
@BoykoAlex@martinlippert Thanks for the feedback! I didn't think about the indentation style issues (tabs/spaces), so using the IDE's format action makes a lot of sense. I'll give it a try in both editors and see how it works. I'll also make sure to follow the history for adding preferences to ensure nothing is missed.
I've updated the implementation to combine LS-side query formatting with the IDE's built-in formatting. The custom formatter (JpqlQueryFormatter, MongoQueryFormatter) splits the query into meaningful multiline segments, and then the IDE's native format action is triggered after inserting the Query annotation to handle indentation and styling according to the user's workspace settings.
This approach is implemented for the "Add Query" code action. Note that while the "Turn into @query" CodeLens isn't explicitly blocked, the IDE formatting may not work properly there.
I'm currently verifying this behavior in Eclipse and have pushed the changes to the draft PR for review.
Activity
catturtle123 commentedon Jan 19, 2026
Hi @BoykoAlex,
I noticed this issue is currently assigned to you for the
5.1.0.RELEASEmilestone.Could you let me know if you have already started working on this? If not, would it be okay for me to pick this up and submit a PR?
If I may contribute, here is my proposed approach:
1. Implementation Plan
I plan to modify
DataRepositoryAotMetadataCodeLensProvider.""") to avoid excessive escaping.2. Testing Plan
I plan to add a new test case to
DataRepositoryAotMetadataCodeLensProviderJdbcTest.@Queryannotation correctly uses the text block syntax and preserves the raw SQL structure.Please feel free to let me know if I can pick this up and if the proposed direction looks good to you.
BoykoAlex commentedon Jan 19, 2026
@catturtle123 please go ahead since I haven't started this at all. Thanks very much! I guess only 2 additional things came to my mind:
IJavaProjectshould have info about the java version.catturtle123 commentedon Jan 20, 2026
That sounds good to me.
Originally, since the issue was raised in the context of JDBC, I was thinking of keeping the change minimal and scoped just to JDBC repositories.
But I agree there’s no strong reason to limit it, and applying this to all repository types makes sense and keeps the behavior consistent.
Regarding the Java version, I agree with this approach and will follow it when deciding whether to use text blocks.
I’ll rely on the Java version information from IJavaProject and reflect this logic in the implementation and accompanying tests when preparing the PR.
catturtle123 commentedon Jan 20, 2026
I've submitted a PR to address this issue.
PR: #1762
martinlippert commentedon Jan 22, 2026
Thanks a lot @catturtle123 for jumping on this and working on a PR. That is awesome.
So, looks like the first step here is done and @BoykoAlex merged the PR #1762. Now, I think we need to go one step further... :-)
Now the query param is using a text block, but the result is missing important things:
Instead of turning this into a single line text block, the text block should be split into multiple lines. The line break should be done at keyword boundaries, so that instead of generating:
the code lens action should generate:
Ideally the decision whether to turn this into a single line query or a multiline text block query should also get a preference setting, so that users can decide which style they prefer.
BoykoAlex commentedon Jan 23, 2026
@catturtle123 Splitting the query properly on multiple lines needs a bit of investigation work. You'd need to find the proper parser for the query and split it somehow around parser tree nodes, i.e. "select" clause one line, "where" clause second line and so on. Probably for each query type you'd have a "formatter" class which would share common interface. Give formatter a string query, the formatter uinder the hood parser the query and splits it over multiple lines a line per clause... Anyway, this would my rough idea for it.
catturtle123 commentedon Jan 23, 2026
@martinlippert and @BoykoAlex, thank you for the great feedback!
Regarding the design, I'm planning to handle this via a global preference in the user settings so users can toggle their preferred query style.
As suggested by Alex, I'll introduce a common formatting interface and create separate implementation classes for each specific query type to keep the logic modular and extensible. For JPQL and SQL specifically, I’ve encapsulated the formatting logic using the existing JpqlLexer inside a dedicated JpqlQueryFormatter class.
Also, do you have a preference for the default style? Should it stay as the standard single-line string to maintain current behavior, or would you prefer the new multiline text block to be the default?
Does this design and the use of the lexer sound like the right direction to you?
martinlippert commentedon Jan 23, 2026
My preference would be:
I don't know whether you need a parser/lexer to split the string or if a simple keyword matching would be enough, but that is up to you. @BoykoAlex has a much deeper insight into this, so I would follow his advice.
As a side note: I would prefer to keep all this in a branch for now and not yet merge things into main to avoid a situation where we would have to revert changes before shipping a release due to this not being fully done.
catturtle123 commentedon Jan 23, 2026
@martinlippert
Thanks for the guidance.
I’ll continue working on this in a separate branch on my fork for now.
Once the feature is ready, would you prefer me to:
Happy to follow whichever workflow you prefer.
martinlippert commentedon Jan 23, 2026
I think you can have a branch in your fork and create a regular draft PR from that, and then update the PR with new commits on your branch for further improvements. Then, if we decide to merge the PR, we can squash the PR into one commit and merge. Would that work for you?
catturtle123 commentedon Jan 23, 2026
That works for me, thanks.
I’ll open a draft PR from my fork shortly and continue to push updates there. Squash-and-merge on completion sounds good to me.
catturtle123 commentedon Jan 23, 2026
A draft PR for this has been opened for discussion: #1763.
BoykoAlex commentedon Jan 23, 2026
@catturtle123 I saw the daft PR for the formatting. Wow! That was fast!
There is another idea i had in mind for the formatting... I'd try select this annotation with the query and use VScode command to format this code manually in the editor. If the results are okay perhaps we don't need to add all this support on the LS side to format the query and instead just use a "format" command that would perform the formatting on the client (add the command to the list on the server side). Therefore I'd check how the format works in VSCode java editor for the query. Then check the same in Eclipse...
I'm not convinced that IDE formatter would have better results but I'd give it atry before proceeding with the LS side solution.
martinlippert commentedon Jan 23, 2026
@catturtle123 Two additional comments here:
I would prefer the format action as well, it makes things a lot easier. At the moment, you insert 8 spaces, but the code around the query might have a different style (tabs) and/or a different indentation. So using the format action from the surrounding IDE would solve those issues automatically for you. Otherwise, you would need to take those things into account yourself. I think you could take the indentation of the annotated method into account for this, for example.
you check a preference and decide which way to go, but haven't seen the other code that a new preference requires (default settings, data transfer, vscode definition, etc.). If you want to know more details about this, you can find other commits in the history that added preferences where you could take a look at the exact changes for this.
Hope this helps!
catturtle123 commentedon Jan 24, 2026
@BoykoAlex @martinlippert Thanks for the feedback! I didn't think about the indentation style issues (tabs/spaces), so using the IDE's format action makes a lot of sense. I'll give it a try in both editors and see how it works. I'll also make sure to follow the history for adding preferences to ensure nothing is missed.
BoykoAlex commentedon Jan 25, 2026
@catturtle123 we may need to combine both: ls side formatting and then apply client formatting to adopt to the proper identation etc.
catturtle123 commentedon Jan 29, 2026
@BoykoAlex Thanks for the great suggestion
I've updated the implementation to combine LS-side query formatting with the IDE's built-in formatting. The custom formatter (JpqlQueryFormatter, MongoQueryFormatter) splits the query into meaningful multiline segments, and then the IDE's native format action is triggered after inserting the Query annotation to handle indentation and styling according to the user's workspace settings.
This approach is implemented for the "Add Query" code action. Note that while the "Turn into @query" CodeLens isn't explicitly blocked, the IDE formatting may not work properly there.
I'm currently verifying this behavior in Eclipse and have pushed the changes to the draft PR for review.
BoykoAlex commentedon Mar 2, 2026
Number of commits went in to fix this