Indexing AngularJS expressions


I am working on a plugin which basically needs to create an index of all string literals in JavaScript. With AngularJS you can use string literals inside the HTML template (like <div ng-show="page === 'description'">...</div>). If you have the AngularJS plugin installed then expressions inside ng-show are succesfully parsed as JavaScript - e.g. custom annotator(com.intellij.lang.annotation.Annotator) is run for the content of ng-show.

I've tried to use a FileBasedIndex to index content of Angular directives, but I get there only the HTML PsiElements. Is it possible to scan somehow a HTML file to get JavaScript PsiElements inside Angular directives?

I need this to fix this issue in my plugin:



Comment actions Permalink

Sorry about delayed reply.

JavaScript content is added to HTML templates via injections, so usually you can access JS elements from there using InjectedLanguageManager/InjectedLanguageUtil.

Unfortunately injections are not accessible during indexing. There's a mechanism to bypass that limitation for JS, but it could not be used too because AngularJS plugin relies on indices to decide whether injection is needed.

Comment actions Permalink

Hi Dennis,

Thank you for this answer. I've tried to use InjectedLanguageUtil, but I am not sure if I do it right :)

I've created a word based index which simply checks if there are text occurences of what I am looking for in HTML files.

Then being in the context of QueryExecutorBase<PsiReference, ReferencesSearch.SearchParameters>::processQuery() I want to scan all files with a positive result from my index looking for Angular injected language section. Unfortunately InjectedLanguageUtil::enumerate doesn't give me any results. Is this supposed to work or perhaps my idea is wrong from the very beginning?

Perhaps scanning AngularJS directives is currently not possible?


Comment actions Permalink

Your approach looks ok.

Which element are you passing to enumerate method?

Comment actions Permalink

I have a working solution for working with injected fragments PSI in the indexer. Here it is:

* Scan raw PSI elements and check for all possible injection places.
* Next, extract the raw value for this expression, probable filter it if needed
* Create a PSI element factory for your injected language, that accepts a String, parses it and returns a PSI structure for it
* Now you can use a PSI of injected fragment in your indexer for your needs (eg, store something or use it)

Here is an example of using factory to create a PSI for injected fragment in indexer:


Please sign in to leave a comment.