redhat-developer / quarkus-ls

Language server for Quarkus tooling
Eclipse Public License 2.0
44 stars 15 forks source link

Smart inference of Java type for user tag parameter #913

Open angelozerr opened 11 months ago

angelozerr commented 11 months ago

Given this user tag src/main/resources/tags/myUser.html:

Login: {user.login}

At this step, we cannot know what is user

Given a Qute template which consumes the user tag

{@org.acme.User userFromJava}
{#myUser user=userFromJava /}

At this step we can inject the org.acme.User Java type inside the myUser.html which will provide completion, validation, hover, defintion,etc

This feature is very important to support for instance https://github.com/FroMage/RivieraDEV-Quarkus/blob/main/src/main/resources/templates/tags/organisers.html

FroMage commented 11 months ago

Actually, I think the theory is that I should define a Java class to declare the tags parameters, I'm sure I discussed it with @mkouba but I can't find where. Something like:

public class tags {
 @CheckedTemplates
 public static class Templates {
  public static native TemplateInstance myUser(User user);
 }
}

Or was it?

 @CheckedTemplates
public class tags {
 public static native TemplateInstance myUser(User user);
}

I can't recall.

It's been discussed in https://github.com/quarkusio/quarkus/issues/21859 though, as well as default parameter values (added in https://github.com/quarkusio/quarkus/pull/25488). I don't know if we can declare default template parameter values from the Java side, though. Perhaps only when declaring parameters in the template?

FroMage commented 11 months ago

Otherwise, we can declare tags/myUser.html as such:

{@model.User user}
Login: {user.login}
angelozerr commented 11 months ago

Otherwise, we can declare tags/myUser.html as such:

Indeed it is working. I think it is the best solution, no?

mkouba commented 11 months ago

You can both a type-safe template that corresponds to templates/tags/myUser.html or a parameter declaration inside the tag template (e.g. {@org.acme.User user}).

Both

@CheckedTemplates
public class tags {
   static native TemplateInstance myUser(User user);
}

and

@CheckedTemplate(basePath = "tags"
public class Whatever {
   static native TemplateInstance myUser(User user);
}

should work.

Both solutions are equally good.

FroMage commented 11 months ago

So I wasn't wrong, yay :)

I didn't find this documented in https://quarkus.io/guides/qute-reference#typesafe_templates though, probably it should, no?

mkouba commented 11 months ago

So I wasn't wrong, yay :)

I didn't find this documented in https://quarkus.io/guides/qute-reference#typesafe_templates though, probably it should, no?

Well, tags are regular templates located in a special folder (templates/tags) and a special section is registered each. Otherwise the same rules apply. Of course, we could add a NOTE or something... feel free to send a PR ;-).