Adding sources to libraries via a plugin
Hi,
I am trying to add sources to libraries added from file system by gradle via a plugin in IntelliJ. I am using the following code in a startup activity. I can see that the the sources are attached to the library when I print the library on console but I don't see the sources in Project structure in the IDE. Is there anyway to do this automatically via a plugin?
Module @NotNull [] modules = ModuleManager.getInstance(this.project).getModules();
VirtualFileManager vfManager = VirtualFileManager.getInstance();
VirtualFile vf = vfManager.findFileByUrl("file:///path/to/library/sources");
for (var module : modules) {
ModuleRootManager moduleRootManager = ModuleRootManager.getInstance(module);
OrderEntry[] entries = moduleRootManager.getOrderEntries();
for (OrderEntry orderEntry : entries) {
if (orderEntry instanceof LibraryOrderEntry) {
LibraryOrderEntry libraryOrderEntry = (LibraryOrderEntry) orderEntry;
Library library = libraryOrderEntry.getLibrary();
if (library != null) {
var libModel = library.getModifiableModel();
var libTable = library.getTable();
libModel.addRoot(vf, OrderRootType.SOURCES);
libModel.commit();
if (libTable != null) {
libTable.getModifiableModel().commit();
}
}
}
}
}
Update: This code works and adds the sources but with a few caveats:
1. The added library sources only show up after closing and reopening the project. Is there some way to trigger whatever processing happens on reopening the project from the plugin itself?
2. This works for project level libraries but not for module level libraries. I suspect this is because the library table for module level libraries is null and without committing the library table, the changes to libraries are not reflected. Is there any workaround for this?
Please sign in to leave a comment.
Hi,
I'm not sure this is a good UX to add sources to libraries eagerly. It executes potentially unnecessary code during IDE startup and modifies the project model. And it may happen that a user will never need to browse the sources. I think they should be added as late as possible, if ever.
If the reason is that you want to make it easy for users to attach sources, consider implementing your own action that will be added to the top bar appearing when a library file opened by a user has no sources attached:
https://github.com/JetBrains/intellij-community/blob/idea/233.14808.21/java/openapi/src/com/intellij/codeInsight/AttachSourcesProvider.java
Hi,
Thanks for the suggestion. The problem with this approach is that our projects are imported from gradle build scripts so users would have to click on the action to add the sources every time the project is reloaded.
Is it possible to call this action automatically when a library file opened has no sources attached?
Could you please elaborate on this reloading problem?
I did a little test:
The same worked with downloading sources, so it doesn't seem that reloading the project model invalidates attached sources.
Hi,
I can't reproduce the reloading issue now. However, I can't find this API in the idea library. I am using
com.jetbrains:ideaIU:2023.3.6
for building my plugin and IntelliJ says that it can not resolve the symbolAttachSourcesProvider
.This is a part of Java plugin, so please add a dependency on
com.intellij.java
:https://plugins.jetbrains.com/docs/intellij/plugin-dependencies.html
Thanks Karol, this works for us!