I’m using LightVirtualFile objects to represents files located on an embedded microcontroller device. I download them into memory from the device, let the user edit them via the LightVirtualFile, and then upload them back to the device.
I also add project level .pyi typehint packages for MicroPython specific modules.
However, the LightVirtualFiles aren’t children of the project directory and thus don’t recognize these typehint packages.
I tried creating the LightVirtualFile overriding its parent forcefully to be the project dir - that made stub packages work, but understandably the IDE complained about the LightVirtualFile actually not being found amongst the project directory’s children.
Is there anyway to make the LightVirtualFile see the project libraries? Or is there a better way to go about this than using a LightVirtualFile? Below is how I add the python library.
private fun addMpyLibrary(newStubPackageName: String) {
DumbService.getInstance(project).smartInvokeLater {
ApplicationManager.getApplication().runWriteAction {
// Try to find the stub package
val stubPackage = getStubPackages().first.find { "${it.name}_${it.mpyVersion}" == newStubPackageName }
if (!settings.state.areStubsEnabled || stubPackage == null || !stubPackage.isInstalled) {
return@runWriteAction
}
val modelsProvider = ModifiableModelsProvider.getInstance()
val projectLibraryModel = modelsProvider.getLibraryTableModifiableModel(project)
// Create library
val newLibrary = projectLibraryModel.createLibrary(LIBRARY_NAME, PythonLibraryType.getInstance().kind)
val libraryModel = newLibrary.modifiableModel
// Add roots
val rootUrl = "${MpyPaths.stubBaseDir}/$newStubPackageName"
val stdlibUrl = "$rootUrl/stdlib"
val rootFile = LocalFileSystem.getInstance().findFileByPath(rootUrl)
val stdlibFile = LocalFileSystem.getInstance().findFileByPath(stdlibUrl)
if (rootFile != null) {
libraryModel.addRoot(rootFile, OrderRootType.CLASSES)
}
if (stdlibFile != null) {
libraryModel.addRoot(stdlibFile, OrderRootType.CLASSES)
}
// Commit library changes first
libraryModel.commit()
projectLibraryModel.commit()
// Then add to modules
for (module in ModuleManager.getInstance(project).modules) {
val moduleModel = modelsProvider.getModuleModifiableModel(module)
moduleModel.addLibraryEntry(newLibrary)
modelsProvider.commitModuleModifiableModel(moduleModel)
}
}
}
}