Possible to have injected language fragments evaluate in the context of a specific file?

In my plugin, HTML template files can use symbols from peer JavaScript files for conditional rendering, looping, etc. This is accomplished via {expression} in the HTML markup which ends up being either XmlAttributeValueImpl or XmlTextImpl, both PSI injection hosts.

I have this injecting just fine via multiHostInjector, but these injected fragments don’t have any notion of the symbols available in the associated JavaScript files, so code completion doesn’t work, references are unresolved, etc.

Is there any way to provide an “evaluation context” for these injected fragments so these features work properly in that context?

injected language fragments evaluate in the context of a specific file?

I believe this is unfortunately not possible by design of injected fragments, IDE and many applied plugins treat them as self-sufficient, isolated (and potentially incomplete and broken) code fragments.

You may try to suppress/filter out parser errors and inspection warnings, but adding any evaluate context is barely possible there.

There are other mechanisms that try to evaluate in context such as template languages, probably they are more suitable for similar tasks.

1 Like

Thanks. I’d also thought about using a template language since these are effectively the same types of expressions I use in another HTML-/XML-based template language, but I know that these types of braced expressions are already handled a bit specially in HTML for things like WebComponents and didn’t know if it might cause conflicts.

For now I’m taking the approach of adding explicit completion and reference contributors to these specific HTML files, and those are taking care of ~90% of the use cases I want to address, with the remaining ~10% (perhaps less) being expressions so complex that they should probably be encapsulated in JavaScript anyway from a best practices standpoint. I might also add a simple annotator to provide simple syntax highlighting for these expressions. With those, I’m guessing users of my plugin will be generally happy, at least assuming I can figure out how to deal with the HTML autocompletions issue I also posted here yesterday.

Thanks for the feedback.