How to trigger GeneralHighlightingPass operations after file based indexing process?
I have FileBasedIndex which is used to provide PsiReferences for custom element definied in a plain text files. This refereces are used in JSON object values. There is a problem when I change some lines in indexed plain text file - generated references ale invalid so FileBasedIndex for this file has to be rebuilt, but this very often happens after GeneralHighlightingPass operations performed for opened JSON files, so I have Unresolved symbol annotations. Should I do this in some other way (provide custom references) or can I refresh annotations from GeneralHighlightingPass for opened project files and how?
Please sign in to leave a comment.
File based indexes should always be up to date. If your annotating code accesses the index IntelliJ will detect that the index is out of date and re-index the file. However these indexes do not create references directly, so I assume you're somehow creating them based on the contents of the index, and I'd guess that you're caching something there which is out of date. Can you detail how your references are created?
I create refrences in following way:
findTargetByElementLocator checks is text fragment pointed by ElementLocator saved in my FileBasedIndex is valid (text range, file, pointed text fragment, offset in file) but it seems not - ElementLocator is pointing text fragment for file indexed before changes made in some plain text file, so findTargetByElementLocator returns null. From this I see that file based index for this plain text file was not valid while getReferencesByElement method was called.
I do not use any kind of caching besides FileBasedIndex ;)
Hmm, interesting - my understanding is that your call to getValues() should reindex the file if it's out of date. Can you print out the values at that point, so you can see when this code is being called and also to see if the values have been correctly updated? If getValues() doesn't return up to date values, I think that would be a reportable bug in IntelliJ.
Are you calling ReferenceProvidersRegistry#getReferencesFromProviders(element) from your PsiElement's getReferences() call per the PsiReferenceProvider javadoc? And are you sure that your element's getReferences() is being called during annotation?
I removed dataKey HPP_CHECKLIST_CONFIGURATION_CREDITING_ITEMS from my file:
but FileBasedIndex returns value for HPP_CHECKLIST_CONFIGURATION_CREDITING_ITEMS - it should be removed after
from index storage in com.intellij.util.indexing.MapReduceIndex#updateWithMap.
All other key/values from that file after removed dataKey are invalid (wrong offsets) - values returned from FileBaseIndex are from index storage before reindexing. Maybe MemoryStorage for this index requires to refresh?
Each line "ComponentReferenceProviderBase - Unresolved element:..." indicates that dataKey has existing ElementLocator value in index storage, but it is pointing to invalid place in modified plain text file.
ElementLocator (index value) class has implemented equals and hasCode properly.
I've noticed, if I modify my indexed plain text file externally (i.e. using Notepad++), IDEA detects changes and update indices poperly (no unresolved elements besides removed one).
Hmm, I'm afraid you're reaching the limits of my knowledge about the indexing. If you're sure that the indexing is happening at the right time I would write out all the values being indexed to ensure that there's no bug in your indexing code. If you're sure that's working correctly I'd file a bug against IntelliJ, although to be honest I'd expect this code to be pretty robust. Is your code open source and available somewhere?
Okay, problem looks like this:
1. I open my indexed plain text file and json file
2. Remove some line from text file
3. Switch selected editor to json file
4. IDEA starts indexing plain text file but with wrong content (contains data which existed before edition of text file) - I obtain content for indexing via com.intellij.util.indexing.FileContent#getFile, but com.intellij.util.indexing.FileContent#getPsiFile also return invalid content
5. Built-in JS annotator higlights invalid references based on invalid index built in previous step.
But if I after step 2. switch editor to another file which don't use my FileBasedIndex for resolving references, i.e. another plain text file, and then make step 3. problems dissapears - IDEA will made second indexing, but this time with proper content, so references will be properly resolved.
It is a bug in Intellij IDEA or I do something wrong? Could someone help me with this?
Workaround for reindexing with proper file content:
This is bad idea from performance view to save file (IO operation) upon selection changed (mouse moving)
You need to use FileContent.getContentAsText for indexing. VirtualFile content is different from document content you edited in editor (and only after save they are the same). Complete indexer code reference is needed for better problem diagnostcs
I change my indexing code to use com.intellij.util.indexing.FileContent#getContentAsText for receieving file content and it fix my problem. Thanks for helpful advice.
Hi Marcin,
It looks like you were implementing a CSV editor? Any chance you could share some of that code? I am doing something similar.
I have custom implementation of PSI for CSV files, but it is highly integrated with mechanisms specific for my ogranization (for internal use only), so probably, it would be easier for You to create your own implementation. You can use IntelliJ tutorial to create Your own PSI implementation for CSV files: http://www.jetbrains.org/intellij/sdk/docs/tutorials/custom_language_support_tutorial.html