Check file exists in modules class path from LanguageFileType & read all files

I'm completely new to IntelliJ plugin development.

 

I need to provide suggestions if atleast one file exists in the classpath of the current module. For this, I extended LanguageFileType &  am trying to override the following method

boolean isMyFileType(@NotNull final VirtualFile file)

 

I've read somewhere that VirtualFiles dont belong to any individual project as they may not be backed by real file.

I don't know how to access current module details via VirtualFile & also how to read the file if it exists in the classpath of the module.

 

Your help is appreciated

0
9 comments

> I need to provide suggestions

Where do you want to provide suggestions? In some completion popups?

0
Avatar
Permanently deleted user

Yes.

0

So you don't need to override LanguageFileType. An implementation of LanguageFileType represents a specific kind of files (e.g. java files, xml files, etc), so you don't need to implement it unless you're providing support for a new kind of files.

In order to extend completion popup you need to implement CompletionContributor extension, see its javadoc for details.

> if atleast one file exists in the classpath of the current module

Could you please be more specific? What do you mean by 'classpath of the current module' here? Does it include output directories or source directories? What kind of files you're interested in?

1
Avatar
Permanently deleted user

Thanks for your quick response.

> Could you please be more specific? What do you mean by 'classpath of the current module' here? Does it include output directories or source directories? What kind of files you're interested in?

I need to read a bunch of files that follow a pattern `/META-INF/custom-config.json` that can exist in either in the module dependencies (from one or more libraries)/the output of the module.
0
Avatar
Permanently deleted user

This `/META-INF/custom-config.json` can come from multiple sources

1. From the library (more than one library in classpath can contain this file, in which case I need to process all of them)

2. From the module's output if my own custom annotation processor is included as a dependency for the module. My custom annotation processor would generate this file based on annotations that are already present in the source

For the 2nd case, not sure if IDEA would auto trigger annotation processors at regular intervals, but I need to update my suggestions based on the output of the annotation processor result 

In order to achieve 2nd case, I need hook into IDEA API that can notify my plugin everytime annotation processing is complete

 

Overall, my CompletionContributor would rely on the aggregate of files from libraries & the output of annotation processor.

1
Avatar
Permanently deleted user

Any idea on which APIs might help me here.

 

Thanks

0

If the file name is fixed you can use FilenameIndex.processFilesByName to process all files with such name. Pass GlobalSearchScope.moduleWithDependenciesAndLibrariesScope to it to search only inside dependencies of the given module.

Alternatively you can use OrderEnumerator (OrderEnumerator.orderEntries(module).recursively().classes().getRoots()) to collect output roots of module and its dependencies (including libraries), and then iterate over these roots and use VirtualFile::findFileByRelativePath to check whether a file is present under that root.

Annotation processing is performed as part of compilation so you can subscribe to CompilerTopics.COMPILATION_STATUS to get notified when it's finished.

1
Avatar
Permanently deleted user

Thank you for your help.

 

One last qn, If I register the following code in application service, does IDEA automatically execute the code as soon as the IDE is loaded?

 

ApplicationManager.getApplication().executeOnPooledThread(new Runnable() {
    public void run() {
        ApplicationManager.getApplication().runReadAction(new Runnable() {
            public void run() {
                final MessageBusConnection conn = myProject.getMessageBus().connect();
conn.subscribe(CompilerTopics.COMPILATION_STATUS, new CompilationStatusListener() {
// code to handle when compilation is finished etc
}); } }); } });
0

Services are loaded lazily on demand when 'getService' method is called (see the docs). You can register an ProjectComponent which is initialized when the IDE is loaded, but it would be better to avoid this, because it would mean that the plugin will register the listener even if it isn't actually needed (e.g. user currently works on a project where your annotation processing isn't used). So it would be better to somehow determine that you really need to have to listen for compilation events and register the listener only if it's the case.

0

Please sign in to leave a comment.