Completion using arbitrary langauge servers in Java text blocks#1536
Completion using arbitrary langauge servers in Java text blocks#1536danthe1st wants to merge 1 commit into
Conversation
|
I think this would indeed be a very nice feature to have. How to do identify the type of language in the text block? The code creates a doc with |
|
I think this would be a nice addition. Regarding content recognition: // language=HTML
String html = "<body><h1>hi</h1></body>"; |
That is one of the main unanswered questions that I mentioned in the PR and that I'd like to get feedback on. I guess that it would be inferred from the source code in some way and ideally, that could be configured by both users (via a preference screen) and plugins which may have more information on that (e.g. plugins may know methods that expect |
I guess it might be useful to define a (configurable) regex for comments right before text blocks containing one group for the language? |
|
I don't really have a perfect solution in mind, but I usually don't like those approaches very much that require the developers to add things to the source code just to make the tooling happy (or working). |
I think we can still provide default values (i.e. have a configuration that's somewhat compatible with IntelliJ's approach) where applicable. |
What exactly do you have in mind here? Not sure I understand the "provide default values". Can you elaborate on that? |
My idea is maybe something like the following:
I guess it might make sense to start with the first one or two and others can be added later if someone is interested/has a good idea on how to do that. |
|
It would be useful to extract the exact strategy that identifies the language into a distinct component and open that up for extensions. The use case that I have in mind here is: In the Spring Tools, we know precisely for example when a SQL statement is used in a My guess is that other tools would also be able to identify those languages, since they are usually not totally random and can be inferred from the wider context. In our case it is the Spring Data specific |
233aca5 to
a584177
Compare
|
I updated the PR with some code that uses |
0f097d4 to
7f3184e
Compare
7c274f5 to
d7ff4ec
Compare
|
@martinlippert I added this interface to the the PR as an extension point (still work in progress as everything else): /**
* Allows plugins providing custom logic for which language servers should be
* used within Java text blocks.
*/
public interface LSJavaTextBlockLanguageDetector {
/**
* Creates a {@link URI} indicating the language server that should be used for
* a Java text block.
*
* @param compilationUnit
* The compilation unit corresponding to the Java
* file.
* @param textViewer
* The text viewer the Java file is opened in.
* @param textBlockRegion
* The source code region of the text block within
* the Java file.
* @return A {@link URI} where documents corresponding to that {@link URI} are
* recognized by that language server. This {@link URI} does not need to
* be resolvable.
*/
@Nullable
URI createURIForTextBlock(ICompilationUnit compilationUnit, ITextViewer textViewer, IRegion textBlockRegion);
}Do you think something along these lines would be useful (I am not yet sure what exactly this should return, I kept it as general as possible for now)? Aside from that, do you think this feature would be useful for Spring Tools in some way (I think you might be doing something similar within your language server (providing things for Java files) but not providing language servers for your dialects)? I am also thinking about possibly adding other capabilities (e.g. highlighting if it is easily possible) at some point but it would be useful to know about whether this would be actually useful for anyone. |
d7ff4ec to
8e5c960
Compare
8e5c960 to
b96e453
Compare
It seems like #1500 (and #1517) are about making that options available to users in some way. I don't know how to make sure lsp4e (specifically getting the |
Yes, looks definitely useful fro our case. The return value would be something like:
I think that should be documented somewhere, otherwise it is (at least to me) somewhat unclear what exactly the return value should be. Or could we also just return the identifier for the language instead of a URI ? Also, what would be the exact identifier for a specific language? Would be good to have an example of how this matches in the end to a language server that gets selected for a specific lanaguge identifier. |
I am not exactly what it should return (or even what parameters would be ideal). One option would be the file extension as a
|
I think it would be nice if we could use language servers from inside Java text blocks.
To demonstrate it, I implemented a prototype that can provide completions in the following way:
With the HTML language server from WildWebDeveloper, this looks like this:
Screencast_20260516_151757.webm
When I say logical content, I mean the parsed

Stringthat Java will know about (which I can get from JDT) which is also shown when hovering over it:Specifically, I have the following questions:
TextBlockSourceContentMapperfor converting positions within the Java code to positions within the logical text block content. Does anyone a better approach (e.g. getting it from JDT in some way) to this?Stringis directly assigned to a variable/used as a parameter namedhtml, it may want to treat it like an.htmlfile)@Language("sql")or@Sqlthat are somehow associated with the string (e.g. by annotating parameters or variables)Stringis passed to certain "known" method (e.g.Connection#prepareStatement), the language server could be inferred.Limitations
This requires the
org.eclipse.lsp4e.resourceFallback.enabledpreference being set totrue(at least for now).Note that this is everything but complete. I am creating a draft pull request to have a prototype as a base for discussions. For example, I hardcoded it using
htmlfor now.